better-ajv-errors
better-ajv-errors copied to clipboard
Implement support for oneOf
Here is my schema:
{
"oneOf": [
{
"properties": {
"target": { "$ref": "#/definitions/target" }
},
"required": ["target"]
},
{
"properties": {
"sql": { "$ref": "#/definitions/sql" }
},
"required": ["sql"]
},
]
}
If my input has neither sql nor target, the error output is the following:
[{"start":{"line":1,"column":1,"offset":0},"error":" should have required property 'sql'","path":""}]
For the reference, here is the error output from Ajv:
[{"keyword":"required","dataPath":"","schemaPath":"#/oneOf/0/required","params":{"missingProperty":"target"},"message":"should have required property 'target'"},{"keyword":"required","dataPath":"","schemaPath":"#/oneOf/1/required","params":{"missingProperty":"sql"},"message":"should have required property 'sql'"},{"keyword":"oneOf","dataPath":"","schemaPath":"#/oneOf","params":{"passingSchemas":null},"message":"should match exactly one schema in oneOf"}]
I was expecting something like:
[{"start":{"line":1,"column":1,"offset":0},"error":" must have either 'sql' or 'target'","path":""}]
In your example, did you mean for:
"target": { "$ref": "#/definitions/sql" }
to be:
"sql": { "$ref": "#/definitions/sql" }
@crizo23 thanks correct, I updated the snippet. Thanks!
I've encountered the same issue. I'm pretty sure the problem is somewhere in helpers.js and its makeTree or filterRedundant methods - I think these methods are trying to be too smart and are filtering out errors we actually care about. I also think there could be a config option to ignore any processing in helpers.js and to return all errors returned by AJV
UPDATE: please ignore most of my comment above. After looking at the source code I now realize that the title of this issue is correct - oneOf just isn't yet supported in filterRedundant. Perhaps I'll attempt a fix
@buremba responding to your original question, I think the error you're expecting is in the AJV results, it's just not returned by better-ajv-errors:
"message":"should match exactly one schema in oneOf"