MCPcopy Index your code
hub / github.com/sinclairzx81/typebox

github.com/sinclairzx81/typebox @1.3.3

Chat with this repo
repository ↗ · DeepWiki ↗ · release 1.3.3 ↗ · + Follow
4,438 symbols 15,364 edges 1,325 files 29 documented · 1% 23 cross-repo links updated 1d ago★ 6,7844 open issues
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

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 that can be statically checked by TypeScript and runtime checked using standard JSON Schema validation.

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

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 | Challenge

TypeBox includes a syntax engine that can transform TypeScript declarations into JSON Schema. The engine is a full syntactic frontend to Type.* and supports many advanced type-level constructs such as Conditional, Mapped, Indexed, Infer, Generics, Distributed types and more. This feature is implemented symmetrically at runtime and statically via TypeScript Template Literal types.

Syntax highlighting is available via the Visual Studio Marketplace

Example

The following uses Script to parse TypeScript declarations into JSON Schema.

// Module
const Math = Type.Script(`
  type Vector2 = { x: number, y: number }
  type Vector3 = Evaluate<Vector2 & { z: number }>
  type Vector4 = Evaluate<Vector3 & { w: number }>
`)

// Dependent Module
const { Mesh } = Type.Script({ ...Math }, `  
  type Vertex = {
    position: Vector4,
    normal: Vector3,
    uv: Vector2
  }
  type Geometry = {
    vertices: Vertex[],
    indices: number[]
  }
  type Material = {
    ambient: Vector4,
    diffuse: Vector4,
    specular: Vector4
  }
  type Mesh = {
    geometry: Geometry,
    material: Material
  }
`)

// Runtime Reflection
Mesh.properties.geometry.properties.vertices.items.properties.position.properties.x
Mesh.properties.geometry.properties.vertices.items.properties.normal.properties.x
Mesh.properties.geometry.properties.vertices.items.properties.uv.properties.x
Mesh.properties.material.properties.diffuse.properties.x
Mesh.properties.material.properties.ambient.properties.x
Mesh.properties.material.properties.specular.properties.x

// Static Inference
function render(mesh: Type.Static<typeof Mesh>) {
  mesh.geometry.vertices[0].position.x
  mesh.geometry.vertices[0].normal.x
  mesh.geometry.vertices[0].uv.x
  mesh.material.diffuse.x
  mesh.material.ambient.x
  mesh.material.specular.x
}

Schema

Documentation | Example 1 | Example 2

TypeBox includes a high-performance JIT compiler that supports JSON Schema Draft 3 through to 2020-12. It is designed to be a lightweight industry-grade alternative to Ajv and offers improved compilation and validation performance. It also provides automatic fallback to dynamic validation in JIT restricted environments such as Cloudflare Workers.

The compiler is available via optional sub module import.

import Schema from 'typebox/schema'

Compile

The compiler accepts JSON Schema and returns Validator instances.

const Vector = Schema.Compile(Type.Object({       // const Vector: Validator<TObject<{
  x: Type.Number(),                                //   x: TNumber
  y: Type.Number(),                                //   y: TNumber
  z: Type.Number()                                 //   z: TNumber
}))                                                // }>>

With JSON Schema

const Vector = Schema.Compile({                    // const Vector: Validator<{
  type: 'object',                                  //   type: "object";
  required: ['x', 'y', 'z'],                       //   required: ["x", "y", "z"];
  properties: {                                    //   properties: { ... };
    x: { type: 'number' },                         // }, { ... }>
    y: { type: 'number' },
    z: { type: 'number' }
  }
})

Validate

Validator instances provide functions to Check and Parse values.

const Vector = Schema.Compile(Type.Script(`{
  x: number
  y: number
  z: number
}`))

const valid = Vector.Check({                       // const valid: boolean
  x: 1,
  y: 0,
  z: 0
}) 

const value = Vector.Parse({                       // const value: {      
  x: 1,                                            //   x: number
  y: 0,                                            //   y: number
  z: 0                                             //   z: number
})                                                 // }

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
minPro

Extension points exported contracts — how you extend this code

StandardTypedV1 (Interface)
(no doc)
example/standard/standard.ts
RouteOptions (Interface)
(no doc)
example/route/route.ts
ReportOptions (Interface)
(no doc)
task/spec/report.ts
PrintOptions (Interface)
(no doc)
task/bench/benchmark/common.ts
Manifest (Interface)
(no doc)
task/website/website.ts
Manifest (Interface)
(no doc)
design/website/app/frontend/components/docs/docs.tsx
Operation (Interface)
(no doc)
test/jsonschema/spec.ts
Node (Interface)
(no doc)
test/typebox/static/type/this.ts

Core symbols most depended-on inside this repo

If
called by 575
src/type/script/parser.ts
push
called by 568
docs/index.js
Ref
called by 521
src/schema/engine/_stack.ts
Context
called by 473
src/schema/build.ts
Extends
called by 415
src/type/script/parser.ts
Ok
called by 376
test/typebox/runtime/value/check/~validate.ts
Fail
called by 304
test/typebox/runtime/value/check/~validate.ts
Convert
called by 291
src/compile/validator.ts

Shape

Function 3,237
Method 936
Interface 186
Class 72
Enum 7

Languages

TypeScript100%

Modules by API surface

docs/index.js2,200 symbols
src/type/script/mapping.ts132 symbols
src/type/script/parser.ts131 symbols
src/guard/emit.ts44 symbols
src/guard/guard.ts37 symbols
src/error/errors.ts34 symbols
src/schema/engine/_context.ts32 symbols
example/prototype/dynamic.ts25 symbols
src/system/hashing/hash.ts21 symbols
src/schema/build.ts21 symbols
example/standard/standard.ts20 symbols
src/guard/globals.ts19 symbols

For agents

$ claude mcp add typebox \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact