<div align='center'>
<h1>TypeBox</h1>
<p>JSON Schema Type Builder with Static Type Resolution for TypeScript</p>
<img src="https://github.com/sinclairzx81/typebox/blob/master/typebox.png?raw=true" />
<br />
<br />
[![npm version](https://badge.fury.io/js/%40sinclair%2Ftypebox.svg)](https://badge.fury.io/js/%40sinclair%2Ftypebox)
[![Downloads](https://img.shields.io/npm/dm/%40sinclair%2Ftypebox.svg)](https://www.npmjs.com/package/%40sinclair%2Ftypebox)
[![GitHub CI](https://github.com/sinclairzx81/typebox/workflows/GitHub%20CI/badge.svg)](https://github.com/sinclairzx81/typebox/actions)
</div>
<a name="Install"></a>
## 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'
```
## Example
```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
// }
```
<a name="Overview"></a>
## Overview
TypeBox is a runtime type builder 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 assertion 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.
This library is designed to enable JSON schema to compose with the same flexibility as TypeScript's type system. It can be used as a simple tool to build up complex schemas or integrated into REST or RPC services to help validate data received over the wire.
License MIT
## Contents
- [Install](#install)
- [Overview](#overview)
- [Usage](#usage)
- [Types](#types)
- [Standard](#types-standard)
- [Extended](#types-extended)
- [Modifiers](#types-modifiers)
- [Options](#types-options)
- [Generics](#types-generics)
- [References](#types-references)
- [Recursive](#types-recursive)
- [Conditional](#types-conditional)
- [Template Literal](#types-template-literal)
- [Guards](#types-guards)
- [Unsafe](#types-unsafe)
- [Strict](#types-strict)
- [Values](#values)
- [Create](#values-create)
- [Clone](#values-clone)
- [Check](#values-check)
- [Convert](#values-convert)
- [Cast](#values-cast)
- [Equal](#values-equal)
- [Hash](#values-hash)
- [Diff](#values-diff)
- [Patch](#values-patch)
- [Errors](#values-errors)
- [Mutate](#values-mutate)
- [Pointer](#values-pointer)
- [TypeCheck](#typecheck)
- [Ajv](#typecheck-ajv)
- [TypeCompiler](#typecheck-typecompiler)
- [TypeSystem](#typesystem)
- [Types](#typesystem-types)
- [Formats](#typesystem-formats)
- [Policies](#typesystem-policies)
- [Benchmark](#benchmark)
- [Compile](#benchmark-compile)
- [Validate](#benchmark-validate)
- [Compression](#benchmark-compression)
- [Contribute](#contribute)
<a name="usage"></a>
## Usage
The following shows general usage.
```typescript
import { Static, Type } from '@sinclair/typebox'
//--------------------------------------------------------------------------------------------
//
// Let's say you have the following type ...
//
//--------------------------------------------------------------------------------------------
type T = {
id: string,
name: string,
timestamp: number
}
//--------------------------------------------------------------------------------------------
//
// ... you can express this type in the following way.
//
//--------------------------------------------------------------------------------------------
const T = Type.Object({ // const T = {
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 T = Static<typeof T> // type T = {
// id: string,
// name: string,
// timestamp: number
// }
//--------------------------------------------------------------------------------------------
//
// ... then use the type both as JSON schema and as a TypeScript type.
//
//--------------------------------------------------------------------------------------------
import { Value } from '@sinclair/typebox/value'
function receive(value: T) { // ... as a Static Type
if(Value.Check(T, value)) { // ... as a JSON Schema
// ok...
}
}
```
<a name='types'></a>
## Types
TypeBox types are JSON schema fragments that can be composed into more complex types. Each fragment is structured such that a JSON schema compliant validator can runtime assert a value the same way TypeScript will statically assert a type. TypeBox provides a set of Standard types which are used create JSON schema compliant schematics as well as an Extended type set used to create schematics for constructs native to JavaScript.
<a name='types-standard'></a>
### Standard Types
The following table lists the Standard TypeBox types. These types are fully compatible with the JSON Schema Draft 6 specification.
```typescript
┌────────────────────────────────┬─────────
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Android原生项目集成最新版本ReactNative (2004个子文件)
.DS_Store 8KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
JSObjectRef.h 41KB
JSCallbackObjectFunctions.h 29KB
JSValue.h 27KB
JSValueRef.h 15KB
JSTypedArray.h 12KB
ObjcRuntimeExtras.h 9KB
JSCallbackObject.h 9KB
JSContext.h 8KB
JSBase.h 8KB
JSContextRef.h 6KB
JSContextRefPrivate.h 6KB
JSExport.h 6KB
JSStringRef.h 6KB
JSContextPrivate.h 5KB
JSRetainPtr.h 5KB
APICast.h 5KB
JSClassRef.h 5KB
JSScriptRefPrivate.h 5KB
JSScript.h 5KB
APICallbackFunction.h 4KB
JSVirtualMachinePrivate.h 4KB
JSWeakObjectMapRefPrivate.h 4KB
OpaqueJSString.h 4KB
JSObjectRefPrivate.h 4KB
JSVirtualMachine.h 3KB
ObjCCallbackFunction.h 3KB
JSCallbackConstructor.h 3KB
JSManagedValue.h 3KB
JSAPIGlobalObject.h 3KB
JSWeakValue.h 3KB
JSCallbackFunction.h 3KB
APIUtils.h 3KB
JSStringRefCF.h 2KB
JSRemoteInspector.h 2KB
JSWeakObjectMapRefInternal.h 2KB
JSValueInternal.h 2KB
JSStringRefBSTR.h 2KB
JSContextInternal.h 2KB
JSContextRefInternal.h 2KB
JSAPIValueWrapper.h 2KB
JSVirtualMachineInternal.h 2KB
JSBasePrivate.h 2KB
JSScriptSourceProvider.h 2KB
JSAPIWrapperObject.h 2KB
JSMarkingConstraintPrivate.h 2KB
JSWrapperMap.h 2KB
JSScriptInternal.h 2KB
JSCTestRunnerUtils.h 2KB
JSHeapFinalizerPrivate.h 2KB
JSWeakPrivate.h 2KB
JSContextRefInspectorSupport.h 2KB
JavaScript.h 2KB
WebKitAvailability.h 2KB
JSBaseInternal.h 2KB
JavaScriptCore.h 2KB
JSManagedValueInternal.h 2KB
JSStringRefPrivate.h 2KB
JSValuePrivate.h 1KB
angular.html 690B
emhermesc.js 1.35MB
flow_parser.js 726KB
source-map.debug.js 266KB
joi-browser.min.js 146KB
js-yaml.js 108KB
source-map.js 104KB
typebox.js 97KB
printer.js 91KB
command.js 68KB
parser.js 56KB
react-shallow-renderer.js 46KB
fixtures.js 46KB
index.js 45KB
index.es.js 44KB
fixtures.js 44KB
loader.js 43KB
js-yaml.min.js 42KB
source-map-consumer.js 40KB
Server.js 38KB
_stream_readable.js 35KB
react-shallow-renderer.js 34KB
argument_parser.js 34KB
react-shallow-renderer.js 34KB
regenerate.js 34KB
errors.js 34KB
index.js 33KB
index.js 33KB
fixtures.js 33KB
keys.js 32KB
_stream_readable.js 31KB
base.js 29KB
string.js 27KB
fixtures.js 27KB
parse.js 27KB
dumper.js 27KB
index.js 27KB
source-map.min.js 26KB
共 2004 条
- 1
- 2
- 3
- 4
- 5
- 6
- 21
资源评论
请叫我农民伯伯
- 粉丝: 9
- 资源: 20
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功