$ npm install typebox
import Type from 'typebox'
const T = Type.Object({ // const T = {
x: Type.Number(), // type: 'object',
y: Type.Number(), // required: ['x', 'y', 'z'],
z: Type.Number() // properties: {
}) // x: { type: 'number' },
// y: { type: 'number' },
// z: { type: 'number' }
// }
// }
type T = Type.Static<typeof T> // type T = {
// x: number,
// y: number,
// z: number
// }
TypeBox is a runtime type system that creates in-memory Json Schema objects that infer as TypeScript types. The schematics produced by this library are designed to match the static type checking rules of the TypeScript compiler. TypeBox offers a unified type system that can be statically checked by TypeScript and validated at runtime using standard Json Schema.
This library is designed to allow Json Schema to compose similar to how types compose within TypeScript's type system. It can be used as a simple tool to build up complex schematics or integrated into REST and RPC services to help validate data received over the wire.
License: MIT
If upgrading from @sinclair/typebox
refer to the 1.0 migration guide at the following URL.
TypeBox includes many functions to create Json Schema types. Each function returns a small Json Schema fragment that corresponds to a TypeScript type. TypeBox uses function composition to combine schema fragments into more complex types. It provides a set of functions that are used to model Json Schema schematics as well as a set of functions that model constructs native to JavaScript and TypeScript.
The following creates a Json Schema type and infers with Static.
import Type from 'typebox'
const T = Type.Object({ // const T = {
x: Type.Number(), // type: 'object',
y: Type.Number(), // required: ['x', 'y', 'z'],
z: Type.Number() // properties: {
}) // x: { type: 'number' },
// y: { type: 'number' },
// z: { type: 'number' }
// }
// }
type T = Type.Static<typeof T> // type T = {
// x: number,
// y: number,
// z: number
// }
TypeBox can translate TypeScript syntax into Json Schema. The Script function is a fully type-safe, syntactic frontend to the TypeBox type builder API, allowing Json Schema to be constructed and mapped using TypeScript type expressions encoded as strings.
The following uses Script to construct and map Json Schema.
import Type from 'typebox'
const T = Type.Script(`{
x: number,
y: number,
z: number
}`) // const T = {
// type: 'object',
// required: ['x', 'y', 'z'],
// properties: {
// x: { type: 'number' },
// y: { type: 'number' },
// z: { type: 'number' }
// }
// }
const S = Type.Script({ T }, `{
[K in keyof T]: T[K] | null
}`) // const S = {
// type: 'object',
// required: ['x', 'y', 'z'],
// properties: {
// x: {
// anyOf: [
// { type: 'number' },
// { type: 'null' }
// ]
// },
// y: {
// anyOf: [
// { type: 'number' },
// { type: 'null' }
// ]
// },
// z: {
// anyOf: [
// { type: 'number' },
// { type: 'null' }
// ]
// },
// }
// }
type S = Type.Static<typeof S> // type S = {
// x: number | null,
// y: number | null,
// z: number | null
// }
The TypeBox Value module provides functions to Check and Parse JavaScript values. It also includes functions such as Clone, Repair, Encode, Decode, Diff and Patch which perform various structural operations on JavaScript values.
The Value module is available via optional import.
import Value from 'typebox/value'
The following uses the Value module to Check and Parse a value.
const T = Type.Object({
x: Type.Number(),
y: Type.Number(),
z: Type.Number()
})
// Check
const A = Value.Check(T, { // const A: boolean = true
x: 1,
y: 2,
z: 3
})
// Parse
const B = Value.Parse(T, { // const B: {
x: 1, // x: number,
y: 2, // y: number,
z: 3 // z: number
}) // } = ...
The TypeBox Compile module provides functions to convert types into high-performance validators. The compiler is tuned for fast compilation as well as fast validation.
The Compile module is available via optional import.
import { Compile } from 'typebox/compile'
The following uses the Compile module to Check and Parse a value.
const C = Compile(Type.Object({ // const C: Validator<{}, TObject<{
x: Type.Number(), // x: TNumber,
y: Type.Number(), // y: TNumber,
z: Type.Number() // z: TNumber
})) // }>>
// Check
const A = C.Check({ // const A: boolean = true
x: 1,
y: 2,
z: 3
})
// Parse
const B = C.Parse({ // const B: {
x: 1, // x: number,
y: 2, // y: number,
z: 3 // z: number
}) // } = ...
TypeBox is open to community contribution. Please ensure you submit an issue before submitting a pull request. The TypeBox project prefers open community discussion before accepting new features.