Comparing version 0.5.8 to 0.5.9
@@ -20,5 +20,5 @@ 'use strict'; | ||
* Creates validator instance. | ||
* Usage: `jv(opts)` | ||
* Usage: `Ajv(opts)` | ||
* @param {Object} opts optional options | ||
* @return {Object} jv instance | ||
* @return {Object} ajv instance | ||
*/ | ||
@@ -42,2 +42,3 @@ function Ajv(opts) { | ||
this.getSchema = getSchema; | ||
this.removeSchema = removeSchema; | ||
this.addFormat = addFormat; | ||
@@ -55,11 +56,9 @@ this.errorsText = errorsText; | ||
* @param {Any} data to be validated | ||
* @return {Boolean} validation result. Errors from the last validation will be available in `jv.errors` (and also in compiled schema: `schema.errors`). | ||
* @return {Boolean} validation result. Errors from the last validation will be available in `ajv.errors` (and also in compiled schema: `schema.errors`). | ||
*/ | ||
function validate(schemaKeyRef, data) { | ||
var v; | ||
if (typeof schemaKeyRef == 'string') { | ||
var v = getSchema(schemaKeyRef); | ||
if (!v) { | ||
v = getRef(schemaKeyRef); | ||
if (!v) throw new Error('no schema with key or ref "' + schemaKeyRef + '"'); | ||
} | ||
v = getSchema(schemaKeyRef); | ||
if (!v) throw new Error('no schema with key or ref "' + schemaKeyRef + '"'); | ||
} else v = _addSchema(schemaKeyRef); | ||
@@ -118,9 +117,9 @@ | ||
/** | ||
* Get compiled schema from the instance by `key`. | ||
* @param {String} key `key` that was passed to `addSchema` (or `schema.id`). | ||
* Get compiled schema from the instance by `key` or `ref`. | ||
* @param {String} keyRef `key` that was passed to `addSchema` or full schema reference (`schema.id` or resolved id). | ||
* @return {Function} schema validating function (with property `schema`). | ||
*/ | ||
function getSchema(key) { | ||
key = resolve.normalizeId(key); | ||
return self._schemas[key]; | ||
function getSchema(keyRef) { | ||
keyRef = resolve.normalizeId(keyRef); | ||
return self._schemas[keyRef] || self._refs[keyRef]; | ||
} | ||
@@ -130,10 +129,18 @@ | ||
/** | ||
* Get compiled schema from the instance by `id`. | ||
* @param {String} id `schema.id` or any reference in any of previously passed schemas. | ||
* @return {Function} schema validating function (with property `schema`). | ||
* Remove cached schema | ||
* Even if schema is referenced by other schemas it still can be removed as other schemas have local references | ||
* @param {String|Object} schemaKeyRef key, ref or schema object | ||
*/ | ||
function getRef(ref) { | ||
ref = resolve.normalizeId(ref); | ||
// TODO | ||
return self._refs[ref]; | ||
function removeSchema(schemaKeyRef) { | ||
if (typeof schemaKeyRef == 'string') { | ||
schemaKeyRef = resolve.normalizeId(schemaKeyRef); | ||
var v = self._schemas[schemaKeyRef] || self._refs[schemaKeyRef]; | ||
delete self._schemas[schemaKeyRef]; | ||
delete self._refs[schemaKeyRef]; | ||
var str = stableStringify(v.schema); | ||
self._cache.put(str); | ||
} else { | ||
var str = stableStringify(schemaKeyRef); | ||
self._cache.put(str); | ||
} | ||
} | ||
@@ -140,0 +147,0 @@ |
{ | ||
"name": "ajv", | ||
"version": "0.5.8", | ||
"description": "Another JSON schema Validator", | ||
"version": "0.5.9", | ||
"description": "Another JSON Schema Validator", | ||
"main": "lib/ajv.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
@@ -129,5 +129,12 @@ # ajv - Another JSON Schema Validator | ||
Retrieve compiled schema previously added with `addSchema`. Validating function has `schema` property with the reference to the original schema. | ||
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) | ||
@@ -134,0 +141,0 @@ |
@@ -5,3 +5,4 @@ 'use strict'; | ||
var Ajv = require('../lib/ajv') | ||
, should = require('chai').should(); | ||
, should = require('chai').should() | ||
, stableStringify = require('json-stable-stringify'); | ||
@@ -185,2 +186,40 @@ | ||
}); | ||
describe('removeSchema method', function() { | ||
it('should remove schema by key', function() { | ||
var schema = { type: 'integer' } | ||
, str = stableStringify(schema); | ||
var v = ajv.addSchema(schema, 'int'); | ||
ajv.getSchema('int') .should.equal(v); | ||
ajv._cache.get(str) .should.equal(v); | ||
ajv.removeSchema('int'); | ||
should.not.exist(ajv.getSchema('int')); | ||
should.not.exist(ajv._cache.get(str)); | ||
}); | ||
it('should remove schema by id', function() { | ||
var schema = { id: '//e.com/int.json', type: 'integer' } | ||
, str = stableStringify(schema); | ||
var v = ajv.addSchema(schema); | ||
ajv.getSchema('//e.com/int.json') .should.equal(v); | ||
ajv._cache.get(str) .should.equal(v); | ||
ajv.removeSchema('//e.com/int.json'); | ||
should.not.exist(ajv.getSchema('//e.com/int.json')); | ||
should.not.exist(ajv._cache.get(str)); | ||
}); | ||
it('should remove schema by schema object', function() { | ||
var schema = { type: 'integer' } | ||
, str = stableStringify(schema); | ||
var v = ajv.addSchema(schema); | ||
ajv._cache.get(str) .should.equal(v); | ||
ajv.removeSchema({ type: 'integer' }); | ||
// should.not.exist(ajv.getSchema('int')); | ||
should.not.exist(ajv._cache.get(str)); | ||
}); | ||
}); | ||
}); |
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
338580
9050
220