From 6831b68f640f8f36a6099cafc443ced85f518d8a Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Fri, 22 Feb 2019 11:16:30 +0000 Subject: [PATCH] feat: extract method to validate custom keyword definition --- lib/ajv.d.ts | 10 ++++++++++ lib/ajv.js | 1 + lib/keyword.js | 30 ++++++++++++++++++++++++------ 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/lib/ajv.d.ts b/lib/ajv.d.ts index 6593d07..8b0d9ab 100644 --- a/lib/ajv.d.ts +++ b/lib/ajv.d.ts @@ -122,6 +122,14 @@ declare namespace ajv { */ removeKeyword(keyword: string): Ajv; /** + * Validate keyword + * @this Ajv + * @param {object} definition keyword definition object + * @param {boolean} throwError true to throw exception if definition is invalid + * @return {boolean} validation result + */ + validateKeyword(definition: KeywordDefinition, throwError: boolean): boolean; + /** * Convert array of error message objects to string * @param {Array} errors optional array of validation errors, if not passed errors from the instance are used. * @param {object} options optional options with properties `separator` and `dataVar`. @@ -219,6 +227,8 @@ declare namespace ajv { metaSchema?: object; // schema: false makes validate not to expect schema (ValidateFunction) schema?: boolean; + statements?: boolean; + dependencies?: Array; modifying?: boolean; valid?: boolean; // one and only one of the following properties should be present diff --git a/lib/ajv.js b/lib/ajv.js index 0a97367..105315a 100644 --- a/lib/ajv.js +++ b/lib/ajv.js @@ -30,6 +30,7 @@ var customKeyword = require('./keyword'); Ajv.prototype.addKeyword = customKeyword.add; Ajv.prototype.getKeyword = customKeyword.get; Ajv.prototype.removeKeyword = customKeyword.remove; +Ajv.prototype.validateKeyword = customKeyword.validate; var errorClasses = require('./compile/error_classes'); Ajv.ValidationError = errorClasses.Validation; diff --git a/lib/keyword.js b/lib/keyword.js index 5ea565c..cf4d699 100644 --- a/lib/keyword.js +++ b/lib/keyword.js @@ -7,7 +7,8 @@ var metaSchema = require('./refs/json-schema-draft-07.json'); module.exports = { add: addKeyword, get: getKeyword, - remove: removeKeyword + remove: removeKeyword, + validate: validateKeyword }; var definitionSchema = { @@ -61,11 +62,7 @@ function addKeyword(keyword, definition) { throw new Error('Keyword ' + keyword + ' is not a valid identifier'); if (definition) { - this.validateKeyword = this.validateKeyword - || this.compile(definitionSchema, true); - - if (!this.validateKeyword(definition)) - throw new Error('custom keyword definition is invalid: ' + this.errorsText(this.validateKeyword.errors)); + this.validateKeyword(definition, true); var dataType = definition.type; if (Array.isArray(dataType)) { @@ -158,3 +155,24 @@ function removeKeyword(keyword) { } return this; } + + +/** + * Validate keyword definition + * @this Ajv + * @param {Object} definition keyword definition object. + * @param {Boolean} throwError true to throw exception if definition is invalid + * @return {boolean} validation result + */ +function validateKeyword(definition, throwError) { + validateKeyword.errors = null; + var v = this._validateKeyword = this._validateKeyword + || this.compile(definitionSchema, true); + + if (v(definition)) return true; + validateKeyword.errors = v.errors; + if (throwError) + throw new Error('custom keyword definition is invalid: ' + this.errorsText(v.errors)); + else + return false; +}