Skip to content

Instantly share code, notes, and snippets.

@wheresrhys
Created December 18, 2020 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wheresrhys/e4395058a89b9d958c5f7beaaad618ad to your computer and use it in GitHub Desktop.
Save wheresrhys/e4395058a89b9d958c5f7beaaad618ad to your computer and use it in GitHub Desktop.
JSON schema test
/*** 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
}
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