feat: "contains" and "uniqueItems" should be validated after type coercion, closes #611

master
Evgeny Poberezkin 2017-10-29 18:56:28 +00:00
parent 5e17f9b4ee
commit 2e95b0531f
2 changed files with 34 additions and 1 deletions

View File

@ -11,7 +11,7 @@ module.exports = function rules() {
{ type: 'string',
rules: [ 'maxLength', 'minLength', 'pattern', 'format' ] },
{ type: 'array',
rules: [ 'maxItems', 'minItems', 'uniqueItems', 'contains', 'items' ] },
rules: [ 'maxItems', 'minItems', 'items', 'contains', 'uniqueItems' ] },
{ type: 'object',
rules: [ 'maxProperties', 'minProperties', 'required', 'dependencies', 'propertyNames',
{ 'properties': ['additionalProperties', 'patternProperties'] } ] },

View File

@ -416,6 +416,39 @@ describe('Type coercion', function () {
});
it('should check "uniqueItems" after coercion', function() {
var schema = {
items: {type: 'number'},
uniqueItems: true
};
instances.forEach(function (_ajv) {
var validate = _ajv.compile(schema);
validate([1, '2', 3]). should.equal(true);
validate([1, '2', 2]). should.equal(false);
validate.errors.length .should.equal(1);
validate.errors[0].keyword .should.equal('uniqueItems');
});
});
it('should check "contains" after coercion', function() {
var schema = {
items: {type: 'number'},
contains: {const: 2}
};
instances.forEach(function (_ajv) {
var validate = _ajv.compile(schema);
validate([1, '2', 3]). should.equal(true);
validate([1, '3', 4]). should.equal(false);
validate.errors.pop().keyword .should.equal('contains');
});
});
function testRules(rules, cb) {
for (var toType in rules) {
for (var fromType in rules[toType]) {