Skip to content

Using Typebox validator on non-Typebox JSON schema #1189

Answered by sinclairzx81
yacinehmito asked this question in Q&A
Discussion options

You must be logged in to vote

Hi,


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).

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 sche…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@yacinehmito
Comment options

Answer selected by sinclairzx81
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants