-
-
Notifications
You must be signed in to change notification settings - Fork 290
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
Improve handling of oneOf error reporting #31
Comments
Upon further investigation, this is probably a mis-use of JSON Schema. If there's multiple different, but similar, schemas, they should each get a different schema URI. |
This is addressed in much more detail in #64 because I did not realize that you had re-filed the This is why I am so bothered by all of the open issues and pages in the old repo. Leaving them open without any link to this repo makes it look like they're not tracked here, which means duplicate filings. Would you be willing to close this one? Or I could close the other and import all of the info from it, but the title of this issue is much more narrow. |
Now that we have |
I think we can still offer some guidance on how validators can expose these cases to developers. They can produce output like:
|
@awwright true, but does that go in the spec or on the web site? An implementation notes section perhaps? I'm asking because I don't know- I do not have an opinion on where it should go, I'm just trying to learn what the appropriate scope for this sort of thing is in a spec. |
The spec is probably an appropriate place for a limited amount of advice on how to write the implementation. JSON (RFC7159) might be a good example, while all it is is a media type; the RFC also has a brief section on parsers. |
@awwright The |
I summary of our conversation with @handrews: I do not understand how the value that selectWith points to maps into any subschema inside oneOf/anyOf without using any complex algorythms. The alternative idea is to have another validation keyword, e.g. selectCases that would have a map of subschemas. |
@epoberezkin I'm not at all attached to the instance pointer version of In my experience, engineers needed to ramp up a bit on how to use "oneOf" but settled into it pretty well once they tried it out a bit. That's my main reason for continuing to object to imperative approaches. It's a dramatic departure to solve a "problem" that in my experience just needs a bit of explanation and best practices documentation. The web site needs a schema patterns cookbook section, really. |
So there seems to be a consensus developing around @epoberezkin if/then/else proposal in #180 (which avoids the complexity of |
The thing I don't like about it is that it also uses a pointer to the data in a way similar to proposed $data reference without using it explicitly. So I think select can be considered, but only if we adopt $data. With it we could: {
"select": { "$data": "0/kind" },
"selectCases": {
"value1": { "$ref": "schema1" },
"value2": { "$ref": "schema2" }
},
"selectDefault": { "$ref": "defaultSchema" }
} and you can The same can be expressed with conditional: {
"anyOf": [
{
"if": { "properties": { "kind": { "const": "value1" } } },
"then": { "$ref": "schema1" },
"else": false
},
{
"if": { "properties": { "kind": { "const": "value2" } } },
"then": { "$ref": "schema2" },
"else": false
},
{
"if": {
"not": {
"required": ["kind"],
"properties": { "kind": { "enum": [ "value1", "value2" ] } }
}
},
"then": { "$ref": "defaultSchema" }
}
]
} It's nowhere near as bad as if/then/else expressed with "anyOf" etc. But given how common this use case is (and usually you have much more than 2 options) I would very much like to see a shorter and much more expressive select form. But let's think about it. The key for it is $data, introducing select without it is a bad idea I think. |
@epoberezkin yeah, with |
I agree. But I've just realised that handling the default case makes it particularly ugly, as you have to repeat all the conditions in the negated form (because there is no order defined for anyOf validation) |
Hmm.... let's wait and see how much demand there really is for the default case. |
I agree |
I'm not sure why I put this in the draft-07 milestone. I guess because we were putting if/then/else in this milestone? I'm going to bump it out to future. We'll either implement this or close it unimplemented depending on feedback on if/then/else. Note also that this all somewhat like OpenAPI's |
I just posted https://stackoverflow.com/questions/49823500/how-to-validate-a-json-object-against-a-json-schema-based-on-objects-type-descr/49996397#49996397 which provides some insights into this problem. |
@awwright I think this is addressed by the output guidance in the latest draft? Plus Since this issues ended up with a couple of related ideas in it, and the discussion is by now quite outdated, I'm going to close it and if there are still outstanding concerns can you file them as new separate issues? |
I've searched through several threads here. There's no clarity for how I should implement an I'm here to +1 a switch statement. Barring that, is there a way to provide a mechanism for resolving oneOf ambiguity that can be guaranteed by the spec, as opposed to a gray area left up to implementors? Here's what my code looks like today:
|
Instead of nested
Bonus: this also allows you to have overlapping conditions. If you don't have overlapping conditions (i.e. they are mutually exclusive) and your implementation supports short-circuiting, then you could change the |
In cases where
oneOf
is used for multiple mutually exclusive options, it is frequently the case that the option to pick is a single key within the instance. E.g. the "type" property will be from the enum "Animal, Vegetable, Mineral" and the appropriate schema to apply is picked based on the value of this property.Right now, all schemas must be tested (similar to an O(n) operation). Only one schema corresponding with an e.g. "type" property need be tested (similar to an O(log n) or O(1) operation), and only errors against that schema need be reported. Otherwise we get bizarre, non-helpful errors like so:
The text was updated successfully, but these errors were encountered: