Skip to main content
Deno 2 is finally here πŸŽ‰οΈ
Learn more

TypeBox

JSON Schema Type Builder with Static Type Resolution for TypeScript



npm version Downloads Build License

Install

$ npm install typebox

Usage

import Type from 'typebox'

const T = Type.Object({                     // const T = {
  x: Type.Number(),                         //   type: 'object',
  y: Type.Number(),                         //   properties: {
  z: Type.Number()                          //     x: { type: 'number' },
})                                          //     y: { type: 'number' },
                                            //     z: { type: 'number' }
                                            //   },
                                            //   required: ['x', 'y', 'z']
                                            // }

type T = Type.Static<typeof T>              // type T = {
                                            //   x: number,
                                            //   y: number,
                                            //   z: number
                                            // }

Overview

Documentation

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, RPC and MCP services to help validate data received over the wire.

License: MIT

Contents

Type

Documentation | Example

TypeBox types are JSON Schema fragments that compose into more complex types. The library offers a set of types used to construct JSON Schema compliant schematics as well as a set of extended types used to model constructs native to the JavaScript language. The schematics produced by TypeBox can be passed directly to any JSON Schema compliant validator.

Example

The following creates a User type and infers with Static.

import Type from 'typebox'


// Type

const User = Type.Object({                       // const User = {
  id: Type.String(),                             //   type: 'object',
  name: Type.String(),                           //   properties: {
  email: Type.String({ format: 'email' })        //     id: { type: 'string' },
})                                               //     name: { type: 'string' },
                                                 //     email: { 
                                                 //       type: 'string', 
                                                 //       format: 'email' 
                                                 //     }
                                                 //   }
                                                 //   required: [
                                                 //     'id', 
                                                 //     'name', 
                                                 //     'email'
                                                 //   ]
                                                 // }


// Static

type User = Type.Static<typeof User>              // type User = {
                                                  //   id: string,
                                                  //   name: string,
                                                  //   email: string
                                                  // }

Script

Documentation | Example 1 | Example 2

The TypeBox Script function is a micro DSL for constructing JSON Schema from TypeScript syntax. It offers a full syntactic frontend to Type.* with broad support for type-level expressions including Conditional, Mapped, Infer, Generic, Distributive types and more. This feature is implemented symmetrically at both runtime and in the type system.

Example

The following uses the Script function to parse TypeScript interfaces into JSON Schema.

import Type from 'typebox'


// Script

const { User, UserUpdate } = Type.Script(`

  interface Entity {
    id: string
  }

  interface User extends Entity { 
    name: string, 
    email: string 
  }

  type UserUpdate = Evaluate<
    Pick<User, keyof Entity> & 
    Partial<Omit<User, keyof Entity>>
  >

`)


// Reflect

console.log(User)                                // {
                                                 //   type: 'object',
                                                 //   properties: {
                                                 //     id: { type: 'string' },
                                                 //     name: { type: 'string' },
                                                 //     email: { type: 'string' }
                                                 //   },
                                                 //   required: [
                                                 //     'id', 
                                                 //     'name', 
                                                 //     'email'
                                                 //   ]
                                                 // }

console.log(UserUpdate)                          // {
                                                 //   type: 'object',
                                                 //   properties: {
                                                 //     id: { type: 'string' },
                                                 //     name: { type: 'string' },
                                                 //     email: { type: 'string' }
                                                 //   },
                                                 //   required: ['id']
                                                 // }


// Static

type User = Type.Static<typeof User>              // type User = {
                                                  //   id: string,
                                                  //   name: string,
                                                  //   email: string
                                                  // }

type UserUpdate = Type.Static<typeof UserUpdate>  // type UserUpdate = {
                                                  //   id: string,
                                                  //   name?: string,
                                                  //   email?: string
                                                  // }

Schema

Documentation | Example 1 | Example 2 | Specification

TypeBox has a high-performance JSON Schema compiler with support for drafts 3 through to 2020-12. The compiler is designed to be a lightweight industry-grade alternative to Ajv and offers improved compilation and validation performance. The compiler also offers automatic fallback to dynamic checking in JIT-restricted environments such as Cloudflare Workers.

Example

The following uses the Schema submodule to compile a TypeBox type.

import Schema from 'typebox/schema'


// Compile

const User = Schema.Compile(Type.Object({        // const User = Validator<Type.TObject<{
  id: Type.String(),                             //   id: Type.TString;
  name: Type.String(),                           //   name: Type.TString;
  email: Type.String({ format: 'email' })        //   email: Type.TString;
}))                                              // }>, { ... }>


// Parse

