typebox 1.1.13 → 1.1.14
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.
|
@@ -6,5 +6,5 @@ import { type TUnknown } from '../types/unknown.mjs';
|
|
|
6
6
|
import * as Result from './result.mjs';
|
|
7
7
|
import { type TExtendsParameters } from './parameters.mjs';
|
|
8
8
|
import { type TExtendsReturnType } from './return-type.mjs';
|
|
9
|
-
export type TExtendsConstructor<Inferred extends TProperties, Parameters extends TSchema[], InstanceType extends TSchema, Right extends TSchema> = (Right extends TAny ? Result.TExtendsTrue : Right extends TUnknown ? Result.TExtendsTrue : Right extends TConstructor ? TExtendsParameters<Inferred, Parameters, Right['parameters']> extends Result.TExtendsTrueLike<infer Inferred extends TProperties> ? TExtendsReturnType<Inferred, InstanceType, Right['instanceType']> : Result.TExtendsFalse : Result.TExtendsFalse);
|
|
9
|
+
export type TExtendsConstructor<Inferred extends TProperties, Parameters extends TSchema[], InstanceType extends TSchema, Right extends TSchema> = (Right extends TAny ? Result.TExtendsTrue<Inferred> : Right extends TUnknown ? Result.TExtendsTrue<Inferred> : Right extends TConstructor ? TExtendsParameters<Inferred, Parameters, Right['parameters']> extends Result.TExtendsTrueLike<infer Inferred extends TProperties> ? TExtendsReturnType<Inferred, InstanceType, Right['instanceType']> : Result.TExtendsFalse : Result.TExtendsFalse);
|
|
10
10
|
export declare function ExtendsConstructor<Inferred extends TProperties, Parameters extends TSchema[], InstanceType extends TSchema, Right extends TSchema>(inferred: Inferred, parameters: [...Parameters], returnType: InstanceType, right: Right): TExtendsConstructor<Inferred, Parameters, InstanceType, Right>;
|
|
@@ -12,10 +12,7 @@ import * as Result from './result.mjs';
|
|
|
12
12
|
import { TemplateLiteralDecode } from '../engine/template-literal/decode.mjs';
|
|
13
13
|
import { EnumValuesToUnion } from '../engine/enum/index.mjs';
|
|
14
14
|
function ExtendsRightInfer(inferred, name, left, right) {
|
|
15
|
-
|
|
16
|
-
return (Result.IsExtendsTrueLike(check)
|
|
17
|
-
? Result.ExtendsTrue(Memory.Assign(Memory.Assign(inferred, check.inferred), { [name]: left }))
|
|
18
|
-
: Result.ExtendsFalse());
|
|
15
|
+
return Result.Match(ExtendsLeft(inferred, left, right), checkInferred => Result.ExtendsTrue(Memory.Assign(Memory.Assign(inferred, checkInferred), { [name]: left })), () => Result.ExtendsFalse());
|
|
19
16
|
}
|
|
20
17
|
function ExtendsRightAny(inferred, left) {
|
|
21
18
|
return Result.ExtendsTrue(inferred);
|
|
@@ -6,5 +6,5 @@ import { type TUnknown } from '../types/unknown.mjs';
|
|
|
6
6
|
import * as Result from './result.mjs';
|
|
7
7
|
import { type TExtendsParameters } from './parameters.mjs';
|
|
8
8
|
import { type TExtendsReturnType } from './return-type.mjs';
|
|
9
|
-
export type TExtendsFunction<Inferred extends TProperties, Parameters extends TSchema[], ReturnType extends TSchema, Right extends TSchema> = (Right extends TAny ? Result.TExtendsTrue : Right extends TUnknown ? Result.TExtendsTrue : Right extends TFunction ? TExtendsParameters<Inferred, Parameters, Right['parameters']> extends Result.TExtendsTrueLike<infer Inferred extends TProperties> ? TExtendsReturnType<Inferred, ReturnType, Right['returnType']> : Result.TExtendsFalse : Result.TExtendsFalse);
|
|
9
|
+
export type TExtendsFunction<Inferred extends TProperties, Parameters extends TSchema[], ReturnType extends TSchema, Right extends TSchema> = (Right extends TAny ? Result.TExtendsTrue<Inferred> : Right extends TUnknown ? Result.TExtendsTrue<Inferred> : Right extends TFunction ? TExtendsParameters<Inferred, Parameters, Right['parameters']> extends Result.TExtendsTrueLike<infer Inferred extends TProperties> ? TExtendsReturnType<Inferred, ReturnType, Right['returnType']> : Result.TExtendsFalse : Result.TExtendsFalse);
|
|
10
10
|
export declare function ExtendsFunction<Inferred extends TProperties, Parameters extends TSchema[], ReturnType extends TSchema, Right extends TSchema>(inferred: Inferred, parameters: [...Parameters], returnType: ReturnType, right: Right): TExtendsFunction<Inferred, Parameters, ReturnType, Right>;
|
|
@@ -9,12 +9,9 @@ function ParameterCompare(inferred, left, leftRest, right, rightRest) {
|
|
|
9
9
|
const checkRight = IsInfer(right) ? right : left;
|
|
10
10
|
const isLeftOptional = IsOptional(left);
|
|
11
11
|
const isRightOptional = IsOptional(right);
|
|
12
|
-
|
|
13
|
-
return (!isLeftOptional && isRightOptional
|
|
12
|
+
return ((!isLeftOptional && isRightOptional)
|
|
14
13
|
? Result.ExtendsFalse() // 'fail: left-required-but-right-is-optional'
|
|
15
|
-
: Result.
|
|
16
|
-
? ExtendsParameters(check.inferred, leftRest, rightRest)
|
|
17
|
-
: Result.ExtendsFalse() // 'fail: left-and-right-did-not-match'
|
|
14
|
+
: Result.Match(ExtendsLeft(inferred, checkLeft, checkRight), inferred => ExtendsParameters(inferred, leftRest, rightRest), () => Result.ExtendsFalse()) // 'fail: left-and-right-did-not-match'
|
|
18
15
|
);
|
|
19
16
|
}
|
|
20
17
|
function ParameterRight(inferred, left, leftRest, rightRest) {
|
|
@@ -23,11 +23,7 @@ function Reversed(types) {
|
|
|
23
23
|
return IsSchema(inferrable);
|
|
24
24
|
}
|
|
25
25
|
function ElementsCompare(inferred, reversed, left, leftRest, right, rightRest) {
|
|
26
|
-
|
|
27
|
-
return (Result.IsExtendsTrueLike(check)
|
|
28
|
-
? Elements(check.inferred, reversed, leftRest, rightRest)
|
|
29
|
-
: Result.ExtendsFalse() // 'left-and-right-not-compared'
|
|
30
|
-
);
|
|
26
|
+
return Result.Match(ExtendsLeft(inferred, left, right), checkInferred => Elements(checkInferred, reversed, leftRest, rightRest), () => Result.ExtendsFalse()); // 'left-and-right-not-compared'
|
|
31
27
|
}
|
|
32
28
|
function ElementsLeft(inferred, reversed, leftRest, right, rightRest) {
|
|
33
29
|
const inferable = TryRestInferable(right);
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -28,21 +28,21 @@ $ npm install typebox
|
|
|
28
28
|
```typescript
|
|
29
29
|
import Type from 'typebox'
|
|
30
30
|
|
|
31
|
-
const T = Type.Object({
|
|
32
|
-
x: Type.Number(),
|
|
33
|
-
y: Type.Number(),
|
|
34
|
-
z: Type.Number()
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
type T = Type.Static<typeof T>
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
31
|
+
const T = Type.Object({ // const T = {
|
|
32
|
+
x: Type.Number(), // type: 'object',
|
|
33
|
+
y: Type.Number(), // properties: {
|
|
34
|
+
z: Type.Number() // x: { type: 'number' },
|
|
35
|
+
}) // y: { type: 'number' },
|
|
36
|
+
// z: { type: 'number' }
|
|
37
|
+
// },
|
|
38
|
+
// required: ['x', 'y', 'z']
|
|
39
|
+
// }
|
|
40
|
+
|
|
41
|
+
type T = Type.Static<typeof T> // type T = {
|
|
42
|
+
// x: number,
|
|
43
|
+
// y: number,
|
|
44
|
+
// z: number
|
|
45
|
+
// }
|
|
46
46
|
```
|
|
47
47
|
|
|
48
48
|
## Overview
|
|
@@ -58,7 +58,6 @@ License: MIT
|
|
|
58
58
|
## Contents
|
|
59
59
|
|
|
60
60
|
- [Type](#Type)
|
|
61
|
-
- [Value](#Value)
|
|
62
61
|
- [Script](#Script)
|
|
63
62
|
- [Schema](#Schema)
|
|
64
63
|
- [Versions](#Versions)
|
|
@@ -69,157 +68,163 @@ License: MIT
|
|
|
69
68
|
|
|
70
69
|
## Type
|
|
71
70
|
|
|
72
|
-
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/type/overview)
|
|
71
|
+
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/type/overview) | [Example](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAFQJ5gKZwGZQiOByGFVAIwgA88AoSgehrgFonmXW32POvueHb7kafo16ix4ic2oBjCADsAzvACqC1FDgBeREQB0AeWIArVNJgAKAN5wbtu-Yc26cWYpVqN2y5RvAAJgBcOmi6AMowUMByAObmAJQANI7JKfbONoRoQXgQxqYweAk+cHIAhiCoQYKoYRFRsYmpTQ7pcGDYaLDAqApB3jaoIKXAADZVeuGRMVaY0EMw2YPDI3hwAL5xya2+gXDWmZX4SlPRq2tFG81X11v0dmUVfXAH2cf1Z0U3X99w23BLoyexR+INSfwyRFedRihTgwNBCLSd3sGDmpQW+ABKzhiNxdj+a3heIRrUJxLxrSgqAAjgBXYBU3YAbSJ5J+fzw-lhrLZXw5D1Q3N5JORNjwWKowtBrQAujypc1nIThJJVWr1XxnOF0cBpCqNQbDZxqAc4Kp1FpgjVtTBdQAeA4QDBmjwAPmuzlN5s8e3lCqarX8QTeMU+-u+rQFweh0TD4ZurSx0ZOfvjjiVQA)
|
|
73
72
|
|
|
74
|
-
TypeBox
|
|
73
|
+
TypeBox types are JSON Schema fragments that can compose into more complex types. The library offers a set of types used to construct JSON Schema compliant schematics as well as a set of extended types used to model constructs native to the JavaScript language. The schematics produced by TypeBox can be passed directly to any JSON Schema compliant validator.
|
|
75
74
|
|
|
76
75
|
## Example
|
|
77
76
|
|
|
78
|
-
The following creates a
|
|
77
|
+
The following creates a User type and infers with Static.
|
|
79
78
|
|
|
80
79
|
```typescript
|
|
81
80
|
import Type from 'typebox'
|
|
82
81
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
82
|
+
// -------------------------------------------------------------------------------
|
|
83
|
+
// Type
|
|
84
|
+
// -------------------------------------------------------------------------------
|
|
85
|
+
|
|
86
|
+
const User = Type.Object({ // const User = {
|
|
87
|
+
id: Type.String(), // type: 'object',
|
|
88
|
+
name: Type.String(), // properties: {
|
|
89
|
+
email: Type.String({ format: 'email' }) // id: { type: 'string' },
|
|
90
|
+
}) // name: { type: 'string' },
|
|
91
|
+
// email: {
|
|
92
|
+
// type: 'string',
|
|
93
|
+
// format: 'email'
|
|
94
|
+
// }
|
|
95
|
+
// }
|
|
96
|
+
// required: [
|
|
97
|
+
// 'id',
|
|
98
|
+
// 'name',
|
|
99
|
+
// 'email'
|
|
100
|
+
// ]
|
|
101
|
+
// }
|
|
102
|
+
|
|
103
|
+
// -------------------------------------------------------------------------------
|
|
104
|
+
// Static
|
|
105
|
+
// -------------------------------------------------------------------------------
|
|
106
|
+
|
|
107
|
+
type User = Type.Static<typeof User> // type User = {
|
|
108
|
+
// id: string,
|
|
109
|
+
// name: string,
|
|
110
|
+
// email: string
|
|
111
|
+
// }
|
|
98
112
|
```
|
|
99
113
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
```typescript
|
|
103
|
-
const T = Type.String({ // const T = {
|
|
104
|
-
format: 'email' // type: 'string',
|
|
105
|
-
}) // format: 'email'
|
|
106
|
-
// }
|
|
107
|
-
|
|
108
|
-
const S = Type.Number({ // const S = {
|
|
109
|
-
minimum: 0, // type: 'number',
|
|
110
|
-
maximum: 100 // minimum: 0,
|
|
111
|
-
}) // maximum: 100
|
|
112
|
-
// }
|
|
113
|
-
```
|
|
114
|
+
## Script
|
|
114
115
|
|
|
115
|
-
|
|
116
|
+
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/script/overview) | [Example 1](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAFQJ5gKZwGZQiOByGFVAIwgA88AoSgehrgFonmXW32POvueHb6AygGMowMDH6Ne0mbLnNqQiADsAzvADecAKqrUUADQ69UbWAAmAQxjoAvnAC8iIgDpho8QAoABtTjBlGygMSyF0AFFA4EI4DUo4f3MALjh1UWUAc3jbPwCgkLDjfThUMhtlc1U4SJhopFi4eITlSxBUFLSAjKMmkpBLYAAbDph0jMa4HPjCNCLTC2t0J3CAN0tBgFdFgB5egAVgIQBrbd19IyPUJAgMaqjCAD44ADIJhL3LWGB17YB5EGipxMFyuNzutUeD3iUO8AEpqHQpPJkSiUZIAEqoDCDVBCCSI1GEom8RQqVQQHEuQYQDKeM5QWEJJnMlmstksxFxdncnm83mIhIzdr4CDEABWuJgeAMvT5cvlbIFcDA2DQX1QqhSXIVOt1SoSwGSDSFKTwnUyeEmMt1Nvl+rgLTaWrgJvw5oylts1ttPu59tQ-SGztdZtGXU9st9UYSSq9kejvqVUFQAEcNsBk0aANrxhO2+14Q3St55qMFx2oYu50s6gsBgaDKg1sv0BIAXWrzbliKmSjUFNQVJpdJMZisNkZec5na7-NbLqIptFErx0pns7985VEDVtQ1WvXG8V84NRq0Ifdnu9R4V9orwcXbrDFqth5vzP9geGxsfobGEffW95zjQC7XnZM0wzVBs0Lcw8A7UDu3oKYCWJND0IUREBBgaxDkkDCCOJaghTmRxnDQNwcNqIRtiFMF6SeJCF1mekyO1RC5yZQ0RjGa8OM3Jl71SZ9ujfRClXrINhLGMTQJ7YiiDmMdFjI5AKOw3CaLo256WUmxGMREjdIWGw2NkwClW46Suj4-jj0E1pUAAfh4mzzPfCSvxc6zMncm95KAA) | [Example 2](https://www.typescriptlang.org/play/#code/JYWwDg9gTgLgBAFQJ5gKZwGZQiOByGFVAIwgA88AoSgehrgFonmXW32POvnb7k1ejbsJGj21AMYQAdgGd4AUWkxghOAF5ERAHQB5YgCtUEmAAoA3pThxgAEwBcWtNoDKMKMGkBzUwEpKAL7+lFJy8ACqsqhQGk6o2gCSytEYAIYSqKYA2koqhAC6ADRwltbSqSCojvzxbh7efoVWcKggqcAANtU6dZ4+-kGSMvJwkdHhYLapMOiaNdoKAG6pHQCu05nzSTNQUSbZzfMACsASANamY1DF8wDSqEi6GKa5qki+vk3Wx6mwwCumea6ECqS5Ra5xbT3R7PV6ED7+fIfah0IRidEYzHcQQAJVQGA6xhggixpLJWKGcgghO0HQgPiuvmszJZrLZ7NZqNKHJ5vL5vNR1kIaEceAghiJeC+-Jlsp5grgYGwaD+qFkjm5cq1WoV1jsGrgwqq+Hk9S8eDgAWl2pt-N1cHKlQNRtFpr6FqtzVt3o59ta7S6JUNRFd7ndlq9PqjzIVnuj8esCqgqAAjqtgMmHHAspGE977Xg7FK4Lm8zaC47UMXS2WdfQWXh-Z0qLWfQr8jXWzLUQFKbJqfE6QzwRMpjMmQmuZ2u3yFS78OKjCYpdOZ-L64rldEVGqNau1+z7fqg-O8G7vB7rQfuxuyhVjeZgyKTWGL5ar9fZ7eWm1Os6Qy+Zoevun4xhucagXaG7JmmGaoFmWSFrYeAdpBUERiS5JYdhnCCG40ynJhOHEcR1BGqM4KxPM+EqBIAA8RoQBgFHRAAfLKqLkVcsSamh67Mse55eB+fFsgqlaOEJImiSyCpNoGQkgWhPZkUQLFQKOGxUT0MAEfRjHMVcmkzOxcCcWpRmTFpmi8TJnIboJr7CUpkHifeAD8klOdJdlmRu8meXAim+Ye9ABEAA)
|
|
116
117
|
|
|
117
|
-
|
|
118
|
+
TypeBox can transform TypeScript definitions into JSON Schema. The Script function provides an optional programmatic syntax to rapidly convert type definitions into JSON Schema, or serve more generally as an alternative to Type.* builders. The Script function is designed to handle a wide array of complex TypeScript type-level expressions.
|
|
118
119
|
|
|
119
|
-
|
|
120
|
+
### Example
|
|
120
121
|
|
|
121
|
-
The
|
|
122
|
+
The following uses the Script to parse interfaces into JSON Schema.
|
|
122
123
|
|
|
123
124
|
```typescript
|
|
124
|
-
import
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
### Example
|
|
125
|
+
import Type from 'typebox'
|
|
128
126
|
|
|
129
|
-
|
|
127
|
+
// -------------------------------------------------------------------------------
|
|
128
|
+
// Script
|
|
129
|
+
// -------------------------------------------------------------------------------
|
|
130
130
|
|
|
131
|
-
|
|
132
|
-
const T = Type.Object({
|
|
133
|
-
x: Type.Number(),
|
|
134
|
-
y: Type.Number(),
|
|
135
|
-
z: Type.Number()
|
|
136
|
-
})
|
|
137
|
-
|
|
138
|
-
const A = Value.Parse(T, { // const A: {
|
|
139
|
-
x: 1, // x: number,
|
|
140
|
-
y: 0, // y: number,
|
|
141
|
-
z: 0 // z: number
|
|
142
|
-
}) // } = ...
|
|
143
|
-
```
|
|
131
|
+
const { User, UserUpdate } = Type.Script(`
|
|
144
132
|
|
|
145
|
-
|
|
133
|
+
interface Entity {
|
|
134
|
+
id: string
|
|
135
|
+
}
|
|
146
136
|
|
|
147
|
-
|
|
137
|
+
interface User extends Entity {
|
|
138
|
+
name: string,
|
|
139
|
+
email: string
|
|
140
|
+
}
|
|
148
141
|
|
|
149
|
-
|
|
142
|
+
type UserUpdate = Evaluate<
|
|
143
|
+
Pick<User, keyof Entity> &
|
|
144
|
+
Partial<Omit<User, keyof Entity>>
|
|
145
|
+
>
|
|
146
|
+
`)
|
|
150
147
|
|
|
151
|
-
|
|
152
|
-
// ----------------------------------------------------------
|
|
153
|
-
// Script
|
|
154
|
-
// ----------------------------------------------------------
|
|
155
|
-
const T = Type.Script(`{
|
|
156
|
-
x: number,
|
|
157
|
-
y: string,
|
|
158
|
-
z: boolean
|
|
159
|
-
}`)
|
|
160
|
-
|
|
161
|
-
// ----------------------------------------------------------
|
|
148
|
+
// -------------------------------------------------------------------------------
|
|
162
149
|
// Reflect
|
|
163
|
-
//
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
//
|
|
169
|
-
//
|
|
170
|
-
//
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
//
|
|
176
|
-
//
|
|
177
|
-
//
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
150
|
+
// -------------------------------------------------------------------------------
|
|
151
|
+
|
|
152
|
+
console.log(User) // {
|
|
153
|
+
// type: 'object',
|
|
154
|
+
// properties: {
|
|
155
|
+
// id: { type: 'string' },
|
|
156
|
+
// name: { type: 'string' },
|
|
157
|
+
// email: { type: 'string' }
|
|
158
|
+
// },
|
|
159
|
+
// required: [
|
|
160
|
+
// 'id',
|
|
161
|
+
// 'name',
|
|
162
|
+
// 'email'
|
|
163
|
+
// ]
|
|
164
|
+
// }
|
|
165
|
+
|
|
166
|
+
console.log(UserUpdate) // {
|
|
167
|
+
// type: 'object',
|
|
168
|
+
// properties: {
|
|
169
|
+
// id: { type: 'string' },
|
|
170
|
+
// name: { type: 'string' },
|
|
171
|
+
// email: { type: 'string' }
|
|
172
|
+
// },
|
|
173
|
+
// required: ['id']
|
|
174
|
+
// }
|
|
175
|
+
|
|
176
|
+
// -------------------------------------------------------------------------------
|
|
177
|
+
// Static
|
|
178
|
+
// -------------------------------------------------------------------------------
|
|
179
|
+
|
|
180
|
+
type User = Type.Static<typeof User> // type User = {
|
|
181
|
+
// id: string,
|
|
182
|
+
// name: string,
|
|
183
|
+
// email: string
|
|
184
|
+
// }
|
|
185
|
+
|
|
186
|
+
type UserUpdate = Type.Static<typeof UserUpdate> // type UserUpdate = {
|
|
187
|
+
// id: string,
|
|
188
|
+
// name?: string,
|
|
189
|
+
// email?: string
|
|
190
|
+
// }
|
|
191
|
+
|
|
183
192
|
```
|
|
184
193
|
|
|
185
194
|
<a name="Schema"></a>
|
|
186
195
|
|
|
187
196
|
## Schema
|
|
188
197
|
|
|
189
|
-
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/schema/overview)
|
|
198
|
+
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/schema/overview) | [Example 1](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAZQMYAsCmICGcBmUIhwDkMAnmGgEYQAeA9AM6oaZEBQokscAKuWrnyES-ajXZs6dOAFo58hYqXKVqteo0zJ0gMIEwwADZptszeYuWr8tmyQQAdg3gBVBmihwAvImZYAdHrgRmgAFHwU-gDylABWaEgwoQDebHBwwAAmAFy8-P4IMFDADgDmoQCUADRpcA6YIGi5EWgFRSXl1bUsRs35hcVlKbjQWDC5RD2GRHAAvhVs8wum1qtr61pScAAKmFDuKxtHx6q29k7wAK7unj5uHv67+2HJ6W-vH5-vW+fOcNceXKpdJZCYANgArDgAIyYABMlAAzEgACyZCFoME4ADsmAAHJQAJxIAAMmWhRCqX2pcC2IJycGcg1KNXS9UaEwBUEpNN5fO+0jZDSajPaZVZcCmnJuAAFMgRMCV-PYQDN+by6ZKsL1RczFhV1YajR8trNvHB-JagA) | [Example 2](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAZQMYAsCmICGcBmUIhwDkMAnmGgEYQAeA9AM6oaZEBQbddcAtH-wMFDhI0WPESenbgGECYYABs003pPUbNW-hyQQAdg3gBVBmihwAvImZYAdHPBK0ACgDebOHDIUAXMQhKACs0JBgiABpPODB8ClhgNAZ-Dy8vYAATFO9yNH8iIyhgfQBzIjgAXyi0uH1MEDy4Nxy-YkLissrqtJYlbJ9Ggpgi0vKK6KroqDQARwBXYGmsuABtaK8iTMi4deI6hu3dol7Fdi8AXTYKgEoOLjVtR6en1QAFTCgzVWef38ldAxGOBzMwWaymcx2d6fVzNGrwhGIuD3PSGeAg8wpaKZfIANgArDgAIyYABMlAAzEgACwZfFoXE4ADsmAAHJQAJxIAAMGSJ2yRiPu6WW7VK3X2gwxUAFgrl8q8wtq9UaYpK3RO+WlAAEMgRMMU7HoQOUFXKlZq4GqrtczXb7fD7hUrHA7G6gA)
|
|
190
199
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
```typescript
|
|
194
|
-
import Schema from 'typebox/schema'
|
|
195
|
-
```
|
|
200
|
+
TypeBox includes high performance validation compiler for JSON Schema Drafts 3 through to 2020-12. The compiler supports both TypeBox and JSON Schema, and will convert schematics into high performance validation routines. The compiler is designed to be a lightweight spec compliant alternative to Ajv for high-throughput applications.
|
|
196
201
|
|
|
197
202
|
### Example
|
|
198
203
|
|
|
199
|
-
The following uses the Schema submodule to compile
|
|
204
|
+
The following uses the Schema submodule to compile a TypeBox type.
|
|
200
205
|
|
|
201
206
|
```typescript
|
|
202
|
-
|
|
207
|
+
import Schema from 'typebox/schema'
|
|
208
|
+
|
|
209
|
+
// -------------------------------------------------------------------------------
|
|
203
210
|
// Compile
|
|
204
|
-
//
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
})
|
|
214
|
-
|
|
215
|
-
// ----------------------------------------------------------
|
|
211
|
+
// -------------------------------------------------------------------------------
|
|
212
|
+
|
|
213
|
+
const User = Schema.Compile(Type.Object({
|
|
214
|
+
id: Type.String(),
|
|
215
|
+
name: Type.String(),
|
|
216
|
+
email: Type.String({ format: 'email' })
|
|
217
|
+
}))
|
|
218
|
+
|
|
219
|
+
// -------------------------------------------------------------------------------
|
|
216
220
|
// Parse
|
|
217
|
-
//
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
221
|
+
// -------------------------------------------------------------------------------
|
|
222
|
+
|
|
223
|
+
const user = User.Parse({ // const user: {
|
|
224
|
+
id: '65f1a2b3c4d5e6f7a8b9c0d1', // id: string,
|
|
225
|
+
name: 'user', // name: string,
|
|
226
|
+
email: 'user@domain.com' // email: string
|
|
227
|
+
}) // } = ...
|
|
223
228
|
```
|
|
224
229
|
|
|
225
230
|
<a name="Versions"></a>
|
|
@@ -228,25 +233,10 @@ const R = C.Parse({ x: 0, y: 0, z: 0 }) // const R: {
|
|
|
228
233
|
|
|
229
234
|
TypeBox provides two distinct versions that span two generations of the TypeScript compiler.
|
|
230
235
|
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
```
|
|
236
|
-
|
|
237
|
-
Developed against TypeScript 4-6 and maintained under Long Term Support (LTS) for existing infrastructure on the 0.x revision line. ESM and CJS compatible.
|
|
238
|
-
|
|
239
|
-
### Version 1.x
|
|
240
|
-
|
|
241
|
-
```bash
|
|
242
|
-
$ npm install typebox # 1.x - Latest | TS 7 Native
|
|
243
|
-
```
|
|
244
|
-
|
|
245
|
-
Developed against the TypeScript 7 native compiler with advanced type inference and JSON Schema 2020-12 compliant validation, with backwards compatibility for `0.x` types. ESM only.
|
|
246
|
-
|
|
247
|
-
### Additional
|
|
248
|
-
|
|
249
|
-
The `1.x` version is recommended for most new projects and is the active development line that targets optimizations enabled by the TypeScript 7 native compiler. The `0.x` version is maintained under LTS for environments requiring CJS and ESM compatibility as well as support for older TypeScript compiler versions. For issues relating to `0.x` please submit them to the [TypeBox 0.x](https://github.com/sinclairzx81/sinclair-typebox) repository.
|
|
236
|
+
| Version | TypeScript | Description |
|
|
237
|
+
| :--- | :--- | :--- |
|
|
238
|
+
| **1.x** | **6.0 - 7.0+** | **Latest.** Developed against the TypeScript 7 native compiler. Provides advanced type inference and native JSON Schema 2020-12 support. Includes backwards compatibility with `0.x` types. **ESM only.** |
|
|
239
|
+
| **0.x** | **5.0 - 6.0** | **LTS.** Developed against older TypeScript versions and actively maintained under Long Term Support. Compatible with both **ESM and CJS**. Issues should be submitted to the [Sinclair TypeBox](https://github.com/sinclairzx81/sinclair-typebox) repository. |
|
|
250
240
|
|
|
251
241
|
## Contribute
|
|
252
242
|
|