Validators provide optimized implementations of the functions available in the Value submodule.
The following compiles a Validator and calls Check.
import { Compile, Validator } from 'typebox/compile'
const T = Compile(Type.Object({ // const T: Validator<{}, TObject<{
x: Type.Number(), // x: TNumber,
y: Type.Number(), // y: TNumber,
z: Type.Number(), // z: TNumber
})) // }>>
const R = T.Check({ x: 1, y: 2, z: 3 }) // const R: boolean = true
The following functions are available on Validator instances.
Checks a value matches a type.
const R = C.Check(value) // const R: boolean
Returns a errors for the value or empty if no error.
const R = C.Errors(value) // const R: ValidationError[]
Cleans a value
const R = C.Clean(value) // const R: unknown
Returns generated code for this Validator.
const R = C.Code() // const R: string
Converts a value
const R = C.Convert(value) // const R: unknown
Creates a value of the Validator type
const R = C.Create(value) // const R: ...
Decodes a value, running any Decode callbacks if available.
const R = C.Decode(value) // const R: ...
Creates defaults for the given value
const R = C.Default(value) // const R: unknown
Encodes a value, running any Encode callbacks if available.
const R = C.Encode(value) // const R: ...
Returns true if the Validator is using evaluated optimizations.
const R = C.IsAccelerated() // const R: boolean
Parses a value to the given type.
const R = C.Parse(value) // const R: ...