Typebox
JSON Schema Type Builder with Static Type Resolution for TypeScript
README
Install
npm
- ```bash
- $ npm install @sinclair/typebox --save
- ```
deno
- ```typescript
- import { Static, Type } from 'npm:@sinclair/typebox'
- ```
esm
- ```typescript
- import { Static, Type } from 'https://esm.sh/@sinclair/typebox'
- ```
Usage
- ```typescript
- import { Static, Type } from '@sinclair/typebox'
- const T = Type.Object({ // const T = {
- x: Type.Number(), // type: 'object',
- y: Type.Number(), // required: ['x', 'y', 'z'],
- z: Type.Number() // properties: {
- }) // x: { type: 'number' },
- // y: { type: 'number' },
- // z: { type: 'number' }
- // }
- // }
- type T = Static<typeof T> // type T = {
- // x: number,
- // y: number,
- // z: number
- // }
- ```
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.