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

"anyOf" and "oneOf" give incorrect error message #214

Open
noahhoffman opened this issue Aug 23, 2018 · 2 comments
Open

"anyOf" and "oneOf" give incorrect error message #214

noahhoffman opened this issue Aug 23, 2018 · 2 comments

Comments

@noahhoffman
Copy link

noahhoffman commented Aug 23, 2018

Using "anyOf" or "oneOf" gives an incorrect message by always indicating the the attribute defined in the first index of the anyOf or oneOf array is required.

Given the following schema:

{ "$schema":"http://json-schema.org/draft-07/schema#", "$id":"test.schema.json", "title":"test", "type": "object", "properties": { "options": { "type":"object", "anyOf": [ {"required": ["one"]}, {"required": ["two"]} ], "properties":{ "foo": { "type":"object" }, "bar": { "type":"object" } } } } }

and the following JSON
{ "options":{ "foo":{} } }


Expected Results:

Generated at https://www.jsonschemavalidator.net/
expectederrormsg


Actual Results:

Code used to validate and show errors reported:
result, err := gojsonschema.Validate(schemaLoader, requestLoader) if !result.Valid() { for _, err := range result.Errors() { log.Println() } }

Actual Error Message:

options: Must validate at least one schema (anyOf)
layers: one is required

@johandorland
Copy link
Collaborator

Error messages for anyOf and oneOf are tricky as it's not always as clear what the error message should be. If you have multiple clauses in a oneOf and none of them pass and then put all errors of all clauses in the resulting error set that can lead to a massive amount of errors. Most of those errors will be nonsense as only one of them needs to be satisfied, and in the case of oneOf, satisfying too many will lead to a different error.

Therefore the original author chose to only return one single error for anyOf and oneOf by heuristically looking for the clause that matched most closely (has the least amounts of errors), and only return the errors from that clause. However in practice (and also your example), all clauses are equally unsatisfied and therefore the first one is chosen.

Most implementations, like https://www.jsonschemavalidator.net/ do this differently. I'm not sold on the way it's currently done in gojsonschema, but I don't hate it either. If anyone convinces me that this really should be changed and submits a PR I'm open to it.

@handrews
Copy link

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

No branches or pull requests

3 participants