better-ajv-errors icon indicating copy to clipboard operation
better-ajv-errors copied to clipboard

Implement support for oneOf

Open buremba opened this issue 5 years ago • 4 comments

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":""}]

buremba avatar Jan 20 '20 23:01 buremba

In your example, did you mean for: "target": { "$ref": "#/definitions/sql" } to be: "sql": { "$ref": "#/definitions/sql" }

chrisolson443 avatar Oct 08 '20 20:10 chrisolson443

@crizo23 thanks correct, I updated the snippet. Thanks!

buremba avatar Oct 08 '20 21:10 buremba

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

chrisolson443 avatar Oct 08 '20 22:10 chrisolson443

@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"

chrisolson443 avatar Oct 08 '20 22:10 chrisolson443