Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

oneOf returns unwanted errors #2394

Closed
VasudhaMathur opened this issue Mar 13, 2024 · 1 comment
Closed

oneOf returns unwanted errors #2394

VasudhaMathur opened this issue Mar 13, 2024 · 1 comment

Comments

@VasudhaMathur
Copy link

What version of Ajv you are you using?
8.12.0

What problem do you want to solve?

Schema:
var schema = { type: 'object', additionalProperties: false, properties: { children: { type: 'array', items: { oneOf: [{ type: 'object', properties: { a: { type: 'string' } }, required: ['a'], additionalProperties: false }, { type: 'object', properties: { b: { type: 'string' } }, required: ['b'], additionalProperties: false }] } } } };

Data:
const data = { "children": [ { a: 'a' }, { b: 'b' }, { c: 'c' } ] };

Code:
var Ajv = require('ajv'); var ajv = new Ajv(); ajv.validate(schema, data); console.log('Errors:', ajv.errors);

Errors:
Array of 3 items:
0: Object instancePath: "/children/2" schemaPath: "#/properties/children/items/oneOf/0/required" keyword: "required" params: Object {missingProperty: "a"} message: "must have required property 'a'"

1: Object instancePath: "/children/2" schemaPath: "#/properties/children/items/oneOf/1/required" keyword: "required" params: Object {missingProperty: "b"} message: "must have required property 'b'"

2: Object instancePath: "/children/2" schemaPath: "#/properties/children/items/oneOf" keyword: "oneOf" params: Object {passingSchemas: null} message: "must match exactly one schema in oneOf"

In real use case I have 20+ properties. And if in the data there's a typo, I get 20+ errors, when I'm looking for only "must match exactly one schema in oneOf". Is there a way to update the schema such that I get only the oneOf error?

@jasoniangreen
Copy link
Collaborator

jasoniangreen commented Mar 13, 2024

I am still getting to grips with all the details of AJV but I do know that oneOf is one of (no pun intended) the most misunderstood and misused features of AJV. The most common advice I hear when dealing with oneOf is that you probably want to actually use anyOf. If the types in your oneOf have no overlap, then I think it's equivalent, but more efficient since it doesn't need to check ALL of the different types when it finds a match.

As for the error reporting I think that what you want may have once been the behaviour according to this response by EP so I can only assume it was changed to report all of the schemas that didn't match for good reasons.

So AFAIK this is not something that we'll be open to changing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants