- 0.34.0Latest
- 0.33.22
- 0.33.22
- 0.33.21
- 0.33.20
- 0.33.20
- 0.33.19
- 0.33.18
- 0.33.18
- 0.33.17
- 0.33.16
- 0.33.15
- 0.33.14
- 0.33.12
- 0.33.11
- 0.33.10
- 0.33.9
- 0.33.8
- 0.33.7
- 0.33.7
- 0.33.6
- 0.33.6
- 0.33.5
- 0.33.4
- 0.33.3
- 0.33.2
- 0.33.2
- 0.33.1
- 0.33.0
- 0.32.35
- 0.32.34
- 0.32.33
- 0.32.32
- 0.32.30
- 0.32.29
- 0.32.28
- 0.32.27
- 0.32.27
- 0.32.25
- 0.32.24
- 0.32.23
- 0.32.22
- 0.32.21
- 0.32.20
- 0.32.19
- 0.32.18
- 0.32.18
- 0.32.17
- 0.32.16
- 0.32.15
- 0.32.14
- 0.32.13
- 0.32.12
- 0.32.11
- 0.32.10
- 0.32.9
- 0.32.8
- 0.32.7
- 0.32.6
- 0.32.5
- 0.32.4
- 0.32.3
- 0.32.2
- 0.32.1
- 0.32.0
- 0.31.28
- 0.31.27
- 0.31.26
- 0.31.25
- 0.31.24
- 0.31.23
- 0.31.23
- 0.31.22
- 0.31.21
- 0.31.20
- 0.31.19
- 0.31.18
- 0.31.17
- 0.31.16
- 0.31.15
- 0.31.14
- 0.31.13
- 0.31.11
- 0.31.10
- 0.31.9
- 0.31.8
- 0.31.8
- 0.31.7
- 0.31.6
- 0.31.5
- 0.31.4
- 0.31.3
- 0.31.2
- 0.31.1
- 0.31.1
- 0.31.0
- 0.30.4
- 0.30.3
- 0.30.2
- 0.30.1
- 0.30.0
- 0.29.6
- 0.29.5
- 0.29.4
- 0.29.3
- 0.29.2
- 0.29.2
- 0.29.1
- 0.29.1
- 0.29.0
- 0.28.20
- 0.28.19
- 0.28.18
- 0.28.17
- 0.28.16
- 0.28.16
- 0.28.15
- 0.28.14
- 0.28.14
- 0.28.13
- 0.28.12
- 0.28.11
- 0.28.10
- 0.28.9
- 0.28.8
- 0.28.7
- 0.28.6
- 0.28.5
- 0.28.4
- 0.28.3
- 0.28.2
- 0.28.1
- 0.28.1
- 0.28.0
- 0.27.8
- 0.27.7
- 0.27.6
- 0.27.5
- 0.27.4
- 0.27.3
- 0.27.2
- 0.27.1
- 0.27.0
- 0.26.8
- 0.26.7
- 0.26.6
- 0.26.5
- 0.26.4
- 0.26.3
- 0.26.2
- 0.26.1
- 0.26.0
- 0.26.0-dev.5
- 0.26.0-dev.4
- 0.26.0-dev.3
- 0.26.0-dev.2
- 0.26.0-dev.1
- 0.26.0-dev
- 0.25.24
- 0.25.23
- 0.25.22
- 0.25.21
- 0.25.20
- 0.25.19
- 0.25.18
- 0.25.17
- 0.25.16
- 0.25.15
- 0.25.14
- 0.25.13
- 0.25.12
- 0.25.11
- 0.25.10
- 0.25.9
- 0.25.8
- 0.25.7
- 0.25.6
- 0.25.5
- 0.25.4
- 0.25.3
- 0.25.2
- 0.25.1
- 0.25.0
- 0.24.51
- 0.24.50
- 0.24.49
- 0.24.48
- 0.24.47
- 0.24.46
- 0.24.45
- 0.24.44
- 0.24.43
- 0.24.42
- 0.24.41
- 0.24.40
- 0.24.39
- 0.24.38
- 0.24.37
- 0.24.36
- 0.24.35
- 0.24.34
- 0.24.33
- 0.24.32
- 0.24.31
- 0.24.30
- 0.24.29
- 0.24.28
- 0.24.27
- 0.24.26
- 0.24.25
- 0.24.24
- 0.24.23
- 0.24.22
- 0.24.21
- 0.24.20
- 0.24.19
- 0.24.18
- 0.24.17
- 0.24.16
- 0.24.15
- 0.24.14
- 0.24.13
- 0.24.12
- 0.24.11
- 0.24.10
- 0.24.9
- 0.24.8
- 0.24.7
- 0.24.6
- 0.24.5
- 0.24.4
- 0.24.3
- 0.24.2
- 0.24.1
- 0.24.0
- 0.23.4
- 0.23.3
- 0.23.1
- 0.23.0
- 0.22.1
- 0.22.0
- 0.21.2
- 0.21.2
- 0.21.1
- 0.21.0
- 0.20.6
- 0.20.5
- 0.20.4
- 0.20.3
- 0.20.2
- 0.20.1
- 0.20.0
- 0.19.2
- 0.19.1
- 0.19.0
- 0.18.1
- 0.18.0
- 0.17.8
- 0.17.7
Install
Node
$ npm install @sinclair/typebox --save
Deno
import { Static, Type } from 'https://deno.land/x/typebox/src/typebox.ts'
Example
import { Static, Type } from '@sinclair/typebox'
const T = Type.String() // const T = { type: 'string' }
type T = Static<typeof T> // type T = string
Overview
TypeBox is a type builder library that creates in-memory JSON Schema objects that can be statically inferred as TypeScript types. The schemas produced by this library are designed to match the static type checking rules of the TypeScript compiler. TypeBox enables one to create unified types that can be statically checked by TypeScript and runtime asserted using standard JSON Schema validation.
TypeBox can be used as a simple tool to build up complex schemas or integrated into RPC or REST services to help validate JSON data received over the wire.
License MIT
Contents
- Install
- Overview
- Usage
- Types
- Modifiers
- Options
- Extended Types
- Reference Types
- Recursive Types
- Generic Types
- Unsafe Types
- Conditional Types
- Values
- Guards
- Strict
- Validation
- Compiler
- Benchmark
- Contribute
Usage
The following demonstrates TypeBoxβs general usage.
import { Static, Type } from '@sinclair/typebox'
//--------------------------------------------------------------------------------------------
//
// Let's say you have the following type ...
//
//--------------------------------------------------------------------------------------------
type T = {
id: string,
name: string,
timestamp: number
}
//--------------------------------------------------------------------------------------------
//
// ... you can express this type in the following way.
//
//--------------------------------------------------------------------------------------------
const T = Type.Object({ // const T = {
id: Type.String(), // type: 'object',
name: Type.String(), // properties: {
timestamp: Type.Integer() // id: {
}) // type: 'string'
// },
// name: {
// type: 'string'
// },
// timestamp: {
// type: 'integer'
// }
// },
// required: [
// 'id',
// 'name',
// 'timestamp'
// ]
// }
//--------------------------------------------------------------------------------------------
//
// ... then infer back to the original static type this way.
//
//--------------------------------------------------------------------------------------------
type T = Static<typeof T> // type T = {
// id: string,
// name: string,
// timestamp: number
// }
//--------------------------------------------------------------------------------------------
//
// ... then use the type both as JSON schema and as a TypeScript type.
//
//--------------------------------------------------------------------------------------------
function receive(value: T) { // ... as a Type
if(JSON.validate(T, value)) { // ... as a Schema
// ok...
}
}
Types
The following table outlines the TypeBox mappings between TypeScript and JSON schema.
ββββββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββ
β TypeBox β TypeScript β JSON Schema β
β β β β
ββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β const T = Type.Any() β type T = any β const T = { } β
β β β β
ββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β const T = Type.Unknown() β type T = unknown β const T = { } β
β β β β
ββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β const T = Type.String() β type T = string β const T = { β
β β β type: 'string' β
β β β } β
β β β β
ββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β const T = Type.Number() β type T = number β const T = { β
β β β type: 'number' β
β β β } β
β β β β
ββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β const T = Type.Integer() β type T = number β const T = { β
β β β type: 'integer' β
β β β } β
β β β β
ββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β const T = Type.Boolean() β type T = boolean β const T = { β
β β β type: 'boolean' β
β β β } β
β β β β
ββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β const T = Type.Null() β type T = null β const T = { β
β β β type: 'null' β
β β β } β
β β β β
ββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β const T = Type.RegEx(/foo/) β type T = string β const T = { β
β β β type: 'string', β
β β β pattern: 'foo' β
β β β } β
β β β β
ββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β const T = Type.Literal(42) β type T = 42 β const T = { β
β β β const: 42, β
β β β type: 'number' β
β β β } β
β β β β
ββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β const T = Type.Array( β type T = number[] β const T = { β
β Type.Number() β β type: 'array', β
β ) β β items: { β
β β β type: 'number' β
β β β } β
β β β } β
β β β β
ββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β const T = Type.Object({ β type T = { β const T = { β
β x: Type.Number(), β x: number, β type: 'object', β
β y: Type.Number() β y: number β properties: { β
β }) β } β x: { β
β β β type: 'number' β
β β β }, β
β β β y: { β
β β β type: 'number' β
β β β } β
β β β }, β
β β β required: ['x', 'y'] β
β β β } β
β β β β
ββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β const T = Type.Tuple([ β type T = [number, number] β const T = { β
β Type.Number(), β β type: 'array', β
β Type.Number() β β items: [{ β
β ]) β β type: 'number' β
β β β }, { β
β β β type: 'number' β
β β β }], β
β β β additionalItems: false, β
β β β minItems: 2, β
β β β maxItems: 2 β
β β β } β
β β β β
β β β β
ββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β enum Foo { β enum Foo { β const T = { β
β A, β A, β anyOf: [{ β
β B β B β type: 'number', β
β } β } β const: 0 β
β β β }, { β
β const T = Type.Enum(Foo) β type T = Foo β type: 'number', β
β β β const: 1 β
β β β }] β
β β β } β
β β β β
ββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β const T = Type.KeyOf( β type T = keyof { β const T = { β
β Type.Object({ β x: number, β anyOf: [{ β
β x: Type.Number(), β y: number β type: 'string', β
β y: Type.Number() β } β const: 'x' β
β }) β β }, { β
β ) β β type: 'string', β
β β β const: 'y' β
β β β }] β
β β β } β
β β β β
ββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β const T = Type.Union([ β type T = string | number β const T = { β
β Type.String(), β β anyOf: [{ β
β Type.Number() β β type: 'string' β
β ]) β β }, { β
β β β type: 'number' β
β β β }] β
β β β } β
β β β β
ββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β const T = Type.Intersect([ β type T = { β const T = { β
β Type.Object({ β x: number β type: 'object', β
β x: Type.Number() β } & { β properties: { β
β }), β y: number β x: { β
β Type.Object({ β } β type: 'number' β
β y: Type.Number() β β }, β
β }) β β y: { β
β ]) β β type: 'number' β
β β β } β
β β β }, β
β β β required: ['x', 'y'] β
β β β } β
β β β β
ββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β const T = Type.Record( β type T = Record< β const T = { β
β Type.String(), β string, β type: 'object', β
β Type.Number() β number, β patternProperties: { β
β ) β > β '^.*$': { β
β β β type: 'number' β
β β β } β
β β β } β
β β β } β
β β β β
ββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β const T = Type.Partial( β type T = Partial<{ β const T = { β
β Type.Object({ β x: number, β type: 'object', β
β x: Type.Number(), β y: number β properties: { β
β y: Type.Number() | }> β x: { β
β }) β β type: 'number' β
β ) β β }, β
β β β y: { β
β β β type: 'number' β
β β β } β
β β β } β
β β β } β
β β β β
ββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β const T = Type.Required( β type T = Required<{ β const T = { β
β Type.Object({ β x?: number, β type: 'object', β
β x: Type.Optional( β y?: number β properties: { β
β Type.Number() | }> β x: { β
β ), β β type: 'number' β
β y: Type.Optional( β β }, β
β Type.Number() β β y: { β
β ) β β type: 'number' β
β }) β β } β
β ) β β }, β
β β β required: ['x', 'y'] β
β β β } β
β β β β
ββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β const T = Type.Pick( β type T = Pick<{ β const T = { β
β Type.Object({ β x: number, β type: 'object', β
β x: Type.Number(), β y: number β properties: { β
β y: Type.Number() | }, 'x'> β x: { β
β }), ['x'] β β type: 'number' β
β ) β β } β
β β β }, β
β β β required: ['x'] β
β β β } β
β β β β
ββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β const T = Type.Omit( β type T = Omit<{ β const T = { β
β Type.Object({ β x: number, β type: 'object', β
β x: Type.Number(), β y: number β properties: { β
β y: Type.Number() | }, 'x'> β y: { β
β }), ['x'] β β type: 'number' β
β ) β β } β
β β β }, β
β β β required: ['y'] β
β β β } β
β β β β
ββββββββββββββββββββββββββββββββββ΄ββββββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββββ
Modifiers
TypeBox provides modifiers that can be applied to an objects properties. This allows for optional
and readonly
to be applied to that property. The following table illustates how they map between TypeScript and JSON Schema.
ββββββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββ
β TypeBox β TypeScript β JSON Schema β
β β β β
ββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β const T = Type.Object({ β type T = { β const T = { β
β name: Type.Optional( β name?: string β type: 'object', β
β Type.String() β } β properties: { β
β ) β β name: { β
β }) β β type: 'string' β
β β β } β
β β β } β
β β β } β
β β β β
ββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β const T = Type.Object({ β type T = { β const T = { β
β name: Type.Readonly( β readonly name: string β type: 'object', β
β Type.String() β } β properties: { β
β ) β β name: { β
β }) β β type: 'string' β
β β β } β
β β β }, β
β β β required: ['name'] β
β β β } β
β β β β
ββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β const T = Type.Object({ β type T = { β const T = { β
β name: Type.ReadonlyOptional( β readonly name?: string β type: 'object', β
β Type.String() β } β properties: { β
β ) β β name: { β
β }) β β type: 'string' β
β β β } β
β β β } β
β β β } β
β β β β
ββββββββββββββββββββββββββββββββββ΄ββββββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββββ
Options
You can pass additional JSON schema options on the last argument of any given type. The following are some examples.
// string must be an email
const T = Type.String({ format: 'email' })
// number must be a multiple of 2
const T = Type.Number({ multipleOf: 2 })
// array must have at least 5 integer values
const T = Type.Array(Type.Integer(), { minItems: 5 })
Extended Types
In addition to JSON schema types, TypeBox provides several extended types that allow for function
and constructor
types to be composed. These additional types are not valid JSON Schema and will not validate using typical JSON Schema validation. However, these types can be used to frame JSON schema and describe callable interfaces that may receive JSON validated data. These types are as follows.
ββββββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββ
β TypeBox β TypeScript β Extended Schema β
β β β β
ββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β const T = Type.Constructor([ β type T = new ( β const T = { β
β Type.String(), β arg0: string, β type: 'constructor' β
β Type.Number() β arg1: number β parameters: [{ β
β ], Type.Boolean()) β ) => boolean β type: 'string' β
β β β }, { β
β β β type: 'number' β
β β β }], β
β β β return: { β
β β β type: 'boolean' β
β β β } β
β β β } β
β β β β
ββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β const T = Type.Function([ β type T = ( β const T = { β
| Type.String(), β arg0: string, β type : 'function', β
β Type.Number() β arg1: number β parameters: [{ β
β ], Type.Boolean()) β ) => boolean β type: 'string' β
β β β }, { β
β β β type: 'number' β
β β β }], β
β β β return: { β
β β β type: 'boolean' β
β β β } β
β β β } β
β β β β
ββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β const T = Type.Uint8Array() β type T = Uint8Array β const T = { β
β β β type: 'object', β
β β β specialized: 'Uint8Array' β
β β β } β
β β β β
ββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β const T = Type.Promise( β type T = Promise<string> β const T = { β
β Type.String() β β type: 'promise', β
β ) β β item: { β
β β β type: 'string' β
β β β } β
β β β } β
β β β β
ββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β const T = Type.Undefined() β type T = undefined β const T = { β
β β β type: 'object', β
β β β specialized: 'Undefined' β
β β β } β
β β β β
ββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β const T = Type.Void() β type T = void β const T = { β
β β β type: 'null' β
β β β } β
β β β β
ββββββββββββββββββββββββββββββββββ΄ββββββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββββ
Reference Types
Use Type.Ref(...)
to create referenced types. The target type must specify an $id
.
const T = Type.String({ $id: 'T' }) // const T = {
// $id: 'T',
// type: 'string'
// }
const R = Type.Ref(T) // const R = {
// $ref: 'T'
// }
Recursive Types
Use Type.Recursive(...)
to create recursive types.
const Node = Type.Recursive(Node => Type.Object({ // const Node = {
id: Type.String(), // $id: 'Node',
nodes: Type.Array(Node) // type: 'object',
}), { $id: 'Node' }) // properties: {
// id: {
// type: 'string'
// },
// nodes: {
// type: 'array',
// items: {
// $ref: 'Node'
// }
// }
// },
// required: [
// 'id',
// 'nodes'
// ]
// }
type Node = Static<typeof Node> // type Node = {
// id: string
// nodes: Node[]
// }
function test(node: Node) {
const id = node.nodes[0].nodes[0] // id is string
.nodes[0].nodes[0]
.id
}
Generic Types
Use functions to create generic types. The following creates a generic Nullable<T>
type.
import { Type, Static, TSchema } from '@sinclair/typebox'
const Nullable = <T extends TSchema>(type: T) => Type.Union([type, Type.Null()])
const T = Nullable(Type.String()) // const T = {
// anyOf: [{
// type: 'string'
// }, {
// type: 'null'
// }]
// }
type T = Static<typeof T> // type T = string | null
const U = Nullable(Type.Number()) // const U = {
// anyOf: [{
// type: 'number'
// }, {
// type: 'null'
// }]
// }
type U = Static<typeof U> // type U = number | null
Unsafe Types
Use Type.Unsafe(...)
to create custom schemas with user defined inference rules.
const T = Type.Unsafe<string>({ type: 'number' }) // const T = {
// type: 'number'
// }
type T = Static<typeof T> // type T = string
The Type.Unsafe(...)
function can be used to create schemas for validators that require specific schema representations. An example of this would be OpenAPIβs nullable
and enum
schemas which are not provided by TypeBox. The following demonstrates using Type.Unsafe(...)
to create these types.
import { Type, Static, TSchema } from '@sinclair/typebox'
//--------------------------------------------------------------------------------------------
//
// Nullable<T>
//
//--------------------------------------------------------------------------------------------
function Nullable<T extends TSchema>(schema: T) {
return Type.Unsafe<Static<T> | null>({ ...schema, nullable: true })
}
const T = Nullable(Type.String()) // const T = {
// type: 'string',
// nullable: true
// }
type T = Static<typeof T> // type T = string | null
//--------------------------------------------------------------------------------------------
//
// StringEnum<string[]>
//
//--------------------------------------------------------------------------------------------
function StringEnum<T extends string[]>(values: [...T]) {
return Type.Unsafe<T[number]>({ type: 'string', enum: values })
}
const T = StringEnum(['A', 'B', 'C']) // const T = {
// enum: ['A', 'B', 'C']
// }
type T = Static<typeof T> // type T = 'A' | 'B' | 'C'
Conditional Types
Use the Conditional
module to create conditionally mapped types.
import { Conditional } from '@sinclair/typebox/conditional'
The following table shows the TypeBox mappings between TypeScript and JSON schema.
ββββββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββ
β TypeBox β TypeScript β JSON Schema β
β β β β
ββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β const T = Conditional.Extends( β type T = β const T = { β
β Type.String(), β string extends number β const: false, β
β Type.Number(), β true : false β type: 'boolean' β
β Type.Literal(true), β β } β
β Type.Literal(false) β β β
β ) β β β
β β β β
ββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β const T = Conditional.Extract( β type T = Extract< β const T = { β
β Type.Union([ β 'a' | 'b' | 'c', β anyOf: [{ β
β Type.Literal('a'), β 'a' | 'f' β const: 'a' β
β Type.Literal('b'), β > β type: 'string' β
β Type.Literal('c') β β }] β
β ]), β β } β
β Type.Union([ β β β
β Type.Literal('a'), β β β
β Type.Literal('f') β β β
β ]) β β β
β ) β β β
β β β β
ββββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β const T = Conditional.Exclude( β type T = Exclude< β const T = { β
β Type.Union([ β 'a' | 'b' | 'c', β anyOf: [{ β
β Type.Literal('a'), β 'a' β const: 'b', β
β Type.Literal('b'), β > β type: 'string' β
β Type.Literal('c') β β }, { β
β ]), β β const: 'c', β
β Type.Union([ β β type: 'string' β
β Type.Literal('a') β β }] β
β ]) β β } β
β ) β β β
β β β β
ββββββββββββββββββββββββββββββββββ΄ββββββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββββ
Values
Use the Value
module to perform type operations on values.
import { Value } from '@sinclair/typebox/value'
const T = Type.Object({ x: Type.Number(), y: Type.Number() })
//--------------------------------------------------------------------------------------------
//
// Use Value.Create(T) to create a value from T.
//
//--------------------------------------------------------------------------------------------
const V = Value.Create(T) // const V = { x: 0, y: 0 }
//--------------------------------------------------------------------------------------------
//
// Use Value.Check(T, ...) to check if a value is of type T.
//
//--------------------------------------------------------------------------------------------
const R = Value.Check(T, { x: 1 }) // const R = false
//--------------------------------------------------------------------------------------------
//
// Use Value.Cast(T, ...) to immutably cast a value into T.
//
//--------------------------------------------------------------------------------------------
const A = Value.Cast(T, null) // const A = { x: 0, y: 0 }
const B = Value.Cast(T, { x: 1 }) // const B = { x: 1, y: 0 }
const C = Value.Cast(T, { x: 1, y: 2, z: 3 }) // const C = { x: 1, y: 2 }
Guards
Use the TypeGuard
module to test if values are valid TypeBox types.
import { TypeGuard } from '@sinclair/typebox/guard'
const T = Type.String()
if(TypeGuard.TString(T)) {
// T is TString
}
Strict
TypeBox schemas contain the Kind
and Modifier
symbol properties. These properties are provided to enable runtime type reflection on schemas, as well as helping TypeBox internally compose types. These properties are not strictly valid JSON schema; so in some cases it may be desirable to omit them. TypeBox provides a Type.Strict()
function that will omit these properties if necessary.
const T = Type.Object({ // const T = {
name: Type.Optional(Type.String()) // [Kind]: 'Object',
}) // type: 'object',
// properties: {
// name: {
// [Kind]: 'String',
// type: 'string',
// [Modifier]: 'Optional'
// }
// }
// }
const U = Type.Strict(T) // const U = {
// type: 'object',
// properties: {
// name: {
// type: 'string'
// }
// }
// }
Validation
TypeBox schemas target JSON Schema draft 6 so any validator capable of draft 6 should be fine. A good library to use for validation in JavaScript environments is Ajv. The following example shows setting up Ajv to work with TypeBox.
$ npm install ajv ajv-formats --save
//--------------------------------------------------------------------------------------------
//
// Import TypeBox and Ajv
//
//--------------------------------------------------------------------------------------------
import { Type } from '@sinclair/typebox'
import addFormats from 'ajv-formats'
import Ajv from 'ajv'
//--------------------------------------------------------------------------------------------
//
// Setup Ajv validator with the following options and formats
//
//--------------------------------------------------------------------------------------------
const ajv = addFormats(new Ajv({}), [
'date-time',
'time',
'date',
'email',
'hostname',
'ipv4',
'ipv6',
'uri',
'uri-reference',
'uuid',
'uri-template',
'json-pointer',
'relative-json-pointer',
'regex'
])
//--------------------------------------------------------------------------------------------
//
// Create a TypeBox type
//
//--------------------------------------------------------------------------------------------
const T = Type.Object({
x: Type.Number(),
y: Type.Number(),
z: Type.Number()
})
//--------------------------------------------------------------------------------------------
//
// Validate Data
//
//--------------------------------------------------------------------------------------------
const R = ajv.validate(T, { x: 1, y: 2, z: 3 }) // const R = true
Please refer to the official Ajv documentation for additional information on using Ajv.
Compiler
TypeBox provides an optional high performance runtime compiler and type checker that can be used in applications that require extremely fast validation. This compiler is optimized for TypeBox types whose schematics are known in advance. If defining custom types with Type.Unsafe<T>
please consider Ajv.
import { TypeCompiler } from '@sinclair/typebox/compiler'
Use the Compile(...)
function to compile a type.
const C = TypeCompiler.Compile(Type.Object({ // const C: TypeCheck<TObject<{
x: Type.Number(), // x: TNumber;
y: Type.Number(), // y: TNumber;
z: Type.Number() // z: TNumber;
})) // }>>
const R = C.Check({ x: 1, y: 2, z: 3 }) // const R = true
Validation errors can be read with the Errors(...)
function.
const C = TypeCompiler.Compile(Type.Object({ // const C: TypeCheck<TObject<{
x: Type.Number(), // x: TNumber;
y: Type.Number(), // y: TNumber;
z: Type.Number() // z: TNumber;
})) // }>>
const value = { }
const errors = [...C.Errors(value)] // const errors = [{
// schema: { type: 'number' },
// path: '/x',
// value: undefined,
// message: 'Expected number'
// }, {
// schema: { type: 'number' },
// path: '/y',
// value: undefined,
// message: 'Expected number'
// }, {
// schema: { type: 'number' },
// path: '/z',
// value: undefined,
// message: 'Expected number'
// }]
Compiled routines can be inspected with the .Code()
function.
const C = TypeCompiler.Compile(Type.String()) // const C: TypeCheck<TString>
console.log(C.Code()) // return function check(value) {
// return (
// (typeof value === 'string')
// )
// }
Benchmark
This project maintains a set of benchmarks that measure Ajv and TypeCompiler compilation and validation performance. These benchmarks can be run locally by cloning this repository and running npm run benchmark
. The results below show for Ajv version 8.11.0.
For additional comparative benchmarks, please refer to typescript-runtime-type-benchmarks.
Compile
This benchmark measures compilation performance for varying types. You can review this benchmark here.
ββββββββββββββββββββ¬βββββββββββββ¬βββββββββββββββ¬βββββββββββββββ¬βββββββββββββββ
β (index) β Iterations β Ajv β TypeCompiler β Performance β
ββββββββββββββββββββΌβββββββββββββΌβββββββββββββββΌβββββββββββββββΌβββββββββββββββ€
β Number β 2000 β ' 394 ms' β ' 9 ms' β ' 43.78 x' β
β String β 2000 β ' 320 ms' β ' 9 ms' β ' 35.56 x' β
β Boolean β 2000 β ' 326 ms' β ' 6 ms' β ' 54.33 x' β
β Null β 2000 β ' 256 ms' β ' 6 ms' β ' 42.67 x' β
β RegEx β 2000 β ' 494 ms' β ' 12 ms' β ' 41.17 x' β
β ObjectA β 2000 β ' 2813 ms' β ' 41 ms' β ' 68.61 x' β
β ObjectB β 2000 β ' 2949 ms' β ' 30 ms' β ' 98.30 x' β
β Tuple β 2000 β ' 1258 ms' β ' 19 ms' β ' 66.21 x' β
β Union β 2000 β ' 1308 ms' β ' 22 ms' β ' 59.45 x' β
β Vector4 β 2000 β ' 1589 ms' β ' 17 ms' β ' 93.47 x' β
β Matrix4 β 2000 β ' 932 ms' β ' 11 ms' β ' 84.73 x' β
β Literal_String β 2000 β ' 343 ms' β ' 6 ms' β ' 57.17 x' β
β Literal_Number β 2000 β ' 380 ms' β ' 6 ms' β ' 63.33 x' β
β Literal_Boolean β 2000 β ' 369 ms' β ' 4 ms' β ' 92.25 x' β
β Array_Number β 2000 β ' 730 ms' β ' 6 ms' β ' 121.67 x' β
β Array_String β 2000 β ' 764 ms' β ' 7 ms' β ' 109.14 x' β
β Array_Boolean β 2000 β ' 791 ms' β ' 8 ms' β ' 98.88 x' β
β Array_ObjectA β 2000 β ' 3550 ms' β ' 33 ms' β ' 107.58 x' β
β Array_ObjectB β 2000 β ' 3709 ms' β ' 33 ms' β ' 112.39 x' β
β Array_Tuple β 2000 β ' 2209 ms' β ' 15 ms' β ' 147.27 x' β
β Array_Union β 2000 β ' 1733 ms' β ' 18 ms' β ' 96.28 x' β
β Array_Vector4 β 2000 β ' 2279 ms' β ' 16 ms' β ' 142.44 x' β
β Array_Matrix4 β 2000 β ' 1587 ms' β ' 11 ms' β ' 144.27 x' β
ββββββββββββββββββββ΄βββββββββββββ΄βββββββββββββββ΄βββββββββββββββ΄βββββββββββββββ
Validate
This benchmark measures validation performance for varying types. You can review this benchmark here.
ββββββββββββββββββββ¬βββββββββββββ¬βββββββββββββββ¬βββββββββββββββ¬βββββββββββββββ¬βββββββββββββββ
β (index) β Iterations β ValueCheck β Ajv β TypeCompiler β Performance β
ββββββββββββββββββββΌβββββββββββββΌβββββββββββββββΌβββββββββββββββΌβββββββββββββββΌβββββββββββββββ€
β Number β 1000000 β ' 27 ms' β ' 6 ms' β ' 4 ms' β ' 1.50 x' β
β String β 1000000 β ' 23 ms' β ' 20 ms' β ' 11 ms' β ' 1.82 x' β
β Boolean β 1000000 β ' 21 ms' β ' 19 ms' β ' 10 ms' β ' 1.90 x' β
β Null β 1000000 β ' 24 ms' β ' 18 ms' β ' 10 ms' β ' 1.80 x' β
β RegEx β 1000000 β ' 170 ms' β ' 43 ms' β ' 36 ms' β ' 1.19 x' β
β ObjectA β 1000000 β ' 567 ms' β ' 34 ms' β ' 23 ms' β ' 1.48 x' β
β ObjectB β 1000000 β ' 985 ms' β ' 50 ms' β ' 36 ms' β ' 1.39 x' β
β Tuple β 1000000 β ' 119 ms' β ' 24 ms' β ' 14 ms' β ' 1.71 x' β
β Union β 1000000 β ' 302 ms' β ' 26 ms' β ' 14 ms' β ' 1.86 x' β
β Recursive β 1000000 β ' 3071 ms' β ' 397 ms' β ' 177 ms' β ' 2.24 x' β
β Vector4 β 1000000 β ' 135 ms' β ' 24 ms' β ' 11 ms' β ' 2.18 x' β
β Matrix4 β 1000000 β ' 632 ms' β ' 41 ms' β ' 30 ms' β ' 1.37 x' β
β Literal_String β 1000000 β ' 49 ms' β ' 19 ms' β ' 9 ms' β ' 2.11 x' β
β Literal_Number β 1000000 β ' 56 ms' β ' 18 ms' β ' 9 ms' β ' 2.00 x' β
β Literal_Boolean β 1000000 β ' 56 ms' β ' 19 ms' β ' 9 ms' β ' 2.11 x' β
β Array_Number β 1000000 β ' 408 ms' β ' 31 ms' β ' 17 ms' β ' 1.82 x' β
β Array_String β 1000000 β ' 458 ms' β ' 32 ms' β ' 20 ms' β ' 1.60 x' β
β Array_Boolean β 1000000 β ' 431 ms' β ' 34 ms' β ' 24 ms' β ' 1.42 x' β
β Array_ObjectA β 1000000 β ' 13322 ms' β ' 2549 ms' β ' 1636 ms' β ' 1.56 x' β
β Array_ObjectB β 1000000 β ' 16341 ms' β ' 2865 ms' β ' 2074 ms' β ' 1.38 x' β
β Array_Tuple β 1000000 β ' 1640 ms' β ' 92 ms' β ' 71 ms' β ' 1.30 x' β
β Array_Union β 1000000 β ' 4803 ms' β ' 237 ms' β ' 89 ms' β ' 2.66 x' β
β Array_Recursive β 1000000 β ' 53759 ms' β ' 7694 ms' β ' 2600 ms' β ' 2.96 x' β
β Array_Vector4 β 1000000 β ' 2099 ms' β ' 96 ms' β ' 52 ms' β ' 1.85 x' β
β Array_Matrix4 β 1000000 β ' 11436 ms' β ' 384 ms' β ' 310 ms' β ' 1.24 x' β
ββββββββββββββββββββ΄βββββββββββββ΄βββββββββββββββ΄βββββββββββββββ΄βββββββββββββββ΄βββββββββββββββ
Compression
The following table lists esbuild compiled and minified sizes for each TypeBox import.
ββββββββββββββββββββββββ¬βββββββββββββ¬βββββββββββββ¬ββββββββββββββ
β (index) β Compiled β Minified β Compression β
ββββββββββββββββββββββββΌβββββββββββββΌβββββββββββββΌββββββββββββββ€
β typebox/compiler β ' 47 kb' β ' 23 kb' β '1.99 x' β
β typebox/conditional β ' 41 kb' β ' 16 kb' β '2.46 x' β
β typebox/guard β ' 20 kb' β ' 9 kb' β '2.06 x' β
β typebox/value β ' 54 kb' β ' 25 kb' β '2.14 x' β
β typebox β ' 11 kb' β ' 5 kb' β '1.89 x' β
ββββββββββββββββββββββββ΄βββββββββββββ΄βββββββββββββ΄ββββββββββββββ
Contribute
TypeBox is open to community contribution. Please ensure you submit an open issue before submitting your pull request. The TypeBox project preferences open community discussion prior to accepting new features.