feat: improve oneOf error reporting, #573
parent
2e95b0531f
commit
a68e9a7535
|
@ -182,7 +182,7 @@
|
||||||
_limitProperties:"{ limit: {{=$schemaValue}} }",
|
_limitProperties:"{ limit: {{=$schemaValue}} }",
|
||||||
multipleOf: "{ multipleOf: {{=$schemaValue}} }",
|
multipleOf: "{ multipleOf: {{=$schemaValue}} }",
|
||||||
not: "{}",
|
not: "{}",
|
||||||
oneOf: "{}",
|
oneOf: "{ passingSchemas: {{=$passingSchemas}} }",
|
||||||
pattern: "{ pattern: {{#def.schemaValueQS}} }",
|
pattern: "{ pattern: {{#def.schemaValueQS}} }",
|
||||||
patternGroups: "{ reason: '{{=$reason}}', limit: {{=$limit}}, pattern: '{{=it.util.escapeQuotes($pgProperty)}}' }",
|
patternGroups: "{ reason: '{{=$reason}}', limit: {{=$limit}}, pattern: '{{=it.util.escapeQuotes($pgProperty)}}' }",
|
||||||
propertyNames: "{ propertyName: '{{=$invalidName}}' }",
|
propertyNames: "{ propertyName: '{{=$invalidName}}' }",
|
||||||
|
|
|
@ -3,11 +3,17 @@
|
||||||
{{# def.setupKeyword }}
|
{{# def.setupKeyword }}
|
||||||
{{# def.setupNextLevel }}
|
{{# def.setupNextLevel }}
|
||||||
|
|
||||||
var {{=$errs}} = errors;
|
{{
|
||||||
var prevValid{{=$lvl}} = false;
|
var $currentBaseId = $it.baseId
|
||||||
var {{=$valid}} = false;
|
, $prevValid = 'prevValid' + $lvl
|
||||||
|
, $passingSchemas = 'passingSchemas' + $lvl;
|
||||||
|
}}
|
||||||
|
|
||||||
|
var {{=$errs}} = errors
|
||||||
|
, {{=$prevValid}} = false
|
||||||
|
, {{=$valid}} = false
|
||||||
|
, {{=$passingSchemas}} = null;
|
||||||
|
|
||||||
{{ var $currentBaseId = $it.baseId; }}
|
|
||||||
{{# def.setCompositeRule }}
|
{{# def.setCompositeRule }}
|
||||||
|
|
||||||
{{~ $schema:$sch:$i }}
|
{{~ $schema:$sch:$i }}
|
||||||
|
@ -24,13 +30,17 @@ var {{=$valid}} = false;
|
||||||
{{?}}
|
{{?}}
|
||||||
|
|
||||||
{{? $i }}
|
{{? $i }}
|
||||||
if ({{=$nextValid}} && prevValid{{=$lvl}})
|
if ({{=$nextValid}} && {{=$prevValid}}) {
|
||||||
{{=$valid}} = false;
|
{{=$valid}} = false;
|
||||||
else {
|
{{=$passingSchemas}} = [{{=$passingSchemas}}, {{=$i}}];
|
||||||
|
} else {
|
||||||
{{ $closingBraces += '}'; }}
|
{{ $closingBraces += '}'; }}
|
||||||
{{?}}
|
{{?}}
|
||||||
|
|
||||||
if ({{=$nextValid}}) {{=$valid}} = prevValid{{=$lvl}} = true;
|
if ({{=$nextValid}}) {
|
||||||
|
{{=$valid}} = {{=$prevValid}} = true;
|
||||||
|
{{=$passingSchemas}} = {{=$i}};
|
||||||
|
}
|
||||||
{{~}}
|
{{~}}
|
||||||
|
|
||||||
{{# def.resetCompositeRule }}
|
{{# def.resetCompositeRule }}
|
||||||
|
|
|
@ -539,6 +539,39 @@ describe('Validation errors', function () {
|
||||||
validate(1.5) .should.equal(true);
|
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