const user = User.Parse({                        // const user: {
  id: '65f1a2b3c4d5e6f7a8b9c0d1',                //   id: string,
  name: 'user',                                  //   name: string,
  email: 'user@domain.com'                       //   email: string
})                                               // } = ...

Coverage

The following table shows specification coverage implemented by TypeBox.

Ref: JSON Schema Test Suite

Spec 3 4 6 7 2019-09 2020-12 v1
additionalItems βœ… βœ… βœ… βœ… βœ… - -
additionalProperties βœ… βœ… βœ… βœ… βœ… βœ… βœ…
allOf - βœ… βœ… βœ… βœ… βœ… βœ…
anchor - - - - βœ… βœ… βœ…
anyOf - βœ… βœ… βœ… βœ… βœ… βœ…
boolean_schema - - βœ… βœ… βœ… βœ… βœ…
const - - βœ… βœ… βœ… βœ… βœ…
contains - - βœ… βœ… βœ… βœ… βœ…
content - - - - βœ… βœ… βœ…
default βœ… βœ… βœ… βœ… βœ… βœ… βœ…
dependencies 17/18 βœ… βœ… βœ… - - -
dependentRequired - - - - βœ… βœ… βœ…
dependentSchemas - - - - βœ… βœ… βœ…
dynamicRef - - - - - 38/44 19/27
enum 14/16 βœ… βœ… βœ… βœ… βœ… βœ…
exclusiveMaximum - - βœ… βœ… βœ… βœ… βœ…
exclusiveMinimum - - βœ… βœ… βœ… βœ… βœ…
if-then-else - - - βœ… βœ… βœ… βœ…
infinite-loop-detection βœ… βœ… βœ… βœ… βœ… βœ… βœ…
items βœ… βœ… βœ… βœ… βœ… βœ… βœ…
maxContains - - - - βœ… βœ… βœ…
maximum 13/14 13/14 βœ… βœ… βœ… βœ… βœ…
maxItems βœ… βœ… βœ… βœ… βœ… βœ… βœ…
maxLength βœ… βœ… βœ… βœ… βœ… βœ… βœ…
maxProperties - βœ… βœ… βœ… βœ… βœ… βœ…
minContains - - - - βœ… βœ… βœ…
minimum 12/13 16/17 βœ… βœ… βœ… βœ… βœ…
minItems βœ… βœ… βœ… βœ… βœ… βœ… βœ…
minLength βœ… βœ… βœ… βœ… βœ… βœ… βœ…
minProperties - βœ… βœ… βœ… βœ… βœ… βœ…
multipleOf - βœ… βœ… βœ… βœ… βœ… βœ…
not - βœ… βœ… βœ… βœ… βœ… βœ…
oneOf - βœ… βœ… βœ… βœ… βœ… βœ…
pattern βœ… βœ… βœ… βœ… βœ… βœ… βœ…
patternProperties βœ… βœ… βœ… βœ… βœ… βœ… βœ…
prefixItems - - - - - βœ… βœ…
properties βœ… βœ… βœ… βœ… βœ… βœ… βœ…
propertyNames - - βœ… βœ… βœ… βœ… βœ…
recursiveRef - - - - βœ… - -
ref 23/27 37/45 67/70 75/78 79/81 77/79 77/79
required 3/4 βœ… βœ… βœ… βœ… βœ… βœ…
type 73/80 βœ… βœ… βœ… βœ… βœ… βœ…
unevaluatedItems - - - - βœ… 65/71 64/71
unevaluatedProperties - - - - βœ… βœ… 124/125
uniqueItems βœ… βœ… βœ… βœ… βœ… βœ… βœ…

Performance

