You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
Copy file name to clipboardExpand all lines: lib/ajv.js
+27-20Lines changed: 27 additions & 20 deletions
Original file line number
Diff line number
Diff line change
@@ -17,9 +17,9 @@ function SCHEMA_URI_FORMAT_FUNC(str) {
17
17
18
18
/**
19
19
* Creates validator instance.
20
-
* Usage: `jv(opts)`
20
+
* Usage: `Ajv(opts)`
21
21
* @param {Object} opts optional options
22
-
* @return {Object} jv instance
22
+
* @return {Object} ajv instance
23
23
*/
24
24
functionAjv(opts){
25
25
if(!(thisinstanceofAjv))returnnewAjv(opts);
@@ -38,6 +38,7 @@ function Ajv(opts) {
38
38
this.addSchema=addSchema;
39
39
this.validateSchema=validateSchema;
40
40
this.getSchema=getSchema;
41
+
this.removeSchema=removeSchema;
41
42
this.addFormat=addFormat;
42
43
this.errorsText=errorsText;
43
44
@@ -50,15 +51,13 @@ function Ajv(opts) {
50
51
* 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.
51
52
* @param {String|Object} schemaKeyRef key, ref or schema object
52
53
* @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`).
54
55
*/
55
56
functionvalidate(schemaKeyRef,data){
57
+
varv;
56
58
if(typeofschemaKeyRef=='string'){
57
-
varv=getSchema(schemaKeyRef);
58
-
if(!v){
59
-
v=getRef(schemaKeyRef);
60
-
if(!v)thrownewError('no schema with key or ref "'+schemaKeyRef+'"');
61
-
}
59
+
v=getSchema(schemaKeyRef);
60
+
if(!v)thrownewError('no schema with key or ref "'+schemaKeyRef+'"');
62
61
}elsev=_addSchema(schemaKeyRef);
63
62
64
63
varvalid=v(data);
@@ -112,25 +111,33 @@ function Ajv(opts) {
112
111
113
112
114
113
/**
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).
117
116
* @return {Function} schema validating function (with property `schema`).
118
117
*/
119
-
functiongetSchema(key){
120
-
key=resolve.normalizeId(key);
121
-
returnself._schemas[key];
118
+
functiongetSchema(keyRef){
119
+
keyRef=resolve.normalizeId(keyRef);
120
+
returnself._schemas[keyRef]||self._refs[keyRef];
122
121
}
123
122
124
123
125
124
/**
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
0 commit comments