-
-
Notifications
You must be signed in to change notification settings - Fork 864
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
Question regarding format & oneOf error validation object #427
Comments
@krishnanms "oneOf" requires the data to be valid against exactly one schema, unlike "anyOf" that requires that data is valid against any of the schemas (i.e. possibly valid against more than one schema). In general, the approach to error reporting is that if all the errors are fixed then the data will be valid against the schema. That is not the case with oneOf though. In your case if Ajv were to return the errors from schemas inside oneOf it would return:
That's what Ajv would return if you used anyOf, by the way. That was the motivation to not report underlying errors from oneOf. I will re-consider this decision, in the meanwhile you can:
In general it is always better to use "anyOf" in cases when schemas are mutually exclusive (both can't be valid at the same time), as in your case, and only use "oneOf" when you need to make sure that ONLY ONE schema is valid. "oneOf" will always validate data against ALL schemas, while "anyOf" always short circuits - as soon as the data is valid against some schema, validation of anyOf succeeds. |
@epoberezkin Thanks for the detailed response. I was able to work around this, by setting |
@krishnanms interesting. I didn't realise it depends on allErrors option, I don't think it should. This option affects continuing validation after the first failure that determines the validation result, and oneOf keyword is the first such failure in both cases. So it should be made consistent. |
If you decide to improve it please make PR to 5.0.0 branch. |
What version of Ajv are you using? Does the issue happen if you use the latest version?
4.11.3.
Ajv options object (see https://github.com/epoberezkin/ajv#options):
JSON Schema (please make it as small as possible to reproduce the issue):
Data (please make it as small as possible to reproduce the issue):
Your code (please use
options
,schema
anddata
as variables):https://runkit.com/krishnanms/58af9965e4edc300143368dd
Validation result, data AFTER validation, error messages:
What results did you expect?
The
message
shows'should match exactly one schema in oneOf'
, while the schema validation failed due to an invalidformat
for the ip address, shouldn't this be the keyword and cause for the validation failure rather thanoneOf
?Are you going to resolve the issue?
I am not sure I can.
The text was updated successfully, but these errors were encountered: