Skip to content

Concepts Setup #766

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

You must be logged in to vote

@MentalGear Hi


For Validating data, does one always have to compile the schema, or is there also a function to do it on the fly?

TypeBox supports both JIT and Dynamic checking.

import { TypeCompiler } from '@sinclair/typebox/compiler'
import { Value } from '@sinclair/typebox/value'
import { Type } from '@sinclair/typebox'

// JIT (fast)
const R1 = TypeCompiler.Compile(Type.String()).Check('hello')

// Dynamic (slow)
const R2 = Value.Check(Type.String(), 'hello')

For example, I have a class that uses the compiled schema for checking the input data during construction, but afterwards in the setters, uses 'Type.Pick' to check only against one property.

Each type generated with TypeBox (…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by MentalGear
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