metaschema
Advanced tools
Comparing version 1.3.4 to 1.4.0
@@ -5,2 +5,12 @@ # Changelog | ||
## [1.4.0][] - 2022-02-23 | ||
- Fix nullable field long-form | ||
- Optional for nested structures | ||
- Shorthand for optional nested structure | ||
- Fix paths in validation errors | ||
- Unify nested schema fields to | ||
`{ type: 'schema', schema: Schema, /* other fields */ }` | ||
- Support Schema#validate function | ||
## [1.3.4][] - 2021-09-10 | ||
@@ -104,3 +114,4 @@ | ||
[unreleased]: https://github.com/metarhia/metaschema/compare/v1.3.4...HEAD | ||
[unreleased]: https://github.com/metarhia/metaschema/compare/v1.4.0...HEAD | ||
[1.4.0]: https://github.com/metarhia/metaschema/compare/v1.3.4...v1.4.0 | ||
[1.3.4]: https://github.com/metarhia/metaschema/compare/v1.3.3...v1.3.4 | ||
@@ -107,0 +118,0 @@ [1.3.3]: https://github.com/metarhia/metaschema/compare/v1.3.2...v1.3.3 |
@@ -169,11 +169,12 @@ 'use strict'; | ||
if (this.preprocessIndex(key, entry)) continue; | ||
if (entry.json) { | ||
const json = Schema.from(entry.json); | ||
this.fields[key] = Object.assign({}, entry, { type: 'json', json }); | ||
} else { | ||
const schema = Schema.from(entry); | ||
this.fields[key] = schema; | ||
this.references = new Set([...this.references, ...schema.references]); | ||
this.relations = new Set([...this.relations, ...schema.relations]); | ||
} | ||
const schema = Schema.from(entry.schema || entry); | ||
const required = !key.endsWith('?'); | ||
const name = required ? key : key.slice(0, -1); | ||
this.fields[name] = Object.assign( | ||
{ required }, | ||
entry.schema ? entry : {}, | ||
{ type: 'schema', schema } | ||
); | ||
this.references = new Set([...this.references, ...schema.references]); | ||
this.relations = new Set([...this.relations, ...schema.relations]); | ||
continue; | ||
@@ -183,3 +184,3 @@ } | ||
if (!required) type = type.substring(1); | ||
const def = short ? toLongForm(type, entry) : entry; | ||
const def = short ? toLongForm(type, entry) : { ...entry, type }; | ||
if (!Reflect.has(def, 'required')) def.required = required; | ||
@@ -210,2 +211,8 @@ if (def.length) def.length = formatLength(def.length); | ||
static extractSchema(def) { | ||
if (def instanceof Schema) return def; | ||
if (def.schema instanceof Schema) return def.schema; | ||
return null; | ||
} | ||
checkConsistency() { | ||
@@ -245,2 +252,17 @@ const warn = []; | ||
check(value, path = '') { | ||
if (this.validate) { | ||
try { | ||
const res = this.validate(value, path); | ||
const valid = typeof res === 'boolean' ? res : Boolean(res.valid); | ||
const errors = (res && res.errors) || []; | ||
return { valid, errors }; | ||
} catch (err) { | ||
return { | ||
valid: false, | ||
errors: [ | ||
`Field "${path || this.name}" validation failed ${String(err)}`, | ||
], | ||
}; | ||
} | ||
} | ||
const target = this.kind === 'scalar' ? { value } : value || {}; | ||
@@ -253,3 +275,4 @@ const keys = Object.keys(target); | ||
const shortDef = this.fields[shorthand]; | ||
const errs = check(shorthand, shortDef, value); | ||
const name = path ? `${path}.${shorthand}` : shorthand; | ||
const errs = check(name, shortDef, value); | ||
if (errs.length === 0) return { valid: true, errors }; | ||
@@ -259,2 +282,3 @@ } | ||
for (const name of names) { | ||
const nestedPath = path ? `${path}.${name}` : name; | ||
const value = target[name]; | ||
@@ -266,20 +290,17 @@ let def = this.fields[name]; | ||
if (!def) { | ||
errors.push(`Field "${path}${name}" is not expected`); | ||
errors.push(`Field "${nestedPath}" is not expected`); | ||
continue; | ||
} | ||
if (def instanceof Schema) { | ||
const subcheck = def.check(value, name + '.'); | ||
const schema = Schema.extractSchema(def); | ||
if (schema) { | ||
if (!def.required && value === undefined) continue; | ||
const subcheck = schema.check(value, nestedPath); | ||
if (!subcheck.valid) errors.push(...subcheck.errors); | ||
continue; | ||
} | ||
if (def.json instanceof Schema) { | ||
const subcheck = def.json.check(value, name + '.'); | ||
if (!subcheck.valid) errors.push(...subcheck.errors); | ||
continue; | ||
} | ||
if (def.required && !keys.includes(name)) { | ||
errors.push(`Field "${path}${name}" is required`); | ||
errors.push(`Field "${nestedPath}" is required`); | ||
continue; | ||
} | ||
const errs = check(name, def, value); | ||
const errs = check(nestedPath, def, value); | ||
if (errs.length > 0) errors.push(...errs); | ||
@@ -286,0 +307,0 @@ } |
@@ -37,3 +37,5 @@ type Scope = 'global' | 'system' | 'local' | 'memory'; | ||
relations: Set<Relation>; | ||
validate: Function | null; | ||
validate: | ||
| ((value: any, path: string) => boolean | { valid: boolean; errors?: [] }) | ||
| null; | ||
format: Function | null; | ||
@@ -40,0 +42,0 @@ parse: Function | null; |
{ | ||
"name": "metaschema", | ||
"version": "1.3.4", | ||
"version": "1.4.0", | ||
"author": "Timur Shemsedinov <timur.shemsedinov@gmail.com>", | ||
@@ -43,19 +43,19 @@ "description": "Metadata Schema and Interface Definition Language (IDL)", | ||
"engines": { | ||
"node": "^12.9 || 14 || 16" | ||
"node": "^12.9 || 14 || 16 || 17" | ||
}, | ||
"dependencies": { | ||
"metautil": "^3.5.11", | ||
"metautil": "^3.5.18", | ||
"metavm": "^1.0.3" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^16.9.1", | ||
"eslint": "^7.32.0", | ||
"@types/node": "^17.0.19", | ||
"eslint": "^8.9.0", | ||
"eslint-config-metarhia": "^7.0.1", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-import": "^2.24.2", | ||
"eslint-config-prettier": "^8.4.0", | ||
"eslint-plugin-import": "^2.25.4", | ||
"eslint-plugin-prettier": "^4.0.0", | ||
"metatests": "^0.7.2", | ||
"prettier": "^2.4.0", | ||
"typescript": "^4.4.2" | ||
"prettier": "^2.5.1", | ||
"typescript": "^4.5.5" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24185
496
Updatedmetautil@^3.5.18