- 1.0.4Latest
- 1.0.3
- 1.0.2
- 1.0.2
- 1.0.1
- 1.0.0
- 1.0.0-rc
- 0.34.41
- 0.34.40
- 0.34.39
- 0.34.38
- 0.34.36
- 0.34.35
- 0.34.34
- 0.34.33
- 0.34.32
- 0.34.31
- 0.34.30
- 0.34.29
- 0.34.28
- 0.34.27
- 0.34.26
- 0.34.25
- 0.34.24
- 0.34.23
- 0.34.22
- 0.34.21
- 0.34.20
- 0.34.19
- 0.34.18
- 0.34.17
- 0.34.16
- 0.34.15
- 0.34.14
- 0.34.13
- 0.34.12
- 0.34.11
- 0.34.10
- 0.34.9
- 0.34.8
- 0.34.7
- 0.34.6
- 0.34.5
- 0.34.3
- 0.34.1
- 0.34.1
- 0.34.0
- 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
$ npm install typebox --save # 1.0.0
$ npm install @sinclair/typebox --save # 0.34.x
Usage
import Type, { type Static } 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 = Static<typeof T> // type T = {
// x: number,
// y: number,
// z: number
// }
Overview
TypeBox is a runtime type system that creates in-memory Json Schema objects that infer as TypeScript types. The schemas produced by this library are designed to match the static type checking rules of the TypeScript compiler. TypeBox offers a unified type that can be statically checked by TypeScript and runtime checked with Json Schema.
TypeBox is designed to bring TypeScript level programmability to Json Schema and to offer a high performance validation solution for JavaScript. It can be used as a simple tool to build up complex schema or integrated into REST and RPC services to help validate data received over the wire.
License MIT
Contents
Docs
Full documentation for TypeBox can be found on the TypeBox website.
Type
TypeBox provides many functions to build Json Schema. Each function returns a small Json Schema fragment which maps to a corresponding TypeScript type. TypeBox uses function composition to compose schema fragments into higher order types. TypeBox provides a set of types to model Json Schema schematics. It also offers a set of extended schematics to model constructs native to TypeScript and JavaScript.
Example
The following creates Json Schema and infers with Static.
import Type, { type Static } 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 = Static<typeof T> // type T = {
// x: number,
// y: number,
// z: number
// }
Script
TypeBox is a type engine that uses Json Schema as a portable AST for runtime type representation. The Script function provides a syntax DSL to the engine and enables Json Schema to be created using native TypeScript syntax. This engine supports full type-safety for string encoded types.
Example
The following uses Script to create and map types.
import Type from 'typebox'
const T = Type.Script(`{
x: number,
y: number,
z: number
}`) // const T = TObject<{
// x: TNumber,
// y: TNumber,
// z: TNumber
// }>
const S = Type.Script({ T }, `{
[K in keyof T]: T[K] | null
}`) // const S: TObject<{
// x: TUnion<[TNumber, TNull]>,
// y: TUnion<[TNumber, TNull]>,
// z: TUnion<[TNumber, TNull]>
// }>
type S = Static<typeof S> // type S = {
// x: number | null,
// y: number | null,
// z: number | null
// }
Value
The Value module provides functions to Check and Parse JavaScript values. This module also includes functions that perform structural operations values such as Clone, Repair, Encode, Decode, Diff, Patch and Hash. This module has support for Json Schema and Standard Schema.
The Value module is available via optional import.
import Value from 'typebox/value'
Example
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
}) // } = ...
Compile
The Compile module provides functions to transform types into high-performance validators. The compiler is tuned for fast compilation as well as validation. The compiler can accept both Json Schema and Standard Schema schematics however compiler optimizations are limited to Json Schema only.
The compiler is available via optional import.
import { Compile } from 'typebox/compile'
Example
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(T, { // 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
}) // } = ...
Contribute
TypeBox is open to community contribution. Please ensure you submit an issue before submitting your pull request. The TypeBox project requires open community discussion before accepting new features.