chore: Convert from ajv to ts-matches (#1415)

This commit is contained in:
J M
2022-05-10 11:00:56 -06:00
committed by GitHub
parent 10d7a3d585
commit cc6cbbfb07
3 changed files with 174 additions and 171 deletions

View File

@@ -22,7 +22,6 @@
"@start9labs/argon2": "^0.1.0", "@start9labs/argon2": "^0.1.0",
"@start9labs/emver": "^0.1.5", "@start9labs/emver": "^0.1.5",
"aes-js": "^3.1.2", "aes-js": "^3.1.2",
"ajv": "^6.12.6",
"ansi-to-html": "^0.7.2", "ansi-to-html": "^0.7.2",
"core-js": "^3.21.1", "core-js": "^3.21.1",
"dompurify": "^2.3.6", "dompurify": "^2.3.6",
@@ -36,6 +35,7 @@
"patch-db-client": "file: ../../../patch-db/client", "patch-db-client": "file: ../../../patch-db/client",
"pbkdf2": "^3.1.2", "pbkdf2": "^3.1.2",
"rxjs": "^6.6.7", "rxjs": "^6.6.7",
"ts-matches": "^5.1.0",
"tslib": "^2.3.0", "tslib": "^2.3.0",
"uuid": "^8.3.2", "uuid": "^8.3.2",
"zone.js": "^0.11.5" "zone.js": "^0.11.5"
@@ -3881,6 +3881,7 @@
"version": "6.12.6", "version": "6.12.6",
"resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz", "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz",
"integrity": "sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==", "integrity": "sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==",
"dev": true,
"dependencies": { "dependencies": {
"fast-deep-equal": "^3.1.1", "fast-deep-equal": "^3.1.1",
"fast-json-stable-stringify": "^2.0.0", "fast-json-stable-stringify": "^2.0.0",
@@ -6746,7 +6747,8 @@
"node_modules/fast-deep-equal": { "node_modules/fast-deep-equal": {
"version": "3.1.3", "version": "3.1.3",
"resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz",
"integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==" "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==",
"dev": true
}, },
"node_modules/fast-glob": { "node_modules/fast-glob": {
"version": "3.2.11", "version": "3.2.11",
@@ -6772,7 +6774,8 @@
"node_modules/fast-json-stable-stringify": { "node_modules/fast-json-stable-stringify": {
"version": "2.1.0", "version": "2.1.0",
"resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz", "resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz",
"integrity": "sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==" "integrity": "sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==",
"dev": true
}, },
"node_modules/fast-levenshtein": { "node_modules/fast-levenshtein": {
"version": "2.0.6", "version": "2.0.6",
@@ -8469,7 +8472,8 @@
"node_modules/json-schema-traverse": { "node_modules/json-schema-traverse": {
"version": "0.4.1", "version": "0.4.1",
"resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz", "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz",
"integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg==" "integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg==",
"dev": true
}, },
"node_modules/json5": { "node_modules/json5": {
"version": "2.2.1", "version": "2.2.1",
@@ -11548,6 +11552,7 @@
"version": "2.1.1", "version": "2.1.1",
"resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz",
"integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==", "integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==",
"dev": true,
"engines": { "engines": {
"node": ">=6" "node": ">=6"
} }
@@ -13234,6 +13239,11 @@
"tree-kill": "cli.js" "tree-kill": "cli.js"
} }
}, },
"node_modules/ts-matches": {
"version": "5.1.5",
"resolved": "https://registry.npmjs.org/ts-matches/-/ts-matches-5.1.5.tgz",
"integrity": "sha512-PdashZCpn30SFH9mboHq3/rmC3wntKajw5IkQcSX9HhbcV7FoP3/nJzjFII6ZhRyoAV0mrDXWoblJulpMlh65g=="
},
"node_modules/ts-node": { "node_modules/ts-node": {
"version": "10.7.0", "version": "10.7.0",
"resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.7.0.tgz", "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.7.0.tgz",
@@ -13549,6 +13559,7 @@
"version": "4.4.1", "version": "4.4.1",
"resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz", "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz",
"integrity": "sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==", "integrity": "sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==",
"dev": true,
"dependencies": { "dependencies": {
"punycode": "^2.1.0" "punycode": "^2.1.0"
} }
@@ -17135,6 +17146,7 @@
"version": "6.12.6", "version": "6.12.6",
"resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz", "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz",
"integrity": "sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==", "integrity": "sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==",
"dev": true,
"requires": { "requires": {
"fast-deep-equal": "^3.1.1", "fast-deep-equal": "^3.1.1",
"fast-json-stable-stringify": "^2.0.0", "fast-json-stable-stringify": "^2.0.0",
@@ -19183,7 +19195,8 @@
"fast-deep-equal": { "fast-deep-equal": {
"version": "3.1.3", "version": "3.1.3",
"resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz",
"integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==" "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==",
"dev": true
}, },
"fast-glob": { "fast-glob": {
"version": "3.2.11", "version": "3.2.11",
@@ -19206,7 +19219,8 @@
"fast-json-stable-stringify": { "fast-json-stable-stringify": {
"version": "2.1.0", "version": "2.1.0",
"resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz", "resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz",
"integrity": "sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==" "integrity": "sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==",
"dev": true
}, },
"fast-levenshtein": { "fast-levenshtein": {
"version": "2.0.6", "version": "2.0.6",
@@ -20460,7 +20474,8 @@
"json-schema-traverse": { "json-schema-traverse": {
"version": "0.4.1", "version": "0.4.1",
"resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz", "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz",
"integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg==" "integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg==",
"dev": true
}, },
"json5": { "json5": {
"version": "2.2.1", "version": "2.2.1",
@@ -22772,7 +22787,8 @@
"punycode": { "punycode": {
"version": "2.1.1", "version": "2.1.1",
"resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz",
"integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==" "integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==",
"dev": true
}, },
"qrcode": { "qrcode": {
"version": "1.5.0", "version": "1.5.0",
@@ -24040,6 +24056,11 @@
"integrity": "sha512-L0Orpi8qGpRG//Nd+H90vFB+3iHnue1zSSGmNOOCh1GLJ7rUKVwV2HvijphGQS2UmhUZewS9VgvxYIdgr+fG1A==", "integrity": "sha512-L0Orpi8qGpRG//Nd+H90vFB+3iHnue1zSSGmNOOCh1GLJ7rUKVwV2HvijphGQS2UmhUZewS9VgvxYIdgr+fG1A==",
"dev": true "dev": true
}, },
"ts-matches": {
"version": "5.1.5",
"resolved": "https://registry.npmjs.org/ts-matches/-/ts-matches-5.1.5.tgz",
"integrity": "sha512-PdashZCpn30SFH9mboHq3/rmC3wntKajw5IkQcSX9HhbcV7FoP3/nJzjFII6ZhRyoAV0mrDXWoblJulpMlh65g=="
},
"ts-node": { "ts-node": {
"version": "10.7.0", "version": "10.7.0",
"resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.7.0.tgz", "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.7.0.tgz",
@@ -24272,6 +24293,7 @@
"version": "4.4.1", "version": "4.4.1",
"resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz", "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz",
"integrity": "sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==", "integrity": "sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==",
"dev": true,
"requires": { "requires": {
"punycode": "^2.1.0" "punycode": "^2.1.0"
} }

View File

@@ -35,7 +35,6 @@
"@start9labs/argon2": "^0.1.0", "@start9labs/argon2": "^0.1.0",
"@start9labs/emver": "^0.1.5", "@start9labs/emver": "^0.1.5",
"aes-js": "^3.1.2", "aes-js": "^3.1.2",
"ajv": "^6.12.6",
"ansi-to-html": "^0.7.2", "ansi-to-html": "^0.7.2",
"core-js": "^3.21.1", "core-js": "^3.21.1",
"dompurify": "^2.3.6", "dompurify": "^2.3.6",
@@ -50,6 +49,7 @@
"pbkdf2": "^3.1.2", "pbkdf2": "^3.1.2",
"rxjs": "^6.6.7", "rxjs": "^6.6.7",
"tslib": "^2.3.0", "tslib": "^2.3.0",
"ts-matches": "^5.1.0",
"uuid": "^8.3.2", "uuid": "^8.3.2",
"zone.js": "^0.11.5" "zone.js": "^0.11.5"
}, },

View File

@@ -1,82 +1,92 @@
import * as Ajv from 'ajv'
import { applyOperation } from 'fast-json-patch' import { applyOperation } from 'fast-json-patch'
import {
Parser,
shape,
string,
literal,
boolean,
object,
deferred,
dictionary,
anyOf,
} from 'ts-matches'
export type ValidVersion = 1 | 2 export type ValidVersion = 1 | 2
function has<Obj extends {}, K extends string>(obj: Obj, key: K): obj is (Obj & { [P in K]: unknown }) { function has<Obj extends {}, K extends string>(
obj: Obj,
key: K,
): obj is Obj & { [P in K]: unknown } {
return key in obj return key in obj
} }
const matchPropertiesV1 = shape(
const ajv = new Ajv({ jsonPointers: true, allErrors: true, nullable: true }) {
const ajvWithDefaults = new Ajv({ jsonPointers: true, allErrors: true, useDefaults: true, nullable: true, removeAdditional:true }) name: string,
value: string,
const schemaV1 = { description: string,
'type': 'object', copyable: boolean,
'properties': { qr: boolean,
'name': { 'type': 'string' },
'value': { 'type': 'string' },
'description': { 'type': 'string', 'nullable': true, 'default': null },
'copyable': { 'type': 'boolean', 'default': false },
'qr': { 'type': 'boolean', 'default': false },
}, },
'required': ['name', 'value', 'copyable', 'qr'], ['description', 'copyable', 'qr'],
'additionalProperties': false, { description: null as null, copyable: false, qr: false } as const,
} )
const schemaV1Compiled = ajv.compile(schemaV1) type PropertiesV1 = typeof matchPropertiesV1._TYPE
function isSchemaV1(properties: unknown): properties is PropertiesV1 {
return schemaV1Compiled(properties) as any
}
const _schemaV1CompiledWithDefaults = ajvWithDefaults.compile(schemaV1)
function schemaV1CompiledWithDefaults(properties: unknown): properties is PropertiesV1 {
return _schemaV1CompiledWithDefaults(properties) as any
}
const schemaV2 = {
'anyOf': [
{
'type': 'object',
'properties': {
'type': { 'type': 'string', 'const': 'string' },
'value': { 'type': 'string' },
'description': { 'type': 'string', 'nullable': true, 'default': null },
'copyable': { 'type': 'boolean', 'default': false },
'qr': { 'type': 'boolean', 'default': false },
'masked': { 'type': 'boolean', 'default': false },
},
'required': ['type', 'value', 'description', 'copyable', 'qr', 'masked'],
'additionalProperties': false,
},
{
'type': 'object',
'properties': {
'type': { 'type': 'string', 'const': 'object' },
'value': {
'type': 'object',
'patternProperties': {
'^.*$': {
'$ref': '#',
},
},
},
'description': { 'type': 'string', 'nullable': true, 'default': null },
}, type PackagePropertiesV2 = {
'required': ['type', 'value', 'description'], [name: string]: PackagePropertyString | PackagePropertyObject
'additionalProperties': false,
},
],
} }
const schemaV2Compiled = ajv.compile(schemaV2)
const schemaV2CompiledWithDefaults = ajvWithDefaults.compile(schemaV2)
export function parsePropertiesPermissive (properties: unknown, errorCallback: (err: Error) => any = console.warn): PackageProperties { const [matchPackagePropertiesV2, setPPV2] = deferred<PackagePropertiesV2>()
const matchPackagePropertyString = shape(
{
type: literal('string'),
description: string,
value: string,
copyable: boolean,
qr: boolean,
masked: boolean,
},
['description', 'copyable', 'qr', 'masked'],
{
description: null as null,
copyable: false,
qr: false,
masked: false,
} as const,
)
type PackagePropertyString = typeof matchPackagePropertyString._TYPE
const matchPackagePropertyObject = shape(
{
type: literal('object'),
value: matchPackagePropertiesV2,
description: string.optional(),
},
['description'],
{ description: null as null },
)
const matchPropertyV2 = anyOf(
matchPackagePropertyString,
matchPackagePropertyObject,
)
type PackagePropertyObject = typeof matchPackagePropertyObject._TYPE
setPPV2(dictionary([string, matchPropertyV2]))
export function parsePropertiesPermissive(
properties: unknown,
errorCallback: (err: Error) => any = console.warn,
): PackageProperties {
if (typeof properties !== 'object' || properties === null) { if (typeof properties !== 'object' || properties === null) {
errorCallback(new TypeError(`${properties} is not an object`)) errorCallback(new TypeError(`${properties} is not an object`))
return {} return {}
} }
// @TODO still need this conditional? // @TODO still need this conditional?
if (!has(properties, 'version') || !has(properties, 'data') || typeof properties.version !== 'number' || !properties.data) { if (
!has(properties, 'version') ||
!has(properties, 'data') ||
typeof properties.version !== 'number' ||
!properties.data
) {
return Object.entries(properties) return Object.entries(properties)
.filter(([_, value]) => { .filter(([_, value]) => {
if (typeof value === 'string') { if (typeof value === 'string') {
@@ -99,8 +109,7 @@ export function parsePropertiesPermissive (properties: unknown, errorCallback: (
.reduce((acc, { name, value }) => { .reduce((acc, { name, value }) => {
acc[name] = value acc[name] = value
return acc return acc
}, { }) }, {})
} }
switch (properties.version) { switch (properties.version) {
case 1: case 1:
@@ -108,124 +117,96 @@ export function parsePropertiesPermissive (properties: unknown, errorCallback: (
case 2: case 2:
return parsePropertiesV2Permissive(properties.data, errorCallback) return parsePropertiesV2Permissive(properties.data, errorCallback)
default: default:
errorCallback(new Error(`unknown properties version ${properties.version}, attempting to parse as v2`)) errorCallback(
new Error(
`unknown properties version ${properties.version}, attempting to parse as v2`,
),
)
return parsePropertiesV2Permissive(properties.data, errorCallback) return parsePropertiesV2Permissive(properties.data, errorCallback)
} }
} }
function parsePropertiesV1Permissive(
properties: unknown,
function parsePropertiesV1Permissive (properties: unknown, errorCallback: (err: Error) => any): PackageProperties { errorCallback: (err: Error) => any,
): PackageProperties {
if (!Array.isArray(properties)) { if (!Array.isArray(properties)) {
errorCallback(new TypeError(`${properties} is not an array`)) errorCallback(new TypeError(`${properties} is not an array`))
return {} return {}
} }
const parsedProperties : PackagePropertiesV2 = {}; return properties.reduce(
for(const idx in properties) { (prev: PackagePropertiesV2, cur: unknown, idx: number) => {
const cur:unknown = properties[idx] const result = matchPropertiesV1.enumParsed(cur)
if(isSchemaV1(cur)) { if ('value' in result) {
parsedProperties[cur.name] = { const value = result.value
type: 'string', prev[value.name] = {
value: cur.value, type: 'string',
description: cur.description, value: value.value,
copyable: cur.copyable, description: value.description,
qr: cur.qr, copyable: value.copyable,
masked: false, qr: value.qr,
} masked: false,
} }
else if (schemaV1Compiled.errors) { } else {
for (let err of schemaV1Compiled.errors) { const error = result.error
errorCallback(new Error(`/data/${idx}${err.dataPath}: ${err.message}`)) const message = Parser.validatorErrorAsString(error)
if (err.dataPath) { let dataPath = error.keys.map(x => JSON.parse(x)).join('/')
applyOperation(cur, { op: 'replace', path: err.dataPath, value: undefined }) errorCallback(new Error(`/data/${idx}: ${message}`))
if (dataPath) {
applyOperation(cur, {
op: 'replace',
path: dataPath,
value: undefined,
})
} }
} }
if (!schemaV1CompiledWithDefaults(cur)) { return prev
for (let err of _schemaV1CompiledWithDefaults.errors) { },
errorCallback(new Error(`/data/${idx}${err.dataPath}: ${err.message}`)) {},
} )
continue
}
parsedProperties[cur.name] = {
type: 'string',
value: cur.value,
description: cur.description,
copyable: cur.copyable,
qr: cur.qr,
masked: false,
}
}
}
return parsedProperties
} }
function parsePropertiesV2Permissive (properties: unknown, errorCallback: (err: Error) => any): PackageProperties { function parsePropertiesV2Permissive(
if (typeof properties !== 'object' || properties === null) { properties: unknown,
errorCallback(new TypeError(`${properties} is not an object`)) errorCallback: (err: Error) => any,
): PackageProperties {
if (!object.test(properties)) {
return {} return {}
} }
return Object.entries(properties).reduce((prev, [name, value], idx) => { return Object.entries(properties).reduce(
schemaV2Compiled(value) (prev: PackageProperties, [name, value], idx) => {
if (schemaV2Compiled.errors) { const result = matchPropertyV2.enumParsed(value)
for (let err of schemaV2Compiled.errors) { if ('value' in result) {
errorCallback(new Error(`/data/${idx}${err.dataPath}: ${err.message}`)) prev[name] = result.value
if (err.dataPath) { } else {
applyOperation(value, { op: 'replace', path: err.dataPath, value: undefined }) const error = result.error
const message = Parser.validatorErrorAsString(error)
let dataPath = error.keys.map(x => JSON.parse(x)).join('/')
errorCallback(new Error(`/data/${idx}: ${message}`))
if (dataPath) {
applyOperation(properties, {
op: 'replace',
path: dataPath,
value: undefined,
})
} }
} }
if (!schemaV2CompiledWithDefaults(value)) { return prev
for (let err of schemaV2CompiledWithDefaults.errors) { },
errorCallback(new Error(`/data/${idx}${err.dataPath}: ${err.message}`))
} {},
return prev )
}
}
prev[name] = value
return prev
}, { })
}
interface PropertiesV1 {
name: string
value: string
description: string | null
copyable: boolean
qr: boolean
} }
type PackagePropertiesV1 = PropertiesV1[] type PackagePropertiesV1 = PropertiesV1[]
export type PackageProperties = PackagePropertiesV2 export type PackageProperties = PackagePropertiesV2
export type PackagePropertiesVersioned<T extends number> = { export type PackagePropertiesVersioned<T extends number> = {
version: T, version: T
data: PackagePropertiesVersionedData<T> data: PackagePropertiesVersionedData<T>
} }
export type PackagePropertiesVersionedData<T extends number> = export type PackagePropertiesVersionedData<T extends number> = T extends 1
T extends 1 ? PackagePropertiesV1 : ? PackagePropertiesV1
T extends 2 ? PackagePropertiesV2 : : T extends 2
never ? PackagePropertiesV2
: never
interface PackagePropertiesV2 {
[name: string]: PackagePropertyString | PackagePropertyObject
}
interface PackagePropertyBase {
type: 'string' | 'object'
description: string | null
}
interface PackagePropertyString extends PackagePropertyBase {
type: 'string'
value: string
copyable: boolean
qr: boolean
masked: boolean
}
interface PackagePropertyObject extends PackagePropertyBase {
type: 'object'
value: PackagePropertiesV2
}