Skip to content

Commit a97e674

Browse files
committed
removeSchema method, closes #8
1 parent b16989f commit a97e674

File tree

4 files changed

+77
-24
lines changed

4 files changed

+77
-24
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,14 @@ Errors will be available at `ajv.errors`.
126126

127127
##### .getSchema(String key) -> Function<Object data>
128128

129-
Retrieve compiled schema previously added with `addSchema`. Validating function has `schema` property with the reference to the original schema.
129+
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.
130+
131+
132+
##### .removeSchema(Object schema|String key|String ref)
133+
134+
Remove added/cached schema. Even if schema is referenced by other schemas it can be safely removed as dependent schemas have local references.
135+
136+
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.
130137

131138

132139
##### .addFormat(String name, String|RegExp|Function format)

lib/ajv.js

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ function SCHEMA_URI_FORMAT_FUNC(str) {
1717

1818
/**
1919
* Creates validator instance.
20-
* Usage: `jv(opts)`
20+
* Usage: `Ajv(opts)`
2121
* @param {Object} opts optional options
22-
* @return {Object} jv instance
22+
* @return {Object} ajv instance
2323
*/
2424
function Ajv(opts) {
2525
if (!(this instanceof Ajv)) return new Ajv(opts);
@@ -38,6 +38,7 @@ function Ajv(opts) {
3838
this.addSchema = addSchema;
3939
this.validateSchema = validateSchema;
4040
this.getSchema = getSchema;
41+
this.removeSchema = removeSchema;
4142
this.addFormat = addFormat;
4243
this.errorsText = errorsText;
4344

@@ -50,15 +51,13 @@ function Ajv(opts) {
5051
* 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.
5152
* @param {String|Object} schemaKeyRef key, ref or schema object
5253
* @param {Any} data to be validated
53-
* @return {Boolean} validation result. Errors from the last validation will be available in `jv.errors` (and also in compiled schema: `schema.errors`).
54+
* @return {Boolean} validation result. Errors from the last validation will be available in `ajv.errors` (and also in compiled schema: `schema.errors`).
5455
*/
5556
function validate(schemaKeyRef, data) {
57+
var v;
5658
if (typeof schemaKeyRef == 'string') {
57-
var v = getSchema(schemaKeyRef);
58-
if (!v) {
59-
v = getRef(schemaKeyRef);
60-
if (!v) throw new Error('no schema with key or ref "' + schemaKeyRef + '"');
61-
}
59+
v = getSchema(schemaKeyRef);
60+
if (!v) throw new Error('no schema with key or ref "' + schemaKeyRef + '"');
6261
} else v = _addSchema(schemaKeyRef);
6362

6463
var valid = v(data);
@@ -112,25 +111,33 @@ function Ajv(opts) {
112111

113112

114113
/**
115-
* Get compiled schema from the instance by `key`.
116-
* @param {String} key `key` that was passed to `addSchema` (or `schema.id`).
114+
* Get compiled schema from the instance by `key` or `ref`.
115+
* @param {String} keyRef `key` that was passed to `addSchema` or full schema reference (`schema.id` or resolved id).
117116
* @return {Function} schema validating function (with property `schema`).
118117
*/
119-
function getSchema(key) {
120-
key = resolve.normalizeId(key);
121-
return self._schemas[key];
118+
function getSchema(keyRef) {
119+
keyRef = resolve.normalizeId(keyRef);
120+
return self._schemas[keyRef] || self._refs[keyRef];
122121
}
123122

124123

125124
/**
126-
* Get compiled schema from the instance by `id`.
127-
* @param {String} id `schema.id` or any reference in any of previously passed schemas.
128-
* @return {Function} schema validating function (with property `schema`).
125+
* Remove cached schema
126+
* Even if schema is referenced by other schemas it still can be removed as other schemas have local references
127+
* @param {String|Object} schemaKeyRef key, ref or schema object
129128
*/
130-
function getRef(ref) {
131-
ref = resolve.normalizeId(ref);
132-
// TODO
133-
return self._refs[ref];
129+
function removeSchema(schemaKeyRef) {
130+
if (typeof schemaKeyRef == 'string') {
131+
schemaKeyRef = resolve.normalizeId(schemaKeyRef);
132+
var v = self._schemas[schemaKeyRef] || self._refs[schemaKeyRef];
133+
delete self._schemas[schemaKeyRef];
134+
delete self._refs[schemaKeyRef];
135+
var str = stableStringify(v.schema);
136+
self._cache.put(str);
137+
} else {
138+
var str = stableStringify(schemaKeyRef);
139+
self._cache.put(str);
140+
}
134141
}
135142

136143

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "ajv",
3-
"version": "0.5.8",
4-
"description": "Another JSON schema Validator",
3+
"version": "0.5.9",
4+
"description": "Another JSON Schema Validator",
55
"main": "lib/ajv.js",
66
"scripts": {
77
"test": "./node_modules/.bin/mocha --reporter=spec spec/*.spec.js"

spec/ajv.spec.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33

44
var Ajv = require('../lib/ajv')
5-
, should = require('chai').should();
5+
, should = require('chai').should()
6+
, stableStringify = require('json-stable-stringify');
67

78

89
describe('Ajv', function () {
@@ -181,4 +182,42 @@ describe('Ajv', function () {
181182
v .should.equal(validate);
182183
});
183184
});
185+
186+
187+
describe('removeSchema method', function() {
188+
it('should remove schema by key', function() {
189+
var schema = { type: 'integer' }
190+
, str = stableStringify(schema);
191+
var v = ajv.addSchema(schema, 'int');
192+
ajv.getSchema('int') .should.equal(v);
193+
ajv._cache.get(str) .should.equal(v);
194+
195+
ajv.removeSchema('int');
196+
should.not.exist(ajv.getSchema('int'));
197+
should.not.exist(ajv._cache.get(str));
198+
});
199+
200+
it('should remove schema by id', function() {
201+
var schema = { id: '//e.com/int.json', type: 'integer' }
202+
, str = stableStringify(schema);
203+
var v = ajv.addSchema(schema);
204+
ajv.getSchema('//e.com/int.json') .should.equal(v);
205+
ajv._cache.get(str) .should.equal(v);
206+
207+
ajv.removeSchema('//e.com/int.json');
208+
should.not.exist(ajv.getSchema('//e.com/int.json'));
209+
should.not.exist(ajv._cache.get(str));
210+
});
211+
212+
it('should remove schema by schema object', function() {
213+
var schema = { type: 'integer' }
214+
, str = stableStringify(schema);
215+
var v = ajv.addSchema(schema);
216+
ajv._cache.get(str) .should.equal(v);
217+
218+
ajv.removeSchema({ type: 'integer' });
219+
// should.not.exist(ajv.getSchema('int'));
220+
should.not.exist(ajv._cache.get(str));
221+
});
222+
});
184223
});

0 commit comments

Comments
 (0)