feat: improve oneOf error reporting, #573
parent
2e95b0531f
commit
a68e9a7535
|
@ -182,7 +182,7 @@
|
|||
_limitProperties:"{ limit: {{=$schemaValue}} }",
|
||||
multipleOf: "{ multipleOf: {{=$schemaValue}} }",
|
||||
not: "{}",
|
||||
oneOf: "{}",
|
||||
oneOf: "{ passingSchemas: {{=$passingSchemas}} }",
|
||||
pattern: "{ pattern: {{#def.schemaValueQS}} }",
|
||||
patternGroups: "{ reason: '{{=$reason}}', limit: {{=$limit}}, pattern: '{{=it.util.escapeQuotes($pgProperty)}}' }",
|
||||
propertyNames: "{ propertyName: '{{=$invalidName}}' }",
|
||||
|
|
|
@ -3,11 +3,17 @@
|
|||
{{# def.setupKeyword }}
|
||||
{{# def.setupNextLevel }}
|
||||
|
||||
var {{=$errs}} = errors;
|
||||
var prevValid{{=$lvl}} = false;
|
||||
var {{=$valid}} = false;
|
||||
{{
|
||||
var $currentBaseId = $it.baseId
|
||||
, $prevValid = 'prevValid' + $lvl
|
||||
, $passingSchemas = 'passingSchemas' + $lvl;
|
||||
}}
|
||||
|
||||
var {{=$errs}} = errors
|
||||
, {{=$prevValid}} = false
|
||||
, {{=$valid}} = false
|
||||
, {{=$passingSchemas}} = null;
|
||||
|
||||
{{ var $currentBaseId = $it.baseId; }}
|
||||
{{# def.setCompositeRule }}
|
||||
|
||||
{{~ $schema:$sch:$i }}
|
||||
|
@ -24,13 +30,17 @@ var {{=$valid}} = false;
|
|||
{{?}}
|
||||
|
||||
{{? $i }}
|
||||
if ({{=$nextValid}} && prevValid{{=$lvl}})
|
||||
if ({{=$nextValid}} && {{=$prevValid}}) {
|
||||
{{=$valid}} = false;
|
||||
else {
|
||||
{{=$passingSchemas}} = [{{=$passingSchemas}}, {{=$i}}];
|
||||
} else {
|
||||
{{ $closingBraces += '}'; }}
|
||||
{{?}}
|
||||
|
||||
if ({{=$nextValid}}) {{=$valid}} = prevValid{{=$lvl}} = true;
|
||||
if ({{=$nextValid}}) {
|
||||
{{=$valid}} = {{=$prevValid}} = true;
|
||||
{{=$passingSchemas}} = {{=$i}};
|
||||
}
|
||||
{{~}}
|
||||
|
||||
{{# def.resetCompositeRule }}
|
||||
|
|
|
@ -539,6 +539,39 @@ describe('Validation errors', function () {
|
|||
validate(1.5) .should.equal(true);
|
||||
}
|
||||
});
|
||||
|
||||
it('should return passing schemas in error params', function() {
|
||||
var schema = {
|
||||
oneOf: [
|
||||
{ type: 'number' },
|
||||
{ type: 'integer' },
|
||||
{ const: 1.5 }
|
||||
]
|
||||
};
|
||||
|
||||
test(ajv);
|
||||
test(fullAjv);
|
||||
|
||||
function test(_ajv) {
|
||||
var validate = _ajv.compile(schema);
|
||||
validate(1) .should.equal(false);
|
||||
var err = validate.errors.pop();
|
||||
err.keyword .should.equal('oneOf');
|
||||
err.params .should.eql({passingSchemas: [0, 1]});
|
||||
|
||||
validate(1.5) .should.equal(false);
|
||||
err = validate.errors.pop();
|
||||
err.keyword .should.equal('oneOf');
|
||||
err.params .should.eql({passingSchemas: [0, 2]});
|
||||
|
||||
validate(2.5) .should.equal(true);
|
||||
|
||||
validate('foo') .should.equal(false);
|
||||
err = validate.errors.pop();
|
||||
err.keyword .should.equal('oneOf');
|
||||
err.params .should.eql({passingSchemas: null});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue