fix: removeAdditional option breaking custom keywords, closes #955, closes epoberezkin/ajv-keywords#91
parent
f6d25def2b
commit
187e0212bd
|
@ -43,8 +43,6 @@ var definitionSchema = {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var validateDefinition;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Define custom keyword
|
* Define custom keyword
|
||||||
* @this Ajv
|
* @this Ajv
|
||||||
|
@ -56,7 +54,6 @@ function addKeyword(keyword, definition) {
|
||||||
/* jshint validthis: true */
|
/* jshint validthis: true */
|
||||||
/* eslint no-shadow: 0 */
|
/* eslint no-shadow: 0 */
|
||||||
var RULES = this.RULES;
|
var RULES = this.RULES;
|
||||||
|
|
||||||
if (RULES.keywords[keyword])
|
if (RULES.keywords[keyword])
|
||||||
throw new Error('Keyword ' + keyword + ' is already defined');
|
throw new Error('Keyword ' + keyword + ' is already defined');
|
||||||
|
|
||||||
|
@ -64,10 +61,11 @@ 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) {
|
||||||
validateDefinition = validateDefinition || this.compile(definitionSchema);
|
this.validateKeyword = this.validateKeyword
|
||||||
|
|| this.compile(definitionSchema, true);
|
||||||
|
|
||||||
if (!validateDefinition(definition))
|
if (!this.validateKeyword(definition))
|
||||||
throw new Error('custom keyword definition is invalid: ' + this.errorsText(validateDefinition.errors));
|
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)) {
|
||||||
|
|
|
@ -0,0 +1,48 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
var Ajv = require('../ajv');
|
||||||
|
require('../chai').should();
|
||||||
|
|
||||||
|
|
||||||
|
describe('issue #955: option removeAdditional breaks custom keywords', function() {
|
||||||
|
it('should support custom keywords with option removeAdditional', function() {
|
||||||
|
var ajv = new Ajv({removeAdditional: 'all'});
|
||||||
|
|
||||||
|
ajv.addKeyword('minTrimmedLength', {
|
||||||
|
type: 'string',
|
||||||
|
compile: function(schema) {
|
||||||
|
return function(str) {
|
||||||
|
return str.trim().length >= schema;
|
||||||
|
};
|
||||||
|
},
|
||||||
|
metaSchema: {type: 'integer'}
|
||||||
|
});
|
||||||
|
|
||||||
|
var schema = {
|
||||||
|
type: 'object',
|
||||||
|
properties: {
|
||||||
|
foo: {
|
||||||
|
type: 'string',
|
||||||
|
minTrimmedLength: 3
|
||||||
|
}
|
||||||
|
},
|
||||||
|
required: ['foo']
|
||||||
|
};
|
||||||
|
|
||||||
|
var validate = ajv.compile(schema);
|
||||||
|
|
||||||
|
var data = {
|
||||||
|
foo: ' bar ',
|
||||||
|
baz: ''
|
||||||
|
};
|
||||||
|
validate(data) .should.equal(true);
|
||||||
|
data .should.not.have.property('baz');
|
||||||
|
|
||||||
|
data = {
|
||||||
|
foo: ' ba ',
|
||||||
|
baz: ''
|
||||||
|
};
|
||||||
|
validate(data) .should.equal(false);
|
||||||
|
data .should.not.have.property('baz');
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue