maasglobal / io-ts-from-json-schema Goto Github PK
View Code? Open in Web Editor NEWIotsfjs is a static code generation utility used for converting json schema files into io-ts runtime validators.
License: MIT License
Iotsfjs is a static code generation utility used for converting json schema files into io-ts runtime validators.
License: MIT License
Because of historical reasons io-ts-from-json-schema converter handles oneOf
as if it was anyOf
. We should probably remove the incorrect oneOf
implementation. We can later decide wether or not we wish to do a proper reimplementation.
This :
"MyModel": {
"description": "Ullamco commodo enim nisi in occaecat esse eiusmod ex culpa ea commodo aliquip velit nostrud. Nostrud esse ad tempor adipisicing cillum ipsum exercitation ut. \nPariatur pariatur est consectetur irure laborum non anim amet pariatur dolore mollit aliqua cillum culpa. \nConsequat officia amet consequat commodo labore ex occaecat laboris anim laboris amet.",
"example": {…},
"properties": {…},
"title": "MyModel",
"type": "object"
},
generates that:
// MyModel
// Ullamco commodo enim nisi in occaecat esse eiusmod ex culpa ea commodo aliquip velit nostrud. Nostrud esse ad tempor adipisicing cillum ipsum exercitation ut.
Pariatur pariatur est consectetur irure laborum non anim amet pariatur dolore mollit aliqua cillum culpa.
Consequat officia amet consequat commodo labore ex occaecat laboris anim laboris amet.
export type MyModel = t.Branded…
Hi @cyberixae and thank you for the great work you are doing here!
I am trying to use your library to generate endpoints for my open source project ts-endpoint but I cannot wrap my head around the best way to do it... I am trying to generate Endpoint
definitions (here are a couple of examples), do you have any suggestions on how to approach the problem?
And thanks again for the amazing work!
The way tuples are defined have been going through some changes https://json-schema.org/understanding-json-schema/reference/array.html#tuple-validation
Previously the tuple [string, number]
would be defined as
{
"type": "array",
"items": [
{ "type": "string"},
{ "type": "number"}
]
}
With some of the newer drafts the same tuple [string, number]
would be defined as
{
"type": "array",
"prefixItems": [
{ "type": "string"},
{ "type": "number"}
]
}
The io-ts-from-json-schema converter currently only supports the old tuple schema definition.
There are also some other changes related to how other keywords are interpreted in relation to the tuple definition.
Consider the following example that defined two error types as null
. At the moment iotsfjs translates these into never
instead of null
. It would be nice if the two nulls were distinguishable at the type level but at least they should not be never
.
"definitions": {
"errorFoo": {
"type": "null"
},
"errorBar": {
"type": "null"
},
}
According to the specification the following schema is supposed match { foo: 'foo' }
and any item that is not an object to begin with.
{
"properties": {
"foo": { "const": "foo" }
}
}
In a similar manner the following schema is supposed to match the tuple [ 'bar', 123 ]
and any item that is not an array at all.
{
"items": [
{ "const": "bar" },
{ "const": 123 }
]
}
Iotsfjs currently fails to validate these requirements because I haven't been able to find a good way to describe them in the TypeScript's static type system. Essentially they could be described with somethings like { foo: 'foo' } | Not<Record<string, any>>
and ['bar', 123] | Not<Array<any>>
but TypeScript doesn't provide Not
out of box. It might be possible to represent them with conditional types but this gets quite complicated very easily.
Ideally the converter code would be divided into several modules that match the modules defined in the JSON schema specification.
The generated types have doctests that can be converted to actual tests with doctest-ts. During this conversion doctest-ts creates describes around test cases, and the names of the describes do not currently match tests that get generated. When a test fails it is therefore sometimes difficult to see which test failed.
In the following example bar ends up not being a number.
{
"definitions": {
"foo": { "type": "number" },
"bar": { "$ref": "#/definitions/foo" },
}
}
I'm starting to think that checking of properties
keyword should not be dependent on "type": "object"
. My current understanding is that each keyword should be processed separately. i.e. processing of properties
should not depend on processing of type
. If someone wishes to define a null
with properties
, the converter should probably ignore the absurdity of that. There are various other cases where we already ignore context when processing keywords to support all kinds of unexpected scenarios.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.