postmanlabs/is-my-json-valid

Name: is-my-json-valid

Owner: Postman

Description: A JSONSchema validator that uses code generation to be extremely fast

Forked from: mafintosh/is-my-json-valid

Created: 2017-07-06 10:28:44.0

Updated: 2017-07-06 10:28:46.0

Pushed: 2017-07-06 10:55:55.0

Homepage:

Size: 134

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

is-my-json-valid

A JSONSchema validator that uses code generation to be extremely fast

install is-my-json-valid

It passes the entire JSONSchema v4 test suite except for remoteRefs and maxLength/minLength when using unicode surrogate pairs.

build status

Usage

Simply pass a schema to compile it

validator = require('is-my-json-valid')

validate = validator({
quired: true,
pe: 'object',
operties: {
hello: {
  required: true,
  type: 'string'
}



ole.log('should be valid', validate({hello: 'world'}))
ole.log('should not be valid', validate({}))

et the last list of errors by checking validate.errors
he following will print [{field: 'data.hello', message: 'is required'}]
ole.log(validate.errors)

You can also pass the schema as a string

validate = validator('{"type": ... }')

Optionally you can use the require submodule to load a schema from __dirname

validator = require('is-my-json-valid/require')
validate = validator('my-schema.json')
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

validate = validator({
pe: 'string',
quired: true,
rmat: 'only-a'

rmats: {
'only-a': /^a+$/



ole.log(validate('aa')) // true
ole.log(validate('ab')) // false
External schemas

You can pass in external schemas that you reference using the $ref attribute as the schemas option

ext = {
quired: true,
pe: 'string'


schema = {
ef: '#ext' // references another schema called ext


ass the external schemas as an option
validate = validator(schema, {schemas: {ext: ext}})

date('hello') // returns true
date(42) // return false
Filtering away additional properties

is-my-json-valid supports filtering away properties not in the schema

filter = validator.filter({
quired: true,
pe: 'object',
operties: {
hello: {type: 'string', required: true}

ditionalProperties: false


doc = {hello: 'world', notInSchema: true}
ole.log(filter(doc)) // {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

validate = validator({
quired: true,
pe: 'object',
operties: {
hello: {
  required: true,
  type: 'string'
}


rbose: true


date({hello: 100});
ole.log(validate.errors) // {field: 'data.hello', message: 'is the wrong type', value: 100, type: 'string'}
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:

validate = validator({
pe: 'object',
operties: {
x: {
  type: 'number'
}

quired: ['x', 'y']

eedy: true


date({x: 'string'});
ole.log(validate.errors) // [{field: 'data.y', message: 'is required'},
                         //  {field: 'data.x', message: 'is the wrong type'}]
Error messages

Here is a list of possible message values for errors:

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


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.