ajv/README.md

687 lines
35 KiB
Markdown
Raw Normal View History

2015-06-07 23:59:04 +03:00
# ajv - Another JSON Schema Validator
2015-05-20 03:55:53 +03:00
2015-09-13 02:50:56 +03:00
Currently the fastest JSON Schema validator for node.js and browser.
It uses [doT templates](https://github.com/olado/doT) to generate super-fast validating functions.
2015-11-22 03:31:01 +03:00
[![Build Status](https://travis-ci.org/epoberezkin/ajv.svg)](https://travis-ci.org/epoberezkin/ajv)
2015-06-14 13:40:24 +03:00
[![npm version](https://badge.fury.io/js/ajv.svg)](http://badge.fury.io/js/ajv)
2015-07-21 21:06:42 +03:00
[![Code Climate](https://codeclimate.com/github/epoberezkin/ajv/badges/gpa.svg)](https://codeclimate.com/github/epoberezkin/ajv)
2015-08-11 02:46:00 +03:00
[![Test Coverage](https://codeclimate.com/github/epoberezkin/ajv/badges/coverage.svg)](https://codeclimate.com/github/epoberezkin/ajv/coverage)
2015-06-18 16:59:11 +03:00
2015-11-22 03:31:01 +03:00
NB: [Upgrading to version 2.0.0](https://github.com/epoberezkin/ajv/releases/tag/2.0.0).
## Features
- ajv implements full [JSON Schema draft 4](http://json-schema.org/) standard:
- all validation keywords (see [JSON-Schema validation keywords](https://github.com/epoberezkin/ajv/blob/master/KEYWORDS.md))
- full support of remote refs (remote schemas have to be added with `addSchema` or compiled to be available)
- support of circular dependencies between schemas
- correct string lengths for strings with unicode pairs (can be turned off)
- [formats](#formats) defined by JSON Schema draft 4 standard and custom formats (can be turned off)
- [validates schemas against meta-schema](#api-validateschema)
- supports [browsers](#using-in-browser) and nodejs 0.10-5.0
- [asynchronous loading](#asynchronous-compilation) of referenced schemas during compilation
2015-11-22 03:31:01 +03:00
- "All errors" validation mode with [option allErrors](#options)
- [error messages with parameters](#validation-errors) describing error reasons to allow creating custom error messages
2015-11-24 09:35:50 +03:00
- i18n error messages support with [ajv-i18n](https://github.com/epoberezkin/ajv-i18n) package (version >= 1.0.0)
- [filtering data](#filtering-data) from additional properties
2015-12-13 00:18:41 +03:00
- [custom keywords](#defining-custom-keywords)
2015-12-25 03:59:36 +03:00
- keywords `switch`, `constant`, `contains`, `patternGroups`, `formatMaximum` / `formatMinimum` and `exclusiveFormatMaximum` / `exclusiveFormatMinimum` from [JSON-schema v5 proposals](https://github.com/json-schema/json-schema/wiki/v5-Proposals) with [option v5](#options)
- [v5 meta-schema](https://raw.githubusercontent.com/epoberezkin/ajv/master/lib/refs/json-schema-v5.json#) for schemas using v5 keywords.
- NEW: [v5 $data reference](v5-$data-reference) to use values from the validated data as values for the schema keywords.
2015-06-07 23:55:40 +03:00
Currently ajv is the only validator that passes all the tests from [JSON Schema Test Suite](https://github.com/json-schema/JSON-Schema-Test-Suite) (according to [json-schema-benchmark](https://github.com/ebdrup/json-schema-benchmark), apart from the test that requires that `1.0` is not an integer that is impossible to satisfy in JavaScript).
2015-06-07 23:55:40 +03:00
## Performance
2015-06-15 10:00:40 +03:00
ajv generates code to turn JSON schemas into javascript functions that are efficient for v8 optimization.
2015-06-15 10:00:40 +03:00
Currently ajv is the fastest validator according to these benchmarks:
2015-06-15 10:00:40 +03:00
- [json-schema-benchmark](https://github.com/ebdrup/json-schema-benchmark) - 70% faster than the second place
- [jsck benchmark](https://github.com/pandastrike/jsck#benchmarks) - 20-190% faster
- [z-schema benchmark](https://rawgit.com/zaggino/z-schema/master/benchmark/results.html)
- [themis benchmark](https://cdn.rawgit.com/playlyfe/themis/master/benchmark/results.html)
2015-06-15 10:00:40 +03:00
2015-05-30 01:22:11 +03:00
## Install
```
2015-05-30 01:32:47 +03:00
npm install ajv
2015-05-30 01:22:11 +03:00
```
## Usage
2015-10-19 22:25:27 +03:00
Try it in the node REPL: https://tonicdev.com/npm/ajv
2015-08-08 12:55:37 +03:00
The fastest validation call:
2015-05-30 01:22:11 +03:00
```
2015-06-08 02:19:34 +03:00
var Ajv = require('ajv');
var ajv = Ajv(); // options can be passed, e.g. {allErrors: true}
2015-05-30 01:32:47 +03:00
var validate = ajv.compile(schema);
2015-05-30 23:08:31 +03:00
var valid = validate(data);
if (!valid) console.log(validate.errors);
2015-05-30 01:22:11 +03:00
```
2015-08-08 12:55:37 +03:00
or with less code
2015-05-30 01:22:11 +03:00
```
2015-05-30 23:08:31 +03:00
// ...
var valid = ajv.validate(schema, data);
if (!valid) console.log(ajv.errors);
2015-05-30 23:08:31 +03:00
// ...
2015-05-30 01:22:11 +03:00
```
2015-06-08 02:19:34 +03:00
or
```
// ...
ajv.addSchema(schema, 'mySchema');
var valid = ajv.validate('mySchema', data);
2015-06-17 04:05:52 +03:00
if (!valid) console.log(ajv.errorsText());
2015-06-08 02:19:34 +03:00
// ...
```
ajv compiles schemas to functions and caches them in all cases (using stringified schema as a key - using [json-stable-stringify](https://github.com/substack/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.
2015-05-30 01:22:11 +03:00
2015-08-09 14:49:05 +03:00
The best performance is achieved when using compiled functions returned by `compile` or `getSchema` methods (there is no additional function call).
2015-08-08 12:55:37 +03:00
__Please note__: every time validation function or `ajv.validate` are called `errors` property is overwritten. You need to copy `errors` array reference to another variable if you want to use it later (e.g., in the callback). See [Validation errors](#validation-errors)
2015-10-22 23:14:17 +03:00
2015-05-30 01:22:11 +03:00
## Using in browser
You can require ajv directly from the code you browserify - in this case ajv will be a part of your bundle.
2015-12-06 23:34:46 +03:00
If you need to use ajv in several bundles you can create a separate browserified bundle using `npm run bundle` script (thanks to [siddo420](https://github.com/siddo420)).
Then you need to load ajv in the browser:
```
<script src="ajv.bundle.js"></script>
```
Now you can use it as shown above - `require` will be global and you can `require('ajv')`.
Ajv was tested with these browsers:
[![Sauce Test Status](https://saucelabs.com/browser-matrix/epoberezkin.svg)](https://saucelabs.com/u/epoberezkin)
2015-07-22 00:36:20 +03:00
## Formats
The following formats are supported for string validation with "format" keyword:
2015-12-20 00:23:40 +03:00
- _date_: full-date according to [RFC3339](http://tools.ietf.org/html/rfc3339#section-5.6).
2015-12-06 23:34:46 +03:00
- _time_: time with optional time-zone.
- _date-time_: date-time from the same source (time-zone is mandatory). `date`, `time` and `date-time` validate ranges in `full` mode and only regexp in `fast` mode (see [options](#options)).
2015-07-22 00:36:20 +03:00
- _uri_: full uri with optional protocol.
- _email_: email address.
2015-12-20 00:23:40 +03:00
- _hostname_: host name acording to [RFC1034](http://tools.ietf.org/html/rfc1034#section-3.5).
2015-07-22 00:36:20 +03:00
- _ipv4_: IP address v4.
- _ipv6_: IP address v6.
- _regex_: tests whether a string is a valid regular expression by passing it to RegExp constructor.
2015-12-20 00:23:40 +03:00
- _uuid_: Universally Unique IDentifier according to [RFC4122](http://tools.ietf.org/html/rfc4122).
- _json-pointer_: JSON-pointer according to [RFC6901](https://tools.ietf.org/html/rfc6901).
2015-12-20 00:51:17 +03:00
- _relative-json-pointer_: relative JSON-pointer according to [this draft](http://tools.ietf.org/html/draft-luff-relative-json-pointer-00).
2015-07-22 00:36:20 +03:00
2015-12-20 00:51:17 +03:00
There are two modes of format validation: `fast` and `full`. This mode affects formats `date`, `time`, `date-time`, `uri`, `email`, and `hostname`. See [Options](#options) for details.
2015-07-22 00:36:20 +03:00
2015-07-22 01:16:02 +03:00
You can add additional formats and replace any of the formats above using [addFormat](#api-addformat) method.
2015-07-22 00:36:20 +03:00
You can find patterns used for format validation and the sources that were used in [formats.js](https://github.com/epoberezkin/ajv/blob/master/lib/compile/formats.js).
2015-12-25 03:59:36 +03:00
## $data reference
With `v5` option you can use values from the validated data as the values for the schema keywords. See [v5 proposal](https://github.com/json-schema/json-schema/wiki/$data-(v5-proposal)) for more information about how it works.
`$data` reference is supported in the keywords: constant, enum, format, maximum/minimum, exclusiveMaximum / exclusiveMinimum, maxLength / minLength, maxItems / minItems, maxProperties / minProperties, formatMaximum / formatMinimum, exclusiveFormatMaximum / exclusiveFormatMinimum, multipleOf, pattern, required, uniqueItems.
The value of "$data" should be a [relative JSON-pointer](http://tools.ietf.org/html/draft-luff-relative-json-pointer-00).
Examples.
This schema requires that the value in property `smaller` is less or equal than the value in the property larger:
```
var schema = {
"properties": {
"smaller": {
"type": number,
"maximum": { "$data": "1/larger" }
},
"larger": { "type": number }
}
};
var validData = {
smaller: 5,
larger: 7
};
```
This schema requires that the properties have the same format as their field names:
```
var schema = {
"additionalProperties": {
"type": "string",
"format": { "$data": "0#" }
}
};
var validData = {
'date-time': '1963-06-19T08:30:06.283185Z',
email: 'joe.bloggs@example.com'
}
```
2015-11-13 15:16:31 +03:00
## Defining custom keywords
2015-11-22 04:27:52 +03:00
Starting from version 2.0.0 ajv supports custom keyword definitions.
2015-11-13 15:16:31 +03:00
WARNING: The main drawback of extending JSON-schema standard with custom keywords is the loss of portability of your schemas - it may not be possible to support these custom keywords on some other platforms. Also your schemas may be more challenging to read for other people. If portability is important you may prefer using additional validation logic outside of JSON-schema rather than putting it inside your JSON-schema.
2015-11-13 15:16:31 +03:00
The advantages of using custom keywords are:
- they allow you keeping a larger portion of your validation logic in the schema
- they make your schemas more expressive and less verbose
- they are fun to use
You can define custom keywords with [addKeyword](#api-addkeyword) method. Keywords are defined on the `ajv` instance level - new instances will not have previously defined keywords.
Ajv allows defining keywords with:
- validation function
- compilation function
- macro function
- inline compilation function that should return code (as string) that will be inlined in the currently compiled schema.
2015-11-13 15:16:31 +03:00
### Define keyword with validation function (NOT RECOMMENDED)
2015-11-13 15:16:31 +03:00
Validation function will be called during data validation. It will be passed schema, data and parentSchema (if it has 3 arguments) at validation time and it should return validation result as boolean. It can return an array of validation errors via `.errors` property of itself (otherwise a standard error will be used).
2015-11-13 15:16:31 +03:00
This way to define keywords is added as a way to quickly test your keyword and is not recommended because of worse performance than compiling schemas.
2015-11-13 15:16:31 +03:00
2015-12-06 23:34:46 +03:00
Example. `constant` keyword from version 5 proposals (that is equivalent to `enum` keyword with one item):
2015-11-13 15:16:31 +03:00
```
ajv.addKeyword('constant', { validate: function (schema, data) {
return typeof schema == 'object && schema !== null'
? deepEqual(schema, data)
: schema === data;
} });
2015-11-13 15:16:31 +03:00
var schema = { "constant": 2 };
var validate = ajv.compile(schema);
console.log(validate(2)); // true
console.log(validate(3)); // false
var schema = { "constant": { "foo": "bar" } };
var validate = ajv.compile(schema);
console.log(validate({foo: 'bar'})); // true
console.log(validate({foo: 'baz'})); // false
```
2015-12-06 23:34:46 +03:00
`constant` keyword is already available in Ajv with option `v5: true`.
2015-11-13 15:16:31 +03:00
### Define keyword with "compilation" function
2015-11-13 15:16:31 +03:00
Compilation function will be called during schema compilation. It will be passed schema and parent schema and it should return a validation function. This validation function will be passed data during validation; it should return validation result as boolean and it can return an array of validation errors via `.errors` property of itself (otherwise a standard error will be used).
In some cases it is the best approach to define keywords, but it has the performance cost of an extra function call during validation. If keyword logic can be expressed via some other JSON-schema then `macro` keyword definition is more efficient (see below).
2015-11-13 15:16:31 +03:00
Example. `range` and `exclusiveRange` keywords using compiled schema:
```
ajv.addKeyword('range', { type: 'number', compile: function (sch, parentSchema) {
var min = sch[0];
var max = sch[1];
return parentSchema.exclusiveRange === true
? function (data) { return data > min && data < max; }
: function (data) { return data >= min && data <= max; }
} });
2015-11-13 15:16:31 +03:00
var schema = { "range": [2, 4], "exclusiveRange": true };
var validate = ajv.compile(schema);
console.log(validate(2.01)); // true
console.log(validate(3.99)); // true
console.log(validate(2)); // false
console.log(validate(4)); // false
```
2015-11-13 15:16:31 +03:00
### Define keyword with "macro" function
2015-11-13 15:16:31 +03:00
"Macro" function is called during schema compilation. It is passed schema and parent schema and it should return another schema that will be applied to the data in addition to the original schema (if schemas have different keys they are merged, otherwise `allOf` keyword is used).
2015-11-13 15:16:31 +03:00
It is the most efficient approach (in cases when the keyword logic can be expressed with another JSON-schema) because it is usually easy to implement and there is no extra function call during validation.
2015-12-06 23:34:46 +03:00
The disadvantage of macro keywords is that you won't receive any keyword related error message if it fails (and you can't define custom errors for them).
2015-12-06 23:34:46 +03:00
Example. `range` and `exclusiveRange` keywords from the previous example defined with macro:
2015-11-13 15:16:31 +03:00
```
ajv.addKeyword('range', { macro: function (schema, parentSchema) {
return {
minimum: schema[0],
maximum: schema[1],
exclusiveMinimum: !!parentSchema.exclusiveRange,
exclusiveMaximum: !!parentSchema.exclusiveRange
};
} });
2015-11-13 15:16:31 +03:00
```
2015-12-06 23:34:46 +03:00
Example. `contains` keyword from version 5 proposals that requires that the array has at least one item matching schema (see https://github.com/json-schema/json-schema/wiki/contains-(v5-proposal)):
```
ajv.addKeyword('contains', { macro: function (schema) {
return { "not": { "items": { "not": schema } } };
} });
var schema = {
"contains": {
"type": "number",
"minimum": 4,
"exclusiveMinimum": true
}
};
var validate = ajv.compile(schema);
console.log(validate([1,2,3])); // false
console.log(validate([2,3,4])); // false
console.log(validate([3,4,5])); // true, number 5 matches schema inside "contains"
```
2015-12-06 23:34:46 +03:00
`contains` keyword is already available in Ajv with option `v5: true`.
2015-11-22 03:31:01 +03:00
See the example of defining recursive macro keyword `deepProperties` in the [test](https://github.com/epoberezkin/ajv/blob/master/spec/custom.spec.js#L151).
2015-11-13 15:16:31 +03:00
### Define keyword with "inline" compilation function
2015-12-23 19:56:33 +03:00
Inline compilation function is called during schema compilation. It is passed four parameters: `it` (the current schema compilation context), `keyword` (added in v3.0 to allow compiling multiple keywords with a single function), `schema` and `parentSchema` and it should return the code (as a string) that will be inlined in the code of compiled schema. This code can be either an expression that evaluates to the validation result (boolean) or a set of statements that assigns the validation result to a variable.
While it can be more difficult to define keywords with "inline" functions, it can have the best performance.
Example `even` keyword:
```
2015-12-23 19:56:33 +03:00
ajv.addKeyword('even', { type: 'number', inline: function (it, keyword, schema) {
var op = schema ? '===' : '!==';
return 'data' + (it.dataLevel || '') + ' % 2 ' + op + ' 0';
} });
var schema = { "even": true };
var validate = ajv.compile(schema);
console.log(validate(2)); // true
console.log(validate(3)); // false
```
`'data' + (it.dataLevel || '')` in the example above is the reference to the currently validated data. Also note that `schema` (keyword schema) is the same as `it.schema.even`, so schema is not strictly necessary here - it is passed for convenience.
Example `range` keyword defined using [doT template](https://github.com/olado/doT):
```
var doT = require('dot');
var inlineRangeTemplate = doT.compile("\
{{ \
var $data = 'data' + (it.dataLevel || '') \
, $min = it.schema.range[0] \
, $max = it.schema.range[1] \
, $gt = it.schema.exclusiveRange ? '>' : '>=' \
, $lt = it.schema.exclusiveRange ? '<' : '<='; \
}} \
var valid{{=it.level}} = {{=$data}} {{=$gt}} {{=$min}} && {{=$data}} {{=$lt}} {{=$max}}; \
");
ajv.addKeyword('range', {
type: 'number',
inline: inlineRangeTemplate,
statements: true
});
```
`'valid' + it.level` in the example above is the expected name of the variable that should be set to the validation result.
Property `statements` in the keyword definition should be set to `true` if the validation code sets the variable instead of evaluating to the validation result.
### Defining errors in custom keywords
All custom keywords but macro keywords can create custom error messages.
2015-12-06 23:34:46 +03:00
Validating and compiled keywords should define errors by assigning them to `.errors` property of the validation function.
2015-11-22 03:31:01 +03:00
Inline custom keyword should increase error counter `errors` and add error to `vErrors` array (it can be null). See [example range keyword](https://github.com/epoberezkin/ajv/blob/master/spec/custom_rules/range_with_errors.jst).
When inline keyword performs validation Ajv checks whether it created errors by comparing errors count before and after validation. To skip this check add option `errors` (can be `"full"`, `true` or `false`) to keyword definition:
```
ajv.addKeyword('range', {
type: 'number',
inline: inlineRangeTemplate,
statements: true,
errors: true // keyword should create custom errors when validation fails
// or errors: 'full' // created errors should have dataPath already set
});
```
Each error object should have properties `keyword`, `message` and `params`, other properties will be added.
Inlined keywords can optionally define `dataPath` property in error objects, that will be added by ajv unless `errors` option of the keyword is `"full"`.
If custom keyword doesn't create errors, the default error will be created in case the keyword fails validation (see [Validation errors](#validation-errors)).
## Asynchronous compilation
2015-12-06 23:34:46 +03:00
During asynchronous compilation remote references are loaded using supplied function. See `compileAsync` method and `loadSchema` option.
Example:
```
var ajv = Ajv({ loadSchema: loadSchema });
ajv.compileAsync(schema, function (err, validate) {
if (err) return;
var valid = validate(data);
});
function loadSchema(uri, callback) {
request.json(uri, function(err, res, body) {
if (err || res.statusCode >= 400)
callback(err || new Error('Loading error: ' + res.statusCode));
else
callback(null, body);
});
}
```
2015-07-30 11:21:36 +03:00
## Filtering data
With [option `removeAdditional`](#options) (added by [andyscott](https://github.com/andyscott)) you can filter data during the validation.
This option modifies original object.
TODO: example
2015-07-30 11:21:36 +03:00
2015-06-08 02:19:34 +03:00
## API
2015-06-08 02:29:04 +03:00
##### Ajv(Object options) -&gt; Object
2015-06-08 02:19:34 +03:00
Create ajv instance.
2015-06-08 09:52:51 +03:00
All the instance methods below are bound to the instance, so they can be used without the instance.
2015-06-08 02:19:34 +03:00
2015-06-08 02:29:04 +03:00
##### .compile(Object schema) -&gt; Function&lt;Object data&gt;
2015-06-08 02:19:34 +03:00
2015-06-08 09:51:34 +03:00
Generate validating function and cache the compiled schema for future use.
2015-06-08 02:19:34 +03:00
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.
2015-06-08 09:52:51 +03:00
2015-07-26 12:35:08 +03:00
Unless the option `validateSchema` is false, the schema will be validated against meta-schema and if schema is invalid the error will be thrown. See [options](#options).
2015-06-17 04:05:52 +03:00
2015-06-08 02:19:34 +03:00
##### .compileAsync(Object schema, Function callback)
Asyncronous version of `compile` method that loads missing remote schemas using asynchronous function in `options.loadSchema`. Callback will always be called with 2 parameters: error (or null) and validating function. Error will be not null in the following cases:
- missing schema can't be loaded (`loadSchema` calls callback with error).
- the schema containing missing reference is loaded, but the reference cannot be resolved.
- schema (or some referenced schema) is invalid.
The function compiles schema and loads the first missing schema multiple times, until all missing schemas are loaded.
2015-12-06 23:34:46 +03:00
See example in [Asynchronous compilation](#asynchronous-compilation).
2015-06-08 02:29:04 +03:00
##### .validate(Object schema|String key|String ref, data) -&gt; Boolean
2015-06-08 02:19:34 +03:00
Validate data using passed schema (it will be compiled and cached).
2015-06-08 09:51:34 +03:00
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.
2015-06-08 02:19:34 +03:00
Validation errors will be available in the `errors` property of ajv instance (`null` if there were no errors).
2015-10-22 23:14:17 +03:00
__Please note__: every time this method is called the errors are overwritten so you need to copy them to another variable if you want to use them later.
2015-06-08 02:19:34 +03:00
2015-07-04 03:58:08 +03:00
##### .addSchema(Array&lt;Object&gt;|Object schema [, String key])
2015-06-08 02:19:34 +03:00
2015-07-04 03:58:08 +03:00
Add schema(s) to validator instance. From version 1.0.0 this method does not compile schemas (but it still validates them). Because of that change, dependencies can be added in any order and circular dependencies are supported. It also prevents unnecessary compilation of schemas that are containers for other schemas but not used as a whole.
2015-06-08 02:19:34 +03:00
2015-08-17 01:00:43 +03:00
Array of schemas can be passed (schemas should have ids), the second parameter will be ignored.
2015-06-08 02:19:34 +03:00
2015-08-17 01:00:43 +03:00
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.
2015-06-08 02:19:34 +03:00
2015-07-04 03:58:08 +03:00
Once the schema is added, it (and all the references inside it) can be referenced in other schemas and used to validate data.
2015-06-08 02:19:34 +03:00
2015-07-04 03:58:08 +03:00
Although `addSchema` does not compile schemas, explicit compilation is not required - the schema will be compiled when it is used first time.
2015-06-08 02:19:34 +03:00
2015-07-26 12:37:56 +03:00
By default the schema is validated against meta-schema before it is added, and if the schema does not pass validation the exception is thrown. This behaviour is controlled by `validateSchema` option.
##### .addMetaSchema(Object schema [, String key])
2015-08-08 12:55:37 +03:00
Adds meta schema that can be used to validate other schemas. That function should be used instead of `addSchema` because there may be instance options that would compile a meta schema incorrectly (at the moment it is `removeAdditional` option).
There is no need to explicitly add draft 4 meta schema (http://json-schema.org/draft-04/schema and http://json-schema.org/schema) - it is added by default, unless option `meta` is set to `false`. You only need to use it if you have a changed meta-schema that you want to use to validate your schemas. See `validateSchema`.
2015-08-08 12:55:37 +03:00
2015-12-20 00:23:40 +03:00
With option `v5: true` [meta-schema that includes v5 keywords](https://raw.githubusercontent.com/epoberezkin/ajv/master/lib/refs/json-schema-v5.json) also added.
2015-12-13 00:18:41 +03:00
2015-08-08 12:55:37 +03:00
##### <a name="api-validateschema"></a>.validateSchema(Object schema) -&gt; Boolean
2015-06-20 16:59:06 +03:00
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.
2015-12-13 00:18:41 +03:00
If schema doesn't have `$schema` property it is validated against draft 4 meta-schema (option `meta` should not be false) or against [v5 meta-schema](https://raw.githubusercontent.com/epoberezkin/ajv/master/lib/refs/json-schema-v5.json#) if option `v5` is true.
2015-12-13 00:18:41 +03:00
If schema has `$schema` property then the schema with this id (that should be previously added) is used to validate passed schema.
2015-06-20 16:59:06 +03:00
Errors will be available at `ajv.errors`.
2015-06-08 02:19:34 +03:00
2015-06-08 02:29:04 +03:00
##### .getSchema(String key) -&gt; Function&lt;Object data&gt;
2015-06-08 02:19:34 +03:00
2015-06-20 20:40:13 +03:00
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.
2015-06-08 02:19:34 +03:00
##### <a name="api-addformat"></a>.addFormat(String name, String|RegExp|Function|Object format)
2015-06-13 15:42:58 +03:00
Add custom format to validate strings. It can also be used to replace pre-defined formats for ajv instance.
2015-07-21 21:18:03 +03:00
Strings are converted to RegExp.
2015-06-13 15:42:58 +03:00
Function should return validation result as `true` or `false`.
If object is passed it should have properties `validate` and `compare`. `validate` can be a string, RegExp or a function as described above. `compare` is a comparison function that accepts two strings and compares them according to the format meaning. This function is used with keywords `formatMaximum`/`formatMinimum` (from [v5 proposals](https://github.com/json-schema/json-schema/wiki/v5-Proposals) - `v5` option should be used). It should return `1` if the first value is bigger than the second value, `-1` if it is smaller and `0` if it is equal.
2015-06-13 15:42:58 +03:00
Custom formats can be also added via `formats` option.
##### <a name="api-addkeyword"></a>.addKeyword(String keyword, Object definition)
2015-11-13 15:16:31 +03:00
Add custom validation keyword to ajv instance.
Keyword should be a valid JavaScript identifier.
2015-12-06 23:34:46 +03:00
Keyword should be different from all standard JSON schema keywords and different from previously defined keywords. There is no way to redefine keywords or to remove keyword definition from the instance.
2015-11-13 15:16:31 +03:00
Keyword definition is an object with the following properties:
- _type_: optional string or array of strings with data type(s) that the keyword will apply to. If keyword is validating another type the validation function will not be called, so there is no need to check for data type inside validation function if `type` property is used.
- _validate_: validating function
- _compile_: compiling function
- _macro_: macro function
- _inline_: compiling function that returns code (as string)
2015-11-13 15:16:31 +03:00
_validate_, _compile_, _macro_ and _inline_ are mutually exclusive, only one should be used at a time.
With _macro_ function _type_ must not be specified, the types that the keyword will be applied for will be determined by the final schema.
2015-11-22 03:31:01 +03:00
See [Defining custom keywords](#defining-custom-keywords) for more details.
2015-11-13 15:16:31 +03:00
2015-07-22 00:38:13 +03:00
##### .errorsText([Array&lt;Object&gt; errors [, Object options]]) -&gt; String
2015-06-17 04:05:52 +03:00
2015-07-22 00:36:20 +03:00
Returns the text with all errors in a String.
2015-06-17 04:05:52 +03:00
2015-07-22 00:36:20 +03:00
Options can have properties `separator` (string used to separate errors, ", " by default) and `dataVar` (the variable name that dataPaths are prefixed with, "data" by default).
2015-06-17 04:05:52 +03:00
2015-05-30 01:22:11 +03:00
## Options
2015-09-13 02:50:56 +03:00
Defaults:
```
{
allErrors: false,
removeAdditional: false,
verbose: false,
format: 'fast',
formats: {},
schemas: {},
meta: true,
validateSchema: true,
inlineRefs: true,
missingRefs: true,
loadSchema: function(uri, cb) { /* ... */ cb(err, schema); },
uniqueItems: true,
unicode: true,
beautify: false,
cache: new Cache,
2015-11-22 03:31:01 +03:00
errorDataPath: 'object',
2015-09-13 02:50:56 +03:00
jsonPointers: false,
messages: true
2015-11-22 03:31:01 +03:00
v5: true
2015-09-13 02:50:56 +03:00
}
```
- _allErrors_: check all rules collecting all errors. Default is to return after the first error.
- _removeAdditional_: remove additional properties. Default is not to remove. If the option is 'all', then all additional properties are removed, regardless of `additionalProperties` keyword in schema (and no validation is made for them). If the option is `true` (or truthy), only additional properties with `additionalProperties` keyword equal to `false` are removed. If the option is 'failing', then additional properties that fail schema validation will be removed too (where `additionalProperties` keyword is schema).
- _verbose_: include the reference to the part of the schema (`schema` and `parentSchema`) 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.
2015-06-13 15:42:58 +03:00
- _formats_: an object with custom formats. Keys and values will be passed to `addFormat` method.
2015-06-18 16:59:11 +03:00
- _schemas_: an array or object of schemas that will be added to the instance. If the order is important, pass array. In this case schemas must have IDs in them. Otherwise the object can be passed - `addSchema(value, key)` will be called for each schema in this object.
- _meta_: add [meta-schema](http://json-schema.org/documentation.html) so it can be used by other schemas (true by default).
- _validateSchema_: validate added/compiled schemas against meta-schema (true by default). `$schema` property in the schema can either be http://json-schema.org/schema or http://json-schema.org/draft-04/schema or absent (draft-4 meta-schema will be used) or can be a reference to the schema previously added with `addMetaSchema` method. If the validation fails, the exception is thrown. Pass "log" in this option to log error instead of throwing exception. Pass `false` to skip schema validation.
2015-08-23 23:16:36 +03:00
- _inlineRefs_: by default the referenced schemas that don't have refs in them are inlined, regardless of their size - that substantially improves performance at the cost of the bigger size of compiled schema functions. Pass `false` to not inline referenced schemas (they will be compiled as separate functions). Pass integer number to limit the maximum number of keywords of the schema that will be inlined.
- _missingRefs_: by default if the reference cannot be resolved during compilation the exception is thrown. The thrown error has properties `missingRef` (with hash fragment) and `missingSchema` (without it). Both properties are resolved relative to the current base id (usually schema id, unless it was substituted). Pass 'ignore' to log error during compilation and pass validation. Pass 'fail' to log error and successfully compile schema but fail validation if this rule is checked.
- _loadSchema_: asynchronous function that will be used to load remote schemas when the method `compileAsync` is used and some reference is missing (option `missingRefs` should not be 'fail' or 'ignore'). This function should accept 2 parameters: remote schema uri and node-style callback. See example in Asynchronous compilation.
2015-06-08 02:19:34 +03:00
- _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](https://github.com/beautify-web/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.
- _cache_: an optional instance of cache to store compiled schemas using stable-stringified schema as a key. For example, set-associative cache [sacjs](https://github.com/epoberezkin/sacjs) can be used. If not passed then a simple hash is used which is good enough for the common use case (a limited number of statically defined schemas). Cache should have methods `put(key, value)`, `get(key)` and `del(key)`.
2015-11-22 03:31:01 +03:00
- _errorDataPath_: set `dataPath` to point to 'object' (default) or to 'property' (default behavior in versions before 2.0) when validating keywords `required`, `additionalProperties` and `dependencies`.
- _jsonPointers_: set `dataPath` propery of errors using [JSON Pointers](https://tools.ietf.org/html/rfc6901) instead of JavaScript property access notation.
2015-11-22 03:31:01 +03:00
- _messages_: Include human-readable messages in errors. `true` by default. `messages: false` can be added when custom messages are used (e.g. with [ajv-i18n](https://github.com/epoberezkin/ajv-i18n)).
2015-12-20 00:23:40 +03:00
- _v5_: add keywords `switch`, `constant`, `contains`, `patternGroups`, `formatMaximum` / `formatMinimum` and `exclusiveFormatMaximum` / `exclusiveFormatMinimum` from [JSON-schema v5 proposals](https://github.com/json-schema/json-schema/wiki/v5-Proposals). With this option added schemas without `$schema` property are validated against [v5 meta-schema](https://raw.githubusercontent.com/epoberezkin/ajv/master/lib/refs/json-schema-v5.json#).
2015-09-13 02:50:56 +03:00
2015-06-05 23:37:36 +03:00
## Validation errors
In case of validation failure Ajv assigns the array of errors to `.errors` property of validation function (or to `.errors` property of ajv instance in case `validate` or `validateSchema` methods were called).
### Error objects
Each error is an object with the following properties:
- _keyword_: validation keyword. For user defined validation keywords it is set to `"custom"` (with the exception of macro keywords and unless keyword definition defines its own errors).
- _dataPath_: the path to the part of the data that was validated. By default `dataPath` uses JavaScript property access notation (e.g., `".prop[1].subProp"`). When the option `jsonPointers` is true (see [Options](#options)) `dataPath` will be set using JSON pointer standard (e.g., `"/prop/1/subProp"`).
2015-12-20 00:23:40 +03:00
- _schemaPath_: the path (JSON-pointer as a URI fragment) to the schema of the keyword that failed validation.
- _params_: the object with the additional information about error that can be used to create custom error messages (e.g., using [ajv-i18n](https://github.com/epoberezkin/ajv-i18n) package). See below for parameters set by all keywords.
- _message_: the standard error message (can be excluded with option `messages` set to false).
- _schema_: the schema of the keyword (added with `verbose` option).
- _parentSchema_: the schema containing the keyword (added with `verbose` option)
- _data_: the data validated by the keyword (added with `verbose` option).
### Error parameters
Properties of `params` object in errors depend on the keyword that failed validation.
- `maxItems`, `minItems`, `maxLength`, `minLength`, `maxProperties`, `minProperties` - property `limit` (number, the schema of the keyword).
- `additionalItems` - property `limit` (the maximum number of allowed items in case when `items` keyword is an array of schemas and `additionalItems` is false).
- `additionalProperties` - property `additionalProperty` (the property not used in `properties` and `patternProperties` keywords).
2015-11-28 17:59:08 +03:00
- `patternGroups` (with v5 option) - properties:
- `pattern`
- `reason` ("minimum"/"maximum"),
- `limit` (max/min allowed number of properties matching number)
- `dependencies` - properties:
- `property` (dependent property),
- `missingProperty` (required missing dependency - only the first one is reported currently)
- `deps` (required dependencies, comma separated list as a string),
- `depsCount` (the number of required dependedncies).
- `format` - property `format` (the schema of the keyword).
- `maximum`, `minimum` - properties:
- `limit` (number, the schema of the keyword),
- `exclusive` (boolean, the schema of `exclusiveMaximum` or `exclusiveMinimum`),
- `comparison` (string, comparison operation to compare the data to the limit, with the data on the left and the limit on the right; can be "<", "<=", ">", ">=")
- `multipleOf` - property `multipleOf` (the schema of the keyword)
- `pattern` - property `pattern` (the schema of the keyword)
- `required` - property `missingProperty` (required property that is missing).
- `type` - property `type` (required type(s), a string, can be a comma-separated list)
- `uniqueItems` - properties `i` and `j` (indices of duplicate items).
- `$ref` - property `ref` with the referenced schema URI.
- custom keywords (in case keyword definition doesn't create errors) - property `keyword` (the keyword name).
## Command line interface
Simple JSON-schema validation can be done from command line using [ajv-cli](https://github.com/jessedc/ajv-cli) package. At the moment it does not support referenced schemas.
2015-06-05 23:37:36 +03:00
## Tests
```
2015-07-22 01:23:37 +03:00
npm install
2015-06-05 23:37:36 +03:00
git submodule update --init
npm test
```
## Contributing
2015-08-01 16:26:04 +03:00
All validation functions are generated using doT templates in [dot](https://github.com/epoberezkin/ajv/tree/master/lib/dot) folder. Templates are precompiled so doT is not a run-time dependency.
`npm run build` - compiles templates to [dotjs](https://github.com/epoberezkin/ajv/tree/master/lib/dotjs) folder.
`npm run watch` - automatically compiles templates when files in dot folder change
## Changes history
2015-08-18 23:36:11 +03:00
See https://github.com/epoberezkin/ajv/releases
2015-06-19 23:34:34 +03:00
## License
[MIT](https://github.com/epoberezkin/ajv/blob/master/LICENSE)