Concepts Setup #766
-
Typebox seems really optimized for performance, but for people coming from ZOD or other js validator the concepts can be a bit uncommon.
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
@MentalGear Hi
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')
Each type generated with TypeBox (including those derived from Pick) and considered distinct types. You will need to compile each separately (as it's not possible change logic to check only sub properties a type once it's compiled). import { TypeCompiler } from '@sinclair/typebox/compiler'
import { Type } from '@sinclair/typebox'
const Vector4 = Type.Object({
x: Type.Number(),
y: Type.Number(),
z: Type.Number(),
w: Type.Number(),
})
const Vector3 = Type.Pick(Vector4, ['x', 'y', 'z'])
const Vector2 = Type.Pick(Vector4, ['x', 'y'])
const Scalar = Type.Index(Vector4, ['x'])
// compile each type separately
const C1 = TypeCompiler.Compile(Vector4)
const C2 = TypeCompiler.Compile(Vector3)
const C3 = TypeCompiler.Compile(Vector2)
const C4 = TypeCompiler.Compile(Scalar) However, if you use
Not currently. However there has been some consideration given to splitting JIT compiled modules into distinct functions that can be used to construct custom types. This isn't a feature of the library today (but may be explored in future)
You will need to refer to the documentation of superforms as different libraries handle error generation differently. Hope this helps |
Beta Was this translation helpful? Give feedback.
@MentalGear Hi
TypeBox supports both JIT and Dynamic checking.
Each type generated with TypeBox (…