typebox
v1.3.8
Published
Json Schema Type Builder with Static Type Resolution for TypeScript
Readme
Install
$ npm install typeboxUsage
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
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
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 runtime TypeScript engine that can transform TypeScript definitions to JSON Schema. The engine is fully type-safe and supports many programmable constructs including Conditional, Mapped, Indexed, Generics, Distributive Generics, and more.
Example
Syntax highlighting is available via the Visual Studio Marketplace
// Module
const { Post } = Type.Script(`
type User = {
id: number,
name: string
}
type Comment = {
id: number,
text: string,
author: User
}
type Post = {
id: number,
title: string,
body: string,
author: User,
comments: Comment[]
}
`)
// Reflection
Post.properties.id
Post.properties.title
Post.properties.author.properties.id
Post.properties.author.properties.name
Post.properties.comments.items.properties.text
Post.properties.comments.items.properties.author.properties.id
Post.properties.comments.items.properties.author.properties.name
// Inference
function present(post: Type.Static<typeof Post>) {
post.id
post.title
post.author.id
post.author.name
post.comments[0].text
post.comments[0].author.id
post.comments[0].author.name
}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.
| 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 | 22/27 | 37/45 | 67/70 | 75/78 | 79/81 | 77/79 | 77/79 | | required | 3/4 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | type | 73/80 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | unevaluatedItems | - | - | - | - | ✅ | ✅ | 70/71 | | unevaluatedProperties | - | - | - | - | ✅ | ✅ | 128/129 | | uniqueItems | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
Performance
The following table shows compile performance for various JSON Schema structures using AJV8 as a basis for comparison.
┌──────────────────────┬─────────────┬─────────────┐
│ Compile │ TB1X │ AJV8 │
├──────────────────────┼─────────────┼─────────────┤
│ Boolean │ 39.7K ops/s │ 6.8K ops/s │
│ Number │ 86.2K ops/s │ 7.5K ops/s │
│ String │ 82.6K ops/s │ 8.2K ops/s │
│ Null │ 62K ops/s │ 7.3K ops/s │
│ Literal_String │ 72.1K ops/s │ 5.7K ops/s │
│ Literal_Number │ 72.7K ops/s │ 6.9K ops/s │
│ Literal_Boolean │ 80.9K ops/s │ 7K ops/s │
│ Pattern │ 38.7K ops/s │ 5.7K ops/s │
│ Object_Open │ 16.3K ops/s │ 1.3K ops/s │
│ Object_Close │ 15.7K ops/s │ 952 ops/s │
│ Object_Vector3 │ 40.2K ops/s │ 3.2K ops/s │
│ Object_Basis3 │ 16.1K ops/s │ 834 ops/s │
│ Intersect_And │ 45.7K ops/s │ 3.5K ops/s │
│ Intersect_Structural │ 21K ops/s │ 1.6K ops/s │
│ Union_Or │ 45.1K ops/s │ 3.4K ops/s │
│ Union_Structural │ 27.6K ops/s │ 2K ops/s │
│ Tuple_Values │ 14.7K ops/s │ 2K ops/s │
│ Tuple_Objects │ 3.7K ops/s │ 380 ops/s │
│ Array_Numbers_4 │ 73.6K ops/s │ 4.3K ops/s │
│ Array_Numbers_8 │ 50.9K ops/s │ 3.8K ops/s │
│ Array_Numbers_16 │ 85K ops/s │ 3.9K ops/s │
│ Array_Objects_Open │ 18.9K ops/s │ 789 ops/s │
│ Array_Objects_Close │ 17.9K ops/s │ 909 ops/s │
└──────────────────────┴─────────────┴─────────────┘The following tables shows validation performance for various JSON Schema structures using AJV8 as a basis for comparison.
┌──────────────────────┬──────────────┬──────────────┐
│ Validate │ TB1X │ AJV8 │
├──────────────────────┼──────────────┼──────────────┤
│ Boolean │ 192.2M ops/s │ 189.5M ops/s │
│ Number │ 112.4M ops/s │ 61M ops/s │
│ String │ 113.7M ops/s │ 64.1M ops/s │
│ Null │ 112.8M ops/s │ 64.9M ops/s │
│ Literal_String │ 108M ops/s │ 62.9M ops/s │
│ Literal_Number │ 113.5M ops/s │ 63.2M ops/s │
│ Literal_Boolean │ 109.2M ops/s │ 64.1M ops/s │
│ Pattern │ 26.5M ops/s │ 22.4M ops/s │
│ Object_Open │ 78M ops/s │ 47.2M ops/s │
│ Object_Close │ 38.6M ops/s │ 27.6M ops/s │
│ Object_Vector3 │ 91M ops/s │ 51.3M ops/s │
│ Object_Basis3 │ 41.1M ops/s │ 27.4M ops/s │
│ Intersect_And │ 107.6M ops/s │ 59.9M ops/s │
│ Intersect_Structural │ 83.6M ops/s │ 46.3M ops/s │
│ Union_Or │ 95M ops/s │ 7.9M ops/s │
│ Union_Structural │ 84.5M ops/s │ 52.3M ops/s │
│ Tuple_Values │ 74.7M ops/s │ 53M ops/s │
│ Tuple_Objects │ 32.9M ops/s │ 22.3M ops/s │
│ Array_Numbers_4 │ 93.3M ops/s │ 55.1M ops/s │
│ Array_Numbers_8 │ 90.3M ops/s │ 50.8M ops/s │
│ Array_Numbers_16 │ 76.8M ops/s │ 39.6M ops/s │
│ Array_Objects_Open │ 28.7M ops/s │ 20.4M ops/s │
│ Array_Objects_Close │ 10.3M ops/s │ 10.8M 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.
