fix: improve error reporting from oneOf keyword, closes #427

master
Evgeny Poberezkin 2017-02-25 21:16:55 +00:00
parent 1a949eeea3
commit 3c86da4f1b
3 changed files with 50 additions and 3 deletions

View File

@ -35,7 +35,7 @@
{{= $closingBraces }}
if (!{{=$valid}}) {
{{# def.addError:'anyOf' }}
{{# def.extraError:'anyOf' }}
} else {
{{# def.resetErrors }}
{{? it.opts.allErrors }} } {{?}}

View File

@ -38,7 +38,7 @@ var {{=$valid}} = false;
{{= $closingBraces }}
if (!{{=$valid}}) {
{{# def.error:'oneOf' }}
{{# def.extraError:'oneOf' }}
} else {
{{# def.resetErrors }}
{{? it.opts.allErrors }} } {{?}}

View File

@ -459,7 +459,7 @@ describe('Validation errors', function () {
});
it('should has correct schema path for additionalItems', function() {
it('should have correct schema path for additionalItems', function() {
var schema = {
type: 'array',
items: [ { type: 'integer' }, { type: 'integer' } ],
@ -518,6 +518,53 @@ describe('Validation errors', function () {
});
describe('oneOf errors', function() {
it('should have errors from inner schemas', function() {
var schema = {
oneOf: [
{ type: 'number' },
{ type: 'integer' }
]
};
test(ajv);
test(fullAjv);
function test(_ajv) {
var validate = _ajv.compile(schema);
validate('foo') .should.equal(false);
validate.errors.length .should.equal(3);
validate(1) .should.equal(false);
validate.errors.length .should.equal(1);
validate(1.5) .should.equal(true);
}
});
});
describe('anyOf errors', function() {
it('should have errors from inner schemas', function() {
var schema = {
anyOf: [
{ type: 'number' },
{ type: 'integer' }
]
};
test(ajv);
test(fullAjv);
function test(_ajv) {
var validate = _ajv.compile(schema);
validate('foo') .should.equal(false);
validate.errors.length .should.equal(3);
validate(1) .should.equal(true);
validate(1.5) .should.equal(true);
}
});
});
function testSchema1(schema, schemaPathPrefix) {
_testSchema1(ajv, schema, schemaPathPrefix);
_testSchema1(ajvJP, schema, schemaPathPrefix);