-
-
Notifications
You must be signed in to change notification settings - Fork 886
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
Implement a JSONDataType to make typescript conversion on json schema #1792
Comments
I am not very optimistic about the possibility of it, but please do try - it would be interesting. JSON Schema, is general, is not aligned with TS type system - it allows expressing constraints that are not expressible in typescript type system. Also, in some cases, I think there might be more than one possible type derived from JSON Schema (can be wrong here). Neither is a complete blocker - for the first problem, you would have to define a subset of JSON Schema that is mappable to TS types, and have this type fail on schemas outside of this subset - that actually could be useful. For the second, we will just have to make some choices... The type name should be JSONSchemaDataType |
Well, that might be problematic, as you say choice has to be made in that case But the other way is not, i know that jsonSchema allow complex constraints that typescript, and that's a huge part of why i'm using it, but in a developer perspective having the static type hinting and compilation error, even on a superset type is far better than having nothing at all |
I generally agree with @epoberezkin on all things here. I'm also not sure if there could be multiple possible types, but this exists for JTDDataType too with timestamps being interpreted as My guess is you should be able to start with JTDDataType as a backbone but swap in JSON Schema structures. It seems like it could work at least for simple schemas. There are a few risks I want to call out.
I hope this proves fruitful! |
What version of Ajv you are you using?
8.6.4
What problem do you want to solve?
I want to dynamically infer ts types from a json schema, it will reduce the need for auto-generated file or code-duplication (define your interface 2 times, one for ts, one for ajv)
What do you think is the correct solution to problem?
Implement a type mapper like in JTDDataType from 'ajv/dist/jtd', and update readme/doc accordingly
Will you be able to implement it?
Yes
The text was updated successfully, but these errors were encountered: