Fork of ajv
 
 
 
 
Go to file
Evgeny Poberezkin 5dc25067d1 codeclimate - exclude dotjs 2015-07-21 18:58:49 +01:00
bin added anyOf error, refactored test 2015-06-18 23:01:39 +01:00
lib fixed required for inner level with many properties, closes #19 2015-07-20 17:41:06 +01:00
spec test with invalid data for #19 2015-07-20 17:49:49 +01:00
.codeclimate.yml codeclimate - exclude dotjs 2015-07-21 18:58:49 +01:00
.gitignore fixed broken template 2015-06-12 23:30:50 +01:00
.gitmodules added JSON-schema-test-suite as submodule 2015-06-05 21:35:20 +01:00
.travis.yml load tests from JSON-Schema-Test-Suite in travis 2015-06-16 23:03:02 +01:00
LICENSE Initial commit 2015-05-20 00:23:32 +01:00
README.md readme 2015-07-06 18:46:33 +01:00
package.json fixed required for inner level with many properties, closes #19 2015-07-20 17:41:06 +01:00

README.md

ajv - Another JSON Schema Validator

One of the fastest JSON Schema validators for node.js and browser.

It uses precompiled doT templates to generate super-fast validating functions.

Build Status npm version

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 and custom formats (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.

Same benchmark run on faster CPU with node 0.12.

Benchmark of individual test cases by z-schema.

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.errorsText());
// ...

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 (null if there were no errors) and schema with the reference to the original schema.

Unless options validateSchema is false, the schema will be validated against meta-schema and if schema is invalid the errors will be logged. See options.

.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 (null if there were no errors).

.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.

By default schema is validated against meta-schema before it is compiled and if the schema does not pass validation the exception is thrown. This behaviour is controlled by validateSchema option.

.validateSchema(Object schema) -> Boolean

Validates schema. This method should be used to validate schemas rather than validate due to the inconsistency of uri format in JSON-Schema standart.

By default this method is called automatically when the schema is added, so you rarely need to use it directly.

If schema doesn't have $schema property it is validated against draft 4 meta-schema (option meta should not be false).

If schema has $schema property then the schema with this id (should be previously added) is used to validate passed schema.

Errors will be available at ajv.errors.

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

Retrieve compiled schema previously added with addSchema by the key passed to addSchema or by its full reference (id). Returned validating function has schema property with the reference to the original schema.

.removeSchema(Object schema|String key|String ref)

Remove added/cached schema. Even if schema is referenced by other schemas it can be safely removed as dependent schemas have local references.

Schema can be removed using key passed to addSchema, it's full reference (id) or using actual schema object that will be stable-stringified to remove schema from cache.

.addFormat(String name, String|RegExp|Function format)

Add custom format to validate strings. It can also be used to replace pre-defined formats for ajv instance.

Strins be converted to RegExp.

Function should return validation result as true or false.

Custom formats can be also added via formats option.

.errorsText([Array