npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-schema-consolidate

v1.0.1

Published

Adapter to different JSON-schema (draft4) validators

Downloads

15

Readme

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

|validator|meta| ref |allErrors|formats|compile|fails| |---------|:--:|:---:|:-------:|:-----:|:-----:|:---:| |ajv|✓|✓|✓|✓|✓|-/1| |is-my-json-valid|✓|short|-|✓|✓|3/9| |jayschema|✓|✓|-|✓|-|1/5| |jjv|✓|✓|-|✓|-|3/4| |jsck|✓|✓|-|-|✓|3/11| |jsen|✓|-|-|✓|✓|7/7| |jsonschema|-|full|✓|-|-|4/3| |schemasaurus|-|-|-|RegExp|✓|8/10| |skeemas|✓|full|-|-|-|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