Using Typebox validator on non-Typebox JSON schema #1189
-
On some part of my app, I have some JSON schemas coming from configuration code, so it cannot be turned into Typebox constructs. I still want to validate data against those schemas (knowing I will not get type inference). Is my assumption correct? If so, which constraint makes validation only available to schemas built with Typebox? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi,
It is actually possible to convert raw schematics into TypeBox types. Have a look at the following URL + Example https://github.com/sinclairzx81/typebox/blob/master/example/prototypes/from-schema.ts import { FromSchema } from './prototypes/from-schema'
const T = FromSchema({
type: 'object',
required: ['x', 'y', 'z'],
properties: {
x: { type: 'number' },
y: { type: 'number' },
z: { type: 'number' },
}
} as const) // or 'as unknown' if the schematics originate from elsewhere
console.log(T) The FromSchema type is currently treated as an extension / prototype for now.
The primary constraint is that TypeBox requires specific symbols to be applied appropriately to schematics. TypeBox validators operate almost exclusively by introspecting Kind, Hint, Modifier, and Transform descriptor symbols applied to schematics. As a result, remote schematics require a loading phase to apply these symbols correctly (as shown in the FromSchema example above). There's a couple of reasons for this design ....
In short, the reason schematics can't be loaded directly is because they need to be normalized to the subset of schematics supported by TypeBox then have symbols applied accordingly. Hope this brings some insight. From your query, If you need to load remote schematics, consider the FromSchema function. Alternatively, Ajv is still a really good option for validation. I will say though. The symbol requirement for schematics was devised several years ago to work through some fairly difficult alignment issues between TypeScript and Json Schema, but I've not been entirely happy with the design (mostly because it makes loading remote schematics more difficult than it needs to be). I will be reviewing the implementation at some point, and I think there may be some alternative approaches to schema differentiation that could make removing symbols more feasible. Something for down the road tho. Happy to discuss more if you have any questions |
Beta Was this translation helpful? Give feedback.
Hi,
It is actually possible to convert raw schematics into TypeBox types. Have a look at the following URL + Example
https://github.com/sinclairzx81/typebox/blob/master/example/prototypes/from-schema.ts