All Projects โ†’ sinclairzx81 โ†’ Typebox

sinclairzx81 / Typebox

Licence: other
JSON Schema Type Builder with Static Type Resolution for TypeScript

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Typebox

Janephp
๐ŸŒฑ Jane is a set of libraries to generate Models & API Clients based on JSON Schema / OpenAPI specs
Stars: โœญ 300 (-30.72%)
Mutual labels:  json-schema
Pyjfuzz
PyJFuzz - Python JSON Fuzzer
Stars: โœญ 342 (-21.02%)
Mutual labels:  json-schema
Angular
JSON powered forms for Angular
Stars: โœญ 385 (-11.09%)
Mutual labels:  json-schema
Vue Json Schema Form
ๅŸบไบŽVue/Vue3๏ผŒJson Schema ๅ’Œ ElementUi/antd/iview3 ็ญ‰็”Ÿๆˆ HTML Form ่กจๅ•๏ผŒ็”จไบŽๆดปๅŠจ็ผ–่พ‘ๅ™จใ€h5็ผ–่พ‘ๅ™จใ€cms็ญ‰ๆ•ฐๆฎ้…็ฝฎ๏ผ›ๆ”ฏๆŒๅฏ่ง†ๅŒ–็”Ÿๆˆ่กจๅ•Schema ใ€‚ Generate a form using Vue/Vue3, Json Schema and ElementUi/antdv/iview3
Stars: โœญ 300 (-30.72%)
Mutual labels:  json-schema
Symfony React Sandbox
Example of integration with React and Webpack (Webpack Encore) for universal (isomorphic) React rendering, using Limenius/ReactBundle and Limenius/LiformBundle
Stars: โœญ 325 (-24.94%)
Mutual labels:  json-schema
Json
C++ header-only JSON library
Stars: โœญ 343 (-20.79%)
Mutual labels:  json-schema
Serverless Aws Documentation
Serverless 1.0 plugin to add documentation and models to the serverless generated API Gateway
Stars: โœญ 299 (-30.95%)
Mutual labels:  json-schema
Opendata.cern.ch
Source code for the CERN Open Data portal
Stars: โœญ 411 (-5.08%)
Mutual labels:  json-schema
Json Schema
JSON Schema validator for PHP
Stars: โœญ 331 (-23.56%)
Mutual labels:  json-schema
Vue3 Antd Admin
ๅŸบไบŽvue-cli/vite + vue3.0 + ant-design-vue2.0 + typescript hooks ็š„ๅŸบ็ก€ๅŽๅฐ็ฎก็†็ณป็ปŸๆจกๆฟ RBAC็š„ๆƒ้™็ณป็ปŸ, JSON SchemaๅŠจๆ€่กจๅ•,ๅŠจๆ€่กจๆ ผ,ๆผ‚ไบฎ้”ๅฑ็•Œ้ข
Stars: โœญ 334 (-22.86%)
Mutual labels:  json-schema
Swagger Cli
Swagger 2.0 and OpenAPI 3.0 command-line tool
Stars: โœญ 321 (-25.87%)
Mutual labels:  json-schema
Vue Form Builder
Build powerful vue form with JSON schema and composition api.
Stars: โœญ 325 (-24.94%)
Mutual labels:  json-schema
Formily
Alibaba Group Unified Form Solution -- Support React/ReactNative/Vue2/Vue3
Stars: โœญ 6,554 (+1413.63%)
Mutual labels:  json-schema
Jsonschema
An implementation of the JSON Schema specification for Python
Stars: โœญ 3,474 (+702.31%)
Mutual labels:  json-schema
Vue Json Ui Editor
Edit JSON in UI form with JSON Schema and Vue.js
Stars: โœญ 392 (-9.47%)
Mutual labels:  json-schema
Php Json Schema
High definition PHP structures with JSON-schema based validation
Stars: โœญ 301 (-30.48%)
Mutual labels:  json-schema
Dtsgenerator
TypeScript d.ts file generate from JSON Schema file
Stars: โœญ 344 (-20.55%)
Mutual labels:  json-schema
Ngx Schema Form
HTML form generation based on JSON Schema
Stars: โœญ 430 (-0.69%)
Mutual labels:  json-schema
Datamodel Code Generator
Pydantic model generator for easy conversion of JSON, OpenAPI, JSON Schema, and YAML data sources.
Stars: โœญ 393 (-9.24%)
Mutual labels:  json-schema
Ring Swagger
Swagger Spec for Clojure Web Apps
Stars: โœญ 351 (-18.94%)
Mutual labels:  json-schema