The following table shows compile performance for various JSON Schema structures using AJV8 as a basis for comparison.

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Compile              β”‚ TB1X        β”‚ AJV8        β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Boolean              β”‚ 28.4K ops/s β”‚    7K ops/s β”‚
β”‚ Number               β”‚ 21.8K ops/s β”‚  7.7K ops/s β”‚
β”‚ String               β”‚ 47.8K ops/s β”‚  7.3K ops/s β”‚
β”‚ Null                 β”‚ 35.6K ops/s β”‚  7.8K ops/s β”‚
β”‚ Literal_String       β”‚ 28.6K ops/s β”‚  6.3K ops/s β”‚
β”‚ Literal_Number       β”‚ 46.6K ops/s β”‚  6.2K ops/s β”‚
β”‚ Literal_Boolean      β”‚ 40.8K ops/s β”‚  6.6K ops/s β”‚
β”‚ Pattern              β”‚ 29.7K ops/s β”‚  4.9K ops/s β”‚
β”‚ Object_Open          β”‚  6.8K ops/s β”‚  1.1K ops/s β”‚
β”‚ Object_Close         β”‚  7.4K ops/s β”‚   833 ops/s β”‚
β”‚ Object_Vector3       β”‚ 19.4K ops/s β”‚  2.1K ops/s β”‚
β”‚ Object_Basis3        β”‚    6K ops/s β”‚   895 ops/s β”‚
β”‚ Intersect_And        β”‚   12K ops/s β”‚  3.5K ops/s β”‚
β”‚ Intersect_Structural β”‚  8.4K ops/s β”‚  1.1K ops/s β”‚
β”‚ Union_Or             β”‚ 18.2K ops/s β”‚  2.5K ops/s β”‚
β”‚ Union_Structural     β”‚ 10.9K ops/s β”‚  1.3K ops/s β”‚
β”‚ Tuple_Values         β”‚  7.3K ops/s β”‚  1.6K ops/s β”‚
β”‚ Tuple_Objects        β”‚  1.9K ops/s β”‚   339 ops/s β”‚
β”‚ Array_Numbers_4      β”‚ 29.9K ops/s β”‚  3.4K ops/s β”‚
β”‚ Array_Numbers_8      β”‚ 20.3K ops/s β”‚  3.4K ops/s β”‚
β”‚ Array_Numbers_16     β”‚ 29.4K ops/s β”‚  3.3K ops/s β”‚
β”‚ Array_Objects_Open   β”‚  6.3K ops/s β”‚   684 ops/s β”‚
β”‚ Array_Objects_Close  β”‚  7.3K ops/s β”‚   762 ops/s β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

The following tables shows validation performance for various JSON Schema structures using AJV8 as a basis for comparison.

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Validate             β”‚ TB1X         β”‚ AJV8         β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Boolean              β”‚ 164.1M ops/s β”‚ 181.5M ops/s β”‚
β”‚ Number               β”‚   107M ops/s β”‚  50.2M ops/s β”‚
β”‚ String               β”‚ 102.2M ops/s β”‚  61.9M ops/s β”‚
β”‚ Null                 β”‚ 112.1M ops/s β”‚  48.2M ops/s β”‚
β”‚ Literal_String       β”‚ 102.8M ops/s β”‚  61.5M ops/s β”‚
β”‚ Literal_Number       β”‚ 109.1M ops/s β”‚  46.4M ops/s β”‚
β”‚ Literal_Boolean      β”‚ 109.6M ops/s β”‚  63.3M ops/s β”‚
β”‚ Pattern              β”‚  24.7M ops/s β”‚  20.3M ops/s β”‚
β”‚ Object_Open          β”‚  75.4M ops/s β”‚  37.3M ops/s β”‚
β”‚ Object_Close         β”‚  35.9M ops/s β”‚  21.9M ops/s β”‚
β”‚ Object_Vector3       β”‚  77.6M ops/s β”‚  47.4M ops/s β”‚
β”‚ Object_Basis3        β”‚    37M ops/s β”‚  24.3M ops/s β”‚
β”‚ Intersect_And        β”‚  93.3M ops/s β”‚  61.1M ops/s β”‚
β”‚ Intersect_Structural β”‚    83M ops/s β”‚  36.4M ops/s β”‚
β”‚ Union_Or             β”‚  99.7M ops/s β”‚   8.6M ops/s β”‚
β”‚ Union_Structural     β”‚  81.3M ops/s β”‚  43.5M ops/s β”‚
β”‚ Tuple_Values         β”‚  72.4M ops/s β”‚  41.7M ops/s β”‚
β”‚ Tuple_Objects        β”‚  32.6M ops/s β”‚  22.4M ops/s β”‚
β”‚ Array_Numbers_4      β”‚  94.1M ops/s β”‚  42.8M ops/s β”‚
β”‚ Array_Numbers_8      β”‚  90.6M ops/s β”‚  42.3M ops/s β”‚
β”‚ Array_Numbers_16     β”‚  77.5M ops/s β”‚  40.2M ops/s β”‚
β”‚ Array_Objects_Open   β”‚  26.3M ops/s β”‚  19.6M ops/s β”‚
β”‚ Array_Objects_Close  β”‚   9.1M ops/s β”‚    10M ops/s β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Versions

TypeBox ships two distinct versions that span two generations of the TypeScript compiler.

TypeBox TypeScript Description
1.x 6.0 - 7.0+ Latest. Developed against the TypeScript 7 native compiler. Provides advanced type inference and native JSON Schema 2020-12 support. Includes backwards compatibility with 0.x types. ESM only.
0.x 5.0 - 6.0 LTS. Developed against older TypeScript versions and actively maintained under Long Term Support. Compatible with both ESM and CJS. Issues should be submitted to the Sinclair TypeBox repository.

Contribute

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.