feat: extract method to validate custom keyword definition
parent
187e0212bd
commit
6831b68f64
|
@ -122,6 +122,14 @@ declare namespace ajv {
|
||||||
*/
|
*/
|
||||||
removeKeyword(keyword: string): 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
|
* Convert array of error message objects to string
|
||||||
* @param {Array<object>} errors optional array of validation errors, if not passed errors from the instance are used.
|
* @param {Array<object>} 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`.
|
* @param {object} options optional options with properties `separator` and `dataVar`.
|
||||||
|
@ -219,6 +227,8 @@ declare namespace ajv {
|
||||||
metaSchema?: object;
|
metaSchema?: object;
|
||||||
// schema: false makes validate not to expect schema (ValidateFunction)
|
// schema: false makes validate not to expect schema (ValidateFunction)
|
||||||
schema?: boolean;
|
schema?: boolean;
|
||||||
|
statements?: boolean;
|
||||||
|
dependencies?: Array<string>;
|
||||||
modifying?: boolean;
|
modifying?: boolean;
|
||||||
valid?: boolean;
|
valid?: boolean;
|
||||||
// one and only one of the following properties should be present
|
// one and only one of the following properties should be present
|
||||||
|
|
|
@ -30,6 +30,7 @@ var customKeyword = require('./keyword');
|
||||||
Ajv.prototype.addKeyword = customKeyword.add;
|
Ajv.prototype.addKeyword = customKeyword.add;
|
||||||
Ajv.prototype.getKeyword = customKeyword.get;
|
Ajv.prototype.getKeyword = customKeyword.get;
|
||||||
Ajv.prototype.removeKeyword = customKeyword.remove;
|
Ajv.prototype.removeKeyword = customKeyword.remove;
|
||||||
|
Ajv.prototype.validateKeyword = customKeyword.validate;
|
||||||
|
|
||||||
var errorClasses = require('./compile/error_classes');
|
var errorClasses = require('./compile/error_classes');
|
||||||
Ajv.ValidationError = errorClasses.Validation;
|
Ajv.ValidationError = errorClasses.Validation;
|
||||||
|
|
|
@ -7,7 +7,8 @@ var metaSchema = require('./refs/json-schema-draft-07.json');
|
||||||
module.exports = {
|
module.exports = {
|
||||||
add: addKeyword,
|
add: addKeyword,
|
||||||
get: getKeyword,
|
get: getKeyword,
|
||||||
remove: removeKeyword
|
remove: removeKeyword,
|
||||||
|
validate: validateKeyword
|
||||||
};
|
};
|
||||||
|
|
||||||
var definitionSchema = {
|
var definitionSchema = {
|
||||||
|
@ -61,11 +62,7 @@ function addKeyword(keyword, definition) {
|
||||||
throw new Error('Keyword ' + keyword + ' is not a valid identifier');
|
throw new Error('Keyword ' + keyword + ' is not a valid identifier');
|
||||||
|
|
||||||
if (definition) {
|
if (definition) {
|
||||||
this.validateKeyword = this.validateKeyword
|
this.validateKeyword(definition, true);
|
||||||
|| this.compile(definitionSchema, true);
|
|
||||||
|
|
||||||
if (!this.validateKeyword(definition))
|
|
||||||
throw new Error('custom keyword definition is invalid: ' + this.errorsText(this.validateKeyword.errors));
|
|
||||||
|
|
||||||
var dataType = definition.type;
|
var dataType = definition.type;
|
||||||
if (Array.isArray(dataType)) {
|
if (Array.isArray(dataType)) {
|
||||||
|
@ -158,3 +155,24 @@ function removeKeyword(keyword) {
|
||||||
}
|
}
|
||||||
return this;
|
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;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue