metaschema
Advanced tools
Comparing version 2.1.5 to 2.2.0
@@ -9,3 +9,3 @@ 'use strict'; | ||
class SchemaError { | ||
class ValidationResult { | ||
#path; | ||
@@ -20,6 +20,6 @@ | ||
add(err) { | ||
if (SchemaError.isInstance(err)) { | ||
if (ValidationResult.isInstance(err)) { | ||
this.errors.push(...err.errors); | ||
} else { | ||
const errs = SchemaError.format(err, this.#path); | ||
const errs = ValidationResult.format(err, this.#path); | ||
if (errs) this.errors.push(...errs); | ||
@@ -119,3 +119,3 @@ } | ||
if (!this.options.validate) return null; | ||
const error = new SchemaError(path); | ||
const error = new ValidationResult(path); | ||
try { | ||
@@ -129,2 +129,2 @@ return error.add(this.options.validate(value, path)); | ||
module.exports = { SchemaMetadata, SchemaError }; | ||
module.exports = { SchemaMetadata, ValidationResult }; |
@@ -115,2 +115,3 @@ 'use strict'; | ||
// eslint-disable-next-line class-methods-use-this | ||
functionField() { | ||
@@ -117,0 +118,0 @@ return {}; |
'use strict'; | ||
const { SchemaError } = require('../metadata.js'); | ||
const { ValidationResult } = require('../metadata.js'); | ||
const { formatters, checks } = require('../util.js'); | ||
@@ -30,15 +30,15 @@ | ||
check(value, path) { | ||
const schemaError = new SchemaError(path); | ||
const result = new ValidationResult(path); | ||
const isEmpty = value === null || value === undefined; | ||
if (!this.required && isEmpty) return schemaError; | ||
if (!this.required && isEmpty) return result; | ||
try { | ||
schemaError.add(this.checkType(value, path)); | ||
if (this.validate) schemaError.add(this.validate(value, path)); | ||
result.add(this.checkType(value, path)); | ||
if (this.validate) result.add(this.validate(value, path)); | ||
for (const [name, subCheck] of Object.entries(AbstractType.checks)) { | ||
if (!this[name]) continue; | ||
schemaError.add(subCheck(value, this)); | ||
result.add(subCheck(value, this)); | ||
} | ||
return schemaError; | ||
return result; | ||
} catch (err) { | ||
return schemaError.add(`validation failed ${String(err)}`); | ||
return result.add(`validation failed ${String(err)}`); | ||
} | ||
@@ -45,0 +45,0 @@ } |
@@ -6,3 +6,3 @@ 'use strict'; | ||
const { Preprocessor } = require('./preprocessor.js'); | ||
const { SchemaMetadata, SchemaError } = require('./metadata.js'); | ||
const { SchemaMetadata, ValidationResult } = require('./metadata.js'); | ||
const { Struct } = require('./struct.js'); | ||
@@ -71,5 +71,5 @@ const { isInstanceOf } = require('./util.js'); | ||
check(source, path = this.name) { | ||
const schemaError = new SchemaError(path); | ||
schemaError.add(this.validate(source, path)); | ||
return schemaError.add(this.fields.check(source, path)); | ||
const result = new ValidationResult(path); | ||
result.add(this.validate(source, path)); | ||
return result.add(this.fields.check(source, path)); | ||
} | ||
@@ -76,0 +76,0 @@ |
'use strict'; | ||
const { SchemaError } = require('./metadata.js'); | ||
const { ValidationResult } = require('./metadata.js'); | ||
const { formatters, isInstanceOf } = require('./util.js'); | ||
@@ -23,3 +23,3 @@ | ||
check(source, path = '') { | ||
const schemaError = new SchemaError(path || this.name); | ||
const result = new ValidationResult(path || this.name); | ||
const keys = Object.keys(source); | ||
@@ -32,3 +32,3 @@ const fields = Object.keys(this); | ||
if (!type) { | ||
schemaError.add(`Field "${name}" is not expected`); | ||
result.add(`Field "${name}" is not expected`); | ||
continue; | ||
@@ -39,8 +39,8 @@ } | ||
if (type.required && !keys.includes(name)) { | ||
schemaError.add(`Field "${nestedPath}" is required`); | ||
result.add(`Field "${nestedPath}" is required`); | ||
continue; | ||
} | ||
schemaError.add(type.check(value, nestedPath)); | ||
result.add(type.check(value, nestedPath)); | ||
} | ||
return schemaError; | ||
return result; | ||
} | ||
@@ -47,0 +47,0 @@ } |
@@ -40,3 +40,3 @@ 'use strict'; | ||
const firstKey = (obj) => Object.keys(obj).find((key) => isFirstLetter(key)); | ||
const firstKey = (obj) => Object.keys(obj).find(isFirstLetter); | ||
@@ -43,0 +43,0 @@ const isInstanceOf = (obj, constrName) => obj?.constructor?.name === constrName; |
@@ -29,3 +29,3 @@ type Scope = 'global' | 'system' | 'local' | 'memory'; | ||
interface SchemaError { | ||
interface ValidationResult { | ||
valid: boolean; | ||
@@ -69,3 +69,3 @@ errors: string[]; | ||
findReference(name: string): Schema; | ||
check(value: any): SchemaError; | ||
check(value: any): ValidationResult; | ||
toInterface(): string; | ||
@@ -76,3 +76,3 @@ attach(...namespaces: Array<Model>): void; | ||
toJSON(): object; | ||
validate(value: any, path: string): SchemaError; | ||
validate(value: any, path: string): ValidationResult; | ||
} | ||
@@ -79,0 +79,0 @@ |
{ | ||
"name": "metaschema", | ||
"version": "2.1.5", | ||
"version": "2.2.0", | ||
"author": "Timur Shemsedinov <timur.shemsedinov@gmail.com>", | ||
@@ -18,3 +18,3 @@ "description": "Metadata Schema and Interface Definition Language (IDL)", | ||
"type": "git", | ||
"url": "https://github.com/metarhia/metaschema" | ||
"url": "git+https://github.com/metarhia/metaschema.git" | ||
}, | ||
@@ -31,4 +31,11 @@ "bugs": { | ||
"main": "metaschema.js", | ||
"browser": { | ||
"./metaschema.js": "./dist.js" | ||
}, | ||
"types": "metaschema.d.ts", | ||
"files": ["lib/", "metaschema.d.ts"], | ||
"files": [ | ||
"lib/", | ||
"dist.js", | ||
"metaschema.d.ts" | ||
], | ||
"readmeFilename": "README.md", | ||
@@ -42,19 +49,19 @@ "scripts": { | ||
"engines": { | ||
"node": "16 || 18 || 19 || 20" | ||
"node": "18 || 20 || 21" | ||
}, | ||
"dependencies": { | ||
"metautil": "^3.10.0", | ||
"metavm": "^1.2.5" | ||
"metautil": "^5.2.1", | ||
"metavm": "^1.4.1" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^20.4.2", | ||
"eslint": "^8.45.0", | ||
"@types/node": "^20.9.1", | ||
"eslint": "^8.54.0", | ||
"eslint-config-metarhia": "^8.2.0", | ||
"eslint-config-prettier": "^8.7.0", | ||
"eslint-plugin-import": "^2.27.5", | ||
"eslint-plugin-prettier": "^5.0.0", | ||
"eslint-config-prettier": "^9.0.0", | ||
"eslint-plugin-import": "^2.20.0", | ||
"eslint-plugin-prettier": "^5.0.1", | ||
"metatests": "^0.8.2", | ||
"prettier": "^3.0.0", | ||
"prettier": "^3.1.0", | ||
"typescript": "^5.1.6" | ||
} | ||
} |
@@ -18,6 +18,45 @@ # Metaschema | ||
## Examples | ||
```js | ||
const { Schema } = require('metaschema'); | ||
const schema = Schema.from({ | ||
name: { | ||
first: 'string', | ||
last: 'string', | ||
third: '?string', | ||
}, | ||
age: 'number', | ||
levelOne: { | ||
levelTwo: { | ||
levelThree: { type: 'enum', enum: [1, 2, 3] }, | ||
}, | ||
}, | ||
collection: { array: { array: 'number' } }, | ||
}); | ||
const data = { | ||
name: { | ||
first: 'a', | ||
last: 'b', | ||
}, | ||
age: 5, | ||
levelOne: { levelTwo: { levelThree: 1 } }, | ||
collection: [ | ||
[1, 2, 3], | ||
[3, 5, 6], | ||
], | ||
}; | ||
console.log(schema.check(data)); | ||
// Output: | ||
// ValidationResult { errors: [], valid: true } | ||
``` | ||
## License & Contributors | ||
Copyright (c) 2017-2023 [Metarhia contributors](https://github.com/metarhia/metaschema/graphs/contributors). | ||
Copyright (c) 2017-2024 [Metarhia contributors](https://github.com/metarhia/metaschema/graphs/contributors). | ||
Metaschema is [MIT licensed](./LICENSE).\ | ||
Metaschema is a part of [Metarhia](https://github.com/metarhia) technology stack. |
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
34124
22
977
62
+ Addedmetautil@5.2.4(transitive)
- Removedmetautil@3.15.0(transitive)
Updatedmetautil@^5.2.1
Updatedmetavm@^1.4.1