https://github.com/mafintosh/is-my-json-valid)
is-my-json-valid (FORKED FROMA JSONSchema validator that uses code generation to be extremely fast
npm install is-my-json-valid
It passes the entire JSONSchema v4 test suite except for remoteRefs
and maxLength
/minLength
when using unicode surrogate pairs.
Usage
Simply pass a schema to compile it
var validator =var validate =consoleconsole// get the last list of errors by checking validate.errors// the following will print [{field: 'data.hello', message: 'is required'}]console
You can also pass the schema as a string
var validate =
Optionally you can use the require submodule to load a schema from __dirname
var validator =var validate =
Custom formats
is-my-json-valid supports the formats specified in JSON schema v4 (such as date-time). If you want to add your own custom formats pass them as the formats options to the validator
var validate =console // trueconsole // false
External schemas
You can pass in external schemas that you reference using the $ref
attribute as the schemas
option
var ext =required: truetype: 'string'var schema =$ref: '#ext' // references another schema called ext// pass the external schemas as an optionvar validate =// returns true// return false
Filtering away additional properties
is-my-json-valid supports filtering away properties not in the schema
var filter = validatorvar doc = hello: 'world' notInSchema: trueconsole // {hello: 'world'}
Verbose mode outputs the value on errors
is-my-json-valid outputs the value causing an error when verbose is set to true
var validate =;console // {field: 'data.hello', message: 'is the wrong type', value: 100}
Greedy mode tries to validate as much as possible
By default is-my-json-valid bails on first validation error but when greedy is set to true it tries to validate as much as possible:
var validate =;;console // [{field: 'data.y', message: 'is required'},// {field: 'data.x', message: 'is the wrong type'}]
Performance
is-my-json-valid uses code generation to turn your JSON schema into basic javascript code that is easily optimizeable by v8.
At the time of writing, is-my-json-valid is the fastest validator when running
If you know any other relevant benchmarks open a PR and I'll add them.
License
MIT