Comparing version 0.7.2 to 1.0.0
107
lib/ajv.js
@@ -6,2 +6,3 @@ 'use strict'; | ||
, Cache = require('./cache') | ||
, SchemaObject = require('./compile/schema_obj') | ||
, stableStringify = require('json-stable-stringify') | ||
@@ -46,2 +47,4 @@ , formats = require('./compile/formats'); | ||
this._compile = _compile; | ||
addInitialSchemas(); | ||
@@ -63,3 +66,6 @@ if (this.opts.formats) addInitialFormats(); | ||
if (!v) throw new Error('no schema with key or ref "' + schemaKeyRef + '"'); | ||
} else v = _addSchema(schemaKeyRef); | ||
} else { | ||
var schemaObj = _addSchema(schemaKeyRef); | ||
v = schemaObj.validate || _compile(schemaObj); | ||
} | ||
@@ -78,3 +84,4 @@ var valid = v(data); | ||
function compile(schema) { | ||
return _addSchema(schema); | ||
var schemaObj = _addSchema(schema); | ||
return schemaObj.validate || _compile(schemaObj); | ||
} | ||
@@ -88,4 +95,4 @@ | ||
*/ | ||
function addSchema(schema, key, _skipValidation) { | ||
if (Array.isArray(schema)) { | ||
function addSchema(schema, key, _skipValidation, _meta) { | ||
if (Array.isArray(schema)){ | ||
schema.forEach(function(sch) { addSchema(sch); }); | ||
@@ -97,3 +104,4 @@ return; | ||
checkUnique(key); | ||
self._schemas[key] = _addSchema(schema, _skipValidation); | ||
var schemaObj = self._schemas[key] = _addSchema(schema, _skipValidation); | ||
schemaObj.meta = _meta; | ||
} | ||
@@ -109,6 +117,3 @@ | ||
function addMetaSchema(schema, key, _skipValidation) { | ||
var currentRemoveAdditional = self.opts.removeAdditional; | ||
self.opts.removeAdditional = false; | ||
addSchema(schema, META_SCHEMA_ID, _skipValidation); | ||
self.opts.removeAdditional = currentRemoveAdditional; | ||
addSchema(schema, key, _skipValidation, true); | ||
} | ||
@@ -140,2 +145,11 @@ | ||
function getSchema(keyRef) { | ||
var schemaObj = _getSchemaObj(keyRef); | ||
switch (typeof schemaObj) { | ||
case 'object': return schemaObj.validate || _compile(schemaObj); | ||
case 'string': return getSchema(schemaObj); | ||
} | ||
} | ||
function _getSchemaObj(keyRef) { | ||
keyRef = resolve.normalizeId(keyRef); | ||
@@ -152,13 +166,17 @@ return self._schemas[keyRef] || self._refs[keyRef]; | ||
function removeSchema(schemaKeyRef) { | ||
var str; | ||
if (typeof schemaKeyRef == 'string') { | ||
schemaKeyRef = resolve.normalizeId(schemaKeyRef); | ||
var v = self._schemas[schemaKeyRef] || self._refs[schemaKeyRef]; | ||
delete self._schemas[schemaKeyRef]; | ||
delete self._refs[schemaKeyRef]; | ||
str = stableStringify(v.schema); | ||
self._cache.put(str); | ||
} else { | ||
str = stableStringify(schemaKeyRef); | ||
self._cache.put(str); | ||
switch (typeof schemaKeyRef) { | ||
case 'string': | ||
var schemaObj = _getSchemaObj(schemaKeyRef); | ||
self._cache.del(schemaObj.jsonStr); | ||
delete self._schemas[schemaKeyRef]; | ||
delete self._refs[schemaKeyRef]; | ||
break; | ||
case 'object': | ||
var jsonStr = stableStringify(schemaKeyRef); | ||
self._cache.del(jsonStr); | ||
var id = schemaKeyRef.id; | ||
if (id) { | ||
id = resolve.normalizeId(id); | ||
delete self._refs[id]; | ||
} | ||
} | ||
@@ -170,4 +188,4 @@ } | ||
if (typeof schema != 'object') throw new Error('schema should be object'); | ||
var str = stableStringify(schema); | ||
var cached = self._cache.get(str); | ||
var jsonStr = stableStringify(schema); | ||
var cached = self._cache.get(jsonStr); | ||
if (cached) return cached; | ||
@@ -188,10 +206,47 @@ | ||
var validate = compileSchema.call(self, schema, undefined, localRefs); | ||
if (id[0] != '#') self._refs[id] = validate; | ||
self._cache.put(str, validate); | ||
var schemaObj = new SchemaObject({ | ||
id: id, | ||
schema: schema, | ||
localRefs: localRefs, | ||
jsonStr: jsonStr, | ||
}); | ||
return validate; | ||
if (id[0] != '#') self._refs[id] = schemaObj; | ||
self._cache.put(jsonStr, schemaObj); | ||
return schemaObj; | ||
} | ||
function _compile(schemaObj, root) { | ||
if (schemaObj.compiling) { | ||
schemaObj.validate = callValidate; | ||
callValidate.schema = schemaObj.schema; | ||
callValidate.errors = null; | ||
callValidate.root = root ? root : callValidate; | ||
return callValidate; | ||
} | ||
schemaObj.compiling = true; | ||
var currentRA = self.opts.removeAdditional; | ||
if (currentRA && schemaObj.meta) self.opts.removeAdditional = false; | ||
var v = compileSchema.call(self, schemaObj.schema, root, schemaObj.localRefs); | ||
if (currentRA) self.opts.removeAdditional = currentRA; | ||
schemaObj.validate = v; | ||
schemaObj.refs = v.refs; | ||
schemaObj.refVal = v.refVal; | ||
schemaObj.root = v.root; | ||
return v; | ||
function callValidate() { | ||
var v = schemaObj.validate; | ||
var result = v.apply(null, arguments); | ||
callValidate.errors = v.errors; | ||
return result; | ||
} | ||
} | ||
function errorsText(errors, opts) { | ||
@@ -198,0 +253,0 @@ errors = errors || self.errors; |
@@ -17,1 +17,6 @@ 'use strict'; | ||
}; | ||
Cache.prototype.del = function Cache_del(key) { | ||
delete this._cache[key]; | ||
} |
@@ -5,3 +5,4 @@ 'use strict'; | ||
, equal = require('./equal') | ||
, util = require('./util'); | ||
, util = require('./util') | ||
, SchemaObject = require('./schema_obj'); | ||
@@ -23,4 +24,7 @@ module.exports = resolve; | ||
} | ||
refVal = refVal || this._schemas[ref]; | ||
if (typeof refVal == 'function') return refVal; | ||
if (refVal instanceof SchemaObject) | ||
return refVal.validate || this._compile(refVal, root); | ||
var res = _resolve.call(this, root, ref); | ||
@@ -32,4 +36,8 @@ var schema, v; | ||
} | ||
if (typeof schema == 'function') v = schema; | ||
else if (schema) v = compile.call(this, schema, root); | ||
if (schema instanceof SchemaObject) | ||
var v = schema.validate || compile.call(this, schema.schema, root); | ||
else if (schema) | ||
var v = compile.call(this, schema, root); | ||
if (v && ref[0] != '#') this._refs[ref] = v; | ||
@@ -49,6 +57,9 @@ return v; | ||
if (typeof refVal == 'string') refVal = this._refs[refVal]; | ||
if (typeof refVal == 'function') root = refVal; | ||
else { | ||
if (refVal instanceof SchemaObject) { | ||
if (!refVal.validate) this._compile(refVal); | ||
root = refVal; | ||
} else { | ||
refVal = this._schemas[id]; | ||
if (typeof refVal == 'function') { | ||
if (refVal instanceof SchemaObject) { | ||
if (!refVal.validate) this._compile(refVal); | ||
if (id == normalizeId(ref)) return { schema: refVal, root: root }; | ||
@@ -55,0 +66,0 @@ root = refVal; |
@@ -144,12 +144,12 @@ 'use strict'; | ||
var ERRORS_REGEXP = /[^\.]errors/g | ||
, VAR_ERRORS = 'var errors = 0;' | ||
, INITIALIZE_ERRORS = 'validate.errors = null;' | ||
, RETURN_ERRORS = 'return errors === 0;'; | ||
var ERRORS_REGEXP = /[^v\.]errors/g | ||
, REMOVE_ERRORS = /var errors = 0;|var vErrors = null;|validate.errors = vErrors;/g | ||
, RETURN_VALID = 'return errors === 0;' | ||
, RETURN_TRUE = 'validate.errors = null; return true;'; | ||
function cleanUpVarErrors(out) { | ||
var matches = out.match(ERRORS_REGEXP); | ||
if (matches && matches.length === 2) | ||
return out.replace(VAR_ERRORS, '') | ||
.replace(INITIALIZE_ERRORS, '') | ||
.replace(RETURN_ERRORS, INITIALIZE_ERRORS + ' return true;'); | ||
return out.replace(REMOVE_ERRORS, '') | ||
.replace(RETURN_VALID, RETURN_TRUE); | ||
else | ||
@@ -156,0 +156,0 @@ return out; |
@@ -36,3 +36,3 @@ 'use strict'; | ||
} | ||
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; } else { errors = ' + ($errs) + '; if (validate.errors !== null) { if (' + ($errs) + ') validate.errors.length = ' + ($errs) + '; else validate.errors = null; } '; | ||
out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } else { errors = ' + ($errs) + '; if (vErrors !== null) { if (' + ($errs) + ') vErrors.length = ' + ($errs) + '; else vErrors = null; } '; | ||
if (it.opts.allErrors) { | ||
@@ -39,0 +39,0 @@ out += ' } '; |
@@ -63,3 +63,3 @@ 'use strict'; | ||
} | ||
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; '; | ||
out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; | ||
} | ||
@@ -66,0 +66,0 @@ out += ' } '; |
@@ -25,3 +25,3 @@ 'use strict'; | ||
} | ||
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; '; | ||
out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; | ||
} | ||
@@ -28,0 +28,0 @@ out += ' }'; |
@@ -32,3 +32,3 @@ 'use strict'; | ||
} | ||
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; '; | ||
out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; | ||
} | ||
@@ -35,0 +35,0 @@ out += ' } '; |
@@ -33,3 +33,3 @@ 'use strict'; | ||
} | ||
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; '; | ||
out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; | ||
} | ||
@@ -36,0 +36,0 @@ out += ' } '; |
@@ -27,3 +27,3 @@ 'use strict'; | ||
} | ||
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; '; | ||
out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; | ||
} | ||
@@ -30,0 +30,0 @@ out += '} '; |
@@ -24,3 +24,3 @@ 'use strict'; | ||
} | ||
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; '; | ||
out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; | ||
} | ||
@@ -27,0 +27,0 @@ out += '} '; |
@@ -30,3 +30,3 @@ 'use strict'; | ||
} | ||
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; '; | ||
out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; | ||
} | ||
@@ -33,0 +33,0 @@ out += '} '; |
@@ -24,3 +24,3 @@ 'use strict'; | ||
} | ||
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; '; | ||
out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; | ||
} | ||
@@ -27,0 +27,0 @@ out += '} '; |
@@ -27,3 +27,3 @@ 'use strict'; | ||
} | ||
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; '; | ||
out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; | ||
} | ||
@@ -30,0 +30,0 @@ out += '} '; |
@@ -24,3 +24,3 @@ 'use strict'; | ||
} | ||
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; '; | ||
out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; | ||
} | ||
@@ -27,0 +27,0 @@ out += '} '; |
@@ -30,3 +30,3 @@ 'use strict'; | ||
} | ||
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; '; | ||
out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; | ||
} | ||
@@ -33,0 +33,0 @@ out += '} '; |
@@ -24,3 +24,3 @@ 'use strict'; | ||
} | ||
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; '; | ||
out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; | ||
} | ||
@@ -27,0 +27,0 @@ out += '} '; |
@@ -24,3 +24,3 @@ 'use strict'; | ||
} | ||
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; '; | ||
out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; | ||
} | ||
@@ -27,0 +27,0 @@ out += '} '; |
@@ -30,5 +30,5 @@ 'use strict'; | ||
} | ||
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; '; | ||
out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; | ||
} | ||
out += ' } else { errors = ' + ($errs) + '; if (validate.errors !== null) { if (' + ($errs) + ') validate.errors.length = ' + ($errs) + '; else validate.errors = null; } '; | ||
out += ' } else { errors = ' + ($errs) + '; if (vErrors !== null) { if (' + ($errs) + ') vErrors.length = ' + ($errs) + '; else vErrors = null; } '; | ||
if (it.opts.allErrors) { | ||
@@ -42,3 +42,3 @@ out += ' } '; | ||
} | ||
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; '; | ||
out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; | ||
if ($breakOnError) { | ||
@@ -45,0 +45,0 @@ out += ' if (false) { '; |
@@ -48,5 +48,5 @@ 'use strict'; | ||
} | ||
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; '; | ||
out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; | ||
} | ||
out += '} else { errors = ' + ($errs) + '; if (validate.errors !== null) { if (' + ($errs) + ') validate.errors.length = ' + ($errs) + '; else validate.errors = null; }'; | ||
out += '} else { errors = ' + ($errs) + '; if (vErrors !== null) { if (' + ($errs) + ') vErrors.length = ' + ($errs) + '; else vErrors = null; }'; | ||
if (it.opts.allErrors) { | ||
@@ -53,0 +53,0 @@ out += ' } '; |
@@ -25,3 +25,3 @@ 'use strict'; | ||
} | ||
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; '; | ||
out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; | ||
} | ||
@@ -28,0 +28,0 @@ out += '} '; |
@@ -65,3 +65,3 @@ 'use strict'; | ||
} | ||
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; '; | ||
out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; | ||
} | ||
@@ -68,0 +68,0 @@ if ($breakOnError) { |
@@ -17,3 +17,3 @@ 'use strict'; | ||
} else { | ||
out += ' var errors' + ($lvl) + ' = validate.errors; if (! ' + ('validate') + '(' + ($data) + ', (dataPath || \'\') + ' + (it.errorPath) + ') ) { if (errors' + ($lvl) + ' !== null) validate.errors = errors' + ($lvl) + '.concat(validate.errors); errors = validate.errors.length; } '; | ||
out += ' if (! ' + ('validate') + '(' + ($data) + ', (dataPath || \'\') + ' + (it.errorPath) + ') ) { if (vErrors === null) vErrors = ' + ('validate') + '.errors; else vErrors = vErrors.concat(' + ('validate') + '.errors); errors = vErrors.length; } '; | ||
if ($breakOnError) { | ||
@@ -24,3 +24,3 @@ out += ' else { '; | ||
} else { | ||
out += ' if (! ' + ('root.refVal[0]') + '(' + ($data) + ', (dataPath || \'\') + ' + (it.errorPath) + ') ) { if (validate.errors === null) validate.errors = ' + ('root.refVal[0]') + '.errors; else validate.errors = validate.errors.concat(' + ('root.refVal[0]') + '.errors); errors = validate.errors.length; } '; | ||
out += ' if (! ' + ('root.refVal[0]') + '(' + ($data) + ', (dataPath || \'\') + ' + (it.errorPath) + ') ) { if (vErrors === null) vErrors = ' + ('root.refVal[0]') + '.errors; else vErrors = vErrors.concat(' + ('root.refVal[0]') + '.errors); errors = vErrors.length; } '; | ||
if ($breakOnError) { | ||
@@ -47,3 +47,3 @@ out += ' else { '; | ||
} | ||
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; '; | ||
out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; | ||
} | ||
@@ -62,3 +62,3 @@ if ($breakOnError) { | ||
} else { | ||
out += ' if (! ' + ($refVal) + '(' + ($data) + ', (dataPath || \'\') + ' + (it.errorPath) + ') ) { if (validate.errors === null) validate.errors = ' + ($refVal) + '.errors; else validate.errors = validate.errors.concat(' + ($refVal) + '.errors); errors = validate.errors.length; } '; | ||
out += ' if (! ' + ($refVal) + '(' + ($data) + ', (dataPath || \'\') + ' + (it.errorPath) + ') ) { if (vErrors === null) vErrors = ' + ($refVal) + '.errors; else vErrors = vErrors.concat(' + ($refVal) + '.errors); errors = vErrors.length; } '; | ||
if ($breakOnError) { | ||
@@ -65,0 +65,0 @@ out += ' else { '; |
@@ -45,3 +45,3 @@ 'use strict'; | ||
} | ||
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; '; | ||
out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; | ||
} | ||
@@ -67,3 +67,3 @@ out += ' } else { '; | ||
} | ||
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; '; | ||
out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; | ||
} | ||
@@ -87,3 +87,3 @@ out += ' } else { '; | ||
} | ||
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; } '; | ||
out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } '; | ||
} | ||
@@ -101,3 +101,3 @@ } | ||
} | ||
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; } } '; | ||
out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } } '; | ||
} | ||
@@ -104,0 +104,0 @@ } |
@@ -25,3 +25,3 @@ 'use strict'; | ||
} | ||
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; '; | ||
out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; | ||
} | ||
@@ -28,0 +28,0 @@ out += ' } '; |
@@ -12,4 +12,4 @@ 'use strict'; | ||
it.wasTop = true; | ||
out += ' validate = function (data, dataPath) { \'use strict\'; validate.errors = null;'; | ||
out += ' var errors = 0; '; | ||
out += ' validate = function (data, dataPath) { \'use strict\'; var vErrors = null; '; | ||
out += ' var errors = 0; '; | ||
} else { | ||
@@ -99,3 +99,3 @@ if (it.opts._debug) { | ||
} | ||
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; '; | ||
out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; | ||
} | ||
@@ -158,3 +158,3 @@ out += ' } '; | ||
} | ||
out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; '; | ||
out += ' }; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; '; | ||
} | ||
@@ -167,3 +167,4 @@ out += ' }'; | ||
if ($top) { | ||
out += ' return errors === 0;'; | ||
out += ' validate.errors = vErrors; '; | ||
out += ' return errors === 0; '; | ||
out += ' }'; | ||
@@ -170,0 +171,0 @@ } else { |
{ | ||
"name": "ajv", | ||
"version": "0.7.2", | ||
"version": "1.0.0", | ||
"description": "Another JSON Schema Validator", | ||
@@ -5,0 +5,0 @@ "main": "lib/ajv.js", |
@@ -19,2 +19,3 @@ # ajv - Another JSON Schema Validator | ||
- 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) | ||
@@ -32,2 +33,4 @@ - formats defined by JSON Schema draft 4 standard and custom formats (can be turned off) | ||
[Benchmark of schemas of different complexity by jsck](https://github.com/pandastrike/jsck#benchmarks). | ||
[Benchmark of individual test cases](https://rawgit.com/zaggino/z-schema/master/benchmark/results.html) by [z-schema](https://github.com/zaggino/z-schema). | ||
@@ -153,20 +156,16 @@ | ||
Add and compile schema(s). It does the same as `.compile` with three differences: | ||
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. | ||
- array of schemas can be passed (schemas should have ids), the second parameter will be ignored. | ||
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. | ||
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. | ||
- compiled schema is not returned. | ||
Once the schema is added, it (and all the references inside it) can be referenced in other schemas and used to validate data. | ||
Once the schema added it and all the references inside it can be referenced in other schemas and used to validate data. | ||
Although `addSchema` does not compile schemas, explicit compilation is not required - the schema will be compiled when it is used first time. | ||
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 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. | ||
Version [1.0](https://github.com/epoberezkin/ajv/tree/1.0.0) will only compile schemas when they are used the first time. The order of addition in this version is not important and it supports circular references. Try it from the branch if you need them and please report any issues. | ||
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]) | ||
@@ -268,2 +267,11 @@ | ||
##### 1.0.0 | ||
Only compile schemas when they are used first time. | ||
Order in which schemas are added is not important. | ||
Circular references are supported. | ||
##### 0.7.0 | ||
@@ -288,2 +296,11 @@ | ||
##### 1.0.0 | ||
`addSchema` no longer compiles schemas and its return value is `undefined` | ||
Dependencies can be added in any order (all dependencies should be present when the schema is compiled though) | ||
Circular dependencies support | ||
##### 0.6.1 | ||
@@ -290,0 +307,0 @@ |
@@ -79,5 +79,7 @@ 'use strict'; | ||
it('should add and compile schema with key', function() { | ||
ajv.addSchema({ type: 'integer' }, 'int'); | ||
var res = ajv.addSchema({ type: 'integer' }, 'int'); | ||
should.not.exist(res); | ||
var validate = ajv.getSchema('int'); | ||
validate .should.be.a('function'); | ||
validate(1) .should.equal(true); | ||
@@ -91,3 +93,3 @@ validate(1.1) .should.equal(false); | ||
it('should add and compile schema without key', function() { | ||
var validate = ajv.addSchema({ type: 'integer' }); | ||
ajv.addSchema({ type: 'integer' }); | ||
ajv.validate('', 1) .should.equal(true); | ||
@@ -98,3 +100,3 @@ ajv.validate('', '1') .should.equal(false); | ||
it('should add and compile schema with id', function() { | ||
var validate = ajv.addSchema({ id: '//e.com/int.json', type: 'integer' }); | ||
ajv.addSchema({ id: '//e.com/int.json', type: 'integer' }); | ||
ajv.validate('//e.com/int.json', 1) .should.equal(true); | ||
@@ -105,3 +107,3 @@ ajv.validate('//e.com/int.json', '1') .should.equal(false); | ||
it('should normalize schema keys and ids', function() { | ||
var validate = ajv.addSchema({ id: '//e.com/int.json#', type: 'integer' }, 'int#'); | ||
ajv.addSchema({ id: '//e.com/int.json#', type: 'integer' }, 'int#'); | ||
ajv.validate('int', 1) .should.equal(true); | ||
@@ -184,3 +186,2 @@ ajv.validate('int', '1') .should.equal(false); | ||
var validate = ajv.getSchema('//e.com/int.json'); | ||
validate .should.equal(validate); | ||
validate(1) .should.equal(true); | ||
@@ -197,3 +198,4 @@ validate('1') .should.equal(false); | ||
var v = ajv.getSchema(); | ||
v .should.equal(validate); | ||
v(1) .should.equal(true); | ||
v('1') .should.equal(false); | ||
}); | ||
@@ -209,4 +211,6 @@ }); | ||
var v = ajv.getSchema('int') | ||
ajv._cache.get(str) .should.equal(v); | ||
v .should.be.a('function'); | ||
ajv._cache.get(str).validate .should.equal(v); | ||
ajv.removeSchema('int'); | ||
@@ -221,5 +225,7 @@ should.not.exist(ajv.getSchema('int')); | ||
ajv.addSchema(schema); | ||
var v = ajv.getSchema('//e.com/int.json'); | ||
ajv._cache.get(str) .should.equal(v); | ||
var v = ajv.getSchema('//e.com/int.json') | ||
v .should.be.a('function'); | ||
ajv._cache.get(str).validate .should.equal(v); | ||
ajv.removeSchema('//e.com/int.json'); | ||
@@ -234,4 +240,3 @@ should.not.exist(ajv.getSchema('//e.com/int.json')); | ||
ajv.addSchema(schema); | ||
ajv._cache.get(str) .should.be.a('function'); | ||
ajv._cache.get(str) .should.be.an('object'); | ||
ajv.removeSchema({ type: 'integer' }); | ||
@@ -238,0 +243,0 @@ // should.not.exist(ajv.getSchema('int')); |
@@ -25,2 +25,6 @@ 'use strict'; | ||
require('./remotes/buu.json'), | ||
require('./remotes/tree.json'), | ||
require('./remotes/node.json'), | ||
require('./remotes/second.json'), | ||
require('./remotes/first.json'), | ||
]; | ||
@@ -27,0 +31,0 @@ |
@@ -617,3 +617,3 @@ [ | ||
{ | ||
"description": "empty object is valid", | ||
"description": "empty object is invalid", | ||
"data": {}, | ||
@@ -620,0 +620,0 @@ "valid": false |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
421679
180
11270
0
338