typebox 1.0.67 → 1.0.68
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/build/compile/index.d.mts +2 -6
- package/build/compile/index.mjs +2 -1
- package/package.json +1 -1
- package/readme.md +113 -71
|
@@ -4,9 +4,5 @@ export * from './validator.mjs';
|
|
|
4
4
|
import { Code } from './code.mjs';
|
|
5
5
|
import { Compile } from './compile.mjs';
|
|
6
6
|
import { Validator } from './validator.mjs';
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
Compile: typeof Compile;
|
|
10
|
-
Validator: typeof Validator;
|
|
11
|
-
};
|
|
12
|
-
export default _default;
|
|
7
|
+
export { Code, Compile, Validator };
|
|
8
|
+
export default Compile;
|
package/build/compile/index.mjs
CHANGED
|
@@ -10,4 +10,5 @@ export * from './validator.mjs';
|
|
|
10
10
|
import { Code } from './code.mjs';
|
|
11
11
|
import { Compile } from './compile.mjs';
|
|
12
12
|
import { Validator } from './validator.mjs';
|
|
13
|
-
export
|
|
13
|
+
export { Code, Compile, Validator };
|
|
14
|
+
export default Compile;
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
<h1>TypeBox</h1>
|
|
4
4
|
|
|
5
|
-
<p>
|
|
5
|
+
<p>JSON Schema Type Builder with Static Type Resolution for TypeScript</p>
|
|
6
6
|
|
|
7
7
|
<img src="typebox.png" />
|
|
8
8
|
|
|
@@ -25,12 +25,12 @@ $ npm install typebox
|
|
|
25
25
|
|
|
26
26
|
## Usage
|
|
27
27
|
|
|
28
|
-
A TypeScript engine for
|
|
28
|
+
A TypeScript first validation engine for JSON Schema [Example](https://tsplay.dev/mZMOeN)
|
|
29
29
|
|
|
30
30
|
```typescript
|
|
31
31
|
import Type from 'typebox'
|
|
32
32
|
|
|
33
|
-
//
|
|
33
|
+
// JSON Schema
|
|
34
34
|
|
|
35
35
|
const T = Type.Object({ // const T = {
|
|
36
36
|
x: Type.Number(), // type: 'object',
|
|
@@ -51,49 +51,39 @@ type T = Type.Static<typeof T> // type T = {
|
|
|
51
51
|
// TypeScript
|
|
52
52
|
|
|
53
53
|
const { S } = Type.Script({ T }, `
|
|
54
|
-
type RenameKey<K> =
|
|
55
|
-
K extends 'x' ? 'a' :
|
|
56
|
-
K extends 'y' ? 'b' :
|
|
57
|
-
K extends 'z' ? 'c' :
|
|
58
|
-
K
|
|
59
|
-
|
|
60
54
|
type S = {
|
|
61
|
-
readonly [K in keyof T as
|
|
55
|
+
readonly [K in keyof T as Uppercase<K>]: string
|
|
62
56
|
}
|
|
63
57
|
`)
|
|
64
58
|
|
|
65
|
-
type S = Type.Static<typeof S>
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
59
|
+
type S = Type.Static<typeof S> // type S = {
|
|
60
|
+
// readonly X: string,
|
|
61
|
+
// readonly Y: string,
|
|
62
|
+
// readonly Z: string
|
|
63
|
+
// }
|
|
70
64
|
```
|
|
71
65
|
|
|
72
66
|
## Overview
|
|
73
67
|
|
|
74
68
|
[Documentation](https://sinclairzx81.github.io/typebox/)
|
|
75
69
|
|
|
76
|
-
TypeBox is a runtime type system that creates in-memory
|
|
70
|
+
TypeBox is a runtime type system that creates in-memory JSON Schema objects that infer as TypeScript types. The schematics produced by this library are designed to match the static type checking rules of the TypeScript compiler. TypeBox offers a unified type system that can be statically checked by TypeScript and validated at runtime using standard JSON Schema.
|
|
77
71
|
|
|
78
|
-
This library is designed to allow
|
|
72
|
+
This library is designed to allow JSON Schema to compose similar to how types compose within TypeScript's type system. It can be used as a simple tool to build up complex schematics or integrated into REST and RPC services to help validate data received over the wire.
|
|
79
73
|
|
|
80
74
|
License: MIT
|
|
81
75
|
|
|
82
76
|
## Contents
|
|
83
77
|
|
|
84
|
-
|
|
78
|
+
|
|
85
79
|
- [Type](#Type)
|
|
86
80
|
- [Value](#Value)
|
|
87
|
-
- [Compile](#Compile)
|
|
88
81
|
- [Script](#Script)
|
|
82
|
+
- [Compile](#Compile)
|
|
89
83
|
- [Schema](#Schema)
|
|
84
|
+
- [Legacy](#Legacy)
|
|
90
85
|
- [Contribute](#Contribute)
|
|
91
86
|
|
|
92
|
-
## Upgrade
|
|
93
|
-
|
|
94
|
-
If upgrading from `@sinclair/typebox` refer to the 1.0 migration guide at the following URL.
|
|
95
|
-
|
|
96
|
-
[Migration Guide](https://github.com/sinclairzx81/typebox/blob/main/changelog/1.0.0-migration.md)
|
|
97
87
|
|
|
98
88
|
<a name="Type"></a>
|
|
99
89
|
|
|
@@ -101,11 +91,11 @@ If upgrading from `@sinclair/typebox` refer to the 1.0 migration guide at the fo
|
|
|
101
91
|
|
|
102
92
|
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/type/overview) | [Example](https://tsplay.dev/NaMoBN)
|
|
103
93
|
|
|
104
|
-
TypeBox provides many functions to create
|
|
94
|
+
TypeBox provides many functions to create JSON Schema types. Each function returns a small JSON Schema fragment that can be composed into more complex types. TypeBox includes a set of functions that are used to construct JSON Schema compliant schematics as well as a set of extended functions that return schematics for constructs native to JavaScript.
|
|
105
95
|
|
|
106
96
|
## Example
|
|
107
97
|
|
|
108
|
-
The following creates a
|
|
98
|
+
The following creates a JSON Schema type and infers with Static.
|
|
109
99
|
|
|
110
100
|
```typescript
|
|
111
101
|
import Type from 'typebox'
|
|
@@ -130,17 +120,16 @@ type T = Type.Static<typeof T> // type T = {
|
|
|
130
120
|
Schema options can be passed on the last argument of any given type.
|
|
131
121
|
|
|
132
122
|
```typescript
|
|
133
|
-
const T = Type.String({
|
|
134
|
-
format: 'email'
|
|
135
|
-
})
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
const S = Type.Number({ // const S = {
|
|
139
|
-
minimum: 0, // type: 'number',
|
|
140
|
-
maximum: 100 // minimum: 0,
|
|
141
|
-
}) // maximum: 100
|
|
142
|
-
// }
|
|
123
|
+
const T = Type.String({ // const T = {
|
|
124
|
+
format: 'email' // type: 'string',
|
|
125
|
+
}) // format: 'email'
|
|
126
|
+
// }
|
|
143
127
|
|
|
128
|
+
const S = Type.Number({ // const S = {
|
|
129
|
+
minimum: 0, // type: 'number',
|
|
130
|
+
maximum: 100 // minimum: 0,
|
|
131
|
+
}) // maximum: 100
|
|
132
|
+
// }
|
|
144
133
|
```
|
|
145
134
|
|
|
146
135
|
<a name="Value"></a>
|
|
@@ -173,42 +162,11 @@ const A = Value.Parse(T, { // const A: {
|
|
|
173
162
|
}) // } = ...
|
|
174
163
|
```
|
|
175
164
|
|
|
176
|
-
|
|
177
|
-
<a name="Compile"></a>
|
|
178
|
-
|
|
179
|
-
## Compile
|
|
180
|
-
|
|
181
|
-
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/compile/overview) | [Example](https://tsplay.dev/WyraZw)
|
|
182
|
-
|
|
183
|
-
The Compile submodule is a high-performance Json Schema compliant JIT compiler that compiles schematics into efficient runtime validators. The compiler is optimized for fast compilation and validation and is known to be one of the fastest validation solutions available for JavaScript.
|
|
184
|
-
|
|
185
|
-
```typescript
|
|
186
|
-
import { Compile } from 'typebox/compile'
|
|
187
|
-
```
|
|
188
|
-
|
|
189
|
-
### Example
|
|
190
|
-
|
|
191
|
-
The following uses the compiler to Compile and Parse a value.
|
|
192
|
-
|
|
193
|
-
```typescript
|
|
194
|
-
const C = Compile(Type.Object({ // const C: Validator<{}, TObject<{
|
|
195
|
-
x: Type.Number(), // x: TNumber,
|
|
196
|
-
y: Type.Number(), // y: TNumber,
|
|
197
|
-
z: Type.Number() // z: TNumber
|
|
198
|
-
})) // }>>
|
|
199
|
-
|
|
200
|
-
const A = C.Parse({ // const A: {
|
|
201
|
-
x: 0, // x: number,
|
|
202
|
-
y: 1, // y: number,
|
|
203
|
-
z: 0 // z: number
|
|
204
|
-
}) // } = ...
|
|
205
|
-
```
|
|
206
|
-
|
|
207
165
|
## Script
|
|
208
166
|
|
|
209
167
|
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/script/overview) | [Example 1](https://tsplay.dev/Wk6L1m) | [Example 2](https://tsplay.dev/NnrJoN)
|
|
210
168
|
|
|
211
|
-
TypeBox is a runtime TypeScript DSL engine that can create, transform, and compute
|
|
169
|
+
TypeBox is a runtime TypeScript DSL engine that can create, transform, and compute JSON Schema using native TypeScript syntax. The engine is implemented symmetrically at runtime and within the TypeScript type system, and is intended for use with the TypeScript 7 native compiler and above.
|
|
212
170
|
|
|
213
171
|
```typescript
|
|
214
172
|
// Scripted Type
|
|
@@ -223,7 +181,7 @@ const T = Type.Script(`{
|
|
|
223
181
|
// z: TBoolean
|
|
224
182
|
// }>
|
|
225
183
|
|
|
226
|
-
//
|
|
184
|
+
// JSON Schema Introspection
|
|
227
185
|
|
|
228
186
|
T.type // 'object'
|
|
229
187
|
T.required // ['x', 'y', 'z']
|
|
@@ -248,17 +206,47 @@ type S = Type.Static<typeof S> // type S = {
|
|
|
248
206
|
// }
|
|
249
207
|
```
|
|
250
208
|
|
|
209
|
+
<a name="Compile"></a>
|
|
210
|
+
|
|
211
|
+
## Compile
|
|
212
|
+
|
|
213
|
+
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/compile/overview) | [Example](https://tsplay.dev/WyraZw)
|
|
214
|
+
|
|
215
|
+
The Compile submodule is a high-performance JSON Schema compliant JIT compiler that compiles schematics into efficient runtime validators. The compiler is optimized for fast compilation and validation and is known to be one of the fastest validation solutions available for JavaScript.
|
|
216
|
+
|
|
217
|
+
```typescript
|
|
218
|
+
import Compile from 'typebox/compile'
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
### Example
|
|
222
|
+
|
|
223
|
+
The following uses the compiler to Compile and Parse a value.
|
|
224
|
+
|
|
225
|
+
```typescript
|
|
226
|
+
const C = Compile(Type.Object({ // const C: Validator<{}, TObject<{
|
|
227
|
+
x: Type.Number(), // x: TNumber,
|
|
228
|
+
y: Type.Number(), // y: TNumber,
|
|
229
|
+
z: Type.Number() // z: TNumber
|
|
230
|
+
})) // }>>
|
|
231
|
+
|
|
232
|
+
const A = C.Parse({ // const A: {
|
|
233
|
+
x: 0, // x: number,
|
|
234
|
+
y: 1, // y: number,
|
|
235
|
+
z: 0 // z: number
|
|
236
|
+
}) // } = ...
|
|
237
|
+
```
|
|
238
|
+
|
|
251
239
|
<a name="Schema"></a>
|
|
252
240
|
|
|
253
241
|
## Schema
|
|
254
242
|
|
|
255
243
|
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/schema/overview) | [Example 1](https://tsplay.dev/Wvrv3W) | [Example 2](https://tsplay.dev/m3g0ym)
|
|
256
244
|
|
|
257
|
-
TypeBox is built upon a high-performance validation infrastructure that supports the direct compilation and inference of
|
|
245
|
+
TypeBox is built upon a high-performance validation infrastructure that supports the direct compilation and inference of JSON Schema schematics. TypeBox implements Draft 3 to 2020-12 and is compliance tested via the official JSON Schema [Test Suite](https://github.com/JSON-schema-org/JSON-Schema-Test-Suite). It offers high-performance JIT compilation with automatic fallback to dynamic checking in JIT restricted environments.
|
|
258
246
|
|
|
259
247
|
### Example
|
|
260
248
|
|
|
261
|
-
The following compiles
|
|
249
|
+
The following compiles JSON Schema. Type inference is supported.
|
|
262
250
|
|
|
263
251
|
```typescript
|
|
264
252
|
const C = Compile({
|
|
@@ -278,6 +266,60 @@ const A = C.Parse({ // const A: {
|
|
|
278
266
|
}) // } = ...
|
|
279
267
|
```
|
|
280
268
|
|
|
269
|
+
## Legacy
|
|
270
|
+
|
|
271
|
+
If upgrading from `@sinclair/typebox` 0.34.x refer to the 1.0 migration guide at the following URL.
|
|
272
|
+
|
|
273
|
+
[Migration Guide](https://github.com/sinclairzx81/typebox/blob/main/changelog/1.0.0-migration.md)
|
|
274
|
+
|
|
275
|
+
Most types created with 0.34.x are compatible with V1, and it is possible to run both `typebox` and `@sinclair/typebox` packages side by side.
|
|
276
|
+
|
|
277
|
+
[Compatibility](https://tsplay.dev/Wzr2rW)
|
|
278
|
+
|
|
279
|
+
```typescript
|
|
280
|
+
import { Type } from '@sinclair/typebox' // TB: 0.34.x
|
|
281
|
+
import { Static } from 'typebox' // TB: 1.0.0
|
|
282
|
+
|
|
283
|
+
// Legacy Types
|
|
284
|
+
|
|
285
|
+
const A = Type.Object({
|
|
286
|
+
x: Type.Number(),
|
|
287
|
+
y: Type.Number(),
|
|
288
|
+
z: Type.Number()
|
|
289
|
+
})
|
|
290
|
+
|
|
291
|
+
const B = Type.Object({
|
|
292
|
+
a: Type.Number(),
|
|
293
|
+
b: Type.Number(),
|
|
294
|
+
c: Type.Number()
|
|
295
|
+
})
|
|
296
|
+
|
|
297
|
+
const C = Type.Composite([A, B])
|
|
298
|
+
|
|
299
|
+
// Modern Inference
|
|
300
|
+
|
|
301
|
+
type C = Static<typeof C> // type C = {
|
|
302
|
+
// x: number;
|
|
303
|
+
// y: number;
|
|
304
|
+
// z: number;
|
|
305
|
+
// a: number;
|
|
306
|
+
// b: number;
|
|
307
|
+
// c: number;
|
|
308
|
+
// }
|
|
309
|
+
|
|
310
|
+
// Modern Compile
|
|
311
|
+
|
|
312
|
+
import Compile from 'typebox/compile'
|
|
313
|
+
|
|
314
|
+
const Result = Compile(C).Check({ ... })
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
Revision 0.34.x is actively maintained at the following URL.
|
|
318
|
+
|
|
319
|
+
[TypeBox 0.34.x](https://github.com/sinclairzx81/typebox-legacy)
|
|
320
|
+
|
|
321
|
+
Please submit non-1.0 issues to this repository.
|
|
322
|
+
|
|
281
323
|
## Contribute
|
|
282
324
|
|
|
283
325
|
TypeBox is open to community contribution. Please ensure you submit an issue before submitting a pull request. The TypeBox project prefers open community discussion before accepting new features.
|