addMetaSchema method, closes #26

master
Evgeny Poberezkin 2015-08-08 10:55:37 +01:00
parent a0c342c58f
commit 9466859f92
3 changed files with 32 additions and 9 deletions

View File

@ -39,6 +39,8 @@ npm install ajv
## Usage
The fastest validation call:
```
var Ajv = require('ajv');
var ajv = Ajv(); // options can be passed
@ -47,7 +49,7 @@ var valid = validate(data);
if (!valid) console.log(validate.errors);
```
or
or with less code
```
// ...
@ -68,6 +70,8 @@ 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](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.
The best performance is achieved when using compiled functions (there is no additional function call).
## Using in browser
@ -159,6 +163,13 @@ Version [1.0](https://github.com/epoberezkin/ajv/tree/1.0.0) will only compile s
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.
##### .addMetaSchema(Object schema [, String key]) -> Function
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 explicitely add draft 4 meta schema (http://json-schema.org/draft-04/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`.
##### .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.

View File

@ -80,7 +80,7 @@ function Ajv(opts) {
* Adds schema to the instance.
* @param {Object|Array} schema schema or array of schemas. If array is passed, `key` will be ignored.
* @param {String} key Optional schema key. Can be passed to `validate` method instead of schema object or id/ref. One schema per instance can have empty `id` and `key`.
* @return {Function} compiled schema with method `validate` that accepts `data`.
* @return {Function} compiled schema function.
*/
function addSchema(schema, key, _skipValidation) {
if (Array.isArray(schema))
@ -93,6 +93,22 @@ function Ajv(opts) {
}
/**
* Add schema that will be used to validate other schemas
* removeAdditional option is alway set to false
* @param {Object} schema
* @param {String} key optional schema key
* @return {Function} compiled schema
*/
function addMetaSchema(schema, key, _skipValidation) {
var currentRemoveAdditional = self.opts.removeAdditional;
self.opts.removeAdditional = false;
var validate = addSchema(schema, META_SCHEMA_ID, true);
self.opts.removeAdditional = currentRemoveAdditional;
return validate;
}
/**
* Validate schema
* @param {Object} schema schema to validate
@ -189,12 +205,8 @@ function Ajv(opts) {
function addInitialSchemas() {
if (self.opts.meta !== false) {
var currentRemoveAdditional = self.opts.removeAdditional;
self.opts.removeAdditional = false;
addSchema(require('./refs/json-schema-draft-04.json'), META_SCHEMA_ID, true);
self.opts.removeAdditional = currentRemoveAdditional;
}
if (self.opts.meta !== false)
addMetaSchema(require('./refs/json-schema-draft-04.json'), META_SCHEMA_ID);
var optsSchemas = self.opts.schemas;
if (!optsSchemas) return;

View File

@ -1,6 +1,6 @@
{
"name": "ajv",
"version": "0.6.13",
"version": "0.6.14",
"description": "Another JSON Schema Validator",
"main": "lib/ajv.js",
"scripts": {