TypeBox

JSON Schema Type Builder with Static Type Resolution for TypeScript

npm version GitHub CI

Example

import { Static, Type } from '@sinclair/typebox'

const T = Type.String() /* const T = { "type": "string" } */

type T = Static<typeof T> /* type T = string */

Install

$ npm install @sinclair/typebox --save

Overview

TypeBox is a type builder library that creates in-memory JSON Schema objects that can be statically resolved to TypeScript types. The schemas produced by this library are built to match the static type checking rules of the TypeScript compiler. TypeBox allows one to create a single unified type that can be both statically checked by the TypeScript compiler and runtime asserted using standard JSON schema validation.

TypeBox can be used as a simple tool to build up complex schemas or integrated into RPC or REST services to help validate JSON data received over the wire. TypeBox does not provide any JSON schema validation. Please use libraries such as AJV to validate schemas built with this library.

Requires TypeScript 4.0.3 and above.

License MIT

Contents

Example

The following demonstrates TypeBox's general usage.

import { Type, Static } from '@sinclair/typebox'

//--------------------------------------------------------------------------------------------
//
// Let's say you have the following type ...
//
//--------------------------------------------------------------------------------------------

type Record = {
    id: string,
    name: string,
    timestamp: number
}

//--------------------------------------------------------------------------------------------
//
// ... you can express this type in the following way.
//
//--------------------------------------------------------------------------------------------

