@sinclair/typebox 0.26.8 → 0.26.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/compiler/compiler.js +36 -41
- package/license +22 -22
- package/package.json +50 -47
- package/readme.md +1328 -1328
package/readme.md
CHANGED
|
@@ -1,1328 +1,1328 @@
|
|
|
1
|
-
<div align='center'>
|
|
2
|
-
|
|
3
|
-
<h1>TypeBox</h1>
|
|
4
|
-
|
|
5
|
-
<p>JSON Schema Type Builder with Static Type Resolution for TypeScript</p>
|
|
6
|
-
|
|
7
|
-
<img src="https://github.com/sinclairzx81/typebox/blob/master/typebox.png?raw=true" />
|
|
8
|
-
|
|
9
|
-
<br />
|
|
10
|
-
<br />
|
|
11
|
-
|
|
12
|
-
[](https://badge.fury.io/js/%40sinclair%2Ftypebox)
|
|
13
|
-
[](https://www.npmjs.com/package/%40sinclair%2Ftypebox)
|
|
14
|
-
[](https://github.com/sinclairzx81/typebox/actions)
|
|
15
|
-
|
|
16
|
-
</div>
|
|
17
|
-
|
|
18
|
-
<a name="Install"></a>
|
|
19
|
-
|
|
20
|
-
## Install
|
|
21
|
-
|
|
22
|
-
#### Npm
|
|
23
|
-
```bash
|
|
24
|
-
$ npm install @sinclair/typebox --save
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
#### Deno
|
|
28
|
-
```typescript
|
|
29
|
-
import { Static, Type } from 'npm:@sinclair/typebox'
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
#### Esm
|
|
33
|
-
|
|
34
|
-
```typescript
|
|
35
|
-
import { Static, Type } from 'https://esm.sh/@sinclair/typebox'
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
## Example
|
|
39
|
-
|
|
40
|
-
```typescript
|
|
41
|
-
import { Static, Type } from '@sinclair/typebox'
|
|
42
|
-
|
|
43
|
-
const T = Type.Object({ // const T = {
|
|
44
|
-
x: Type.Number(), // type: 'object',
|
|
45
|
-
y: Type.Number(), // required: ['x', 'y', 'z'],
|
|
46
|
-
z: Type.Number() // properties: {
|
|
47
|
-
}) // x: { type: 'number' },
|
|
48
|
-
// y: { type: 'number' },
|
|
49
|
-
// z: { type: 'number' }
|
|
50
|
-
// }
|
|
51
|
-
// }
|
|
52
|
-
|
|
53
|
-
type T = Static<typeof T> // type T = {
|
|
54
|
-
// x: number,
|
|
55
|
-
// y: number,
|
|
56
|
-
// z: number
|
|
57
|
-
// }
|
|
58
|
-
```
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
<a name="Overview"></a>
|
|
62
|
-
|
|
63
|
-
## Overview
|
|
64
|
-
|
|
65
|
-
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.
|
|
66
|
-
|
|
67
|
-
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.
|
|
68
|
-
|
|
69
|
-
License MIT
|
|
70
|
-
|
|
71
|
-
## Contents
|
|
72
|
-
- [Install](#install)
|
|
73
|
-
- [Overview](#overview)
|
|
74
|
-
- [Usage](#usage)
|
|
75
|
-
- [Types](#types)
|
|
76
|
-
- [Standard](#types-standard)
|
|
77
|
-
- [Extended](#types-extended)
|
|
78
|
-
- [Modifiers](#types-modifiers)
|
|
79
|
-
- [Options](#types-options)
|
|
80
|
-
- [Generics](#types-generics)
|
|
81
|
-
- [References](#types-references)
|
|
82
|
-
- [Recursive](#types-recursive)
|
|
83
|
-
- [Conditional](#types-conditional)
|
|
84
|
-
- [Guards](#types-guards)
|
|
85
|
-
- [Unsafe](#types-unsafe)
|
|
86
|
-
- [Strict](#types-strict)
|
|
87
|
-
- [Values](#values)
|
|
88
|
-
- [Create](#values-create)
|
|
89
|
-
- [Clone](#values-clone)
|
|
90
|
-
- [Check](#values-check)
|
|
91
|
-
- [Convert](#values-convert)
|
|
92
|
-
- [Cast](#values-cast)
|
|
93
|
-
- [Equal](#values-equal)
|
|
94
|
-
- [Hash](#values-hash)
|
|
95
|
-
- [Diff](#values-diff)
|
|
96
|
-
- [Patch](#values-patch)
|
|
97
|
-
- [Errors](#values-errors)
|
|
98
|
-
- [Pointer](#values-pointer)
|
|
99
|
-
- [TypeCheck](#typecheck)
|
|
100
|
-
- [Ajv](#typecheck-ajv)
|
|
101
|
-
- [TypeCompiler](#typecheck-typecompiler)
|
|
102
|
-
- [TypeSystem](#typecheck)
|
|
103
|
-
- [Types](#typesystem-types)
|
|
104
|
-
- [Formats](#typesystem-formats)
|
|
105
|
-
- [Policies](#typesystem-policies)
|
|
106
|
-
- [Benchmark](#benchmark)
|
|
107
|
-
- [Compile](#benchmark-compile)
|
|
108
|
-
- [Validate](#benchmark-validate)
|
|
109
|
-
- [Compression](#benchmark-compression)
|
|
110
|
-
- [Contribute](#contribute)
|
|
111
|
-
|
|
112
|
-
<a name="usage"></a>
|
|
113
|
-
|
|
114
|
-
## Usage
|
|
115
|
-
|
|
116
|
-
The following shows general usage.
|
|
117
|
-
|
|
118
|
-
```typescript
|
|
119
|
-
import { Static, Type } from '@sinclair/typebox'
|
|
120
|
-
|
|
121
|
-
//--------------------------------------------------------------------------------------------
|
|
122
|
-
//
|
|
123
|
-
// Let's say you have the following type ...
|
|
124
|
-
//
|
|
125
|
-
//--------------------------------------------------------------------------------------------
|
|
126
|
-
|
|
127
|
-
type T = {
|
|
128
|
-
id: string,
|
|
129
|
-
name: string,
|
|
130
|
-
timestamp: number
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
//--------------------------------------------------------------------------------------------
|
|
134
|
-
//
|
|
135
|
-
// ... you can express this type in the following way.
|
|
136
|
-
//
|
|
137
|
-
//--------------------------------------------------------------------------------------------
|
|
138
|
-
|
|
139
|
-
const T = Type.Object({ // const T = {
|
|
140
|
-
id: Type.String(), // type: 'object',
|
|
141
|
-
name: Type.String(), // properties: {
|
|
142
|
-
timestamp: Type.Integer() // id: {
|
|
143
|
-
}) // type: 'string'
|
|
144
|
-
// },
|
|
145
|
-
// name: {
|
|
146
|
-
// type: 'string'
|
|
147
|
-
// },
|
|
148
|
-
// timestamp: {
|
|
149
|
-
// type: 'integer'
|
|
150
|
-
// }
|
|
151
|
-
// },
|
|
152
|
-
// required: [
|
|
153
|
-
// 'id',
|
|
154
|
-
// 'name',
|
|
155
|
-
// 'timestamp'
|
|
156
|
-
// ]
|
|
157
|
-
// }
|
|
158
|
-
|
|
159
|
-
//--------------------------------------------------------------------------------------------
|
|
160
|
-
//
|
|
161
|
-
// ... then infer back to the original static type this way.
|
|
162
|
-
//
|
|
163
|
-
//--------------------------------------------------------------------------------------------
|
|
164
|
-
|
|
165
|
-
type T = Static<typeof T> // type T = {
|
|
166
|
-
// id: string,
|
|
167
|
-
// name: string,
|
|
168
|
-
// timestamp: number
|
|
169
|
-
// }
|
|
170
|
-
|
|
171
|
-
//--------------------------------------------------------------------------------------------
|
|
172
|
-
//
|
|
173
|
-
// ... then use the type both as JSON schema and as a TypeScript type.
|
|
174
|
-
//
|
|
175
|
-
//--------------------------------------------------------------------------------------------
|
|
176
|
-
|
|
177
|
-
import { Value } from '@sinclair/typebox/value'
|
|
178
|
-
|
|
179
|
-
function receive(value: T) { // ... as a Static Type
|
|
180
|
-
|
|
181
|
-
if(Value.Check(T, value)) { // ... as a JSON Schema
|
|
182
|
-
|
|
183
|
-
// ok...
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
```
|
|
187
|
-
|
|
188
|
-
<a name='types'></a>
|
|
189
|
-
|
|
190
|
-
## Types
|
|
191
|
-
|
|
192
|
-
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.
|
|
193
|
-
|
|
194
|
-
<a name='types-standard'></a>
|
|
195
|
-
|
|
196
|
-
### Standard Types
|
|
197
|
-
|
|
198
|
-
The following table lists the Standard TypeBox types. These types are fully compatible with the JSON Schema Draft 6 specification.
|
|
199
|
-
|
|
200
|
-
```typescript
|
|
201
|
-
┌────────────────────────────────┬─────────────────────────────┬────────────────────────────────┐
|
|
202
|
-
│ TypeBox │ TypeScript │ JSON Schema │
|
|
203
|
-
│ │ │ │
|
|
204
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
205
|
-
│ const T = Type.Any() │ type T = any │ const T = { } │
|
|
206
|
-
│ │ │ │
|
|
207
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
208
|
-
│ const T = Type.Unknown() │ type T = unknown │ const T = { } │
|
|
209
|
-
│ │ │ │
|
|
210
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
211
|
-
│ const T = Type.String() │ type T = string │ const T = { │
|
|
212
|
-
│ │ │ type: 'string' │
|
|
213
|
-
│ │ │ } │
|
|
214
|
-
│ │ │ │
|
|
215
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
216
|
-
│ const T = Type.Number() │ type T = number │ const T = { │
|
|
217
|
-
│ │ │ type: 'number' │
|
|
218
|
-
│ │ │ } │
|
|
219
|
-
│ │ │ │
|
|
220
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
221
|
-
│ const T = Type.Integer() │ type T = number │ const T = { │
|
|
222
|
-
│ │ │ type: 'integer' │
|
|
223
|
-
│ │ │ } │
|
|
224
|
-
│ │ │ │
|
|
225
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
226
|
-
│ const T = Type.Boolean() │ type T = boolean │ const T = { │
|
|
227
|
-
│ │ │ type: 'boolean' │
|
|
228
|
-
│ │ │ } │
|
|
229
|
-
│ │ │ │
|
|
230
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
231
|
-
│ const T = Type.Null() │ type T = null │ const T = { │
|
|
232
|
-
│ │ │ type: 'null' │
|
|
233
|
-
│ │ │ } │
|
|
234
|
-
│ │ │ │
|
|
235
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
236
|
-
│ const T = Type.Literal(42) │ type T = 42 │ const T = { │
|
|
237
|
-
│ │ │ const: 42, │
|
|
238
|
-
│ │ │ type: 'number' │
|
|
239
|
-
│ │ │ } │
|
|
240
|
-
│ │ │ │
|
|
241
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
242
|
-
│ const T = Type.Array( │ type T = number[] │ const T = { │
|
|
243
|
-
│ Type.Number() │ │ type: 'array', │
|
|
244
|
-
│ ) │ │ items: { │
|
|
245
|
-
│ │ │ type: 'number' │
|
|
246
|
-
│ │ │ } │
|
|
247
|
-
│ │ │ } │
|
|
248
|
-
│ │ │ │
|
|
249
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
250
|
-
│ const T = Type.Object({ │ type T = { │ const T = { │
|
|
251
|
-
│ x: Type.Number(), │ x: number, │ type: 'object', │
|
|
252
|
-
│ y: Type.Number() │ y: number │ required: ['x', 'y'], │
|
|
253
|
-
│ }) │ } │ properties: { │
|
|
254
|
-
│ │ │ x: { │
|
|
255
|
-
│ │ │ type: 'number' │
|
|
256
|
-
│ │ │ }, { │
|
|
257
|
-
│ │ │ type: 'number' │
|
|
258
|
-
│ │ │ } │
|
|
259
|
-
│ │ │ } │
|
|
260
|
-
│ │ │ } │
|
|
261
|
-
│ │ │ │
|
|
262
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
263
|
-
│ const T = Type.Tuple([ │ type T = [number, number] │ const T = { │
|
|
264
|
-
│ Type.Number(), │ │ type: 'array', │
|
|
265
|
-
│ Type.Number() │ │ items: [{ │
|
|
266
|
-
│ ]) │ │ type: 'number' │
|
|
267
|
-
│ │ │ }, { │
|
|
268
|
-
│ │ │ type: 'number' │
|
|
269
|
-
│ │ │ }], │
|
|
270
|
-
│ │ │ additionalItems: false, │
|
|
271
|
-
│ │ │ minItems: 2, │
|
|
272
|
-
│ │ │ maxItems: 2 │
|
|
273
|
-
│ │ │ } │
|
|
274
|
-
│ │ │ │
|
|
275
|
-
│ │ │ │
|
|
276
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
277
|
-
│ enum Foo { │ enum Foo { │ const T = { │
|
|
278
|
-
│ A, │ A, │ anyOf: [{ │
|
|
279
|
-
│ B │ B │ type: 'number', │
|
|
280
|
-
│ } │ } │ const: 0 │
|
|
281
|
-
│ │ │ }, { │
|
|
282
|
-
│ const T = Type.Enum(Foo) │ type T = Foo │ type: 'number', │
|
|
283
|
-
│ │ │ const: 1 │
|
|
284
|
-
│ │ │ }] │
|
|
285
|
-
│ │ │ } │
|
|
286
|
-
│ │ │ │
|
|
287
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
288
|
-
│ const T = Type.KeyOf( │ type T = keyof { │ const T = { │
|
|
289
|
-
│ Type.Object({ │ x: number, │ anyOf: [{ │
|
|
290
|
-
│ x: Type.Number(), │ y: number │ type: 'string', │
|
|
291
|
-
│ y: Type.Number() │ } │ const: 'x' │
|
|
292
|
-
│ }) │ │ }, { │
|
|
293
|
-
│ ) │ │ type: 'string', │
|
|
294
|
-
│ │ │ const: 'y' │
|
|
295
|
-
│ │ │ }] │
|
|
296
|
-
│ │ │ } │
|
|
297
|
-
│ │ │ │
|
|
298
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
299
|
-
│ const T = Type.Union([ │ type T = string | number │ const T = { │
|
|
300
|
-
│ Type.String(), │ │ anyOf: [{ │
|
|
301
|
-
│ Type.Number() │ │ type: 'string' │
|
|
302
|
-
│ ]) │ │ }, { │
|
|
303
|
-
│ │ │ type: 'number' │
|
|
304
|
-
│ │ │ }] │
|
|
305
|
-
│ │ │ } │
|
|
306
|
-
│ │ │ │
|
|
307
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
308
|
-
│ const T = Type.Intersect([ │ type T = { │ const T = { │
|
|
309
|
-
│ Type.Object({ │ x: number │ allOf: [{ │
|
|
310
|
-
│ x: Type.Number() │ } & { │ type: 'object', │
|
|
311
|
-
│ }), │ y: number │ required: ['x'], │
|
|
312
|
-
│ Type.Object({ │ } │ properties: { │
|
|
313
|
-
│ y: Type.Number() │ │ x: { │
|
|
314
|
-
│ ]) │ │ type: 'number' │
|
|
315
|
-
│ ]) │ │ } │
|
|
316
|
-
│ │ │ } │
|
|
317
|
-
│ │ │ }, { │
|
|
318
|
-
│ │ │ type: 'object', |
|
|
319
|
-
│ │ │ required: ['y'], │
|
|
320
|
-
│ │ │ properties: { │
|
|
321
|
-
│ │ │ y: { │
|
|
322
|
-
│ │ │ type: 'number' │
|
|
323
|
-
│ │ │ } │
|
|
324
|
-
│ │ │ }] │
|
|
325
|
-
│ │ │ } │
|
|
326
|
-
│ │ │ │
|
|
327
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
328
|
-
│ const T = Type.Composite([ │ type I = { │ const T = { │
|
|
329
|
-
│ Type.Object({ │ x: number │ type: 'object', │
|
|
330
|
-
│ x: Type.Number() │ } & { │ required: ['x', 'y'], │
|
|
331
|
-
│ }), │ y: number │ properties: { │
|
|
332
|
-
│ Type.Object({ │ } │ x: { │
|
|
333
|
-
│ y: Type.Number() │ │ type: 'number' │
|
|
334
|
-
│ }) │ type T = { │ }, │
|
|
335
|
-
│ ]) │ [K in keyof I]: I[K] │ y: { │
|
|
336
|
-
│ │ } │ type: 'number' │
|
|
337
|
-
│ │ │ } │
|
|
338
|
-
│ │ │ } │
|
|
339
|
-
│ │ │ } │
|
|
340
|
-
│ │ │ │
|
|
341
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
342
|
-
│ const T = Type.Never() │ type T = never │ const T = { │
|
|
343
|
-
│ │ │ not: {} │
|
|
344
|
-
│ │ │ } │
|
|
345
|
-
│ │ │ │
|
|
346
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
347
|
-
│ const T = Type.Not( | type T = string │ const T = { │
|
|
348
|
-
| Type.Union([ │ │ allOf: [{ │
|
|
349
|
-
│ Type.Literal('x'), │ │ not: { │
|
|
350
|
-
│ Type.Literal('y'), │ │ anyOf: [ │
|
|
351
|
-
│ Type.Literal('z') │ │ { const: 'x' }, │
|
|
352
|
-
│ ]), │ │ { const: 'y' }, │
|
|
353
|
-
│ Type.String() │ │ { const: 'z' } │
|
|
354
|
-
│ ) │ │ ] │
|
|
355
|
-
│ │ │ } │
|
|
356
|
-
│ │ │ }, { │
|
|
357
|
-
│ │ │ type: 'string' │
|
|
358
|
-
│ │ │ }] │
|
|
359
|
-
│ │ │ } │
|
|
360
|
-
│ │ │ │
|
|
361
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
362
|
-
│ const T = Type.Extends( │ type T = │ const T = { │
|
|
363
|
-
│ Type.String(), │ string extends number │ const: false, │
|
|
364
|
-
│ Type.Number(), │ true : false │ type: 'boolean' │
|
|
365
|
-
│ Type.Literal(true), │ │ } │
|
|
366
|
-
│ Type.Literal(false) │ │ │
|
|
367
|
-
│ ) │ │ │
|
|
368
|
-
│ │ │ │
|
|
369
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
370
|
-
│ const T = Type.Extract( │ type T = Extract< │ const T = { │
|
|
371
|
-
│ Type.Union([ │ string | number, │ type: 'string' │
|
|
372
|
-
│ Type.String(), │ string │ } │
|
|
373
|
-
│ Type.Number(), │ > │ │
|
|
374
|
-
│ ]), │ │ │
|
|
375
|
-
│ Type.String() │ │ │
|
|
376
|
-
│ ) │ │ │
|
|
377
|
-
│ │ │ │
|
|
378
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
379
|
-
│ const T = Type.Exclude( │ type T = Exclude< │ const T = { │
|
|
380
|
-
│ Type.Union([ │ string | number, │ type: 'number' │
|
|
381
|
-
│ Type.String(), │ string │ } │
|
|
382
|
-
│ Type.Number(), │ > │ │
|
|
383
|
-
│ ]), │ │ │
|
|
384
|
-
│ Type.String() │ │ │
|
|
385
|
-
│ ) │ │ │
|
|
386
|
-
│ │ │ │
|
|
387
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
388
|
-
│ const T = Type.Record( │ type T = Record< │ const T = { │
|
|
389
|
-
│ Type.String(), │ string, │ type: 'object', │
|
|
390
|
-
│ Type.Number() │ number, │ patternProperties: { │
|
|
391
|
-
│ ) │ > │ '^.*$': { │
|
|
392
|
-
│ │ │ type: 'number' │
|
|
393
|
-
│ │ │ } │
|
|
394
|
-
│ │ │ } │
|
|
395
|
-
│ │ │ } │
|
|
396
|
-
│ │ │ │
|
|
397
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
398
|
-
│ const T = Type.Partial( │ type T = Partial<{ │ const T = { │
|
|
399
|
-
│ Type.Object({ │ x: number, │ type: 'object', │
|
|
400
|
-
│ x: Type.Number(), │ y: number │ properties: { │
|
|
401
|
-
│ y: Type.Number() | }> │ x: { │
|
|
402
|
-
│ }) │ │ type: 'number' │
|
|
403
|
-
│ ) │ │ }, │
|
|
404
|
-
│ │ │ y: { │
|
|
405
|
-
│ │ │ type: 'number' │
|
|
406
|
-
│ │ │ } │
|
|
407
|
-
│ │ │ } │
|
|
408
|
-
│ │ │ } │
|
|
409
|
-
│ │ │ │
|
|
410
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
411
|
-
│ const T = Type.Required( │ type T = Required<{ │ const T = { │
|
|
412
|
-
│ Type.Object({ │ x?: number, │ type: 'object', │
|
|
413
|
-
│ x: Type.Optional( │ y?: number │ required: ['x', 'y'], │
|
|
414
|
-
│ Type.Number() | }> │ properties: { │
|
|
415
|
-
│ ), │ │ x: { │
|
|
416
|
-
│ y: Type.Optional( │ │ type: 'number' │
|
|
417
|
-
│ Type.Number() │ │ }, │
|
|
418
|
-
│ ) │ │ y: { │
|
|
419
|
-
│ }) │ │ type: 'number' │
|
|
420
|
-
│ ) │ │ } │
|
|
421
|
-
│ │ │ } │
|
|
422
|
-
│ │ │ } │
|
|
423
|
-
│ │ │ │
|
|
424
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
425
|
-
│ const T = Type.Pick( │ type T = Pick<{ │ const T = { │
|
|
426
|
-
│ Type.Object({ │ x: number, │ type: 'object', │
|
|
427
|
-
│ x: Type.Number(), │ y: number │ properties: { │
|
|
428
|
-
│ y: Type.Number() | }, 'x'> │ x: { │
|
|
429
|
-
│ }), ['x'] │ │ type: 'number' │
|
|
430
|
-
│ ) │ │ } │
|
|
431
|
-
│ │ │ }, │
|
|
432
|
-
│ │ │ required: ['x'] │
|
|
433
|
-
│ │ │ } │
|
|
434
|
-
│ │ │ │
|
|
435
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
436
|
-
│ const T = Type.Omit( │ type T = Omit<{ │ const T = { │
|
|
437
|
-
│ Type.Object({ │ x: number, │ type: 'object', │
|
|
438
|
-
│ x: Type.Number(), │ y: number │ properties: { │
|
|
439
|
-
│ y: Type.Number() | }, 'x'> │ y: { │
|
|
440
|
-
│ }), ['x'] │ │ type: 'number' │
|
|
441
|
-
│ ) │ │ } │
|
|
442
|
-
│ │ │ }, │
|
|
443
|
-
│ │ │ required: ['y'] │
|
|
444
|
-
│ │ │ } │
|
|
445
|
-
│ │ │ │
|
|
446
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
447
|
-
│ const T = Type.Object({ │ type T = { │ const R = { │
|
|
448
|
-
│ x: Type.Number(), │ x: number, │ $ref: 'T' │
|
|
449
|
-
│ y: Type.Number() │ y: number │ } │
|
|
450
|
-
│ }, { $id: 'T' }) | } │ │
|
|
451
|
-
│ │ │ │
|
|
452
|
-
│ const R = Type.Ref(T) │ type R = T │ │
|
|
453
|
-
│ │ │ │
|
|
454
|
-
│ │ │ │
|
|
455
|
-
│ │ │ │
|
|
456
|
-
│ │ │ │
|
|
457
|
-
└────────────────────────────────┴─────────────────────────────┴────────────────────────────────┘
|
|
458
|
-
```
|
|
459
|
-
|
|
460
|
-
<a name='types-extended'></a>
|
|
461
|
-
|
|
462
|
-
### Extended Types
|
|
463
|
-
|
|
464
|
-
TypeBox provides several extended types that can be used to produce schematics for common JavaScript constructs. These types can not be used with standard JSON schema validators; but are useful to help frame schematics for RPC interfaces that may receive JSON validated data. Extended types are prefixed with the `[Extended]` doc comment for convenience. The following table lists the supported types.
|
|
465
|
-
|
|
466
|
-
```typescript
|
|
467
|
-
┌────────────────────────────────┬─────────────────────────────┬────────────────────────────────┐
|
|
468
|
-
│ TypeBox │ TypeScript │ Extended Schema │
|
|
469
|
-
│ │ │ │
|
|
470
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
471
|
-
│ const T = Type.Constructor([ │ type T = new ( │ const T = { │
|
|
472
|
-
│ Type.String(), │ arg0: string, │ type: 'object', │
|
|
473
|
-
│ Type.Number() │ arg1: number │ instanceOf: 'Constructor', │
|
|
474
|
-
│ ], Type.Boolean()) │ ) => boolean │ parameters: [{ │
|
|
475
|
-
│ │ │ type: 'string' │
|
|
476
|
-
│ │ │ }, { │
|
|
477
|
-
│ │ │ type: 'number' │
|
|
478
|
-
│ │ │ }], │
|
|
479
|
-
│ │ │ return: { │
|
|
480
|
-
│ │ │ type: 'boolean' │
|
|
481
|
-
│ │ │ } │
|
|
482
|
-
│ │ │ } │
|
|
483
|
-
│ │ │ │
|
|
484
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
485
|
-
│ const T = Type.Function([ │ type T = ( │ const T = { │
|
|
486
|
-
| Type.String(), │ arg0: string, │ type : 'object', │
|
|
487
|
-
│ Type.Number() │ arg1: number │ instanceOf: 'Function', │
|
|
488
|
-
│ ], Type.Boolean()) │ ) => boolean │ parameters: [{ │
|
|
489
|
-
│ │ │ type: 'string' │
|
|
490
|
-
│ │ │ }, { │
|
|
491
|
-
│ │ │ type: 'number' │
|
|
492
|
-
│ │ │ }], │
|
|
493
|
-
│ │ │ return: { │
|
|
494
|
-
│ │ │ type: 'boolean' │
|
|
495
|
-
│ │ │ } │
|
|
496
|
-
│ │ │ } │
|
|
497
|
-
│ │ │ │
|
|
498
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
499
|
-
│ const T = Type.Promise( │ type T = Promise<string> │ const T = { │
|
|
500
|
-
│ Type.String() │ │ type: 'object', │
|
|
501
|
-
│ ) │ │ instanceOf: 'Promise', │
|
|
502
|
-
│ │ │ item: { │
|
|
503
|
-
│ │ │ type: 'string' │
|
|
504
|
-
│ │ │ } │
|
|
505
|
-
│ │ │ } │
|
|
506
|
-
│ │ │ │
|
|
507
|
-
│ │ │ │
|
|
508
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
509
|
-
│ const T = Type.Uint8Array() │ type T = Uint8Array │ const T = { │
|
|
510
|
-
│ │ │ type: 'object', │
|
|
511
|
-
│ │ │ instanceOf: 'Uint8Array' │
|
|
512
|
-
│ │ │ } │
|
|
513
|
-
│ │ │ │
|
|
514
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
515
|
-
│ const T = Type.Date() │ type T = Date │ const T = { │
|
|
516
|
-
│ │ │ type: 'object', │
|
|
517
|
-
│ │ │ instanceOf: 'Date' │
|
|
518
|
-
│ │ │ } │
|
|
519
|
-
│ │ │ │
|
|
520
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
521
|
-
│ const T = Type.Undefined() │ type T = undefined │ const T = { │
|
|
522
|
-
│ │ │ type: 'null', │
|
|
523
|
-
│ │ │ typeOf: 'Undefined' │
|
|
524
|
-
│ │ │ } │
|
|
525
|
-
│ │ │ │
|
|
526
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
527
|
-
│ const T = Type.RegEx(/foo/) │ type T = string │ const T = { │
|
|
528
|
-
│ │ │ type: 'string', │
|
|
529
|
-
│ │ │ pattern: 'foo' │
|
|
530
|
-
│ │ │ } │
|
|
531
|
-
│ │ │ │
|
|
532
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
533
|
-
│ const T = Type.Symbol() │ type T = symbol │ const T = { │
|
|
534
|
-
│ │ │ type: 'null', │
|
|
535
|
-
│ │ │ typeOf: 'Symbol' │
|
|
536
|
-
│ │ │ } │
|
|
537
|
-
│ │ │ │
|
|
538
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
539
|
-
│ const T = Type.BigInt() │ type T = bigint │ const T = { │
|
|
540
|
-
│ │ │ type: 'null', │
|
|
541
|
-
│ │ │ typeOf: 'BigInt' │
|
|
542
|
-
│ │ │ } │
|
|
543
|
-
│ │ │ │
|
|
544
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
545
|
-
│ const T = Type.Void() │ type T = void │ const T = { │
|
|
546
|
-
│ │ │ type: 'null' │
|
|
547
|
-
│ │ │ typeOf: 'Void' │
|
|
548
|
-
│ │ │ } │
|
|
549
|
-
│ │ │ │
|
|
550
|
-
└────────────────────────────────┴─────────────────────────────┴────────────────────────────────┘
|
|
551
|
-
```
|
|
552
|
-
|
|
553
|
-
<a name='types-modifiers'></a>
|
|
554
|
-
|
|
555
|
-
### Modifiers
|
|
556
|
-
|
|
557
|
-
TypeBox provides modifiers that allow schema properties to be statically inferred as `readonly` or `optional`. The following table shows the supported modifiers and how they map between TypeScript and JSON Schema.
|
|
558
|
-
|
|
559
|
-
```typescript
|
|
560
|
-
┌────────────────────────────────┬─────────────────────────────┬────────────────────────────────┐
|
|
561
|
-
│ TypeBox │ TypeScript │ JSON Schema │
|
|
562
|
-
│ │ │ │
|
|
563
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
564
|
-
│ const T = Type.Object({ │ type T = { │ const T = { │
|
|
565
|
-
│ name: Type.Optional( │ name?: string │ type: 'object', │
|
|
566
|
-
│ Type.String() │ } │ properties: { │
|
|
567
|
-
│ ) │ │ name: { │
|
|
568
|
-
│ }) │ │ type: 'string' │
|
|
569
|
-
│ │ │ } │
|
|
570
|
-
│ │ │ } │
|
|
571
|
-
│ │ │ } │
|
|
572
|
-
│ │ │ │
|
|
573
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
574
|
-
│ const T = Type.Object({ │ type T = { │ const T = { │
|
|
575
|
-
│ name: Type.Readonly( │ readonly name: string │ type: 'object', │
|
|
576
|
-
│ Type.String() │ } │ properties: { │
|
|
577
|
-
│ ) │ │ name: { │
|
|
578
|
-
│ }) │ │ type: 'string' │
|
|
579
|
-
│ │ │ } │
|
|
580
|
-
│ │ │ }, │
|
|
581
|
-
│ │ │ required: ['name'] │
|
|
582
|
-
│ │ │ } │
|
|
583
|
-
│ │ │ │
|
|
584
|
-
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
585
|
-
│ const T = Type.Object({ │ type T = { │ const T = { │
|
|
586
|
-
│ name: Type.ReadonlyOptional( │ readonly name?: string │ type: 'object', │
|
|
587
|
-
│ Type.String() │ } │ properties: { │
|
|
588
|
-
│ ) │ │ name: { │
|
|
589
|
-
│ }) │ │ type: 'string' │
|
|
590
|
-
│ │ │ } │
|
|
591
|
-
│ │ │ } │
|
|
592
|
-
│ │ │ } │
|
|
593
|
-
│ │ │ │
|
|
594
|
-
└────────────────────────────────┴─────────────────────────────┴────────────────────────────────┘
|
|
595
|
-
```
|
|
596
|
-
|
|
597
|
-
<a name='types-options'></a>
|
|
598
|
-
|
|
599
|
-
### Options
|
|
600
|
-
|
|
601
|
-
You can pass JSON Schema options on the last argument of any type. Option hints specific to each type are provided for convenience.
|
|
602
|
-
|
|
603
|
-
```typescript
|
|
604
|
-
// String must be an email
|
|
605
|
-
const T = Type.String({ // const T = {
|
|
606
|
-
format: 'email' // type: 'string',
|
|
607
|
-
}) // format: 'email'
|
|
608
|
-
// }
|
|
609
|
-
|
|
610
|
-
// Mumber must be a multiple of 2
|
|
611
|
-
const T = Type.Number({ // const T = {
|
|
612
|
-
multipleOf: 2 // type: 'number',
|
|
613
|
-
}) // multipleOf: 2
|
|
614
|
-
// }
|
|
615
|
-
|
|
616
|
-
// Array must have at least 5 integer values
|
|
617
|
-
const T = Type.Array(Type.Integer(), { // const T = {
|
|
618
|
-
minItems: 5 // type: 'array',
|
|
619
|
-
}) // minItems: 5,
|
|
620
|
-
// items: {
|
|
621
|
-
// type: 'integer'
|
|
622
|
-
// }
|
|
623
|
-
// }
|
|
624
|
-
|
|
625
|
-
```
|
|
626
|
-
|
|
627
|
-
<a name='types-generics'></a>
|
|
628
|
-
|
|
629
|
-
### Generic Types
|
|
630
|
-
|
|
631
|
-
Generic types can be created with generic functions constrained to type `TSchema`. The following creates a generic `Vector<T>` type.
|
|
632
|
-
|
|
633
|
-
```typescript
|
|
634
|
-
import { Type, Static, TSchema } from '@sinclair/typebox'
|
|
635
|
-
|
|
636
|
-
const Vector = <T extends TSchema>(t: T) => Type.Object({ x: t, y: t, z: t })
|
|
637
|
-
|
|
638
|
-
const NumberVector = Vector(Type.Number()) // const NumberVector = {
|
|
639
|
-
// type: 'object',
|
|
640
|
-
// required: ['x', 'y', 'z'],
|
|
641
|
-
// properties: {
|
|
642
|
-
// x: { type: 'number' },
|
|
643
|
-
// y: { type: 'number' },
|
|
644
|
-
// z: { type: 'number' }
|
|
645
|
-
// }
|
|
646
|
-
// }
|
|
647
|
-
|
|
648
|
-
type NumberVector = Static<typeof NumberVector> // type NumberVector = {
|
|
649
|
-
// x: number,
|
|
650
|
-
// y: number,
|
|
651
|
-
// z: number
|
|
652
|
-
// }
|
|
653
|
-
|
|
654
|
-
const BooleanVector = Vector(Type.Boolean()) // const BooleanVector = {
|
|
655
|
-
// type: 'object',
|
|
656
|
-
// required: ['x', 'y', 'z'],
|
|
657
|
-
// properties: {
|
|
658
|
-
// x: { type: 'boolean' },
|
|
659
|
-
// y: { type: 'boolean' },
|
|
660
|
-
// z: { type: 'boolean' }
|
|
661
|
-
// }
|
|
662
|
-
// }
|
|
663
|
-
|
|
664
|
-
type BooleanVector = Static<typeof BooleanVector> // type BooleanVector = {
|
|
665
|
-
// x: boolean,
|
|
666
|
-
// y: boolean,
|
|
667
|
-
// z: boolean
|
|
668
|
-
// }
|
|
669
|
-
```
|
|
670
|
-
|
|
671
|
-
The following creates a generic `Nullable<T>` type.
|
|
672
|
-
|
|
673
|
-
```typescript
|
|
674
|
-
const Nullable = <T extends TSchema>(schema: T) => Type.Union([schema, Type.Null()])
|
|
675
|
-
|
|
676
|
-
const T = Nullable(Type.String()) // const T = {
|
|
677
|
-
// anyOf: [
|
|
678
|
-
// { type: 'string' },
|
|
679
|
-
// { type: 'null' }
|
|
680
|
-
// ]
|
|
681
|
-
// }
|
|
682
|
-
|
|
683
|
-
type T = Static<typeof T> // type T = string | null
|
|
684
|
-
```
|
|
685
|
-
|
|
686
|
-
<a name='types-references'></a>
|
|
687
|
-
|
|
688
|
-
### Reference Types
|
|
689
|
-
|
|
690
|
-
Reference types are supported with `Type.Ref(...)`. The target type must specify a valid `$id`.
|
|
691
|
-
|
|
692
|
-
```typescript
|
|
693
|
-
const T = Type.String({ $id: 'T' }) // const T = {
|
|
694
|
-
// $id: 'T',
|
|
695
|
-
// type: 'string'
|
|
696
|
-
// }
|
|
697
|
-
|
|
698
|
-
const R = Type.Ref(T) // const R = {
|
|
699
|
-
// $ref: 'T'
|
|
700
|
-
// }
|
|
701
|
-
```
|
|
702
|
-
|
|
703
|
-
<a name='types-recursive'></a>
|
|
704
|
-
|
|
705
|
-
### Recursive Types
|
|
706
|
-
|
|
707
|
-
Recursive types are supported with `Type.Recursive(...)`.
|
|
708
|
-
|
|
709
|
-
```typescript
|
|
710
|
-
const Node = Type.Recursive(Node => Type.Object({ // const Node = {
|
|
711
|
-
id: Type.String(), // $id: 'Node',
|
|
712
|
-
nodes: Type.Array(Node) // type: 'object',
|
|
713
|
-
}), { $id: 'Node' }) // properties: {
|
|
714
|
-
// id: {
|
|
715
|
-
// type: 'string'
|
|
716
|
-
// },
|
|
717
|
-
// nodes: {
|
|
718
|
-
// type: 'array',
|
|
719
|
-
// items: {
|
|
720
|
-
// $ref: 'Node'
|
|
721
|
-
// }
|
|
722
|
-
// }
|
|
723
|
-
// },
|
|
724
|
-
// required: [
|
|
725
|
-
// 'id',
|
|
726
|
-
// 'nodes'
|
|
727
|
-
// ]
|
|
728
|
-
// }
|
|
729
|
-
|
|
730
|
-
type Node = Static<typeof Node> // type Node = {
|
|
731
|
-
// id: string
|
|
732
|
-
// nodes: Node[]
|
|
733
|
-
// }
|
|
734
|
-
|
|
735
|
-
function test(node: Node) {
|
|
736
|
-
const id = node.nodes[0].nodes[0].id // id is string
|
|
737
|
-
}
|
|
738
|
-
```
|
|
739
|
-
|
|
740
|
-
<a name='types-conditional'></a>
|
|
741
|
-
|
|
742
|
-
### Conditional Types
|
|
743
|
-
|
|
744
|
-
Conditional types are supported with `Extends`, `Exclude` and `Extract`.
|
|
745
|
-
|
|
746
|
-
**TypeScript**
|
|
747
|
-
|
|
748
|
-
```typescript
|
|
749
|
-
type T0 = string extends number ? true : false
|
|
750
|
-
// ^ false
|
|
751
|
-
type T1 = Extract<string | number, number>
|
|
752
|
-
// ^ number
|
|
753
|
-
type T2 = Exclude<string | number, number>
|
|
754
|
-
// ^ string
|
|
755
|
-
```
|
|
756
|
-
**TypeBox**
|
|
757
|
-
```typescript
|
|
758
|
-
const T0 = Type.Extends(Type.String(), Type.Number(), Type.Literal(true), Type.Literal(false))
|
|
759
|
-
// ^ TLiteral<false>
|
|
760
|
-
const T1 = Type.Extract(Type.Union([Type.String(), Type.Number()]), Type.Number())
|
|
761
|
-
// ^ TNumber
|
|
762
|
-
const T2 = Type.Exclude(Type.Union([Type.String(), Type.Number()]), Type.Number())
|
|
763
|
-
// ^ TString<string>
|
|
764
|
-
```
|
|
765
|
-
|
|
766
|
-
<a name='types-unsafe'></a>
|
|
767
|
-
|
|
768
|
-
### Unsafe
|
|
769
|
-
|
|
770
|
-
Use `Type.Unsafe(...)` to create custom schematics with user defined inference rules.
|
|
771
|
-
|
|
772
|
-
```typescript
|
|
773
|
-
const T = Type.Unsafe<string>({ type: 'number' }) // const T = {
|
|
774
|
-
// type: 'number'
|
|
775
|
-
// }
|
|
776
|
-
|
|
777
|
-
type T = Static<typeof T> // type T = string
|
|
778
|
-
```
|
|
779
|
-
|
|
780
|
-
The `Type.Unsafe(...)` type can be useful to express specific OpenAPI schema representations.
|
|
781
|
-
|
|
782
|
-
```typescript
|
|
783
|
-
import { Type, Static, TSchema } from '@sinclair/typebox'
|
|
784
|
-
|
|
785
|
-
// Nullable<T>
|
|
786
|
-
|
|
787
|
-
function Nullable<T extends TSchema>(schema: T) {
|
|
788
|
-
return Type.Unsafe<Static<T> | null>({ ...schema, nullable: true })
|
|
789
|
-
}
|
|
790
|
-
|
|
791
|
-
const T = Nullable(Type.String()) // const T = {
|
|
792
|
-
// type: 'string',
|
|
793
|
-
// nullable: true
|
|
794
|
-
// }
|
|
795
|
-
|
|
796
|
-
type T = Static<typeof T> // type T = string | null
|
|
797
|
-
|
|
798
|
-
// StringEnum<string[]>
|
|
799
|
-
|
|
800
|
-
function StringEnum<T extends string[]>(values: [...T]) {
|
|
801
|
-
return Type.Unsafe<T[number]>({ type: 'string', enum: values })
|
|
802
|
-
}
|
|
803
|
-
|
|
804
|
-
const T = StringEnum(['A', 'B', 'C']) // const T = {
|
|
805
|
-
// enum: ['A', 'B', 'C']
|
|
806
|
-
// }
|
|
807
|
-
|
|
808
|
-
type T = Static<typeof T> // type T = 'A' | 'B' | 'C'
|
|
809
|
-
```
|
|
810
|
-
|
|
811
|
-
<a name='types-guards'></a>
|
|
812
|
-
|
|
813
|
-
### Guards
|
|
814
|
-
|
|
815
|
-
TypeBox provides a `TypeGuard` module that can be used for reflection and asserting values as types.
|
|
816
|
-
|
|
817
|
-
```typescript
|
|
818
|
-
import { Type, TypeGuard } from '@sinclair/typebox'
|
|
819
|
-
|
|
820
|
-
const T = Type.String()
|
|
821
|
-
|
|
822
|
-
if(TypeGuard.TString(T)) {
|
|
823
|
-
|
|
824
|
-
// T is TString
|
|
825
|
-
}
|
|
826
|
-
```
|
|
827
|
-
|
|
828
|
-
<a name='types-strict'></a>
|
|
829
|
-
|
|
830
|
-
### Strict
|
|
831
|
-
|
|
832
|
-
TypeBox schemas contain the `Kind` and `Modifier` symbol properties. These properties are used for type composition and reflection. 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 necessary.
|
|
833
|
-
|
|
834
|
-
```typescript
|
|
835
|
-
const T = Type.Object({ // const T = {
|
|
836
|
-
name: Type.Optional(Type.String()) // [Kind]: 'Object',
|
|
837
|
-
}) // type: 'object',
|
|
838
|
-
// properties: {
|
|
839
|
-
// name: {
|
|
840
|
-
// [Kind]: 'String',
|
|
841
|
-
// type: 'string',
|
|
842
|
-
// [Modifier]: 'Optional'
|
|
843
|
-
// }
|
|
844
|
-
// }
|
|
845
|
-
// }
|
|
846
|
-
|
|
847
|
-
const U = Type.Strict(T) // const U = {
|
|
848
|
-
// type: 'object',
|
|
849
|
-
// properties: {
|
|
850
|
-
// name: {
|
|
851
|
-
// type: 'string'
|
|
852
|
-
// }
|
|
853
|
-
// }
|
|
854
|
-
// }
|
|
855
|
-
```
|
|
856
|
-
|
|
857
|
-
<a name='values'></a>
|
|
858
|
-
|
|
859
|
-
## Values
|
|
860
|
-
|
|
861
|
-
TypeBox provides an optional utility module that can be used to perform common operations on JavaScript values. This module includes functionality to create, check and cast values from types as well as check equality, clone, diff and patch JavaScript values. This module is provided via optional import.
|
|
862
|
-
|
|
863
|
-
```typescript
|
|
864
|
-
import { Value } from '@sinclair/typebox/value'
|
|
865
|
-
```
|
|
866
|
-
|
|
867
|
-
<a name='values-create'></a>
|
|
868
|
-
|
|
869
|
-
### Create
|
|
870
|
-
|
|
871
|
-
Use the Create function to create a value from a type. TypeBox will use default values if specified.
|
|
872
|
-
|
|
873
|
-
```typescript
|
|
874
|
-
const T = Type.Object({ x: Type.Number(), y: Type.Number({ default: 42 }) })
|
|
875
|
-
|
|
876
|
-
const A = Value.Create(T) // const A = { x: 0, y: 42 }
|
|
877
|
-
```
|
|
878
|
-
|
|
879
|
-
<a name='values-clone'></a>
|
|
880
|
-
|
|
881
|
-
### Clone
|
|
882
|
-
|
|
883
|
-
Use the Clone function to deeply clone a value
|
|
884
|
-
|
|
885
|
-
```typescript
|
|
886
|
-
const A = Value.Clone({ x: 1, y: 2, z: 3 }) // const A = { x: 1, y: 2, z: 3 }
|
|
887
|
-
```
|
|
888
|
-
|
|
889
|
-
<a name='values-check'></a>
|
|
890
|
-
|
|
891
|
-
### Check
|
|
892
|
-
|
|
893
|
-
Use the Check function to type check a value
|
|
894
|
-
|
|
895
|
-
```typescript
|
|
896
|
-
const T = Type.Object({ x: Type.Number() })
|
|
897
|
-
|
|
898
|
-
const R = Value.Check(T, { x: 1 }) // const R = true
|
|
899
|
-
```
|
|
900
|
-
|
|
901
|
-
<a name='values-convert'></a>
|
|
902
|
-
|
|
903
|
-
### Convert
|
|
904
|
-
|
|
905
|
-
Use the Convert function to convert a value into its target type if a reasonable conversion is possible.
|
|
906
|
-
|
|
907
|
-
```typescript
|
|
908
|
-
const T = Type.Object({ x: Type.Number(), y: Type.Number() })
|
|
909
|
-
|
|
910
|
-
const R1 = Value.Convert(T, { x: '3.14' }) // const R1 = { x: 3.14 }
|
|
911
|
-
|
|
912
|
-
const R2 = Value.Convert(T, { x: 'not a number' }) // const R2 = { x: 'not a number' }
|
|
913
|
-
```
|
|
914
|
-
|
|
915
|
-
<a name='values-cast'></a>
|
|
916
|
-
|
|
917
|
-
### Cast
|
|
918
|
-
|
|
919
|
-
Use the Cast function to cast a value into a type. The cast function will retain as much information as possible from the original value.
|
|
920
|
-
|
|
921
|
-
```typescript
|
|
922
|
-
const T = Type.Object({ x: Type.Number(), y: Type.Number() }, { additionalProperties: false })
|
|
923
|
-
|
|
924
|
-
const X = Value.Cast(T, null) // const X = { x: 0, y: 0 }
|
|
925
|
-
|
|
926
|
-
const Y = Value.Cast(T, { x: 1 }) // const Y = { x: 1, y: 0 }
|
|
927
|
-
|
|
928
|
-
const Z = Value.Cast(T, { x: 1, y: 2, z: 3 }) // const Z = { x: 1, y: 2 }
|
|
929
|
-
```
|
|
930
|
-
|
|
931
|
-
<a name='values-equal'></a>
|
|
932
|
-
|
|
933
|
-
### Equal
|
|
934
|
-
|
|
935
|
-
Use the Equal function to deeply check for value equality.
|
|
936
|
-
|
|
937
|
-
```typescript
|
|
938
|
-
const R = Value.Equal( // const R = true
|
|
939
|
-
{ x: 1, y: 2, z: 3 },
|
|
940
|
-
{ x: 1, y: 2, z: 3 }
|
|
941
|
-
)
|
|
942
|
-
```
|
|
943
|
-
|
|
944
|
-
<a name='values-hash'></a>
|
|
945
|
-
|
|
946
|
-
### Hash
|
|
947
|
-
|
|
948
|
-
Use the Hash function to create a [FNV1A-64](https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function) non cryptographic hash of a value.
|
|
949
|
-
|
|
950
|
-
```typescript
|
|
951
|
-
const A = Value.Hash({ x: 1, y: 2, z: 3 }) // const A = 2910466848807138541n
|
|
952
|
-
|
|
953
|
-
const B = Value.Hash({ x: 1, y: 4, z: 3 }) // const B = 1418369778807423581n
|
|
954
|
-
```
|
|
955
|
-
|
|
956
|
-
<a name='values-diff'></a>
|
|
957
|
-
|
|
958
|
-
### Diff
|
|
959
|
-
|
|
960
|
-
Use the Diff function to produce a sequence of edits to transform one value into another.
|
|
961
|
-
|
|
962
|
-
```typescript
|
|
963
|
-
const E = Value.Diff( // const E = [
|
|
964
|
-
{ x: 1, y: 2, z: 3 }, // { type: 'update', path: '/y', value: 4 },
|
|
965
|
-
{ y: 4, z: 5, w: 6 } // { type: 'update', path: '/z', value: 5 },
|
|
966
|
-
) // { type: 'insert', path: '/w', value: 6 },
|
|
967
|
-
// { type: 'delete', path: '/x' }
|
|
968
|
-
// ]
|
|
969
|
-
```
|
|
970
|
-
|
|
971
|
-
<a name='values-patch'></a>
|
|
972
|
-
|
|
973
|
-
### Patch
|
|
974
|
-
|
|
975
|
-
Use the Patch function to apply edits
|
|
976
|
-
|
|
977
|
-
```typescript
|
|
978
|
-
const A = { x: 1, y: 2 }
|
|
979
|
-
|
|
980
|
-
const B = { x: 3 }
|
|
981
|
-
|
|
982
|
-
const E = Value.Diff(A, B) // const E = [
|
|
983
|
-
// { type: 'update', path: '/x', value: 3 },
|
|
984
|
-
// { type: 'delete', path: '/y' }
|
|
985
|
-
// ]
|
|
986
|
-
|
|
987
|
-
const C = Value.Patch<typeof B>(A, E) // const C = { x: 3 }
|
|
988
|
-
```
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
<a name='values-errors'></a>
|
|
992
|
-
|
|
993
|
-
### Errors
|
|
994
|
-
|
|
995
|
-
Use the Errors function enumerate validation errors.
|
|
996
|
-
|
|
997
|
-
```typescript
|
|
998
|
-
const T = Type.Object({ x: Type.Number(), y: Type.Number() })
|
|
999
|
-
|
|
1000
|
-
const R = [...Value.Errors(T, { x: '42' })] // const R = [{
|
|
1001
|
-
// schema: { type: 'number' },
|
|
1002
|
-
// path: '/x',
|
|
1003
|
-
// value: '42',
|
|
1004
|
-
// message: 'Expected number'
|
|
1005
|
-
// }, {
|
|
1006
|
-
// schema: { type: 'number' },
|
|
1007
|
-
// path: '/y',
|
|
1008
|
-
// value: undefined,
|
|
1009
|
-
// message: 'Expected number'
|
|
1010
|
-
// }]
|
|
1011
|
-
```
|
|
1012
|
-
|
|
1013
|
-
<a name='values-pointer'></a>
|
|
1014
|
-
|
|
1015
|
-
### Pointer
|
|
1016
|
-
|
|
1017
|
-
Use ValuePointer to perform mutable updates on existing values using [RFC6901](https://www.rfc-editor.org/rfc/rfc6901) JSON Pointers.
|
|
1018
|
-
|
|
1019
|
-
```typescript
|
|
1020
|
-
import { ValuePointer } from '@sinclair/typebox/value'
|
|
1021
|
-
|
|
1022
|
-
const A = { x: 0, y: 0, z: 0 }
|
|
1023
|
-
|
|
1024
|
-
ValuePointer.Set(A, '/x', 1) // const A = { x: 1, y: 0, z: 0 }
|
|
1025
|
-
ValuePointer.Set(A, '/y', 1) // const A = { x: 1, y: 1, z: 0 }
|
|
1026
|
-
ValuePointer.Set(A, '/z', 1) // const A = { x: 1, y: 1, z: 1 }
|
|
1027
|
-
```
|
|
1028
|
-
<a name='typecheck'></a>
|
|
1029
|
-
|
|
1030
|
-
## TypeCheck
|
|
1031
|
-
|
|
1032
|
-
TypeBox types target JSON Schema draft 6 so are compatible with any validator that supports this specification. TypeBox also provides a built in type checking compiler designed specifically for high performance compilation and value assertion.
|
|
1033
|
-
|
|
1034
|
-
The following sections detail using Ajv and TypeBox's compiler infrastructure.
|
|
1035
|
-
|
|
1036
|
-
<a name='typecheck-ajv'></a>
|
|
1037
|
-
|
|
1038
|
-
## Ajv
|
|
1039
|
-
|
|
1040
|
-
The following shows the recommended setup for Ajv.
|
|
1041
|
-
|
|
1042
|
-
```bash
|
|
1043
|
-
$ npm install ajv ajv-formats --save
|
|
1044
|
-
```
|
|
1045
|
-
|
|
1046
|
-
```typescript
|
|
1047
|
-
import { Type } from '@sinclair/typebox'
|
|
1048
|
-
import addFormats from 'ajv-formats'
|
|
1049
|
-
import Ajv from 'ajv'
|
|
1050
|
-
|
|
1051
|
-
const ajv = addFormats(new Ajv({}), [
|
|
1052
|
-
'date-time',
|
|
1053
|
-
'time',
|
|
1054
|
-
'date',
|
|
1055
|
-
'email',
|
|
1056
|
-
'hostname',
|
|
1057
|
-
'ipv4',
|
|
1058
|
-
'ipv6',
|
|
1059
|
-
'uri',
|
|
1060
|
-
'uri-reference',
|
|
1061
|
-
'uuid',
|
|
1062
|
-
'uri-template',
|
|
1063
|
-
'json-pointer',
|
|
1064
|
-
'relative-json-pointer',
|
|
1065
|
-
'regex'
|
|
1066
|
-
])
|
|
1067
|
-
|
|
1068
|
-
const C = ajv.compile(Type.Object({
|
|
1069
|
-
x: Type.Number(),
|
|
1070
|
-
y: Type.Number(),
|
|
1071
|
-
z: Type.Number()
|
|
1072
|
-
}))
|
|
1073
|
-
|
|
1074
|
-
const R = C({ x: 1, y: 2, z: 3 }) // const R = true
|
|
1075
|
-
```
|
|
1076
|
-
|
|
1077
|
-
<a name='typecheck-typecompiler'></a>
|
|
1078
|
-
|
|
1079
|
-
### TypeCompiler
|
|
1080
|
-
|
|
1081
|
-
The TypeBox TypeCompiler is a high performance JIT compiler that transforms TypeBox types into optimized JavaScript validation routines. The compiler is tuned for fast compilation as well as fast value assertion. It is designed to serve as a validation backend that can be integrated into larger applications; but can also be used as a general purpose validator.
|
|
1082
|
-
|
|
1083
|
-
The TypeCompiler is provided as an optional import.
|
|
1084
|
-
|
|
1085
|
-
```typescript
|
|
1086
|
-
import { TypeCompiler } from '@sinclair/typebox/compiler'
|
|
1087
|
-
```
|
|
1088
|
-
|
|
1089
|
-
Use the `Compile(...)` function to compile a type.
|
|
1090
|
-
|
|
1091
|
-
```typescript
|
|
1092
|
-
const C = TypeCompiler.Compile(Type.Object({ // const C: TypeCheck<TObject<{
|
|
1093
|
-
x: Type.Number(), // x: TNumber;
|
|
1094
|
-
y: Type.Number(), // y: TNumber;
|
|
1095
|
-
z: Type.Number() // z: TNumber;
|
|
1096
|
-
})) // }>>
|
|
1097
|
-
|
|
1098
|
-
const R = C.Check({ x: 1, y: 2, z: 3 }) // const R = true
|
|
1099
|
-
```
|
|
1100
|
-
|
|
1101
|
-
Use the `Errors(...)` function to produce diagnostic errors for a value. The `Errors(...)` function will return an iterator that if enumerated; will perform an exhaustive check across the entire value and yield any error found. For performance, this function should only be called after failed `Check(...)`. Applications may also choose to yield only the first value to avoid exhaustive error generation.
|
|
1102
|
-
|
|
1103
|
-
```typescript
|
|
1104
|
-
const C = TypeCompiler.Compile(Type.Object({ // const C: TypeCheck<TObject<{
|
|
1105
|
-
x: Type.Number(), // x: TNumber;
|
|
1106
|
-
y: Type.Number(), // y: TNumber;
|
|
1107
|
-
z: Type.Number() // z: TNumber;
|
|
1108
|
-
})) // }>>
|
|
1109
|
-
|
|
1110
|
-
const value = { }
|
|
1111
|
-
|
|
1112
|
-
const errors = [...C.Errors(value)] // const errors = [{
|
|
1113
|
-
// schema: { type: 'number' },
|
|
1114
|
-
// path: '/x',
|
|
1115
|
-
// value: undefined,
|
|
1116
|
-
// message: 'Expected number'
|
|
1117
|
-
// }, {
|
|
1118
|
-
// schema: { type: 'number' },
|
|
1119
|
-
// path: '/y',
|
|
1120
|
-
// value: undefined,
|
|
1121
|
-
// message: 'Expected number'
|
|
1122
|
-
// }, {
|
|
1123
|
-
// schema: { type: 'number' },
|
|
1124
|
-
// path: '/z',
|
|
1125
|
-
// value: undefined,
|
|
1126
|
-
// message: 'Expected number'
|
|
1127
|
-
// }]
|
|
1128
|
-
```
|
|
1129
|
-
|
|
1130
|
-
Compiled routines can be inspected with the `.Code()` function.
|
|
1131
|
-
|
|
1132
|
-
```typescript
|
|
1133
|
-
const C = TypeCompiler.Compile(Type.String()) // const C: TypeCheck<TString>
|
|
1134
|
-
|
|
1135
|
-
console.log(C.Code()) // return function check(value) {
|
|
1136
|
-
// return (
|
|
1137
|
-
// (typeof value === 'string')
|
|
1138
|
-
// )
|
|
1139
|
-
// }
|
|
1140
|
-
```
|
|
1141
|
-
|
|
1142
|
-
<a name='typesystem'></a>
|
|
1143
|
-
|
|
1144
|
-
## TypeSystem
|
|
1145
|
-
|
|
1146
|
-
The TypeBox TypeSystem module provides functionality to define types above and beyond the Standard and Extended type sets as well as control various assertion polices. Configurations made to the TypeSystem module are observed by both `TypeCompiler` and `Value` modules.
|
|
1147
|
-
|
|
1148
|
-
The TypeSystem module is provided as an optional import.
|
|
1149
|
-
|
|
1150
|
-
```typescript
|
|
1151
|
-
import { TypeSystem } from '@sinclair/typebox/system'
|
|
1152
|
-
```
|
|
1153
|
-
|
|
1154
|
-
<a name='typesystem-types'></a>
|
|
1155
|
-
|
|
1156
|
-
### Types
|
|
1157
|
-
|
|
1158
|
-
Use the `Type(...)` function to create a custom type. This function will return a type factory function that can be used to construct the type. The following creates a Point type.
|
|
1159
|
-
|
|
1160
|
-
```typescript
|
|
1161
|
-
type PointOptions = { } // The Type Options
|
|
1162
|
-
|
|
1163
|
-
type PointType = { x: number, y: number } // The Static<T> Type
|
|
1164
|
-
|
|
1165
|
-
const Point = TypeSystem.Type<PointType, PointOptions>('Point', (options, value) => {
|
|
1166
|
-
return (
|
|
1167
|
-
typeof value === 'object' && value !== null &&
|
|
1168
|
-
typeof value.x === 'number' &&
|
|
1169
|
-
typeof value.y === 'number'
|
|
1170
|
-
)
|
|
1171
|
-
})
|
|
1172
|
-
|
|
1173
|
-
const T = Point()
|
|
1174
|
-
|
|
1175
|
-
type T = Static<typeof T> // type T = { x: number, y: number }
|
|
1176
|
-
|
|
1177
|
-
const R = Value.Check(T, { x: 1, y: 2 }) // const R = true
|
|
1178
|
-
```
|
|
1179
|
-
|
|
1180
|
-
<a name='typesystem-formats'></a>
|
|
1181
|
-
|
|
1182
|
-
### Formats
|
|
1183
|
-
|
|
1184
|
-
Use the `Format(...)` function to create a custom string format. The following creates a custom string format that checks for lowercase strings.
|
|
1185
|
-
|
|
1186
|
-
```typescript
|
|
1187
|
-
TypeSystem.Format('lowercase', value => value === value.toLowerCase()) // format should be lowercase
|
|
1188
|
-
|
|
1189
|
-
const T = Type.String({ format: 'lowercase' })
|
|
1190
|
-
|
|
1191
|
-
const A = Value.Check(T, 'action') // const A = true
|
|
1192
|
-
|
|
1193
|
-
const B = Value.Check(T, 'ACTION') // const B = false
|
|
1194
|
-
```
|
|
1195
|
-
|
|
1196
|
-
<a name='typesystem-policies'></a>
|
|
1197
|
-
|
|
1198
|
-
### Policies
|
|
1199
|
-
|
|
1200
|
-
TypeBox validates using JSON Schema assertion policies by default. It is possible to override these policies and have TypeBox assert using TypeScript policies. The following overrides are available.
|
|
1201
|
-
|
|
1202
|
-
```typescript
|
|
1203
|
-
// Allow arrays to validate as object types (default is false)
|
|
1204
|
-
//
|
|
1205
|
-
// const A: {} = [] - allowed in TS
|
|
1206
|
-
|
|
1207
|
-
TypeSystem.AllowArrayObjects = true
|
|
1208
|
-
|
|
1209
|
-
// Allow numeric values to be NaN or + or - Infinity (default is false)
|
|
1210
|
-
//
|
|
1211
|
-
// const A: number = NaN - allowed in TS
|
|
1212
|
-
|
|
1213
|
-
TypeSystem.AllowNaN = true
|
|
1214
|
-
```
|
|
1215
|
-
|
|
1216
|
-
<a name='benchmark'></a>
|
|
1217
|
-
|
|
1218
|
-
## Benchmark
|
|
1219
|
-
|
|
1220
|
-
This project maintains a set of benchmarks that measure Ajv, Value and TypeCompiler compilation and validation performance. These benchmarks can be run locally by cloning this repository and running `npm run benchmark`. The results below show for Ajv version 8.11.2.
|
|
1221
|
-
|
|
1222
|
-
For additional comparative benchmarks, please refer to [typescript-runtime-type-benchmarks](https://moltar.github.io/typescript-runtime-type-benchmarks/).
|
|
1223
|
-
|
|
1224
|
-
<a name='benchmark-compile'></a>
|
|
1225
|
-
|
|
1226
|
-
### Compile
|
|
1227
|
-
|
|
1228
|
-
This benchmark measures compilation performance for varying types. You can review this benchmark [here](https://github.com/sinclairzx81/typebox/blob/master/benchmark/measurement/module/compile.ts).
|
|
1229
|
-
|
|
1230
|
-
```typescript
|
|
1231
|
-
┌────────────────────────────┬────────────┬──────────────┬──────────────┬──────────────┐
|
|
1232
|
-
│ (index) │ Iterations │ Ajv │ TypeCompiler │ Performance │
|
|
1233
|
-
├────────────────────────────┼────────────┼──────────────┼──────────────┼──────────────┤
|
|
1234
|
-
│ Literal_String │ 1000 │ ' 260 ms' │ ' 8 ms' │ ' 32.50 x' │
|
|
1235
|
-
│ Literal_Number │ 1000 │ ' 198 ms' │ ' 4 ms' │ ' 49.50 x' │
|
|
1236
|
-
│ Literal_Boolean │ 1000 │ ' 185 ms' │ ' 5 ms' │ ' 37.00 x' │
|
|
1237
|
-
│ Primitive_Number │ 1000 │ ' 176 ms' │ ' 9 ms' │ ' 19.56 x' │
|
|
1238
|
-
│ Primitive_String │ 1000 │ ' 161 ms' │ ' 9 ms' │ ' 17.89 x' │
|
|
1239
|
-
│ Primitive_String_Pattern │ 1000 │ ' 215 ms' │ ' 12 ms' │ ' 17.92 x' │
|
|
1240
|
-
│ Primitive_Boolean │ 1000 │ ' 133 ms' │ ' 5 ms' │ ' 26.60 x' │
|
|
1241
|
-
│ Primitive_Null │ 1000 │ ' 143 ms' │ ' 8 ms' │ ' 17.88 x' │
|
|
1242
|
-
│ Object_Unconstrained │ 1000 │ ' 1181 ms' │ ' 38 ms' │ ' 31.08 x' │
|
|
1243
|
-
│ Object_Constrained │ 1000 │ ' 1168 ms' │ ' 32 ms' │ ' 36.50 x' │
|
|
1244
|
-
│ Tuple_Primitive │ 1000 │ ' 557 ms' │ ' 16 ms' │ ' 34.81 x' │
|
|
1245
|
-
│ Tuple_Object │ 1000 │ ' 1119 ms' │ ' 17 ms' │ ' 65.82 x' │
|
|
1246
|
-
│ Composite_Intersect │ 1000 │ ' 569 ms' │ ' 22 ms' │ ' 25.86 x' │
|
|
1247
|
-
│ Composite_Union │ 1000 │ ' 513 ms' │ ' 23 ms' │ ' 22.30 x' │
|
|
1248
|
-
│ Math_Vector4 │ 1000 │ ' 802 ms' │ ' 10 ms' │ ' 80.20 x' │
|
|
1249
|
-
│ Math_Matrix4 │ 1000 │ ' 395 ms' │ ' 12 ms' │ ' 32.92 x' │
|
|
1250
|
-
│ Array_Primitive_Number │ 1000 │ ' 282 ms' │ ' 8 ms' │ ' 35.25 x' │
|
|
1251
|
-
│ Array_Primitive_String │ 1000 │ ' 321 ms' │ ' 5 ms' │ ' 64.20 x' │
|
|
1252
|
-
│ Array_Primitive_Boolean │ 1000 │ ' 364 ms' │ ' 5 ms' │ ' 72.80 x' │
|
|
1253
|
-
│ Array_Object_Unconstrained │ 1000 │ ' 1573 ms' │ ' 18 ms' │ ' 87.39 x' │
|
|
1254
|
-
│ Array_Object_Constrained │ 1000 │ ' 1270 ms' │ ' 20 ms' │ ' 63.50 x' │
|
|
1255
|
-
│ Array_Tuple_Primitive │ 1000 │ ' 973 ms' │ ' 18 ms' │ ' 54.06 x' │
|
|
1256
|
-
│ Array_Tuple_Object │ 1000 │ ' 1253 ms' │ ' 16 ms' │ ' 78.31 x' │
|
|
1257
|
-
│ Array_Composite_Intersect │ 1000 │ ' 927 ms' │ ' 20 ms' │ ' 46.35 x' │
|
|
1258
|
-
│ Array_Composite_Union │ 1000 │ ' 1123 ms' │ ' 16 ms' │ ' 70.19 x' │
|
|
1259
|
-
│ Array_Math_Vector4 │ 1000 │ ' 1068 ms' │ ' 10 ms' │ ' 106.80 x' │
|
|
1260
|
-
│ Array_Math_Matrix4 │ 1000 │ ' 488 ms' │ ' 7 ms' │ ' 69.71 x' │
|
|
1261
|
-
└────────────────────────────┴────────────┴──────────────┴──────────────┴──────────────┘
|
|
1262
|
-
```
|
|
1263
|
-
|
|
1264
|
-
<a name='benchmark-validate'></a>
|
|
1265
|
-
|
|
1266
|
-
### Validate
|
|
1267
|
-
|
|
1268
|
-
This benchmark measures validation performance for varying types. You can review this benchmark [here](https://github.com/sinclairzx81/typebox/blob/master/benchmark/measurement/module/check.ts).
|
|
1269
|
-
|
|
1270
|
-
```typescript
|
|
1271
|
-
┌────────────────────────────┬────────────┬──────────────┬──────────────┬──────────────┬──────────────┐
|
|
1272
|
-
│ (index) │ Iterations │ ValueCheck │ Ajv │ TypeCompiler │ Performance │
|
|
1273
|
-
├────────────────────────────┼────────────┼──────────────┼──────────────┼──────────────┼──────────────┤
|
|
1274
|
-
│ Literal_String │ 1000000 │ ' 26 ms' │ ' 6 ms' │ ' 6 ms' │ ' 1.00 x' │
|
|
1275
|
-
│ Literal_Number │ 1000000 │ ' 21 ms' │ ' 19 ms' │ ' 11 ms' │ ' 1.73 x' │
|
|
1276
|
-
│ Literal_Boolean │ 1000000 │ ' 19 ms' │ ' 18 ms' │ ' 10 ms' │ ' 1.80 x' │
|
|
1277
|
-
│ Primitive_Number │ 1000000 │ ' 24 ms' │ ' 19 ms' │ ' 11 ms' │ ' 1.73 x' │
|
|
1278
|
-
│ Primitive_String │ 1000000 │ ' 26 ms' │ ' 17 ms' │ ' 10 ms' │ ' 1.70 x' │
|
|
1279
|
-
│ Primitive_String_Pattern │ 1000000 │ ' 159 ms' │ ' 45 ms' │ ' 37 ms' │ ' 1.22 x' │
|
|
1280
|
-
│ Primitive_Boolean │ 1000000 │ ' 23 ms' │ ' 17 ms' │ ' 10 ms' │ ' 1.70 x' │
|
|
1281
|
-
│ Primitive_Null │ 1000000 │ ' 23 ms' │ ' 18 ms' │ ' 10 ms' │ ' 1.80 x' │
|
|
1282
|
-
│ Object_Unconstrained │ 1000000 │ ' 809 ms' │ ' 35 ms' │ ' 30 ms' │ ' 1.17 x' │
|
|
1283
|
-
│ Object_Constrained │ 1000000 │ ' 1060 ms' │ ' 56 ms' │ ' 45 ms' │ ' 1.24 x' │
|
|
1284
|
-
│ Object_Recursive │ 1000000 │ ' 4965 ms' │ ' 397 ms' │ ' 100 ms' │ ' 3.97 x' │
|
|
1285
|
-
│ Tuple_Primitive │ 1000000 │ ' 159 ms' │ ' 22 ms' │ ' 16 ms' │ ' 1.38 x' │
|
|
1286
|
-
│ Tuple_Object │ 1000000 │ ' 658 ms' │ ' 31 ms' │ ' 27 ms' │ ' 1.15 x' │
|
|
1287
|
-
│ Composite_Intersect │ 1000000 │ ' 695 ms' │ ' 26 ms' │ ' 22 ms' │ ' 1.18 x' │
|
|
1288
|
-
│ Composite_Union │ 1000000 │ ' 503 ms' │ ' 24 ms' │ ' 19 ms' │ ' 1.26 x' │
|
|
1289
|
-
│ Math_Vector4 │ 1000000 │ ' 259 ms' │ ' 22 ms' │ ' 14 ms' │ ' 1.57 x' │
|
|
1290
|
-
│ Math_Matrix4 │ 1000000 │ ' 1007 ms' │ ' 40 ms' │ ' 29 ms' │ ' 1.38 x' │
|
|
1291
|
-
│ Array_Primitive_Number │ 1000000 │ ' 262 ms' │ ' 23 ms' │ ' 17 ms' │ ' 1.35 x' │
|
|
1292
|
-
│ Array_Primitive_String │ 1000000 │ ' 241 ms' │ ' 27 ms' │ ' 24 ms' │ ' 1.13 x' │
|
|
1293
|
-
│ Array_Primitive_Boolean │ 1000000 │ ' 141 ms' │ ' 23 ms' │ ' 20 ms' │ ' 1.15 x' │
|
|
1294
|
-
│ Array_Object_Unconstrained │ 1000000 │ ' 4976 ms' │ ' 70 ms' │ ' 67 ms' │ ' 1.04 x' │
|
|
1295
|
-
│ Array_Object_Constrained │ 1000000 │ ' 5234 ms' │ ' 143 ms' │ ' 120 ms' │ ' 1.19 x' │
|
|
1296
|
-
│ Array_Object_Recursive │ 1000000 │ ' 19605 ms' │ ' 1909 ms' │ ' 350 ms' │ ' 5.45 x' │
|
|
1297
|
-
│ Array_Tuple_Primitive │ 1000000 │ ' 706 ms' │ ' 39 ms' │ ' 32 ms' │ ' 1.22 x' │
|
|
1298
|
-
│ Array_Tuple_Object │ 1000000 │ ' 2951 ms' │ ' 67 ms' │ ' 63 ms' │ ' 1.06 x' │
|
|
1299
|
-
│ Array_Composite_Intersect │ 1000000 │ ' 2969 ms' │ ' 49 ms' │ ' 44 ms' │ ' 1.11 x' │
|
|
1300
|
-
│ Array_Composite_Union │ 1000000 │ ' 2191 ms' │ ' 77 ms' │ ' 41 ms' │ ' 1.88 x' │
|
|
1301
|
-
│ Array_Math_Vector4 │ 1000000 │ ' 1164 ms' │ ' 41 ms' │ ' 25 ms' │ ' 1.64 x' │
|
|
1302
|
-
│ Array_Math_Matrix4 │ 1000000 │ ' 4903 ms' │ ' 115 ms' │ ' 99 ms' │ ' 1.16 x' │
|
|
1303
|
-
└────────────────────────────┴────────────┴──────────────┴──────────────┴──────────────┴──────────────┘
|
|
1304
|
-
```
|
|
1305
|
-
|
|
1306
|
-
<a name='benchmark-compression'></a>
|
|
1307
|
-
|
|
1308
|
-
### Compression
|
|
1309
|
-
|
|
1310
|
-
The following table lists esbuild compiled and minified sizes for each TypeBox module.
|
|
1311
|
-
|
|
1312
|
-
```typescript
|
|
1313
|
-
┌──────────────────────┬────────────┬────────────┬─────────────┐
|
|
1314
|
-
│ (index) │ Compiled │ Minified │ Compression │
|
|
1315
|
-
├──────────────────────┼────────────┼────────────┼─────────────┤
|
|
1316
|
-
│ typebox/compiler │ '108.8 kb' │ ' 48.9 kb' │ '2.23 x' │
|
|
1317
|
-
│ typebox/errors │ ' 93.2 kb' │ ' 41.5 kb' │ '2.24 x' │
|
|
1318
|
-
│ typebox/system │ ' 60.0 kb' │ ' 24.6 kb' │ '2.43 x' │
|
|
1319
|
-
│ typebox/value │ '153.5 kb' │ ' 66.7 kb' │ '2.30 x' │
|
|
1320
|
-
│ typebox │ ' 58.7 kb' │ ' 24.1 kb' │ '2.43 x' │
|
|
1321
|
-
└──────────────────────┴────────────┴────────────┴─────────────┘
|
|
1322
|
-
```
|
|
1323
|
-
|
|
1324
|
-
<a name='contribute'></a>
|
|
1325
|
-
|
|
1326
|
-
## Contribute
|
|
1327
|
-
|
|
1328
|
-
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.
|
|
1
|
+
<div align='center'>
|
|
2
|
+
|
|
3
|
+
<h1>TypeBox</h1>
|
|
4
|
+
|
|
5
|
+
<p>JSON Schema Type Builder with Static Type Resolution for TypeScript</p>
|
|
6
|
+
|
|
7
|
+
<img src="https://github.com/sinclairzx81/typebox/blob/master/typebox.png?raw=true" />
|
|
8
|
+
|
|
9
|
+
<br />
|
|
10
|
+
<br />
|
|
11
|
+
|
|
12
|
+
[](https://badge.fury.io/js/%40sinclair%2Ftypebox)
|
|
13
|
+
[](https://www.npmjs.com/package/%40sinclair%2Ftypebox)
|
|
14
|
+
[](https://github.com/sinclairzx81/typebox/actions)
|
|
15
|
+
|
|
16
|
+
</div>
|
|
17
|
+
|
|
18
|
+
<a name="Install"></a>
|
|
19
|
+
|
|
20
|
+
## Install
|
|
21
|
+
|
|
22
|
+
#### Npm
|
|
23
|
+
```bash
|
|
24
|
+
$ npm install @sinclair/typebox --save
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
#### Deno
|
|
28
|
+
```typescript
|
|
29
|
+
import { Static, Type } from 'npm:@sinclair/typebox'
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
#### Esm
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
import { Static, Type } from 'https://esm.sh/@sinclair/typebox'
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Example
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
import { Static, Type } from '@sinclair/typebox'
|
|
42
|
+
|
|
43
|
+
const T = Type.Object({ // const T = {
|
|
44
|
+
x: Type.Number(), // type: 'object',
|
|
45
|
+
y: Type.Number(), // required: ['x', 'y', 'z'],
|
|
46
|
+
z: Type.Number() // properties: {
|
|
47
|
+
}) // x: { type: 'number' },
|
|
48
|
+
// y: { type: 'number' },
|
|
49
|
+
// z: { type: 'number' }
|
|
50
|
+
// }
|
|
51
|
+
// }
|
|
52
|
+
|
|
53
|
+
type T = Static<typeof T> // type T = {
|
|
54
|
+
// x: number,
|
|
55
|
+
// y: number,
|
|
56
|
+
// z: number
|
|
57
|
+
// }
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
<a name="Overview"></a>
|
|
62
|
+
|
|
63
|
+
## Overview
|
|
64
|
+
|
|
65
|
+
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.
|
|
66
|
+
|
|
67
|
+
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.
|
|
68
|
+
|
|
69
|
+
License MIT
|
|
70
|
+
|
|
71
|
+
## Contents
|
|
72
|
+
- [Install](#install)
|
|
73
|
+
- [Overview](#overview)
|
|
74
|
+
- [Usage](#usage)
|
|
75
|
+
- [Types](#types)
|
|
76
|
+
- [Standard](#types-standard)
|
|
77
|
+
- [Extended](#types-extended)
|
|
78
|
+
- [Modifiers](#types-modifiers)
|
|
79
|
+
- [Options](#types-options)
|
|
80
|
+
- [Generics](#types-generics)
|
|
81
|
+
- [References](#types-references)
|
|
82
|
+
- [Recursive](#types-recursive)
|
|
83
|
+
- [Conditional](#types-conditional)
|
|
84
|
+
- [Guards](#types-guards)
|
|
85
|
+
- [Unsafe](#types-unsafe)
|
|
86
|
+
- [Strict](#types-strict)
|
|
87
|
+
- [Values](#values)
|
|
88
|
+
- [Create](#values-create)
|
|
89
|
+
- [Clone](#values-clone)
|
|
90
|
+
- [Check](#values-check)
|
|
91
|
+
- [Convert](#values-convert)
|
|
92
|
+
- [Cast](#values-cast)
|
|
93
|
+
- [Equal](#values-equal)
|
|
94
|
+
- [Hash](#values-hash)
|
|
95
|
+
- [Diff](#values-diff)
|
|
96
|
+
- [Patch](#values-patch)
|
|
97
|
+
- [Errors](#values-errors)
|
|
98
|
+
- [Pointer](#values-pointer)
|
|
99
|
+
- [TypeCheck](#typecheck)
|
|
100
|
+
- [Ajv](#typecheck-ajv)
|
|
101
|
+
- [TypeCompiler](#typecheck-typecompiler)
|
|
102
|
+
- [TypeSystem](#typecheck)
|
|
103
|
+
- [Types](#typesystem-types)
|
|
104
|
+
- [Formats](#typesystem-formats)
|
|
105
|
+
- [Policies](#typesystem-policies)
|
|
106
|
+
- [Benchmark](#benchmark)
|
|
107
|
+
- [Compile](#benchmark-compile)
|
|
108
|
+
- [Validate](#benchmark-validate)
|
|
109
|
+
- [Compression](#benchmark-compression)
|
|
110
|
+
- [Contribute](#contribute)
|
|
111
|
+
|
|
112
|
+
<a name="usage"></a>
|
|
113
|
+
|
|
114
|
+
## Usage
|
|
115
|
+
|
|
116
|
+
The following shows general usage.
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
import { Static, Type } from '@sinclair/typebox'
|
|
120
|
+
|
|
121
|
+
//--------------------------------------------------------------------------------------------
|
|
122
|
+
//
|
|
123
|
+
// Let's say you have the following type ...
|
|
124
|
+
//
|
|
125
|
+
//--------------------------------------------------------------------------------------------
|
|
126
|
+
|
|
127
|
+
type T = {
|
|
128
|
+
id: string,
|
|
129
|
+
name: string,
|
|
130
|
+
timestamp: number
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
//--------------------------------------------------------------------------------------------
|
|
134
|
+
//
|
|
135
|
+
// ... you can express this type in the following way.
|
|
136
|
+
//
|
|
137
|
+
//--------------------------------------------------------------------------------------------
|
|
138
|
+
|
|
139
|
+
const T = Type.Object({ // const T = {
|
|
140
|
+
id: Type.String(), // type: 'object',
|
|
141
|
+
name: Type.String(), // properties: {
|
|
142
|
+
timestamp: Type.Integer() // id: {
|
|
143
|
+
}) // type: 'string'
|
|
144
|
+
// },
|
|
145
|
+
// name: {
|
|
146
|
+
// type: 'string'
|
|
147
|
+
// },
|
|
148
|
+
// timestamp: {
|
|
149
|
+
// type: 'integer'
|
|
150
|
+
// }
|
|
151
|
+
// },
|
|
152
|
+
// required: [
|
|
153
|
+
// 'id',
|
|
154
|
+
// 'name',
|
|
155
|
+
// 'timestamp'
|
|
156
|
+
// ]
|
|
157
|
+
// }
|
|
158
|
+
|
|
159
|
+
//--------------------------------------------------------------------------------------------
|
|
160
|
+
//
|
|
161
|
+
// ... then infer back to the original static type this way.
|
|
162
|
+
//
|
|
163
|
+
//--------------------------------------------------------------------------------------------
|
|
164
|
+
|
|
165
|
+
type T = Static<typeof T> // type T = {
|
|
166
|
+
// id: string,
|
|
167
|
+
// name: string,
|
|
168
|
+
// timestamp: number
|
|
169
|
+
// }
|
|
170
|
+
|
|
171
|
+
//--------------------------------------------------------------------------------------------
|
|
172
|
+
//
|
|
173
|
+
// ... then use the type both as JSON schema and as a TypeScript type.
|
|
174
|
+
//
|
|
175
|
+
//--------------------------------------------------------------------------------------------
|
|
176
|
+
|
|
177
|
+
import { Value } from '@sinclair/typebox/value'
|
|
178
|
+
|
|
179
|
+
function receive(value: T) { // ... as a Static Type
|
|
180
|
+
|
|
181
|
+
if(Value.Check(T, value)) { // ... as a JSON Schema
|
|
182
|
+
|
|
183
|
+
// ok...
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
<a name='types'></a>
|
|
189
|
+
|
|
190
|
+
## Types
|
|
191
|
+
|
|
192
|
+
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.
|
|
193
|
+
|
|
194
|
+
<a name='types-standard'></a>
|
|
195
|
+
|
|
196
|
+
### Standard Types
|
|
197
|
+
|
|
198
|
+
The following table lists the Standard TypeBox types. These types are fully compatible with the JSON Schema Draft 6 specification.
|
|
199
|
+
|
|
200
|
+
```typescript
|
|
201
|
+
┌────────────────────────────────┬─────────────────────────────┬────────────────────────────────┐
|
|
202
|
+
│ TypeBox │ TypeScript │ JSON Schema │
|
|
203
|
+
│ │ │ │
|
|
204
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
205
|
+
│ const T = Type.Any() │ type T = any │ const T = { } │
|
|
206
|
+
│ │ │ │
|
|
207
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
208
|
+
│ const T = Type.Unknown() │ type T = unknown │ const T = { } │
|
|
209
|
+
│ │ │ │
|
|
210
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
211
|
+
│ const T = Type.String() │ type T = string │ const T = { │
|
|
212
|
+
│ │ │ type: 'string' │
|
|
213
|
+
│ │ │ } │
|
|
214
|
+
│ │ │ │
|
|
215
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
216
|
+
│ const T = Type.Number() │ type T = number │ const T = { │
|
|
217
|
+
│ │ │ type: 'number' │
|
|
218
|
+
│ │ │ } │
|
|
219
|
+
│ │ │ │
|
|
220
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
221
|
+
│ const T = Type.Integer() │ type T = number │ const T = { │
|
|
222
|
+
│ │ │ type: 'integer' │
|
|
223
|
+
│ │ │ } │
|
|
224
|
+
│ │ │ │
|
|
225
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
226
|
+
│ const T = Type.Boolean() │ type T = boolean │ const T = { │
|
|
227
|
+
│ │ │ type: 'boolean' │
|
|
228
|
+
│ │ │ } │
|
|
229
|
+
│ │ │ │
|
|
230
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
231
|
+
│ const T = Type.Null() │ type T = null │ const T = { │
|
|
232
|
+
│ │ │ type: 'null' │
|
|
233
|
+
│ │ │ } │
|
|
234
|
+
│ │ │ │
|
|
235
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
236
|
+
│ const T = Type.Literal(42) │ type T = 42 │ const T = { │
|
|
237
|
+
│ │ │ const: 42, │
|
|
238
|
+
│ │ │ type: 'number' │
|
|
239
|
+
│ │ │ } │
|
|
240
|
+
│ │ │ │
|
|
241
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
242
|
+
│ const T = Type.Array( │ type T = number[] │ const T = { │
|
|
243
|
+
│ Type.Number() │ │ type: 'array', │
|
|
244
|
+
│ ) │ │ items: { │
|
|
245
|
+
│ │ │ type: 'number' │
|
|
246
|
+
│ │ │ } │
|
|
247
|
+
│ │ │ } │
|
|
248
|
+
│ │ │ │
|
|
249
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
250
|
+
│ const T = Type.Object({ │ type T = { │ const T = { │
|
|
251
|
+
│ x: Type.Number(), │ x: number, │ type: 'object', │
|
|
252
|
+
│ y: Type.Number() │ y: number │ required: ['x', 'y'], │
|
|
253
|
+
│ }) │ } │ properties: { │
|
|
254
|
+
│ │ │ x: { │
|
|
255
|
+
│ │ │ type: 'number' │
|
|
256
|
+
│ │ │ }, { │
|
|
257
|
+
│ │ │ type: 'number' │
|
|
258
|
+
│ │ │ } │
|
|
259
|
+
│ │ │ } │
|
|
260
|
+
│ │ │ } │
|
|
261
|
+
│ │ │ │
|
|
262
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
263
|
+
│ const T = Type.Tuple([ │ type T = [number, number] │ const T = { │
|
|
264
|
+
│ Type.Number(), │ │ type: 'array', │
|
|
265
|
+
│ Type.Number() │ │ items: [{ │
|
|
266
|
+
│ ]) │ │ type: 'number' │
|
|
267
|
+
│ │ │ }, { │
|
|
268
|
+
│ │ │ type: 'number' │
|
|
269
|
+
│ │ │ }], │
|
|
270
|
+
│ │ │ additionalItems: false, │
|
|
271
|
+
│ │ │ minItems: 2, │
|
|
272
|
+
│ │ │ maxItems: 2 │
|
|
273
|
+
│ │ │ } │
|
|
274
|
+
│ │ │ │
|
|
275
|
+
│ │ │ │
|
|
276
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
277
|
+
│ enum Foo { │ enum Foo { │ const T = { │
|
|
278
|
+
│ A, │ A, │ anyOf: [{ │
|
|
279
|
+
│ B │ B │ type: 'number', │
|
|
280
|
+
│ } │ } │ const: 0 │
|
|
281
|
+
│ │ │ }, { │
|
|
282
|
+
│ const T = Type.Enum(Foo) │ type T = Foo │ type: 'number', │
|
|
283
|
+
│ │ │ const: 1 │
|
|
284
|
+
│ │ │ }] │
|
|
285
|
+
│ │ │ } │
|
|
286
|
+
│ │ │ │
|
|
287
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
288
|
+
│ const T = Type.KeyOf( │ type T = keyof { │ const T = { │
|
|
289
|
+
│ Type.Object({ │ x: number, │ anyOf: [{ │
|
|
290
|
+
│ x: Type.Number(), │ y: number │ type: 'string', │
|
|
291
|
+
│ y: Type.Number() │ } │ const: 'x' │
|
|
292
|
+
│ }) │ │ }, { │
|
|
293
|
+
│ ) │ │ type: 'string', │
|
|
294
|
+
│ │ │ const: 'y' │
|
|
295
|
+
│ │ │ }] │
|
|
296
|
+
│ │ │ } │
|
|
297
|
+
│ │ │ │
|
|
298
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
299
|
+
│ const T = Type.Union([ │ type T = string | number │ const T = { │
|
|
300
|
+
│ Type.String(), │ │ anyOf: [{ │
|
|
301
|
+
│ Type.Number() │ │ type: 'string' │
|
|
302
|
+
│ ]) │ │ }, { │
|
|
303
|
+
│ │ │ type: 'number' │
|
|
304
|
+
│ │ │ }] │
|
|
305
|
+
│ │ │ } │
|
|
306
|
+
│ │ │ │
|
|
307
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
308
|
+
│ const T = Type.Intersect([ │ type T = { │ const T = { │
|
|
309
|
+
│ Type.Object({ │ x: number │ allOf: [{ │
|
|
310
|
+
│ x: Type.Number() │ } & { │ type: 'object', │
|
|
311
|
+
│ }), │ y: number │ required: ['x'], │
|
|
312
|
+
│ Type.Object({ │ } │ properties: { │
|
|
313
|
+
│ y: Type.Number() │ │ x: { │
|
|
314
|
+
│ ]) │ │ type: 'number' │
|
|
315
|
+
│ ]) │ │ } │
|
|
316
|
+
│ │ │ } │
|
|
317
|
+
│ │ │ }, { │
|
|
318
|
+
│ │ │ type: 'object', |
|
|
319
|
+
│ │ │ required: ['y'], │
|
|
320
|
+
│ │ │ properties: { │
|
|
321
|
+
│ │ │ y: { │
|
|
322
|
+
│ │ │ type: 'number' │
|
|
323
|
+
│ │ │ } │
|
|
324
|
+
│ │ │ }] │
|
|
325
|
+
│ │ │ } │
|
|
326
|
+
│ │ │ │
|
|
327
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
328
|
+
│ const T = Type.Composite([ │ type I = { │ const T = { │
|
|
329
|
+
│ Type.Object({ │ x: number │ type: 'object', │
|
|
330
|
+
│ x: Type.Number() │ } & { │ required: ['x', 'y'], │
|
|
331
|
+
│ }), │ y: number │ properties: { │
|
|
332
|
+
│ Type.Object({ │ } │ x: { │
|
|
333
|
+
│ y: Type.Number() │ │ type: 'number' │
|
|
334
|
+
│ }) │ type T = { │ }, │
|
|
335
|
+
│ ]) │ [K in keyof I]: I[K] │ y: { │
|
|
336
|
+
│ │ } │ type: 'number' │
|
|
337
|
+
│ │ │ } │
|
|
338
|
+
│ │ │ } │
|
|
339
|
+
│ │ │ } │
|
|
340
|
+
│ │ │ │
|
|
341
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
342
|
+
│ const T = Type.Never() │ type T = never │ const T = { │
|
|
343
|
+
│ │ │ not: {} │
|
|
344
|
+
│ │ │ } │
|
|
345
|
+
│ │ │ │
|
|
346
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
347
|
+
│ const T = Type.Not( | type T = string │ const T = { │
|
|
348
|
+
| Type.Union([ │ │ allOf: [{ │
|
|
349
|
+
│ Type.Literal('x'), │ │ not: { │
|
|
350
|
+
│ Type.Literal('y'), │ │ anyOf: [ │
|
|
351
|
+
│ Type.Literal('z') │ │ { const: 'x' }, │
|
|
352
|
+
│ ]), │ │ { const: 'y' }, │
|
|
353
|
+
│ Type.String() │ │ { const: 'z' } │
|
|
354
|
+
│ ) │ │ ] │
|
|
355
|
+
│ │ │ } │
|
|
356
|
+
│ │ │ }, { │
|
|
357
|
+
│ │ │ type: 'string' │
|
|
358
|
+
│ │ │ }] │
|
|
359
|
+
│ │ │ } │
|
|
360
|
+
│ │ │ │
|
|
361
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
362
|
+
│ const T = Type.Extends( │ type T = │ const T = { │
|
|
363
|
+
│ Type.String(), │ string extends number │ const: false, │
|
|
364
|
+
│ Type.Number(), │ true : false │ type: 'boolean' │
|
|
365
|
+
│ Type.Literal(true), │ │ } │
|
|
366
|
+
│ Type.Literal(false) │ │ │
|
|
367
|
+
│ ) │ │ │
|
|
368
|
+
│ │ │ │
|
|
369
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
370
|
+
│ const T = Type.Extract( │ type T = Extract< │ const T = { │
|
|
371
|
+
│ Type.Union([ │ string | number, │ type: 'string' │
|
|
372
|
+
│ Type.String(), │ string │ } │
|
|
373
|
+
│ Type.Number(), │ > │ │
|
|
374
|
+
│ ]), │ │ │
|
|
375
|
+
│ Type.String() │ │ │
|
|
376
|
+
│ ) │ │ │
|
|
377
|
+
│ │ │ │
|
|
378
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
379
|
+
│ const T = Type.Exclude( │ type T = Exclude< │ const T = { │
|
|
380
|
+
│ Type.Union([ │ string | number, │ type: 'number' │
|
|
381
|
+
│ Type.String(), │ string │ } │
|
|
382
|
+
│ Type.Number(), │ > │ │
|
|
383
|
+
│ ]), │ │ │
|
|
384
|
+
│ Type.String() │ │ │
|
|
385
|
+
│ ) │ │ │
|
|
386
|
+
│ │ │ │
|
|
387
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
388
|
+
│ const T = Type.Record( │ type T = Record< │ const T = { │
|
|
389
|
+
│ Type.String(), │ string, │ type: 'object', │
|
|
390
|
+
│ Type.Number() │ number, │ patternProperties: { │
|
|
391
|
+
│ ) │ > │ '^.*$': { │
|
|
392
|
+
│ │ │ type: 'number' │
|
|
393
|
+
│ │ │ } │
|
|
394
|
+
│ │ │ } │
|
|
395
|
+
│ │ │ } │
|
|
396
|
+
│ │ │ │
|
|
397
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
398
|
+
│ const T = Type.Partial( │ type T = Partial<{ │ const T = { │
|
|
399
|
+
│ Type.Object({ │ x: number, │ type: 'object', │
|
|
400
|
+
│ x: Type.Number(), │ y: number │ properties: { │
|
|
401
|
+
│ y: Type.Number() | }> │ x: { │
|
|
402
|
+
│ }) │ │ type: 'number' │
|
|
403
|
+
│ ) │ │ }, │
|
|
404
|
+
│ │ │ y: { │
|
|
405
|
+
│ │ │ type: 'number' │
|
|
406
|
+
│ │ │ } │
|
|
407
|
+
│ │ │ } │
|
|
408
|
+
│ │ │ } │
|
|
409
|
+
│ │ │ │
|
|
410
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
411
|
+
│ const T = Type.Required( │ type T = Required<{ │ const T = { │
|
|
412
|
+
│ Type.Object({ │ x?: number, │ type: 'object', │
|
|
413
|
+
│ x: Type.Optional( │ y?: number │ required: ['x', 'y'], │
|
|
414
|
+
│ Type.Number() | }> │ properties: { │
|
|
415
|
+
│ ), │ │ x: { │
|
|
416
|
+
│ y: Type.Optional( │ │ type: 'number' │
|
|
417
|
+
│ Type.Number() │ │ }, │
|
|
418
|
+
│ ) │ │ y: { │
|
|
419
|
+
│ }) │ │ type: 'number' │
|
|
420
|
+
│ ) │ │ } │
|
|
421
|
+
│ │ │ } │
|
|
422
|
+
│ │ │ } │
|
|
423
|
+
│ │ │ │
|
|
424
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
425
|
+
│ const T = Type.Pick( │ type T = Pick<{ │ const T = { │
|
|
426
|
+
│ Type.Object({ │ x: number, │ type: 'object', │
|
|
427
|
+
│ x: Type.Number(), │ y: number │ properties: { │
|
|
428
|
+
│ y: Type.Number() | }, 'x'> │ x: { │
|
|
429
|
+
│ }), ['x'] │ │ type: 'number' │
|
|
430
|
+
│ ) │ │ } │
|
|
431
|
+
│ │ │ }, │
|
|
432
|
+
│ │ │ required: ['x'] │
|
|
433
|
+
│ │ │ } │
|
|
434
|
+
│ │ │ │
|
|
435
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
436
|
+
│ const T = Type.Omit( │ type T = Omit<{ │ const T = { │
|
|
437
|
+
│ Type.Object({ │ x: number, │ type: 'object', │
|
|
438
|
+
│ x: Type.Number(), │ y: number │ properties: { │
|
|
439
|
+
│ y: Type.Number() | }, 'x'> │ y: { │
|
|
440
|
+
│ }), ['x'] │ │ type: 'number' │
|
|
441
|
+
│ ) │ │ } │
|
|
442
|
+
│ │ │ }, │
|
|
443
|
+
│ │ │ required: ['y'] │
|
|
444
|
+
│ │ │ } │
|
|
445
|
+
│ │ │ │
|
|
446
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
447
|
+
│ const T = Type.Object({ │ type T = { │ const R = { │
|
|
448
|
+
│ x: Type.Number(), │ x: number, │ $ref: 'T' │
|
|
449
|
+
│ y: Type.Number() │ y: number │ } │
|
|
450
|
+
│ }, { $id: 'T' }) | } │ │
|
|
451
|
+
│ │ │ │
|
|
452
|
+
│ const R = Type.Ref(T) │ type R = T │ │
|
|
453
|
+
│ │ │ │
|
|
454
|
+
│ │ │ │
|
|
455
|
+
│ │ │ │
|
|
456
|
+
│ │ │ │
|
|
457
|
+
└────────────────────────────────┴─────────────────────────────┴────────────────────────────────┘
|
|
458
|
+
```
|
|
459
|
+
|
|
460
|
+
<a name='types-extended'></a>
|
|
461
|
+
|
|
462
|
+
### Extended Types
|
|
463
|
+
|
|
464
|
+
TypeBox provides several extended types that can be used to produce schematics for common JavaScript constructs. These types can not be used with standard JSON schema validators; but are useful to help frame schematics for RPC interfaces that may receive JSON validated data. Extended types are prefixed with the `[Extended]` doc comment for convenience. The following table lists the supported types.
|
|
465
|
+
|
|
466
|
+
```typescript
|
|
467
|
+
┌────────────────────────────────┬─────────────────────────────┬────────────────────────────────┐
|
|
468
|
+
│ TypeBox │ TypeScript │ Extended Schema │
|
|
469
|
+
│ │ │ │
|
|
470
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
471
|
+
│ const T = Type.Constructor([ │ type T = new ( │ const T = { │
|
|
472
|
+
│ Type.String(), │ arg0: string, │ type: 'object', │
|
|
473
|
+
│ Type.Number() │ arg1: number │ instanceOf: 'Constructor', │
|
|
474
|
+
│ ], Type.Boolean()) │ ) => boolean │ parameters: [{ │
|
|
475
|
+
│ │ │ type: 'string' │
|
|
476
|
+
│ │ │ }, { │
|
|
477
|
+
│ │ │ type: 'number' │
|
|
478
|
+
│ │ │ }], │
|
|
479
|
+
│ │ │ return: { │
|
|
480
|
+
│ │ │ type: 'boolean' │
|
|
481
|
+
│ │ │ } │
|
|
482
|
+
│ │ │ } │
|
|
483
|
+
│ │ │ │
|
|
484
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
485
|
+
│ const T = Type.Function([ │ type T = ( │ const T = { │
|
|
486
|
+
| Type.String(), │ arg0: string, │ type : 'object', │
|
|
487
|
+
│ Type.Number() │ arg1: number │ instanceOf: 'Function', │
|
|
488
|
+
│ ], Type.Boolean()) │ ) => boolean │ parameters: [{ │
|
|
489
|
+
│ │ │ type: 'string' │
|
|
490
|
+
│ │ │ }, { │
|
|
491
|
+
│ │ │ type: 'number' │
|
|
492
|
+
│ │ │ }], │
|
|
493
|
+
│ │ │ return: { │
|
|
494
|
+
│ │ │ type: 'boolean' │
|
|
495
|
+
│ │ │ } │
|
|
496
|
+
│ │ │ } │
|
|
497
|
+
│ │ │ │
|
|
498
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
499
|
+
│ const T = Type.Promise( │ type T = Promise<string> │ const T = { │
|
|
500
|
+
│ Type.String() │ │ type: 'object', │
|
|
501
|
+
│ ) │ │ instanceOf: 'Promise', │
|
|
502
|
+
│ │ │ item: { │
|
|
503
|
+
│ │ │ type: 'string' │
|
|
504
|
+
│ │ │ } │
|
|
505
|
+
│ │ │ } │
|
|
506
|
+
│ │ │ │
|
|
507
|
+
│ │ │ │
|
|
508
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
509
|
+
│ const T = Type.Uint8Array() │ type T = Uint8Array │ const T = { │
|
|
510
|
+
│ │ │ type: 'object', │
|
|
511
|
+
│ │ │ instanceOf: 'Uint8Array' │
|
|
512
|
+
│ │ │ } │
|
|
513
|
+
│ │ │ │
|
|
514
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
515
|
+
│ const T = Type.Date() │ type T = Date │ const T = { │
|
|
516
|
+
│ │ │ type: 'object', │
|
|
517
|
+
│ │ │ instanceOf: 'Date' │
|
|
518
|
+
│ │ │ } │
|
|
519
|
+
│ │ │ │
|
|
520
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
521
|
+
│ const T = Type.Undefined() │ type T = undefined │ const T = { │
|
|
522
|
+
│ │ │ type: 'null', │
|
|
523
|
+
│ │ │ typeOf: 'Undefined' │
|
|
524
|
+
│ │ │ } │
|
|
525
|
+
│ │ │ │
|
|
526
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
527
|
+
│ const T = Type.RegEx(/foo/) │ type T = string │ const T = { │
|
|
528
|
+
│ │ │ type: 'string', │
|
|
529
|
+
│ │ │ pattern: 'foo' │
|
|
530
|
+
│ │ │ } │
|
|
531
|
+
│ │ │ │
|
|
532
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
533
|
+
│ const T = Type.Symbol() │ type T = symbol │ const T = { │
|
|
534
|
+
│ │ │ type: 'null', │
|
|
535
|
+
│ │ │ typeOf: 'Symbol' │
|
|
536
|
+
│ │ │ } │
|
|
537
|
+
│ │ │ │
|
|
538
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
539
|
+
│ const T = Type.BigInt() │ type T = bigint │ const T = { │
|
|
540
|
+
│ │ │ type: 'null', │
|
|
541
|
+
│ │ │ typeOf: 'BigInt' │
|
|
542
|
+
│ │ │ } │
|
|
543
|
+
│ │ │ │
|
|
544
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
545
|
+
│ const T = Type.Void() │ type T = void │ const T = { │
|
|
546
|
+
│ │ │ type: 'null' │
|
|
547
|
+
│ │ │ typeOf: 'Void' │
|
|
548
|
+
│ │ │ } │
|
|
549
|
+
│ │ │ │
|
|
550
|
+
└────────────────────────────────┴─────────────────────────────┴────────────────────────────────┘
|
|
551
|
+
```
|
|
552
|
+
|
|
553
|
+
<a name='types-modifiers'></a>
|
|
554
|
+
|
|
555
|
+
### Modifiers
|
|
556
|
+
|
|
557
|
+
TypeBox provides modifiers that allow schema properties to be statically inferred as `readonly` or `optional`. The following table shows the supported modifiers and how they map between TypeScript and JSON Schema.
|
|
558
|
+
|
|
559
|
+
```typescript
|
|
560
|
+
┌────────────────────────────────┬─────────────────────────────┬────────────────────────────────┐
|
|
561
|
+
│ TypeBox │ TypeScript │ JSON Schema │
|
|
562
|
+
│ │ │ │
|
|
563
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
564
|
+
│ const T = Type.Object({ │ type T = { │ const T = { │
|
|
565
|
+
│ name: Type.Optional( │ name?: string │ type: 'object', │
|
|
566
|
+
│ Type.String() │ } │ properties: { │
|
|
567
|
+
│ ) │ │ name: { │
|
|
568
|
+
│ }) │ │ type: 'string' │
|
|
569
|
+
│ │ │ } │
|
|
570
|
+
│ │ │ } │
|
|
571
|
+
│ │ │ } │
|
|
572
|
+
│ │ │ │
|
|
573
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
574
|
+
│ const T = Type.Object({ │ type T = { │ const T = { │
|
|
575
|
+
│ name: Type.Readonly( │ readonly name: string │ type: 'object', │
|
|
576
|
+
│ Type.String() │ } │ properties: { │
|
|
577
|
+
│ ) │ │ name: { │
|
|
578
|
+
│ }) │ │ type: 'string' │
|
|
579
|
+
│ │ │ } │
|
|
580
|
+
│ │ │ }, │
|
|
581
|
+
│ │ │ required: ['name'] │
|
|
582
|
+
│ │ │ } │
|
|
583
|
+
│ │ │ │
|
|
584
|
+
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
585
|
+
│ const T = Type.Object({ │ type T = { │ const T = { │
|
|
586
|
+
│ name: Type.ReadonlyOptional( │ readonly name?: string │ type: 'object', │
|
|
587
|
+
│ Type.String() │ } │ properties: { │
|
|
588
|
+
│ ) │ │ name: { │
|
|
589
|
+
│ }) │ │ type: 'string' │
|
|
590
|
+
│ │ │ } │
|
|
591
|
+
│ │ │ } │
|
|
592
|
+
│ │ │ } │
|
|
593
|
+
│ │ │ │
|
|
594
|
+
└────────────────────────────────┴─────────────────────────────┴────────────────────────────────┘
|
|
595
|
+
```
|
|
596
|
+
|
|
597
|
+
<a name='types-options'></a>
|
|
598
|
+
|
|
599
|
+
### Options
|
|
600
|
+
|
|
601
|
+
You can pass JSON Schema options on the last argument of any type. Option hints specific to each type are provided for convenience.
|
|
602
|
+
|
|
603
|
+
```typescript
|
|
604
|
+
// String must be an email
|
|
605
|
+
const T = Type.String({ // const T = {
|
|
606
|
+
format: 'email' // type: 'string',
|
|
607
|
+
}) // format: 'email'
|
|
608
|
+
// }
|
|
609
|
+
|
|
610
|
+
// Mumber must be a multiple of 2
|
|
611
|
+
const T = Type.Number({ // const T = {
|
|
612
|
+
multipleOf: 2 // type: 'number',
|
|
613
|
+
}) // multipleOf: 2
|
|
614
|
+
// }
|
|
615
|
+
|
|
616
|
+
// Array must have at least 5 integer values
|
|
617
|
+
const T = Type.Array(Type.Integer(), { // const T = {
|
|
618
|
+
minItems: 5 // type: 'array',
|
|
619
|
+
}) // minItems: 5,
|
|
620
|
+
// items: {
|
|
621
|
+
// type: 'integer'
|
|
622
|
+
// }
|
|
623
|
+
// }
|
|
624
|
+
|
|
625
|
+
```
|
|
626
|
+
|
|
627
|
+
<a name='types-generics'></a>
|
|
628
|
+
|
|
629
|
+
### Generic Types
|
|
630
|
+
|
|
631
|
+
Generic types can be created with generic functions constrained to type `TSchema`. The following creates a generic `Vector<T>` type.
|
|
632
|
+
|
|
633
|
+
```typescript
|
|
634
|
+
import { Type, Static, TSchema } from '@sinclair/typebox'
|
|
635
|
+
|
|
636
|
+
const Vector = <T extends TSchema>(t: T) => Type.Object({ x: t, y: t, z: t })
|
|
637
|
+
|
|
638
|
+
const NumberVector = Vector(Type.Number()) // const NumberVector = {
|
|
639
|
+
// type: 'object',
|
|
640
|
+
// required: ['x', 'y', 'z'],
|
|
641
|
+
// properties: {
|
|
642
|
+
// x: { type: 'number' },
|
|
643
|
+
// y: { type: 'number' },
|
|
644
|
+
// z: { type: 'number' }
|
|
645
|
+
// }
|
|
646
|
+
// }
|
|
647
|
+
|
|
648
|
+
type NumberVector = Static<typeof NumberVector> // type NumberVector = {
|
|
649
|
+
// x: number,
|
|
650
|
+
// y: number,
|
|
651
|
+
// z: number
|
|
652
|
+
// }
|
|
653
|
+
|
|
654
|
+
const BooleanVector = Vector(Type.Boolean()) // const BooleanVector = {
|
|
655
|
+
// type: 'object',
|
|
656
|
+
// required: ['x', 'y', 'z'],
|
|
657
|
+
// properties: {
|
|
658
|
+
// x: { type: 'boolean' },
|
|
659
|
+
// y: { type: 'boolean' },
|
|
660
|
+
// z: { type: 'boolean' }
|
|
661
|
+
// }
|
|
662
|
+
// }
|
|
663
|
+
|
|
664
|
+
type BooleanVector = Static<typeof BooleanVector> // type BooleanVector = {
|
|
665
|
+
// x: boolean,
|
|
666
|
+
// y: boolean,
|
|
667
|
+
// z: boolean
|
|
668
|
+
// }
|
|
669
|
+
```
|
|
670
|
+
|
|
671
|
+
The following creates a generic `Nullable<T>` type.
|
|
672
|
+
|
|
673
|
+
```typescript
|
|
674
|
+
const Nullable = <T extends TSchema>(schema: T) => Type.Union([schema, Type.Null()])
|
|
675
|
+
|
|
676
|
+
const T = Nullable(Type.String()) // const T = {
|
|
677
|
+
// anyOf: [
|
|
678
|
+
// { type: 'string' },
|
|
679
|
+
// { type: 'null' }
|
|
680
|
+
// ]
|
|
681
|
+
// }
|
|
682
|
+
|
|
683
|
+
type T = Static<typeof T> // type T = string | null
|
|
684
|
+
```
|
|
685
|
+
|
|
686
|
+
<a name='types-references'></a>
|
|
687
|
+
|
|
688
|
+
### Reference Types
|
|
689
|
+
|
|
690
|
+
Reference types are supported with `Type.Ref(...)`. The target type must specify a valid `$id`.
|
|
691
|
+
|
|
692
|
+
```typescript
|
|
693
|
+
const T = Type.String({ $id: 'T' }) // const T = {
|
|
694
|
+
// $id: 'T',
|
|
695
|
+
// type: 'string'
|
|
696
|
+
// }
|
|
697
|
+
|
|
698
|
+
const R = Type.Ref(T) // const R = {
|
|
699
|
+
// $ref: 'T'
|
|
700
|
+
// }
|
|
701
|
+
```
|
|
702
|
+
|
|
703
|
+
<a name='types-recursive'></a>
|
|
704
|
+
|
|
705
|
+
### Recursive Types
|
|
706
|
+
|
|
707
|
+
Recursive types are supported with `Type.Recursive(...)`.
|
|
708
|
+
|
|
709
|
+
```typescript
|
|
710
|
+
const Node = Type.Recursive(Node => Type.Object({ // const Node = {
|
|
711
|
+
id: Type.String(), // $id: 'Node',
|
|
712
|
+
nodes: Type.Array(Node) // type: 'object',
|
|
713
|
+
}), { $id: 'Node' }) // properties: {
|
|
714
|
+
// id: {
|
|
715
|
+
// type: 'string'
|
|
716
|
+
// },
|
|
717
|
+
// nodes: {
|
|
718
|
+
// type: 'array',
|
|
719
|
+
// items: {
|
|
720
|
+
// $ref: 'Node'
|
|
721
|
+
// }
|
|
722
|
+
// }
|
|
723
|
+
// },
|
|
724
|
+
// required: [
|
|
725
|
+
// 'id',
|
|
726
|
+
// 'nodes'
|
|
727
|
+
// ]
|
|
728
|
+
// }
|
|
729
|
+
|
|
730
|
+
type Node = Static<typeof Node> // type Node = {
|
|
731
|
+
// id: string
|
|
732
|
+
// nodes: Node[]
|
|
733
|
+
// }
|
|
734
|
+
|
|
735
|
+
function test(node: Node) {
|
|
736
|
+
const id = node.nodes[0].nodes[0].id // id is string
|
|
737
|
+
}
|
|
738
|
+
```
|
|
739
|
+
|
|
740
|
+
<a name='types-conditional'></a>
|
|
741
|
+
|
|
742
|
+
### Conditional Types
|
|
743
|
+
|
|
744
|
+
Conditional types are supported with `Extends`, `Exclude` and `Extract`.
|
|
745
|
+
|
|
746
|
+
**TypeScript**
|
|
747
|
+
|
|
748
|
+
```typescript
|
|
749
|
+
type T0 = string extends number ? true : false
|
|
750
|
+
// ^ false
|
|
751
|
+
type T1 = Extract<string | number, number>
|
|
752
|
+
// ^ number
|
|
753
|
+
type T2 = Exclude<string | number, number>
|
|
754
|
+
// ^ string
|
|
755
|
+
```
|
|
756
|
+
**TypeBox**
|
|
757
|
+
```typescript
|
|
758
|
+
const T0 = Type.Extends(Type.String(), Type.Number(), Type.Literal(true), Type.Literal(false))
|
|
759
|
+
// ^ TLiteral<false>
|
|
760
|
+
const T1 = Type.Extract(Type.Union([Type.String(), Type.Number()]), Type.Number())
|
|
761
|
+
// ^ TNumber
|
|
762
|
+
const T2 = Type.Exclude(Type.Union([Type.String(), Type.Number()]), Type.Number())
|
|
763
|
+
// ^ TString<string>
|
|
764
|
+
```
|
|
765
|
+
|
|
766
|
+
<a name='types-unsafe'></a>
|
|
767
|
+
|
|
768
|
+
### Unsafe
|
|
769
|
+
|
|
770
|
+
Use `Type.Unsafe(...)` to create custom schematics with user defined inference rules.
|
|
771
|
+
|
|
772
|
+
```typescript
|
|
773
|
+
const T = Type.Unsafe<string>({ type: 'number' }) // const T = {
|
|
774
|
+
// type: 'number'
|
|
775
|
+
// }
|
|
776
|
+
|
|
777
|
+
type T = Static<typeof T> // type T = string
|
|
778
|
+
```
|
|
779
|
+
|
|
780
|
+
The `Type.Unsafe(...)` type can be useful to express specific OpenAPI schema representations.
|
|
781
|
+
|
|
782
|
+
```typescript
|
|
783
|
+
import { Type, Static, TSchema } from '@sinclair/typebox'
|
|
784
|
+
|
|
785
|
+
// Nullable<T>
|
|
786
|
+
|
|
787
|
+
function Nullable<T extends TSchema>(schema: T) {
|
|
788
|
+
return Type.Unsafe<Static<T> | null>({ ...schema, nullable: true })
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
const T = Nullable(Type.String()) // const T = {
|
|
792
|
+
// type: 'string',
|
|
793
|
+
// nullable: true
|
|
794
|
+
// }
|
|
795
|
+
|
|
796
|
+
type T = Static<typeof T> // type T = string | null
|
|
797
|
+
|
|
798
|
+
// StringEnum<string[]>
|
|
799
|
+
|
|
800
|
+
function StringEnum<T extends string[]>(values: [...T]) {
|
|
801
|
+
return Type.Unsafe<T[number]>({ type: 'string', enum: values })
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
const T = StringEnum(['A', 'B', 'C']) // const T = {
|
|
805
|
+
// enum: ['A', 'B', 'C']
|
|
806
|
+
// }
|
|
807
|
+
|
|
808
|
+
type T = Static<typeof T> // type T = 'A' | 'B' | 'C'
|
|
809
|
+
```
|
|
810
|
+
|
|
811
|
+
<a name='types-guards'></a>
|
|
812
|
+
|
|
813
|
+
### Guards
|
|
814
|
+
|
|
815
|
+
TypeBox provides a `TypeGuard` module that can be used for reflection and asserting values as types.
|
|
816
|
+
|
|
817
|
+
```typescript
|
|
818
|
+
import { Type, TypeGuard } from '@sinclair/typebox'
|
|
819
|
+
|
|
820
|
+
const T = Type.String()
|
|
821
|
+
|
|
822
|
+
if(TypeGuard.TString(T)) {
|
|
823
|
+
|
|
824
|
+
// T is TString
|
|
825
|
+
}
|
|
826
|
+
```
|
|
827
|
+
|
|
828
|
+
<a name='types-strict'></a>
|
|
829
|
+
|
|
830
|
+
### Strict
|
|
831
|
+
|
|
832
|
+
TypeBox schemas contain the `Kind` and `Modifier` symbol properties. These properties are used for type composition and reflection. 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 necessary.
|
|
833
|
+
|
|
834
|
+
```typescript
|
|
835
|
+
const T = Type.Object({ // const T = {
|
|
836
|
+
name: Type.Optional(Type.String()) // [Kind]: 'Object',
|
|
837
|
+
}) // type: 'object',
|
|
838
|
+
// properties: {
|
|
839
|
+
// name: {
|
|
840
|
+
// [Kind]: 'String',
|
|
841
|
+
// type: 'string',
|
|
842
|
+
// [Modifier]: 'Optional'
|
|
843
|
+
// }
|
|
844
|
+
// }
|
|
845
|
+
// }
|
|
846
|
+
|
|
847
|
+
const U = Type.Strict(T) // const U = {
|
|
848
|
+
// type: 'object',
|
|
849
|
+
// properties: {
|
|
850
|
+
// name: {
|
|
851
|
+
// type: 'string'
|
|
852
|
+
// }
|
|
853
|
+
// }
|
|
854
|
+
// }
|
|
855
|
+
```
|
|
856
|
+
|
|
857
|
+
<a name='values'></a>
|
|
858
|
+
|
|
859
|
+
## Values
|
|
860
|
+
|
|
861
|
+
TypeBox provides an optional utility module that can be used to perform common operations on JavaScript values. This module includes functionality to create, check and cast values from types as well as check equality, clone, diff and patch JavaScript values. This module is provided via optional import.
|
|
862
|
+
|
|
863
|
+
```typescript
|
|
864
|
+
import { Value } from '@sinclair/typebox/value'
|
|
865
|
+
```
|
|
866
|
+
|
|
867
|
+
<a name='values-create'></a>
|
|
868
|
+
|
|
869
|
+
### Create
|
|
870
|
+
|
|
871
|
+
Use the Create function to create a value from a type. TypeBox will use default values if specified.
|
|
872
|
+
|
|
873
|
+
```typescript
|
|
874
|
+
const T = Type.Object({ x: Type.Number(), y: Type.Number({ default: 42 }) })
|
|
875
|
+
|
|
876
|
+
const A = Value.Create(T) // const A = { x: 0, y: 42 }
|
|
877
|
+
```
|
|
878
|
+
|
|
879
|
+
<a name='values-clone'></a>
|
|
880
|
+
|
|
881
|
+
### Clone
|
|
882
|
+
|
|
883
|
+
Use the Clone function to deeply clone a value
|
|
884
|
+
|
|
885
|
+
```typescript
|
|
886
|
+
const A = Value.Clone({ x: 1, y: 2, z: 3 }) // const A = { x: 1, y: 2, z: 3 }
|
|
887
|
+
```
|
|
888
|
+
|
|
889
|
+
<a name='values-check'></a>
|
|
890
|
+
|
|
891
|
+
### Check
|
|
892
|
+
|
|
893
|
+
Use the Check function to type check a value
|
|
894
|
+
|
|
895
|
+
```typescript
|
|
896
|
+
const T = Type.Object({ x: Type.Number() })
|
|
897
|
+
|
|
898
|
+
const R = Value.Check(T, { x: 1 }) // const R = true
|
|
899
|
+
```
|
|
900
|
+
|
|
901
|
+
<a name='values-convert'></a>
|
|
902
|
+
|
|
903
|
+
### Convert
|
|
904
|
+
|
|
905
|
+
Use the Convert function to convert a value into its target type if a reasonable conversion is possible.
|
|
906
|
+
|
|
907
|
+
```typescript
|
|
908
|
+
const T = Type.Object({ x: Type.Number(), y: Type.Number() })
|
|
909
|
+
|
|
910
|
+
const R1 = Value.Convert(T, { x: '3.14' }) // const R1 = { x: 3.14 }
|
|
911
|
+
|
|
912
|
+
const R2 = Value.Convert(T, { x: 'not a number' }) // const R2 = { x: 'not a number' }
|
|
913
|
+
```
|
|
914
|
+
|
|
915
|
+
<a name='values-cast'></a>
|
|
916
|
+
|
|
917
|
+
### Cast
|
|
918
|
+
|
|
919
|
+
Use the Cast function to cast a value into a type. The cast function will retain as much information as possible from the original value.
|
|
920
|
+
|
|
921
|
+
```typescript
|
|
922
|
+
const T = Type.Object({ x: Type.Number(), y: Type.Number() }, { additionalProperties: false })
|
|
923
|
+
|
|
924
|
+
const X = Value.Cast(T, null) // const X = { x: 0, y: 0 }
|
|
925
|
+
|
|
926
|
+
const Y = Value.Cast(T, { x: 1 }) // const Y = { x: 1, y: 0 }
|
|
927
|
+
|
|
928
|
+
const Z = Value.Cast(T, { x: 1, y: 2, z: 3 }) // const Z = { x: 1, y: 2 }
|
|
929
|
+
```
|
|
930
|
+
|
|
931
|
+
<a name='values-equal'></a>
|
|
932
|
+
|
|
933
|
+
### Equal
|
|
934
|
+
|
|
935
|
+
Use the Equal function to deeply check for value equality.
|
|
936
|
+
|
|
937
|
+
```typescript
|
|
938
|
+
const R = Value.Equal( // const R = true
|
|
939
|
+
{ x: 1, y: 2, z: 3 },
|
|
940
|
+
{ x: 1, y: 2, z: 3 }
|
|
941
|
+
)
|
|
942
|
+
```
|
|
943
|
+
|
|
944
|
+
<a name='values-hash'></a>
|
|
945
|
+
|
|
946
|
+
### Hash
|
|
947
|
+
|
|
948
|
+
Use the Hash function to create a [FNV1A-64](https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function) non cryptographic hash of a value.
|
|
949
|
+
|
|
950
|
+
```typescript
|
|
951
|
+
const A = Value.Hash({ x: 1, y: 2, z: 3 }) // const A = 2910466848807138541n
|
|
952
|
+
|
|
953
|
+
const B = Value.Hash({ x: 1, y: 4, z: 3 }) // const B = 1418369778807423581n
|
|
954
|
+
```
|
|
955
|
+
|
|
956
|
+
<a name='values-diff'></a>
|
|
957
|
+
|
|
958
|
+
### Diff
|
|
959
|
+
|
|
960
|
+
Use the Diff function to produce a sequence of edits to transform one value into another.
|
|
961
|
+
|
|
962
|
+
```typescript
|
|
963
|
+
const E = Value.Diff( // const E = [
|
|
964
|
+
{ x: 1, y: 2, z: 3 }, // { type: 'update', path: '/y', value: 4 },
|
|
965
|
+
{ y: 4, z: 5, w: 6 } // { type: 'update', path: '/z', value: 5 },
|
|
966
|
+
) // { type: 'insert', path: '/w', value: 6 },
|
|
967
|
+
// { type: 'delete', path: '/x' }
|
|
968
|
+
// ]
|
|
969
|
+
```
|
|
970
|
+
|
|
971
|
+
<a name='values-patch'></a>
|
|
972
|
+
|
|
973
|
+
### Patch
|
|
974
|
+
|
|
975
|
+
Use the Patch function to apply edits
|
|
976
|
+
|
|
977
|
+
```typescript
|
|
978
|
+
const A = { x: 1, y: 2 }
|
|
979
|
+
|
|
980
|
+
const B = { x: 3 }
|
|
981
|
+
|
|
982
|
+
const E = Value.Diff(A, B) // const E = [
|
|
983
|
+
// { type: 'update', path: '/x', value: 3 },
|
|
984
|
+
// { type: 'delete', path: '/y' }
|
|
985
|
+
// ]
|
|
986
|
+
|
|
987
|
+
const C = Value.Patch<typeof B>(A, E) // const C = { x: 3 }
|
|
988
|
+
```
|
|
989
|
+
|
|
990
|
+
|
|
991
|
+
<a name='values-errors'></a>
|
|
992
|
+
|
|
993
|
+
### Errors
|
|
994
|
+
|
|
995
|
+
Use the Errors function enumerate validation errors.
|
|
996
|
+
|
|
997
|
+
```typescript
|
|
998
|
+
const T = Type.Object({ x: Type.Number(), y: Type.Number() })
|
|
999
|
+
|
|
1000
|
+
const R = [...Value.Errors(T, { x: '42' })] // const R = [{
|
|
1001
|
+
// schema: { type: 'number' },
|
|
1002
|
+
// path: '/x',
|
|
1003
|
+
// value: '42',
|
|
1004
|
+
// message: 'Expected number'
|
|
1005
|
+
// }, {
|
|
1006
|
+
// schema: { type: 'number' },
|
|
1007
|
+
// path: '/y',
|
|
1008
|
+
// value: undefined,
|
|
1009
|
+
// message: 'Expected number'
|
|
1010
|
+
// }]
|
|
1011
|
+
```
|
|
1012
|
+
|
|
1013
|
+
<a name='values-pointer'></a>
|
|
1014
|
+
|
|
1015
|
+
### Pointer
|
|
1016
|
+
|
|
1017
|
+
Use ValuePointer to perform mutable updates on existing values using [RFC6901](https://www.rfc-editor.org/rfc/rfc6901) JSON Pointers.
|
|
1018
|
+
|
|
1019
|
+
```typescript
|
|
1020
|
+
import { ValuePointer } from '@sinclair/typebox/value'
|
|
1021
|
+
|
|
1022
|
+
const A = { x: 0, y: 0, z: 0 }
|
|
1023
|
+
|
|
1024
|
+
ValuePointer.Set(A, '/x', 1) // const A = { x: 1, y: 0, z: 0 }
|
|
1025
|
+
ValuePointer.Set(A, '/y', 1) // const A = { x: 1, y: 1, z: 0 }
|
|
1026
|
+
ValuePointer.Set(A, '/z', 1) // const A = { x: 1, y: 1, z: 1 }
|
|
1027
|
+
```
|
|
1028
|
+
<a name='typecheck'></a>
|
|
1029
|
+
|
|
1030
|
+
## TypeCheck
|
|
1031
|
+
|
|
1032
|
+
TypeBox types target JSON Schema draft 6 so are compatible with any validator that supports this specification. TypeBox also provides a built in type checking compiler designed specifically for high performance compilation and value assertion.
|
|
1033
|
+
|
|
1034
|
+
The following sections detail using Ajv and TypeBox's compiler infrastructure.
|
|
1035
|
+
|
|
1036
|
+
<a name='typecheck-ajv'></a>
|
|
1037
|
+
|
|
1038
|
+
## Ajv
|
|
1039
|
+
|
|
1040
|
+
The following shows the recommended setup for Ajv.
|
|
1041
|
+
|
|
1042
|
+
```bash
|
|
1043
|
+
$ npm install ajv ajv-formats --save
|
|
1044
|
+
```
|
|
1045
|
+
|
|
1046
|
+
```typescript
|
|
1047
|
+
import { Type } from '@sinclair/typebox'
|
|
1048
|
+
import addFormats from 'ajv-formats'
|
|
1049
|
+
import Ajv from 'ajv'
|
|
1050
|
+
|
|
1051
|
+
const ajv = addFormats(new Ajv({}), [
|
|
1052
|
+
'date-time',
|
|
1053
|
+
'time',
|
|
1054
|
+
'date',
|
|
1055
|
+
'email',
|
|
1056
|
+
'hostname',
|
|
1057
|
+
'ipv4',
|
|
1058
|
+
'ipv6',
|
|
1059
|
+
'uri',
|
|
1060
|
+
'uri-reference',
|
|
1061
|
+
'uuid',
|
|
1062
|
+
'uri-template',
|
|
1063
|
+
'json-pointer',
|
|
1064
|
+
'relative-json-pointer',
|
|
1065
|
+
'regex'
|
|
1066
|
+
])
|
|
1067
|
+
|
|
1068
|
+
const C = ajv.compile(Type.Object({
|
|
1069
|
+
x: Type.Number(),
|
|
1070
|
+
y: Type.Number(),
|
|
1071
|
+
z: Type.Number()
|
|
1072
|
+
}))
|
|
1073
|
+
|
|
1074
|
+
const R = C({ x: 1, y: 2, z: 3 }) // const R = true
|
|
1075
|
+
```
|
|
1076
|
+
|
|
1077
|
+
<a name='typecheck-typecompiler'></a>
|
|
1078
|
+
|
|
1079
|
+
### TypeCompiler
|
|
1080
|
+
|
|
1081
|
+
The TypeBox TypeCompiler is a high performance JIT compiler that transforms TypeBox types into optimized JavaScript validation routines. The compiler is tuned for fast compilation as well as fast value assertion. It is designed to serve as a validation backend that can be integrated into larger applications; but can also be used as a general purpose validator.
|
|
1082
|
+
|
|
1083
|
+
The TypeCompiler is provided as an optional import.
|
|
1084
|
+
|
|
1085
|
+
```typescript
|
|
1086
|
+
import { TypeCompiler } from '@sinclair/typebox/compiler'
|
|
1087
|
+
```
|
|
1088
|
+
|
|
1089
|
+
Use the `Compile(...)` function to compile a type.
|
|
1090
|
+
|
|
1091
|
+
```typescript
|
|
1092
|
+
const C = TypeCompiler.Compile(Type.Object({ // const C: TypeCheck<TObject<{
|
|
1093
|
+
x: Type.Number(), // x: TNumber;
|
|
1094
|
+
y: Type.Number(), // y: TNumber;
|
|
1095
|
+
z: Type.Number() // z: TNumber;
|
|
1096
|
+
})) // }>>
|
|
1097
|
+
|
|
1098
|
+
const R = C.Check({ x: 1, y: 2, z: 3 }) // const R = true
|
|
1099
|
+
```
|
|
1100
|
+
|
|
1101
|
+
Use the `Errors(...)` function to produce diagnostic errors for a value. The `Errors(...)` function will return an iterator that if enumerated; will perform an exhaustive check across the entire value and yield any error found. For performance, this function should only be called after failed `Check(...)`. Applications may also choose to yield only the first value to avoid exhaustive error generation.
|
|
1102
|
+
|
|
1103
|
+
```typescript
|
|
1104
|
+
const C = TypeCompiler.Compile(Type.Object({ // const C: TypeCheck<TObject<{
|
|
1105
|
+
x: Type.Number(), // x: TNumber;
|
|
1106
|
+
y: Type.Number(), // y: TNumber;
|
|
1107
|
+
z: Type.Number() // z: TNumber;
|
|
1108
|
+
})) // }>>
|
|
1109
|
+
|
|
1110
|
+
const value = { }
|
|
1111
|
+
|
|
1112
|
+
const errors = [...C.Errors(value)] // const errors = [{
|
|
1113
|
+
// schema: { type: 'number' },
|
|
1114
|
+
// path: '/x',
|
|
1115
|
+
// value: undefined,
|
|
1116
|
+
// message: 'Expected number'
|
|
1117
|
+
// }, {
|
|
1118
|
+
// schema: { type: 'number' },
|
|
1119
|
+
// path: '/y',
|
|
1120
|
+
// value: undefined,
|
|
1121
|
+
// message: 'Expected number'
|
|
1122
|
+
// }, {
|
|
1123
|
+
// schema: { type: 'number' },
|
|
1124
|
+
// path: '/z',
|
|
1125
|
+
// value: undefined,
|
|
1126
|
+
// message: 'Expected number'
|
|
1127
|
+
// }]
|
|
1128
|
+
```
|
|
1129
|
+
|
|
1130
|
+
Compiled routines can be inspected with the `.Code()` function.
|
|
1131
|
+
|
|
1132
|
+
```typescript
|
|
1133
|
+
const C = TypeCompiler.Compile(Type.String()) // const C: TypeCheck<TString>
|
|
1134
|
+
|
|
1135
|
+
console.log(C.Code()) // return function check(value) {
|
|
1136
|
+
// return (
|
|
1137
|
+
// (typeof value === 'string')
|
|
1138
|
+
// )
|
|
1139
|
+
// }
|
|
1140
|
+
```
|
|
1141
|
+
|
|
1142
|
+
<a name='typesystem'></a>
|
|
1143
|
+
|
|
1144
|
+
## TypeSystem
|
|
1145
|
+
|
|
1146
|
+
The TypeBox TypeSystem module provides functionality to define types above and beyond the Standard and Extended type sets as well as control various assertion polices. Configurations made to the TypeSystem module are observed by both `TypeCompiler` and `Value` modules.
|
|
1147
|
+
|
|
1148
|
+
The TypeSystem module is provided as an optional import.
|
|
1149
|
+
|
|
1150
|
+
```typescript
|
|
1151
|
+
import { TypeSystem } from '@sinclair/typebox/system'
|
|
1152
|
+
```
|
|
1153
|
+
|
|
1154
|
+
<a name='typesystem-types'></a>
|
|
1155
|
+
|
|
1156
|
+
### Types
|
|
1157
|
+
|
|
1158
|
+
Use the `Type(...)` function to create a custom type. This function will return a type factory function that can be used to construct the type. The following creates a Point type.
|
|
1159
|
+
|
|
1160
|
+
```typescript
|
|
1161
|
+
type PointOptions = { } // The Type Options
|
|
1162
|
+
|
|
1163
|
+
type PointType = { x: number, y: number } // The Static<T> Type
|
|
1164
|
+
|
|
1165
|
+
const Point = TypeSystem.Type<PointType, PointOptions>('Point', (options, value) => {
|
|
1166
|
+
return (
|
|
1167
|
+
typeof value === 'object' && value !== null &&
|
|
1168
|
+
typeof value.x === 'number' &&
|
|
1169
|
+
typeof value.y === 'number'
|
|
1170
|
+
)
|
|
1171
|
+
})
|
|
1172
|
+
|
|
1173
|
+
const T = Point()
|
|
1174
|
+
|
|
1175
|
+
type T = Static<typeof T> // type T = { x: number, y: number }
|
|
1176
|
+
|
|
1177
|
+
const R = Value.Check(T, { x: 1, y: 2 }) // const R = true
|
|
1178
|
+
```
|
|
1179
|
+
|
|
1180
|
+
<a name='typesystem-formats'></a>
|
|
1181
|
+
|
|
1182
|
+
### Formats
|
|
1183
|
+
|
|
1184
|
+
Use the `Format(...)` function to create a custom string format. The following creates a custom string format that checks for lowercase strings.
|
|
1185
|
+
|
|
1186
|
+
```typescript
|
|
1187
|
+
TypeSystem.Format('lowercase', value => value === value.toLowerCase()) // format should be lowercase
|
|
1188
|
+
|
|
1189
|
+
const T = Type.String({ format: 'lowercase' })
|
|
1190
|
+
|
|
1191
|
+
const A = Value.Check(T, 'action') // const A = true
|
|
1192
|
+
|
|
1193
|
+
const B = Value.Check(T, 'ACTION') // const B = false
|
|
1194
|
+
```
|
|
1195
|
+
|
|
1196
|
+
<a name='typesystem-policies'></a>
|
|
1197
|
+
|
|
1198
|
+
### Policies
|
|
1199
|
+
|
|
1200
|
+
TypeBox validates using JSON Schema assertion policies by default. It is possible to override these policies and have TypeBox assert using TypeScript policies. The following overrides are available.
|
|
1201
|
+
|
|
1202
|
+
```typescript
|
|
1203
|
+
// Allow arrays to validate as object types (default is false)
|
|
1204
|
+
//
|
|
1205
|
+
// const A: {} = [] - allowed in TS
|
|
1206
|
+
|
|
1207
|
+
TypeSystem.AllowArrayObjects = true
|
|
1208
|
+
|
|
1209
|
+
// Allow numeric values to be NaN or + or - Infinity (default is false)
|
|
1210
|
+
//
|
|
1211
|
+
// const A: number = NaN - allowed in TS
|
|
1212
|
+
|
|
1213
|
+
TypeSystem.AllowNaN = true
|
|
1214
|
+
```
|
|
1215
|
+
|
|
1216
|
+
<a name='benchmark'></a>
|
|
1217
|
+
|
|
1218
|
+
## Benchmark
|
|
1219
|
+
|
|
1220
|
+
This project maintains a set of benchmarks that measure Ajv, Value and TypeCompiler compilation and validation performance. These benchmarks can be run locally by cloning this repository and running `npm run benchmark`. The results below show for Ajv version 8.11.2.
|
|
1221
|
+
|
|
1222
|
+
For additional comparative benchmarks, please refer to [typescript-runtime-type-benchmarks](https://moltar.github.io/typescript-runtime-type-benchmarks/).
|
|
1223
|
+
|
|
1224
|
+
<a name='benchmark-compile'></a>
|
|
1225
|
+
|
|
1226
|
+
### Compile
|
|
1227
|
+
|
|
1228
|
+
This benchmark measures compilation performance for varying types. You can review this benchmark [here](https://github.com/sinclairzx81/typebox/blob/master/benchmark/measurement/module/compile.ts).
|
|
1229
|
+
|
|
1230
|
+
```typescript
|
|
1231
|
+
┌────────────────────────────┬────────────┬──────────────┬──────────────┬──────────────┐
|
|
1232
|
+
│ (index) │ Iterations │ Ajv │ TypeCompiler │ Performance │
|
|
1233
|
+
├────────────────────────────┼────────────┼──────────────┼──────────────┼──────────────┤
|
|
1234
|
+
│ Literal_String │ 1000 │ ' 260 ms' │ ' 8 ms' │ ' 32.50 x' │
|
|
1235
|
+
│ Literal_Number │ 1000 │ ' 198 ms' │ ' 4 ms' │ ' 49.50 x' │
|
|
1236
|
+
│ Literal_Boolean │ 1000 │ ' 185 ms' │ ' 5 ms' │ ' 37.00 x' │
|
|
1237
|
+
│ Primitive_Number │ 1000 │ ' 176 ms' │ ' 9 ms' │ ' 19.56 x' │
|
|
1238
|
+
│ Primitive_String │ 1000 │ ' 161 ms' │ ' 9 ms' │ ' 17.89 x' │
|
|
1239
|
+
│ Primitive_String_Pattern │ 1000 │ ' 215 ms' │ ' 12 ms' │ ' 17.92 x' │
|
|
1240
|
+
│ Primitive_Boolean │ 1000 │ ' 133 ms' │ ' 5 ms' │ ' 26.60 x' │
|
|
1241
|
+
│ Primitive_Null │ 1000 │ ' 143 ms' │ ' 8 ms' │ ' 17.88 x' │
|
|
1242
|
+
│ Object_Unconstrained │ 1000 │ ' 1181 ms' │ ' 38 ms' │ ' 31.08 x' │
|
|
1243
|
+
│ Object_Constrained │ 1000 │ ' 1168 ms' │ ' 32 ms' │ ' 36.50 x' │
|
|
1244
|
+
│ Tuple_Primitive │ 1000 │ ' 557 ms' │ ' 16 ms' │ ' 34.81 x' │
|
|
1245
|
+
│ Tuple_Object │ 1000 │ ' 1119 ms' │ ' 17 ms' │ ' 65.82 x' │
|
|
1246
|
+
│ Composite_Intersect │ 1000 │ ' 569 ms' │ ' 22 ms' │ ' 25.86 x' │
|
|
1247
|
+
│ Composite_Union │ 1000 │ ' 513 ms' │ ' 23 ms' │ ' 22.30 x' │
|
|
1248
|
+
│ Math_Vector4 │ 1000 │ ' 802 ms' │ ' 10 ms' │ ' 80.20 x' │
|
|
1249
|
+
│ Math_Matrix4 │ 1000 │ ' 395 ms' │ ' 12 ms' │ ' 32.92 x' │
|
|
1250
|
+
│ Array_Primitive_Number │ 1000 │ ' 282 ms' │ ' 8 ms' │ ' 35.25 x' │
|
|
1251
|
+
│ Array_Primitive_String │ 1000 │ ' 321 ms' │ ' 5 ms' │ ' 64.20 x' │
|
|
1252
|
+
│ Array_Primitive_Boolean │ 1000 │ ' 364 ms' │ ' 5 ms' │ ' 72.80 x' │
|
|
1253
|
+
│ Array_Object_Unconstrained │ 1000 │ ' 1573 ms' │ ' 18 ms' │ ' 87.39 x' │
|
|
1254
|
+
│ Array_Object_Constrained │ 1000 │ ' 1270 ms' │ ' 20 ms' │ ' 63.50 x' │
|
|
1255
|
+
│ Array_Tuple_Primitive │ 1000 │ ' 973 ms' │ ' 18 ms' │ ' 54.06 x' │
|
|
1256
|
+
│ Array_Tuple_Object │ 1000 │ ' 1253 ms' │ ' 16 ms' │ ' 78.31 x' │
|
|
1257
|
+
│ Array_Composite_Intersect │ 1000 │ ' 927 ms' │ ' 20 ms' │ ' 46.35 x' │
|
|
1258
|
+
│ Array_Composite_Union │ 1000 │ ' 1123 ms' │ ' 16 ms' │ ' 70.19 x' │
|
|
1259
|
+
│ Array_Math_Vector4 │ 1000 │ ' 1068 ms' │ ' 10 ms' │ ' 106.80 x' │
|
|
1260
|
+
│ Array_Math_Matrix4 │ 1000 │ ' 488 ms' │ ' 7 ms' │ ' 69.71 x' │
|
|
1261
|
+
└────────────────────────────┴────────────┴──────────────┴──────────────┴──────────────┘
|
|
1262
|
+
```
|
|
1263
|
+
|
|
1264
|
+
<a name='benchmark-validate'></a>
|
|
1265
|
+
|
|
1266
|
+
### Validate
|
|
1267
|
+
|
|
1268
|
+
This benchmark measures validation performance for varying types. You can review this benchmark [here](https://github.com/sinclairzx81/typebox/blob/master/benchmark/measurement/module/check.ts).
|
|
1269
|
+
|
|
1270
|
+
```typescript
|
|
1271
|
+
┌────────────────────────────┬────────────┬──────────────┬──────────────┬──────────────┬──────────────┐
|
|
1272
|
+
│ (index) │ Iterations │ ValueCheck │ Ajv │ TypeCompiler │ Performance │
|
|
1273
|
+
├────────────────────────────┼────────────┼──────────────┼──────────────┼──────────────┼──────────────┤
|
|
1274
|
+
│ Literal_String │ 1000000 │ ' 26 ms' │ ' 6 ms' │ ' 6 ms' │ ' 1.00 x' │
|
|
1275
|
+
│ Literal_Number │ 1000000 │ ' 21 ms' │ ' 19 ms' │ ' 11 ms' │ ' 1.73 x' │
|
|
1276
|
+
│ Literal_Boolean │ 1000000 │ ' 19 ms' │ ' 18 ms' │ ' 10 ms' │ ' 1.80 x' │
|
|
1277
|
+
│ Primitive_Number │ 1000000 │ ' 24 ms' │ ' 19 ms' │ ' 11 ms' │ ' 1.73 x' │
|
|
1278
|
+
│ Primitive_String │ 1000000 │ ' 26 ms' │ ' 17 ms' │ ' 10 ms' │ ' 1.70 x' │
|
|
1279
|
+
│ Primitive_String_Pattern │ 1000000 │ ' 159 ms' │ ' 45 ms' │ ' 37 ms' │ ' 1.22 x' │
|
|
1280
|
+
│ Primitive_Boolean │ 1000000 │ ' 23 ms' │ ' 17 ms' │ ' 10 ms' │ ' 1.70 x' │
|
|
1281
|
+
│ Primitive_Null │ 1000000 │ ' 23 ms' │ ' 18 ms' │ ' 10 ms' │ ' 1.80 x' │
|
|
1282
|
+
│ Object_Unconstrained │ 1000000 │ ' 809 ms' │ ' 35 ms' │ ' 30 ms' │ ' 1.17 x' │
|
|
1283
|
+
│ Object_Constrained │ 1000000 │ ' 1060 ms' │ ' 56 ms' │ ' 45 ms' │ ' 1.24 x' │
|
|
1284
|
+
│ Object_Recursive │ 1000000 │ ' 4965 ms' │ ' 397 ms' │ ' 100 ms' │ ' 3.97 x' │
|
|
1285
|
+
│ Tuple_Primitive │ 1000000 │ ' 159 ms' │ ' 22 ms' │ ' 16 ms' │ ' 1.38 x' │
|
|
1286
|
+
│ Tuple_Object │ 1000000 │ ' 658 ms' │ ' 31 ms' │ ' 27 ms' │ ' 1.15 x' │
|
|
1287
|
+
│ Composite_Intersect │ 1000000 │ ' 695 ms' │ ' 26 ms' │ ' 22 ms' │ ' 1.18 x' │
|
|
1288
|
+
│ Composite_Union │ 1000000 │ ' 503 ms' │ ' 24 ms' │ ' 19 ms' │ ' 1.26 x' │
|
|
1289
|
+
│ Math_Vector4 │ 1000000 │ ' 259 ms' │ ' 22 ms' │ ' 14 ms' │ ' 1.57 x' │
|
|
1290
|
+
│ Math_Matrix4 │ 1000000 │ ' 1007 ms' │ ' 40 ms' │ ' 29 ms' │ ' 1.38 x' │
|
|
1291
|
+
│ Array_Primitive_Number │ 1000000 │ ' 262 ms' │ ' 23 ms' │ ' 17 ms' │ ' 1.35 x' │
|
|
1292
|
+
│ Array_Primitive_String │ 1000000 │ ' 241 ms' │ ' 27 ms' │ ' 24 ms' │ ' 1.13 x' │
|
|
1293
|
+
│ Array_Primitive_Boolean │ 1000000 │ ' 141 ms' │ ' 23 ms' │ ' 20 ms' │ ' 1.15 x' │
|
|
1294
|
+
│ Array_Object_Unconstrained │ 1000000 │ ' 4976 ms' │ ' 70 ms' │ ' 67 ms' │ ' 1.04 x' │
|
|
1295
|
+
│ Array_Object_Constrained │ 1000000 │ ' 5234 ms' │ ' 143 ms' │ ' 120 ms' │ ' 1.19 x' │
|
|
1296
|
+
│ Array_Object_Recursive │ 1000000 │ ' 19605 ms' │ ' 1909 ms' │ ' 350 ms' │ ' 5.45 x' │
|
|
1297
|
+
│ Array_Tuple_Primitive │ 1000000 │ ' 706 ms' │ ' 39 ms' │ ' 32 ms' │ ' 1.22 x' │
|
|
1298
|
+
│ Array_Tuple_Object │ 1000000 │ ' 2951 ms' │ ' 67 ms' │ ' 63 ms' │ ' 1.06 x' │
|
|
1299
|
+
│ Array_Composite_Intersect │ 1000000 │ ' 2969 ms' │ ' 49 ms' │ ' 44 ms' │ ' 1.11 x' │
|
|
1300
|
+
│ Array_Composite_Union │ 1000000 │ ' 2191 ms' │ ' 77 ms' │ ' 41 ms' │ ' 1.88 x' │
|
|
1301
|
+
│ Array_Math_Vector4 │ 1000000 │ ' 1164 ms' │ ' 41 ms' │ ' 25 ms' │ ' 1.64 x' │
|
|
1302
|
+
│ Array_Math_Matrix4 │ 1000000 │ ' 4903 ms' │ ' 115 ms' │ ' 99 ms' │ ' 1.16 x' │
|
|
1303
|
+
└────────────────────────────┴────────────┴──────────────┴──────────────┴──────────────┴──────────────┘
|
|
1304
|
+
```
|
|
1305
|
+
|
|
1306
|
+
<a name='benchmark-compression'></a>
|
|
1307
|
+
|
|
1308
|
+
### Compression
|
|
1309
|
+
|
|
1310
|
+
The following table lists esbuild compiled and minified sizes for each TypeBox module.
|
|
1311
|
+
|
|
1312
|
+
```typescript
|
|
1313
|
+
┌──────────────────────┬────────────┬────────────┬─────────────┐
|
|
1314
|
+
│ (index) │ Compiled │ Minified │ Compression │
|
|
1315
|
+
├──────────────────────┼────────────┼────────────┼─────────────┤
|
|
1316
|
+
│ typebox/compiler │ '108.8 kb' │ ' 48.9 kb' │ '2.23 x' │
|
|
1317
|
+
│ typebox/errors │ ' 93.2 kb' │ ' 41.5 kb' │ '2.24 x' │
|
|
1318
|
+
│ typebox/system │ ' 60.0 kb' │ ' 24.6 kb' │ '2.43 x' │
|
|
1319
|
+
│ typebox/value │ '153.5 kb' │ ' 66.7 kb' │ '2.30 x' │
|
|
1320
|
+
│ typebox │ ' 58.7 kb' │ ' 24.1 kb' │ '2.43 x' │
|
|
1321
|
+
└──────────────────────┴────────────┴────────────┴─────────────┘
|
|
1322
|
+
```
|
|
1323
|
+
|
|
1324
|
+
<a name='contribute'></a>
|
|
1325
|
+
|
|
1326
|
+
## Contribute
|
|
1327
|
+
|
|
1328
|
+
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.
|