-
Notifications
You must be signed in to change notification settings - Fork 193
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
Option to generate if-then-else
schema for union types
#1209
Comments
Excellent description. I would push back on the validators. The validators have all the same information that this tool does. It is nearly impossible to know which field / fields to use as the type pivot. It gets even more complicated when you add sub-types. The validator should be able to do a "best-fit" and give the appropriate error. |
Yes @Jason3S , you are right... validators do have the same information. However, validators actually did their part by supporting if-then-else and would say "Instead of using an anyOf json-schema, you should be using if-then-else". It's a more clear representation of what is being modeled, as shown in the example documented here. I think one option to figure this out from TypeScript could be to have an annotation for a property that fulfills the role of
This could be used as a hint to generate the schema using |
While it might be easier for validators, we have to consider other common use cases like generating types/classes as in http://github.com/altair-viz/altair, which is generated from the Vega-Lite schema. Since Vega-Lite is the main use case of this library, we need to be careful to support this use case well. |
Definitely wouldn't propose to alter the generation in ways that could break existing implementations. |
I tried to avoid having too many knobs so far and would only be willing to accept this change with comprehensive unit tests. |
🚀 Issue was released in |
Today (v1.0.0) a union type like the following:
Generates the following json schema:
This fulfills the need to validate the schema. However, when there is an error schema validators like
ajv
produce errors for all schema paths. As an example,{ animal_type: 'fish', found_in: 'lake' }
results in the following error:This first line in this error can be misleading since
animal_type
isfish
, andcan_fly
is not a property of fish. The second line is the real error that we'd like to present.The errors can get more confusing as you add more types to the union type... a lot of errors are shown for types that are irrelevant given the provide
animal_type
.A solution to this problem would be to have the option to generate
if-then-else
json schemas. These provide more information to the validators to pick the appropriate schema. So if we generate the following:The same input results in the following, more concise error:
The text was updated successfully, but these errors were encountered: