1.0.1 • Published 10 years ago

json-schema-consolidate v1.0.1

Weekly downloads
2
License
MIT
Repository
github
Last release
10 years ago

json-schema-consolidate

Adapter to different JSON-schema (draft4) validators

Build Status

Supported validators

You must install the validator(s) you use separately.

See Validators compatibility and json-schema-benchmark.

Install

npm install json-schema-consolidate
npm install <validator>

Usage

var consolidate = require('json-schema-consolidate');
var Validator = consolidate('<validator>');
var validator = new Validator(options); // or Validator(options);

or

var consolidate = require('json-schema-consolidate');
var validator = consolidate('<validator>', options);

Validate

var result = validator.validate(schema, json); // { valid: true/false, errors: [...] }

schema can be a string, in which case it will be JSON.parse'd.

If you need to validate with the previously added schema, you can either use getSchema to retrieve it or pass { $ref: '<id>' } as the schema.

If the referenced schema is missing it is an error (for all validators - tv4 is corrected in this case).

For compiling validators, this method will cache compiled schemas using serialized schema as a key (json-stable-stringify is used).

Compile

(create validating function)

var validate = validator.compile(schema);
var result = validate(json); // { valid: true/false, errors: [...] }

For interpreting validators this method will simply return a closure that can be used to validate json, but there will be no performance gain.

Add schema

(that can be referred to in other schemas)

validator.addSchema(schema, id);

If id is not passed, schema.id will be used

schema can be array of schemas, in which case the second parameter is not used.

Get schema

(previously added)

var schema = validator.getSchema(id);

Options

These options are available in all supported validators:

  • allErrors - continue validation after errors and return all validation errors.

  • schemas - include some schemas, same result as calling addSchema method.

  • formats - define additional formats, most validators support RegExp and functions. Format function should return validation success as boolean for ALL validators used with json-schema-consolidate.

Validator specific options can also be passed.

Validators compatibility

validatormetarefallErrorsformatscompilefails
ajv-/1
is-my-json-validshort-3/9
jayschema--1/5
jjv--3/4
jsck--3/11
jsen--7/7
jsonschema-full--4/3
schemasaurus---RegExp8/10
skeemasfull---3/1
themis-3/8
tv4--2/11
z-schema-/6
  • meta: validator can correctly validate schema against meta-schema. Some validators validate valid schemas as invalid or just throw error in this test.

  • ref: support for referencing schemas in other files. Some validators support only full uris, some only short uris and some support both ().

  • allErrors: if supported, the validator will stop after the first error unless this options is set to true.

  • formats: most validators support functions and RegExp (some only with this package) as custom formats (). Some support only RegExp.

  • compile: validators that compile schemas into validating functions. Even if a validator doesn't compile schemas, you can use compile method - it will return a function that will validate using the passed schema.

  • fails: the number of tests that fails. The first number - tests in json-schema-consolidate interface tests, the second - the tests in the official json-schema draft4 test suite.

Running tests

To run tests you need to install json-schema-tests and all validators:

npm install
npm install -g coffee-script
npm install -g json-schema-tests

Then you can run tests with ./test script:

./test
./test --full
./test <validator>
./test <validator> --short

Skipped tests are features in validators that are either not implemented or failing.

License

MIT

1.0.1

10 years ago

1.0.0

10 years ago

0.1.14

10 years ago

0.1.13

10 years ago

0.1.12

10 years ago

0.1.11

10 years ago

0.1.10

10 years ago

0.1.9

10 years ago

0.1.8

10 years ago

0.1.7

10 years ago

0.1.6

10 years ago

0.1.5

10 years ago

0.1.4

10 years ago

0.1.2

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago

0.0.0

10 years ago