Typebox

JSON Schema Type Builder with Static Type Resolution for TypeScript

README

TypeBox

JSON Schema Type Builder with Static Type Resolution for TypeScript

npm version Downloads GitHub CI


Install


npm

  1. ```bash
  2. $ npm install @sinclair/typebox --save
  3. ```

deno

  1. ```typescript
  2. import { Static, Type } from 'npm:@sinclair/typebox'
  3. ```

esm


  1. ```typescript
  2. import { Static, Type } from 'https://esm.sh/@sinclair/typebox'
  3. ```

Usage


  1. ```typescript
  2. import { Static, Type } from '@sinclair/typebox'

  3. const T = Type.Object({    // const T = {
  4.   x: Type.Number(),        //   type: 'object',
  5.   y: Type.Number(),        //   required: ['x', 'y', 'z'],
  6.   z: Type.Number()         //   properties: {
  7. })                         //     x: { type: 'number' },
  8.                            //     y: { type: 'number' },
  9.                            //     z: { type: 'number' }
  10.                            //   }
  11.                            // }

  12. type T = Static<typeof T>  // type T = {
  13.                            //   x: number,
  14.                            //   y: number,
  15.                            //   z: number
  16.                            // }
  17. ```



Overview


TypeBox is a type builder library 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 checking 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.

TypeBox is designed to enable JSON schema to compose with the same flexibility as TypeScript's type system. It can be used either as a simple tool to build up complex schemas or integrated into REST and RPC services to help validate data received over the wire.

License MIT

Contribute


TypeBox is open to community contribution. Please ensure you submit an open issue before submitting your pull request. The TypeBox project preferences open community discussion prior to accepting new features.