Created
December 18, 2020 10:30
-
-
Save wheresrhys/e4395058a89b9d958c5f7beaaad618ad to your computer and use it in GitHub Desktop.
JSON schema test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*** Validating ***/ | |
{ a: 'some' } | |
strict mode: missing type "string" for keyword "pattern" at "#/if/properties/a" (strictTypes) | |
/*** Validating ***/ | |
{ b: 'other' } | |
ValidationError: validation failed | |
at validate (/Users/rhys.evans/Projects/or/treecreeper/packages/tc-schema-validator/validation/ajv-test.js:28:15) | |
at Object.<anonymous> (/Users/rhys.evans/Projects/or/treecreeper/packages/tc-schema-validator/validation/ajv-test.js:33:1) | |
at Module._compile (internal/modules/cjs/loader.js:959:30) | |
at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10) | |
at Module.load (internal/modules/cjs/loader.js:815:32) | |
at Function.Module._load (internal/modules/cjs/loader.js:727:14) | |
at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10) | |
at internal/main/run_main_module.js:17:11 { | |
errors: [ | |
{ | |
keyword: 'additionalProperties', | |
dataPath: '', | |
schemaPath: '#/additionalProperties', | |
params: { additionalProperty: 'b' }, | |
message: 'should NOT have additional properties' | |
} | |
], | |
validation: true, | |
ajv: true | |
} | |
/*** Validating ***/ | |
{ a: 'some', b: 'other' } | |
ValidationError: validation failed | |
at validate (/Users/rhys.evans/Projects/or/treecreeper/packages/tc-schema-validator/validation/ajv-test.js:28:15) | |
at Object.<anonymous> (/Users/rhys.evans/Projects/or/treecreeper/packages/tc-schema-validator/validation/ajv-test.js:34:1) | |
at Module._compile (internal/modules/cjs/loader.js:959:30) | |
at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10) | |
at Module.load (internal/modules/cjs/loader.js:815:32) | |
at Function.Module._load (internal/modules/cjs/loader.js:727:14) | |
at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10) | |
at internal/main/run_main_module.js:17:11 { | |
errors: [ | |
{ | |
keyword: 'additionalProperties', | |
dataPath: '', | |
schemaPath: '#/additionalProperties', | |
params: { additionalProperty: 'b' }, | |
message: 'should NOT have additional properties' | |
} | |
], | |
validation: true, | |
ajv: true | |
} | |
/*** Validating ***/ | |
{ a: 'match', b: 'other' } | |
ValidationError: validation failed | |
at validate (/Users/rhys.evans/Projects/or/treecreeper/packages/tc-schema-validator/validation/ajv-test.js:28:15) | |
at Object.<anonymous> (/Users/rhys.evans/Projects/or/treecreeper/packages/tc-schema-validator/validation/ajv-test.js:35:1) | |
at Module._compile (internal/modules/cjs/loader.js:959:30) | |
at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10) | |
at Module.load (internal/modules/cjs/loader.js:815:32) | |
at Function.Module._load (internal/modules/cjs/loader.js:727:14) | |
at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10) | |
at internal/main/run_main_module.js:17:11 { | |
errors: [ | |
{ | |
keyword: 'additionalProperties', | |
dataPath: '', | |
schemaPath: '#/additionalProperties', | |
params: { additionalProperty: 'b' }, | |
message: 'should NOT have additional properties' | |
} | |
], | |
validation: true, | |
ajv: true | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Ajv = require('ajv').default; | |
const ajv = new Ajv({ allErrors: true }); | |
const schema = { | |
type: 'object', | |
properties: { | |
a: {type: 'string'}, | |
}, | |
additionalProperties: false, | |
if: { | |
properties: { | |
a: {pattern: 'match'} | |
} | |
}, | |
then: { | |
properties: { | |
b: {type: 'string'} | |
} | |
} | |
} | |
const validate = obj => { | |
console.log('/*** Validating ***/') | |
console.log(obj) | |
if (!ajv.validate(schema, obj)) { | |
console.dir(new Ajv.ValidationError(ajv.errors), { depth: 10 }); | |
} | |
} | |
validate({a: 'some'}) | |
validate({b: 'other'}) | |
validate({a: 'some', b: 'other'}) | |
validate({a: 'match', b: 'other'}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment