Fork of ajv
 
 
 
 
Go to file
Evgeny Poberezkin 32e83e5f28 exclude schemas without validation keywords, dont create data variables unless they are used at least twice 2015-06-12 22:12:34 +01:00
lib exclude schemas without validation keywords, dont create data variables unless they are used at least twice 2015-06-12 22:12:34 +01:00
spec exclude schemas without validation keywords, dont create data variables unless they are used at least twice 2015-06-12 22:12:34 +01:00
.gitignore inline simple rules 2015-05-30 09:53:04 +01:00
.gitmodules added JSON-schema-test-suite as submodule 2015-06-05 21:35:20 +01:00
LICENSE Initial commit 2015-05-20 00:23:32 +01:00
README.md fixed validation bug with recursive schemas 2015-06-12 14:22:33 +01:00
package.json exclude schemas without validation keywords, dont create data variables unless they are used at least twice 2015-06-12 22:12:34 +01:00
try.js refactor with defs; use numbered variable names in different levels 2015-06-03 22:30:04 +01:00

README.md

ajv - Another JSON Schema Validator

One of the fastest JSON Schema validators for node.js. It uses doT templates to generate super-fast validating functions.

JSON Schema standard

ajv implements full JSON Schema draft 4 standard:

  • all validation keywords
  • full support of remote refs (remote schemas have to be added with addSchema or compiled to be available)
  • correct string lengths for strings with unicode pairs (can be turned off)
  • formats defined by JSON Schema draft 4 standard (can be turned off)

ajv passes all the tests from JSON Schema Test Suite (apart from the one that requires that 1.0 is not an integer).

Benchmarks

Benchmark of the test suite - json-schema-benchmark.

Install

npm install ajv

Usage

var Ajv = require('ajv');
var ajv = Ajv(); // options can be passed
var validate = ajv.compile(schema);
var valid = validate(data);
if (!valid) console.log(validate.errors);

or

// ...
var valid = ajv.validate(schema, data);
if (!valid) console.log(ajv.errors);
// ...

or

// ...
ajv.addSchema(schema, 'mySchema');
var valid = ajv.validate('mySchema', data);
if (!valid) console.log(ajv.errors);
// ...

ajv compiles schemas to functions and caches them in all cases (using stringified schema as a key - using json-stable-stringify), so that the next time the same schema is used (not necessarily the same object instance) it won't be compiled again.

API

Ajv(Object options) -> Object

Create ajv instance.

All the instance methods below are bound to the instance, so they can be used without the instance.

.compile(Object schema) -> Function<Object data>

Generate validating function and cache the compiled schema for future use.

Validating function returns boolean and has properties errors with the errors from the last validation and schema with the reference to the original schema.

.validate(Object schema|String key|String ref, data) -> Boolean

Validate data using passed schema (it will be compiled and cached).

Instead of the schema you can use the key that was previously passed to addSchema, the schema id if it was present in the schema or any previously resolved reference.

Validation errors will be available in the errors property of ajv instance.

.addSchema(Array<Object>|Object schema [, String key]) -> Function|Array<Function>

Add and compile schema(s). It does the same as .compile with two differences:

  • array of schemas can be passed (schemas should have ids), the second parameter will be ignored.

  • key can be passed that can be used to reference the schema and will be used as the schema id if there is no id inside the schema. If the key is not passed, the schema id will be used as the key.

Once the schema added it and all the references inside it can be referenced in other schemas and used to validate data.

In the current version all the referenced schemas should be added before the schema that uses them is compiled, so the circular references are not supported.

.getSchema(String key) -> Function<Object data>

Retrieve compiled schema previously added with addSchema. Validating function has schema property with the reference to the original schema.

Options

  • allErrors: check all rules collecting all errors. Default is to return after the first error.
  • verbose: include the reference to the part of the schema and validated data in errors (false by default).
  • format: formats validation mode ('fast' by default). Pass 'full' for more correct and slow validation or false not to validate formats at all. E.g., 25:00:00 and 2015/14/33 will be invalid time and date in 'full' mode but it will be valid in 'fast' mode.
  • meta: add meta-schema so it can be used by other schemas (true by default).
  • uniqueItems: validate uniqueItems keyword (true by default).
  • unicode: calculate correct length of strings with unicode pairs (true by default). Pass false to use .length of strings that is faster, but gives "incorrect" lengths of strings with unicode pairs - each unicode pair is counted as two characters.
  • beautify: format the generated function with js-beautify (the validating function is generated without line-breaks). npm install js-beautify to use this option. true or js-beautify options can be passed.

Tests

git submodule update --init
npm test