-
-
Notifications
You must be signed in to change notification settings - Fork 887
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
Limitation: JSONSchemaType<T> does not support unions #1302
Comments
This JSONSchemaType<T> type doesn’t support unions yet, so just use a generic Schema (or SchemaObject) type. You can still compile schemas into type guards with type parameters |
You could also use some type definition for generic JSON Schema, not parameterized on the data type - I’ve seen it in several places, so I don’t think they should be included here. |
Thanks for the quick response. Yeah I added a type param to my type guard which worked post-validation (although it would be nice to validate the actual schema):
Could link to an example? I'm not sure what you mean by this, and I can't find too many alternative examples in the docs. Thanks again. |
Ajv validates the schema itself it at run time, although not on the type level and not that it is correct, just that it is valid.
https://github.com/enriched/ts-json-schema/blob/master/src/JsonSchema.ts I think that what I meant, but I’ve seen in other places - it’s easy to construct a type for a generic JSON schema, not linked to a particular data type |
Thanks for the link. Yep, it's good that it checks it's a valid schema but I meant it would be good if it matched my type definition at compile time. Since it doesn't it will have to be a unit test to ensure it's the correct schema in runtime. Cheers |
@erikbrinkman not sure you’ve seen it. Somebody on Gitter also just asked for it (not sure if you are there). I was planning to look at it some time in April, not sooner - if you could help improve JSONSchemaType before that lots of people would be happy (including myself obviously:) |
It was me! I was hoping to use a schema to describe a property that's For now I'm planning to apply schema validation to a subset of the object, and use some hand-written validation for troublesome bits like this, but perhaps in the future I'll be able to expand the schema-validated surface area to include it. |
@jrr you can also do it without JSONSchemaType, rather than without schema - you can use type parameter explicitly without tying data type to schema. |
I learned a bit about typescript transformation types when working on the This isn't at the top of my priority list, as I generally much prefer JTD, and have used that for everything, but I'll try to take a stab at some point. At some point I mentioned that there is one more complexity, which is the partial schema. e.g. in JSONSchema you can do |
@erikbrinkman I don't think its worthless to only support the verbose method, especially as a first step since its easier to implement. It will cover some % of the cases (including my personal one above) but I've also used the partial schema form too so it's not perfect but you'll have to judge if the time/effort is worth implementing the whole thing. Thanks for looking into this again. |
It also gives a workaround to people who really want type checking to restructure their schema to be verbose if they really want it and can tolerate the duplication of rules. It would be good to keep the full implementation on a roadmap if it's not implemented straight away. Thanks. |
I've looked into this a bit, and there's a problem. The current implementation takes the stance that This property is used to create the Note, you can kind of specify unions, you just need to have a fully defined root object, and then define partial schema off of it. It won't work for everything, but there might be clever ways to make some things wok, e.g. you can't represent
|
Thanks for looking into this. That's an interesting workaround, wrapping the value in an object and have the options all use the same key. If the typing was absolutely necessary it would probably do the trick but be a bit ugly since the wrapper is mostly redundant. |
I would not worry about it, SomeJSONSchema can be implemented separately (I don't think it is even documented at the moment).
The workaround is a bit verbose tbh... For simple types JSON Schema supports
Having array of types on the top level is beneficial for JSON Schema because without it, for example, type coercion would not work correctly. It is a difficult problem, maybe it cannot be solved |
I think something like you mention should be possible if we break the existing behavior. I'm happy to take a stab, but it'd be good to have things a little more fleshed out. Two Two questions I have:
It's also important to note that because this isn't tagged, it's impossible to make sure that every element is specified, for the same reason that you can't convert Thinking about this while I write, I think I have the following structure in mind (loosely as this isn't real typescript):
essentially you can have an anyOf of all of the types or the union of all the types, and the union can also be typed as a Does that make sense and sound reasonable? |
Yes
Yes, that's why I didn't do it :) There are plenty of edge cases and somewhere the line should be drawn.
Interesting :)
Yes. I am very happy with any limitations tbh - I would just add them to the docs. JSONSchemaType is not comprehensive as is anyway, it only highlights how difficult it is to map JSON Schema to types (in general case) |
in v8 |
What version of Ajv are you using? Does the issue happen if you use the latest version?
ajv@7.0.0-beta.1
Ajv options object
N/A (type error)
JSON Schema
See code below
Sample data
N/A (type error)
Your code
Validation result, data AFTER validation, error messages
It complains that the top level schema object doesn't have the keys:
type
,required
,additionalPropertiests
. I thought I could useanyOf
on it's own. It doesn't really make sense to usetype
at the top level because it could be one of three different types but it looks like it expects it to be an object. I also triedoneOf
with the same result.Any suggestions or workarounds welcome, especially if my schema is wrong. Thanks
What results did you expect?
Expected no type error
Are you going to resolve the issue?
No.
The text was updated successfully, but these errors were encountered: