-
-
Notifications
You must be signed in to change notification settings - Fork 266
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
Proposal: Add propertyDiscriminator keyword (aka discriminator) #1413
Comments
The only hesitation I have for this is the use of a tuple construct as part of the definition. In order to understand the keyword, you have to "just know" that the first item in the tuple is a key to match on and the second is the schema (rather than the components having "names" that indicate what they are). Secondly what are the performance implications if someone used a (very) complex object instead of a primitive for the key? {
"propertyDiscriminator": {
"foo": [
[
{ "prop1": "value", "prop2": 42, "array": [ 1, 2, "string" ] },
{
"$ref": "#/$defs/foo-aaa"
}
],
[
3,
{
"$ref": "#/$defs/foo-bbb"
}
]
]
}
} |
I don't know if the name change was intentional, but this should be considered an alternative to Here's my relevant comments about this from the other issue where it was originally proposed.
The biggest question to me is whether the added complexity is worth the extra capability. I believe that if it gets too verbose and complex, people won't use it. I just don't know where the line is. Maybe it will be fine and maybe not. I think it will be important to get a lot of opinions on this. |
I recently had a user of my schema generation lib ask me to support the I think you're probably right for many (most?) cases, but cases like this still need to be considered. |
Yes, there will always be outliers. In #1410 someone was talking about having over 100 conditions they want to select on. I'm not worried about the performance difference mostly because it's possible to optimize to O(1). It will be necessary to build to lookup, but you only have to do that once and can validate as many instances as you like after than at O(1). Also, these comparisons should be fairly fast, so I don't expect that even 100 comparisons will present a significant performance concern. |
Both |
I think the pattern below is also okay for me. {
"propertyDiscriminator": {
"foo": [
{
"propertyValue": 114,
"subSchema": {
"$ref": "#/$defs/foo-aaa"
}
},
{
"propertyValue": 514,
"subSchema": {
"$ref": "#/$defs/foo-bbb"
}
}
]
}
} But this will using a little more words than using 2-tuple [propertyValue,subSchema].
Maybe we should only support int, string, null and boolean in first version of this standard. And waiting for more use case feedback later. @jdesrosiers There are a lot of practices on the Internet that use int enum as a discriminator. Especially in some systems written in C language, enum is an integer, and what is passed after serialization is also an integer. If int enum discriminator cannot be supported, then the scope of application of
So I think the added complexity is worth for wider applicability. |
That's my concern. IMO using an object rather than an array crosses the line of being too verbose.
You forgot "number", but otherwise I agree that limiting to scalar values would be reasonable. However, I'm not sure it's necessary. I don't see any performance implications to allowing it other than it's more expensive to compare structured values than it is compare scalar ones, but that's inherent in the choice to compare on structured values. I don't see it as a concern for this keyword specifically. |
has there been any significant progress? I really need this, if-then-else is so bad. @xiaoxiangmoe @jdesrosiers |
@ErosZy this is a specification change proposal. Spec work takes time. There are a lot of other spec lifecycle management things that we (the JSON Schema team) need to work out before we can get back to making these kinds of changes. My suggestion is to find out if the implementation you're using supports custom keywords, either via vocabularies or some other method. If so, then I suggest writing the functionality you need in order to meet your immediate needs. (We also welcome your feedback on anything that you come up with.) As far as updating the spec goes, please have patience. We are considering this for the next version. Either this or |
Note: |
Closing the issue as the proposal document has been written. Further requested changes should open new issues. |
The pattern of using oneOf to describe a choice between two types has become ubiquitous.
However, this pattern is also notorious for its many shortcomings. There is a better way to describe this kind of constraint that doesn't have all the problems of the oneOf pattern, but it's verbose, error prone, and not particularly intuitive.
But this is too complicated. We may simplify this.
releated issue : #1082 (comment)
The difference between #1082 and this proposal is —— that issue only support string as discriminator.
This proposal is a little more complicated. But it would accept any json value here.
For example:
cc @jdesrosiers @gregsdennis
The text was updated successfully, but these errors were encountered: