Parsing JSON schema into TypeBox #421
-
Hello there! First of all, I want to applaud and thank you for your fantastic work for this library, it is a great concept and I love its overall readability. I am currently using this for a project where I need to generalise data structures stored into persistence while still needing validation and type inference. TypeBox really assisted me in writing JSON schemas directly into my codebase to re-use into these data structures. However, I am encountering a small pain point, where I want to be able to use these schemas stored in persistence. Exporting is no issue, as I can utilise I am not sure if this feature exists, or maybe I missed it during my research on the documentation and discussions, but does TypeBox offer such a feature? And if not, would you consider adding it? Many thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
@Tydax Hi, thanks for the kind words on TypeBox! :) Um, at this stage, there are no immediate plans to support loading / parsing schemas from disk, but there are some considerations going into removing the The classic example of where differentiating schematics becomes a problem is between const A = Type.Strict(Type.Any()) // const A = { } ────┐
// ├─ Should parse as Any or Unknown?
const B = Type.Strict(Type.Unknown()) // const B = { } ────┘ Attempts have been made to keep TB using a strict subset of the JSON Schema specification that could make deserialization (and parsing to the original type) somewhat possible (through some form of schema pattern matching), but cases like the above (as well as String In saying this, I would like to see the following achieved in some capacity, but possibly as an auxiliary library. import { TSchema, TypeGuard } from '@sinclair/typebox'
const T: TSchema = load('./schemas/A.json') // introspect the `A.json` schema and load as `TSchema`
const R = TypeGuard.TSchema(T) // const R = true (if loading was successful) In this scenario, the For now though, loading from disk (or other form of persistence) is somewhat deferred indefinitely until some of these cases are solved for. I'm still not sure of the best approach to take here (this question has been asked a few times over the years), but community experimentation around this aspect would help greatly define how this functionality should work and help find where the edge cases are. Hope this brings a bit of insight and background into this aspect of TB! :) |
Beta Was this translation helpful? Give feedback.
@Tydax Hi, thanks for the kind words on TypeBox! :)
Um, at this stage, there are no immediate plans to support loading / parsing schemas from disk, but there are some considerations going into removing the
[Kind]
and[Modifier]
symbols (which I'm not sure is possible, but if achieved, might make inroads into parsing structures from disk a bit more feasible). The problem currently is that once the schematic is serialized viaStrict
(or simplyJSON.stringify()
), associative static type information is lost and it's difficult to parse them back to their original type (as these Symbols encode all information related to the static type, which also has implications as to how the types compose at…