const Record = Type.Object({        // const Record = {
    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 Record = Static<typeof Record> // type Record = {
                                    //    id: string,
                                    //    name: string,
                                    //    timestamp: number
                                    // }

//--------------------------------------------------------------------------------------------
//
// ... then use the type both as JSON schema and as a TypeScript type.
//
//--------------------------------------------------------------------------------------------

function receive(record: Record) { // ... as a type
    if(JSON.validate(Record, {     // ... as a schema
        id: '42', 
        name: 'dave', 
        timestamp: Date.now() 
    })) {
        // ok...
    }
}

Types

The following table outlines the TypeBox mappings between TypeScript and JSON schema.

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ 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.RegEx(/foo/)	 โ”‚ type T = string             โ”‚ const T = {                    โ”‚
โ”‚                                โ”‚                             โ”‚    type: 'string',             โ”‚
โ”‚                                โ”‚                             โ”‚    pattern: 'foo'              โ”‚
โ”‚                                โ”‚                             โ”‚ }                              โ”‚
โ”‚   	                         โ”‚                             โ”‚                                โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ const T = Type.Literal('foo')  โ”‚ type T = 'foo'              โ”‚ const T = {                    โ”‚
โ”‚                                โ”‚                             โ”‚    type: 'string',             โ”‚
โ”‚                                โ”‚                             โ”‚    enum: ['foo']               โ”‚
โ”‚                                โ”‚                             โ”‚ }                              โ”‚
โ”‚   	                         โ”‚                             โ”‚                                โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ const T = Type.Array(          โ”‚ type T = number[]           โ”‚ const T = {                    โ”‚
โ”‚    Type.Number()               โ”‚                             โ”‚    type: 'array',              โ”‚
โ”‚ )                              โ”‚                             โ”‚    items: {                    โ”‚
โ”‚                                โ”‚                             โ”‚      type: 'number'            โ”‚
โ”‚                                โ”‚                             โ”‚    }                           โ”‚
โ”‚                                โ”‚                             โ”‚ }                              โ”‚
โ”‚   	                         โ”‚                             โ”‚                                โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ const T = Type.Dict(           โ”‚ type T = {                  โ”‚ const T = {                    โ”‚
โ”‚    Type.Number()               โ”‚      [key: string]          โ”‚    type: 'object'              โ”‚
โ”‚ )                              โ”‚ } : number                  โ”‚    additionalProperties: {     โ”‚
โ”‚   	                         โ”‚                             โ”‚      type: 'number'            โ”‚
โ”‚   	                         โ”‚                             โ”‚    }                           โ”‚
โ”‚   	                         โ”‚                             โ”‚ }                              โ”‚
โ”‚   	                         โ”‚                             โ”‚                                โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ const T = Type.Object({        โ”‚ type T = {                  โ”‚ const T = {                    โ”‚
โ”‚   a: Type.Number(),            โ”‚    a: number,               โ”‚   type: 'object',              โ”‚
โ”‚   b: Type.String()             โ”‚    b: string,               โ”‚   additionalProperties: false, โ”‚
โ”‚ })                             โ”‚ }                           โ”‚   properties: {                โ”‚
โ”‚                                โ”‚                             โ”‚      a: {                      โ”‚
โ”‚   	                         โ”‚                             โ”‚        type: 'number'          โ”‚
โ”‚   	                         โ”‚                             โ”‚      },                        โ”‚
โ”‚   	                         โ”‚                             โ”‚      b: {                      โ”‚
โ”‚                                โ”‚                             โ”‚        type: 'string'          โ”‚
โ”‚   	                         โ”‚                             โ”‚      }                         โ”‚
โ”‚   	                         โ”‚                             โ”‚   },                           โ”‚
โ”‚                                โ”‚                             โ”‚   required: ['a', 'b']         โ”‚
โ”‚   	                         โ”‚                             โ”‚ }                              โ”‚
โ”‚   	                         โ”‚                             โ”‚                                โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ const T = Type.Tuple([         โ”‚ type T = [string, number]   โ”‚ const T = {                    โ”‚
โ”‚   Type.String(),               โ”‚                             โ”‚    type: 'array',              โ”‚
โ”‚   Type.Number()                โ”‚                             โ”‚    items: [                    โ”‚
โ”‚ ])                             โ”‚                             โ”‚       {                        โ”‚
โ”‚   	                         โ”‚                             โ”‚         type: 'string'         โ”‚
โ”‚   	                         โ”‚                             โ”‚       }, {                     โ”‚
โ”‚   	                         โ”‚                             โ”‚         type: 'number'         โ”‚
โ”‚   	                         โ”‚                             โ”‚       }                        โ”‚
โ”‚   	                         โ”‚                             โ”‚    ],                          โ”‚
โ”‚   	                         โ”‚                             โ”‚    additionalItems: false,     โ”‚
โ”‚   	                         โ”‚                             โ”‚    minItems: 2,                โ”‚
โ”‚   	                         โ”‚                             โ”‚    maxItems: 2,                โ”‚
โ”‚   	                         โ”‚                             โ”‚ }                              |
โ”‚   	                         โ”‚                             โ”‚                                โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ enum Foo {                     โ”‚ enum Foo {                  โ”‚ const T = {                    โ”‚
โ”‚   A,                           โ”‚   A,                        โ”‚    enum: [0, 1]                โ”‚
โ”‚   B                            โ”‚   B                         โ”‚ }                              โ”‚
โ”‚ }                              โ”‚ }                           โ”‚                                โ”‚
โ”‚                                โ”‚                             โ”‚                                โ”‚
โ”‚ type T = Type.Enum(Foo)        โ”‚ type T = Foo                โ”‚                                โ”‚
โ”‚                                โ”‚                             โ”‚                                โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ const T = Type.Union([         โ”‚ type T = string | number    โ”‚ const T = {                    โ”‚
โ”‚   Type.String(),               โ”‚                             โ”‚    anyOf: [{                   โ”‚
โ”‚   Type.Number()                โ”‚                             โ”‚       type: 'string'           โ”‚
โ”‚ ])                             โ”‚                             โ”‚    }, {                        โ”‚
โ”‚                                โ”‚                             โ”‚       type: 'number'           โ”‚
โ”‚                                โ”‚                             โ”‚    }]                          โ”‚
โ”‚                                โ”‚                             โ”‚ }                              โ”‚
โ”‚   	                         โ”‚                             โ”‚                                โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ const T = Type.Intersect([     โ”‚ type T = {                  โ”‚ const T = {                    โ”‚
โ”‚    Type.Object({               โ”‚    a: string                โ”‚   type: 'object',              โ”‚
โ”‚       a: Type.String()         โ”‚ } & {                       โ”‚   additionalProperties: false, โ”‚
โ”‚    }),                         โ”‚    b: number                โ”‚   properties: {                โ”‚
โ”‚    Type.Object({               โ”‚ }                           โ”‚     a: {                       โ”‚
โ”‚       b: Type.Number()         โ”‚                             โ”‚        type: 'string'          โ”‚
โ”‚   })                           โ”‚                             โ”‚     },                         โ”‚
โ”‚ })                             โ”‚                             โ”‚     b: {                       โ”‚
โ”‚                                โ”‚                             โ”‚        type: 'number'          โ”‚
โ”‚                                โ”‚                             โ”‚     }                          โ”‚
โ”‚                                โ”‚                             โ”‚   },                           โ”‚
โ”‚                                โ”‚                             โ”‚   required: ['a', 'b']         โ”‚
โ”‚                                โ”‚                             โ”‚ }                              โ”‚
โ”‚   	                         โ”‚                             โ”‚                                โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ const T = Type.Partial(        โ”‚ type T = Partial<{          โ”‚ const T = {                    โ”‚
โ”‚    Type.Object({               โ”‚    x: number,               โ”‚   type: 'object',              โ”‚
โ”‚         x: Type.Number(),      โ”‚    y: number                โ”‚   properties: {                โ”‚
โ”‚         y: Type.Number()       | }>                          โ”‚     x: {                       โ”‚
โ”‚    })                          โ”‚                             โ”‚        type: 'number'          โ”‚
โ”‚ )                              โ”‚                             โ”‚     },                         โ”‚
โ”‚                                โ”‚                             โ”‚     y: {                       โ”‚
โ”‚                                โ”‚                             โ”‚        type: 'number'          โ”‚
โ”‚                                โ”‚                             โ”‚     }                          โ”‚
โ”‚                                โ”‚                             โ”‚   }                            โ”‚
โ”‚                                โ”‚                             โ”‚ }                              โ”‚
โ”‚   	                         โ”‚                             โ”‚                                โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ const T = Type.Required(       โ”‚ type T = Required<{         โ”‚ const T = {                    โ”‚
โ”‚    Type.Object({               โ”‚    x?: number,              โ”‚   type: 'object',              โ”‚
โ”‚       x: Type.Optional(        โ”‚    y?: number               โ”‚   properties: {                โ”‚
โ”‚          Type.Number()         | }>                          โ”‚     x: {                       โ”‚
โ”‚       ),                       โ”‚                             โ”‚        type: 'number'          โ”‚
โ”‚       y: Type.Optional(        โ”‚                             โ”‚     },                         โ”‚
โ”‚          Type.Number()         โ”‚                             โ”‚     y: {                       โ”‚
โ”‚       )                        โ”‚                             โ”‚        type: 'number'          โ”‚
โ”‚    })                          โ”‚                             โ”‚     }                          โ”‚
โ”‚ )                              โ”‚                             โ”‚   }                            โ”‚
โ”‚                                โ”‚                             โ”‚   required: ['x', 'y']         โ”‚
โ”‚                                โ”‚                             โ”‚ }                              โ”‚
โ”‚   	                         โ”‚                             โ”‚                                โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ const T = Type.Pick(           โ”‚ type T = Pick<{             โ”‚ const T = {                    โ”‚
โ”‚    Type.Object({               โ”‚    x: number,               โ”‚   type: 'object',              โ”‚
โ”‚       x: Type.Optional(        โ”‚    y: number                โ”‚   properties: {                โ”‚
โ”‚          Type.Number()         | }, 'x'>                     โ”‚     x: {                       โ”‚
โ”‚       ),                       โ”‚                             โ”‚        type: 'number'          โ”‚
โ”‚       y: Type.Optional(        โ”‚                             โ”‚     }                          โ”‚
โ”‚          Type.Number()         โ”‚                             โ”‚   },                           โ”‚
โ”‚       )                        โ”‚                             โ”‚   required: ['x']              โ”‚
โ”‚    })                          โ”‚                             โ”‚ }                              โ”‚
โ”‚ , ['x'])                       โ”‚                             โ”‚                                โ”‚
โ”‚                                โ”‚                             โ”‚                                โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ const T = Type.Omit(           โ”‚ type T = Omit<{             โ”‚ const T = {                    โ”‚
โ”‚    Type.Object({               โ”‚    x: number,               โ”‚   type: 'object',              โ”‚
โ”‚       x: Type.Optional(        โ”‚    y: number,               โ”‚   properties: {                โ”‚
โ”‚          Type.Number()         | }, 'y'>                     โ”‚     x: {                       โ”‚
โ”‚       ),                       โ”‚                             โ”‚        type: 'number'          โ”‚
โ”‚       y: Type.Optional(        โ”‚                             โ”‚     }                          โ”‚
โ”‚          Type.Number()         โ”‚                             โ”‚   },                           โ”‚
โ”‚       )                        โ”‚                             โ”‚   required: ['x']              โ”‚
โ”‚    })                          โ”‚                             โ”‚ }                              โ”‚
โ”‚ , ['y'])                       โ”‚                             โ”‚                                โ”‚
โ”‚                                โ”‚                             โ”‚                                โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Modifiers

TypeBox provides modifiers that can be applied to an objects properties. This allows for optional and readonly to be applied to that property. The following table illustates how they map between TypeScript and JSON Schema.

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ TypeBox                        โ”‚ TypeScript                  โ”‚ JSON Schema                    โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ const T = Type.Object({        โ”‚ type T = {                  โ”‚ const T = {                    โ”‚
โ”‚   name: Type.Optional(         โ”‚    name?: string,           โ”‚   type: 'object',              โ”‚
โ”‚      Type.String(),            โ”‚ }                           โ”‚   properties: {                โ”‚
โ”‚   )	                         โ”‚                             โ”‚      name: {                   โ”‚
โ”‚ })  	                         โ”‚                             โ”‚        type: 'string'          โ”‚
โ”‚   	                         โ”‚                             โ”‚      }                         โ”‚
โ”‚   	                         โ”‚                             โ”‚   }                            โ”‚
โ”‚   	                         โ”‚                             โ”‚ }                              โ”‚
โ”‚   	                         โ”‚                             โ”‚                                โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ const T = Type.Object({        โ”‚ type T = {                  โ”‚ const T = {                    โ”‚
โ”‚   name: Type.Readonly(         โ”‚    readonly name: string,   โ”‚   type: 'object',              โ”‚
โ”‚      Type.String(),            โ”‚ }                           โ”‚   properties: {                โ”‚
โ”‚   )	                         โ”‚                             โ”‚      name: {                   โ”‚
โ”‚ })  	                         โ”‚                             โ”‚        type: 'string'          โ”‚
โ”‚   	                         โ”‚                             โ”‚      }                         โ”‚
โ”‚   	                         โ”‚                             โ”‚   },                           โ”‚
โ”‚                                โ”‚                             โ”‚   required: ['name']           โ”‚
โ”‚   	                         โ”‚                             โ”‚ }                              โ”‚
โ”‚   	                         โ”‚                             โ”‚                                โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ const T = Type.Object({        โ”‚ type T = {                  โ”‚ const T = {                    โ”‚
โ”‚   name: Type.ReadonlyOptional( โ”‚    readonly name?: string,  โ”‚   type: 'object',              โ”‚
โ”‚      Type.String(),            โ”‚ }                           โ”‚   properties: {                โ”‚
โ”‚   )	                         โ”‚                             โ”‚      name: {                   โ”‚
โ”‚ })  	                         โ”‚                             โ”‚        type: 'string'          โ”‚
โ”‚   	                         โ”‚                             โ”‚      }                         โ”‚
โ”‚   	                         โ”‚                             โ”‚   }                            โ”‚
โ”‚                                โ”‚                             โ”‚ }                              โ”‚
โ”‚   	                         โ”‚                             โ”‚                                โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Options

You can pass additional JSON schema properties on the last argument of any given type. The following are some examples.

// string must be an email
const T = Type.String({ format: 'email' })

// number must be a multiple of 2
const T = Type.Number({ multipleOf: 2 })

// array must have at least 5 integer values
const T = Type.Array(Type.Integer(), { minItems: 5 })

Strict

TypeBox includes the properties kind and modifier on each underlying schema. These properties are used to help TypeBox statically resolve the schemas to the appropriate TypeScript type as well as apply the appropriate modifiers to an objects properties (such as optional). These properties are not strictly valid JSON schema so in some cases it may be desirable to omit them. TypeBox provides a Type.Strict() function that will omit these properties if nessasary.

const T = Type.Object({                 // const T = {
    name: Type.Optional(Type.String())  //   kind: Symbol(ObjectKind),
})                                      //   type: 'object',
                                        //   properties: {
                                        //     name: {
                                        //       kind: Symbol(StringKind),
                                        //       type: 'string',
                                        //       modifier: Symbol(OptionalModifier)
                                        //     }
                                        //   }
                                        // }

const U = Type.Strict(T)                // const U = {
                                        //     type: 'object', 
                                        //     properties: { 
                                        //         name: { 
                                        //             type: 'string' 
                                        //         } 
                                        //     } 
                                        // }

Extended Types

In addition to JSON schema types, TypeBox provides several extended types that allow for function and constructor types to be composed. These additional types are not valid JSON Schema and will not validate using typical JSON Schema validation. However, these types can be used to frame JSON schema and describe callable interfaces that may receive JSON validated data. These types are as follows.

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ TypeBox                        โ”‚ TypeScript                  โ”‚ Extended Schema                โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ const T = Type.Constructor([   โ”‚ type T = new (              โ”‚ const T = {                    โ”‚
|    Type.String(),              โ”‚  arg0: string,              โ”‚   type: 'constructor'          โ”‚
โ”‚    Type.Number(),              โ”‚  arg1: number               โ”‚   arguments: [{                โ”‚
โ”‚ ], Type.Boolean())             โ”‚ ) => boolean                โ”‚      type: 'string'            โ”‚
โ”‚                                โ”‚                             โ”‚   }, {                         โ”‚
โ”‚                                โ”‚                             โ”‚      type: 'number'            โ”‚
โ”‚                                โ”‚                             โ”‚   }],                          โ”‚
โ”‚                                โ”‚                             โ”‚   returns: {                   โ”‚
โ”‚                                โ”‚                             โ”‚      type: 'boolean'           โ”‚
โ”‚                                โ”‚                             โ”‚   }                            โ”‚
โ”‚                                โ”‚                             โ”‚ }                              โ”‚
โ”‚   	                         โ”‚                             โ”‚                                โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ const T = Type.Function([      โ”‚ type T = (                  โ”‚ const T = {                    โ”‚
|    Type.String(),              โ”‚  arg0: string,              โ”‚   type : 'function',           โ”‚
โ”‚    Type.Number(),              โ”‚  arg1: number               โ”‚   arguments: [{                โ”‚
โ”‚ ], Type.Boolean())             โ”‚ ) => boolean                โ”‚      type: 'string'            โ”‚
โ”‚                                โ”‚                             โ”‚   }, {                         โ”‚
โ”‚                                โ”‚                             โ”‚      type: 'number'            โ”‚
โ”‚                                โ”‚                             โ”‚   }],                          โ”‚
โ”‚                                โ”‚                             โ”‚   returns: {                   โ”‚
โ”‚                                โ”‚                             โ”‚      type: 'boolean'           โ”‚
โ”‚                                โ”‚                             โ”‚   }                            โ”‚
โ”‚                                โ”‚                             โ”‚ }                              โ”‚
โ”‚   	                         โ”‚                             โ”‚                                โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ const T = Type.Promise(        โ”‚ type T = Promise<string>    โ”‚ const T = {                    โ”‚
|    Type.String()               โ”‚                             โ”‚   type: 'promise',             โ”‚
| )                              โ”‚                             โ”‚   item: {                      โ”‚
โ”‚                                โ”‚                             โ”‚      type: 'string'            โ”‚
โ”‚                                โ”‚                             โ”‚   }                            โ”‚
โ”‚                                โ”‚                             โ”‚ }                              โ”‚
โ”‚   	                         โ”‚                             โ”‚                                โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ const T = Type.Undefined()     โ”‚ type T = undefined          โ”‚ const T = {                    โ”‚
|                                โ”‚                             โ”‚   type: 'undefined'            โ”‚
|                                โ”‚                             โ”‚ }                              โ”‚
โ”‚   	                         โ”‚                             โ”‚                                โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ const T = Type.Void()          โ”‚ type T = void               โ”‚ const T = {                    โ”‚
|                                โ”‚                             โ”‚   type: 'void'                 โ”‚
|                                โ”‚                             โ”‚ }                              โ”‚
โ”‚   	                         โ”‚                             โ”‚                                โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Interfaces

It is possible to create interfaces from TypeBox types. Consider the following code that creates a ControllerInterface type that has a single function createRecord(...). The following is how one would approach this in TypeScript.

interface CreateRecordRequest {
    data: string
}

interface CreateRecordResponse {
    id: string
}

interface ControllerInterface {
    createRecord(record: CreateRecordRequest): Promise<CreateRecordResponse>
}

class Controller implements ControllerInterface {
    async createRecord(record: CreateRecordRequest): Promise<CreateRecordResponse> {
        return { id: '1' }
    }
}

The following is the TypeBox equivalent.

import { Type, Static } from '@sinclair/typebox'

type CreateRecordRequest = Static<typeof CreateRecordRequest>
const CreateRecordRequest = Type.Object({
    data: Type.String()
})
type CreateRecordResponse = Static<typeof CreateRecordResponse>
const CreateRecordResponse = Type.Object({
    id: Type.String()
})

type ControllerInterface = Static<typeof ControllerInterface>
const ControllerInterface = Type.Object({
    createRecord: Type.Function([CreateRecordRequest], Type.Promise(CreateRecordResponse))
})

class Controller implements ControllerInterface {
    async createRecord(record: CreateRecordRequest): Promise<CreateRecordResponse> {
        return { id: '1' }
    }
}

Because TypeBox encodes the type information as JSON schema, it now becomes possible to reflect on the JSON schema to produce sharable metadata that can be used as machine readable documentation.

console.log(JSON.stringify(ControllerInterface, null, 2))
// outputs:
//
// {
//   "type": "object",
//   "properties": {
//     "createRecord": {
//       "type": "function",
//       "arguments": [
//         {
//           "type": "object",
//           "properties": {
//             "data": {
//               "type": "string"
//             }
//           },
//           "required": [
//             "data"
//           ]
//         }
//       ],
//       "returns": {
//         "type": "promise",
//         "item": {
//           "type": "object",
//           "properties": {
//             "id": {
//               "type": "string"
//             }
//           },
//           "required": [
//             "id"
//           ]
//         }
//       }
//     }
//   },
//   "required": [
//     "createRecord"
//   ]
// }

Validation

TypeBox does not provide JSON schema validation out of the box and expects users to select an appropriate JSON schema validation library for their needs. TypeBox schemas should match JSON Schema draft 6 so any library capable of draft 6 should be fine. A good library to use for validation is Ajv. The following example shows setting up Ajv 7 to work with TypeBox.

$ npm install ajv ajv-formats --save
import { Type } from '@sinclair/typebox'
import addFormats from 'ajv-formats'
import Ajv from 'ajv'

// Setup
function setupAjv(): Ajv {
    const ajv = new Ajv()
    ajv.addKeyword('kind')
    ajv.addKeyword('modifier')
    return addFormats(ajv, [
        'date-time', 
        'time', 
        'date', 
        'email',  
        'hostname', 
        'ipv4', 
        'ipv6', 
        'uri', 
        'uri-reference', 
        'uuid',
        'uri-template', 
        'json-pointer', 
        'relative-json-pointer', 
        'regex'
    ])
}

// TypeBox
const User = Type.Object({
    name: Type.String(),
    email: Type.String({ format: 'email' })
})

// Validate
const isValid = setupAjv().validate(User, { 
    name: 'dave', 
    email: '[email protected]' 
})

//
// isValid -> true
//
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].