TypeBox
JSON Schema Type Builder with Static Type Resolution for TypeScript

Install
Npm
- ``` shell
- $ npm install @sinclair/typebox --save
- ```
Deno
- ``` ts
- import { Static, Type } from 'npm:@sinclair/typebox'
- ```
Esm
- ``` ts
- import { Static, Type } from 'https://esm.sh/@sinclair/typebox'
- ```
Example
- ``` ts
- import { Static, Type } from '@sinclair/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 builder 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 assertion rules of the TypeScript compiler. TypeBox enables one to create a unified type that can be statically checked by TypeScript and runtime asserted using standard JSON Schema validation.
This library is designed to enable JSON schema to compose with the same flexibility as TypeScript's type system. It can be used as a simple tool to build up complex schemas or integrated into REST or RPC services to help validate data received over the wire.
License MIT
Contents
Install
Overview
Usage
Types
Standard
Extended
Modifiers
Options
Generics
References
Recursive
Conditional
Template Literal
Indexed
Rest
Guards
Unsafe
Strict
Values
Create
Clone
Check
Convert
Cast
Equal
Hash
Diff
Patch
Errors
Mutate
Pointer
TypeCheck
Ajv
TypeCompiler
TypeSystem
Types
Formats
Policies
Benchmark
Compile
Validate
Compression
Contribute
Usage
The following shows general usage.
- ``` ts
- 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.
- //
- //--------------------------------------------------------------------------------------------
- import { Value } from '@sinclair/typebox/value'
- function receive(value: T) { // ... as a Static Type
- if(Value.Check(T, value)) { // ... as a JSON Schema
- // ok...
- }
- }
- ```
Types
TypeBox types are JSON schema fragments that can be composed into more complex types. Each fragment is structured such that a JSON schema compliant validator can runtime assert a value the same way TypeScript will statically assert a type. TypeBox provides a set of Standard types which are used create JSON schema compliant schematics as well as an Extended type set used to create schematics for constructs native to JavaScript.
Standard Types
The following table lists the Standard TypeBox types. These types are fully compatible with the JSON Schema Draft 6 specification.
- ``` ts
- ┌────────────────────────────────┬─────────────────────────────┬────────────────────────────────┐
- │ 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.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 │ required: ['x', 'y'], │
- │ }) │ } │ properties: { │
- │ │ │ x: { │
- │ │ │ type: 'number' │
- │ │ │ }, │
- │ │ │ y: { │
- │ │ │ type: 'number' │
- │ │ │ } │
- │ │ │ } │
- │ │ │ } │
- │ │ │ │
- ├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
- │ 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 │
- │ │ │ }] │
- │ │ │ } │
- │ │ │