removeSchema method, closes #8
parent
b16989f779
commit
a97e6740e7
|
@ -126,7 +126,14 @@ Errors will be available at `ajv.errors`.
|
|||
|
||||
##### .getSchema(String key) -> Function<Object data>
|
||||
|
||||
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)
|
||||
|
|
47
lib/ajv.js
47
lib/ajv.js
|
@ -17,9 +17,9 @@ function SCHEMA_URI_FORMAT_FUNC(str) {
|
|||
|
||||
/**
|
||||
* Creates validator instance.
|
||||
* Usage: `jv(opts)`
|
||||
* Usage: `Ajv(opts)`
|
||||
* @param {Object} opts optional options
|
||||
* @return {Object} jv instance
|
||||
* @return {Object} ajv instance
|
||||
*/
|
||||
function Ajv(opts) {
|
||||
if (!(this instanceof Ajv)) return new Ajv(opts);
|
||||
|
@ -38,6 +38,7 @@ function Ajv(opts) {
|
|||
this.addSchema = addSchema;
|
||||
this.validateSchema = validateSchema;
|
||||
this.getSchema = getSchema;
|
||||
this.removeSchema = removeSchema;
|
||||
this.addFormat = addFormat;
|
||||
this.errorsText = errorsText;
|
||||
|
||||
|
@ -50,15 +51,13 @@ function Ajv(opts) {
|
|||
* Schema will be compiled and cached (using serialized JSON as key. [json-stable-stringify](https://github.com/substack/json-stable-stringify) is used to serialize.
|
||||
* @param {String|Object} schemaKeyRef key, ref or schema object
|
||||
* @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);
|
||||
|
||||
var valid = v(data);
|
||||
|
@ -112,25 +111,33 @@ function Ajv(opts) {
|
|||
|
||||
|
||||
/**
|
||||
* 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];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"name": "ajv",
|
||||
"version": "0.5.8",
|
||||
"description": "Another JSON schema Validator",
|
||||
"version": "0.5.9",
|
||||
"description": "Another JSON Schema Validator",
|
||||
"main": "lib/ajv.js",
|
||||
"scripts": {
|
||||
"test": "./node_modules/.bin/mocha --reporter=spec spec/*.spec.js"
|
||||
|
|
|
@ -2,7 +2,8 @@
|
|||
|
||||
|
||||
var Ajv = require('../lib/ajv')
|
||||
, should = require('chai').should();
|
||||
, should = require('chai').should()
|
||||
, stableStringify = require('json-stable-stringify');
|
||||
|
||||
|
||||
describe('Ajv', function () {
|
||||
|
@ -181,4 +182,42 @@ describe('Ajv', function () {
|
|||
v .should.equal(validate);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
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));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue