typebox 1.0.55 → 1.0.57
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/compile.d.mts +2 -2
- package/build/compile/validator.d.mts +6 -6
- package/package.json +1 -1
- package/readme.md +106 -40
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type TProperties, type TSchema } from '../type/index.mjs';
|
|
2
2
|
import { Validator } from './validator.mjs';
|
|
3
3
|
/** Compiles a type into a high performance Validator */
|
|
4
|
-
export declare function Compile<const Type extends TSchema
|
|
4
|
+
export declare function Compile<const Type extends TSchema, Result extends Validator = Validator<{}, Type>>(type: Type): Result;
|
|
5
5
|
/** Compiles a type into a high performance Validator */
|
|
6
|
-
export declare function Compile<Context extends TProperties, const Type extends TSchema
|
|
6
|
+
export declare function Compile<Context extends TProperties, const Type extends TSchema, Result extends Validator = Validator<Context, Type>>(context: Context, type: Type): Result;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type TLocalizedValidationError } from '../error/index.mjs';
|
|
2
2
|
import { type StaticDecode, type StaticEncode, type TProperties, type TSchema, Base } from '../type/index.mjs';
|
|
3
|
-
export declare class Validator<Context extends TProperties = TProperties, Type extends TSchema = TSchema
|
|
3
|
+
export declare class Validator<Context extends TProperties = TProperties, Type extends TSchema = TSchema, Encode extends unknown = StaticEncode<Type, Context>, Decode extends unknown = StaticDecode<Type, Context>> extends Base<Encode> {
|
|
4
4
|
private readonly context;
|
|
5
5
|
private readonly type;
|
|
6
6
|
private readonly isEvaluated;
|
|
@@ -20,7 +20,7 @@ export declare class Validator<Context extends TProperties = TProperties, Type e
|
|
|
20
20
|
/** Returns the generated code for this validator. */
|
|
21
21
|
Code(): string;
|
|
22
22
|
/** Checks a value matches the Validator type. */
|
|
23
|
-
Check(value: unknown): value is
|
|
23
|
+
Check(value: unknown): value is Encode;
|
|
24
24
|
/** Returns errors for the given value. */
|
|
25
25
|
Errors(value: unknown): TLocalizedValidationError[];
|
|
26
26
|
/** Cleans a value using the Validator type. */
|
|
@@ -28,15 +28,15 @@ export declare class Validator<Context extends TProperties = TProperties, Type e
|
|
|
28
28
|
/** Converts a value using the Validator type. */
|
|
29
29
|
Convert(value: unknown): unknown;
|
|
30
30
|
/** Creates a value using the Validator type. */
|
|
31
|
-
Create():
|
|
31
|
+
Create(): Encode;
|
|
32
32
|
/** Creates defaults using the Validator type. */
|
|
33
33
|
Default(value: unknown): unknown;
|
|
34
34
|
/** Clones this validator. */
|
|
35
35
|
Clone(): Validator<Context, Type>;
|
|
36
36
|
/** Parses a value */
|
|
37
|
-
Parse(value: unknown):
|
|
37
|
+
Parse(value: unknown): Decode;
|
|
38
38
|
/** Decodes a value */
|
|
39
|
-
Decode(value: unknown):
|
|
39
|
+
Decode(value: unknown): Decode;
|
|
40
40
|
/** Encodes a value */
|
|
41
|
-
Encode(value: unknown):
|
|
41
|
+
Encode(value: unknown): Encode;
|
|
42
42
|
}
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -61,6 +61,8 @@ License: MIT
|
|
|
61
61
|
- [Type](#Type)
|
|
62
62
|
- [Value](#Value)
|
|
63
63
|
- [Compile](#Compile)
|
|
64
|
+
- [Schema](#Schema)
|
|
65
|
+
- [Script](#Script)
|
|
64
66
|
- [Contribute](#Contribute)
|
|
65
67
|
|
|
66
68
|
## Upgrade
|
|
@@ -101,30 +103,29 @@ type T = Type.Static<typeof T> // type T = {
|
|
|
101
103
|
// }
|
|
102
104
|
```
|
|
103
105
|
|
|
104
|
-
|
|
106
|
+
Schema options can be passed on the last argument of any given type.
|
|
105
107
|
|
|
106
108
|
```typescript
|
|
107
|
-
const T = Type.
|
|
109
|
+
const T = Type.String({ // const T = {
|
|
110
|
+
format: 'email' // type: 'string',
|
|
111
|
+
}) // format: 'email'
|
|
112
|
+
// }
|
|
113
|
+
|
|
114
|
+
const S = Type.Number({ // const S = {
|
|
108
115
|
minimum: 0, // type: 'number',
|
|
109
116
|
maximum: 100 // minimum: 0,
|
|
110
117
|
}) // maximum: 100
|
|
111
118
|
// }
|
|
112
119
|
|
|
113
|
-
const S = Type.String({ // const S = {
|
|
114
|
-
format: 'email' // type: 'string',
|
|
115
|
-
}) // format: 'email'
|
|
116
|
-
// }
|
|
117
120
|
```
|
|
118
121
|
|
|
119
122
|
<a name="Value"></a>
|
|
120
123
|
|
|
121
124
|
## Value
|
|
122
125
|
|
|
123
|
-
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/value/overview) | [Example](https://www.typescriptlang.org/play/?#code/
|
|
124
|
-
|
|
125
|
-
The TypeBox Value module provides functions to Check and Parse JavaScript values. It also includes functions such as Clone, Repair, Encode, Decode, Diff and Patch which perform various structural operations on JavaScript values.
|
|
126
|
+
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/value/overview) | [Example](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBANQIYBsCuBTOAzKERwDkMAnmBgEYQAeA9AG6qaEBQokscAKmVrvkVLkq1ViwDGEAHYBneFzgBebrwB0AeQoArDOJgAKAN4s4cagC4V5VQDk0IChij6AlABoTcEpZ7W7Dp1cPUwAvHzV-R2cXFgBfGIlpOTgAQSVEJgxVAAUkKBkMfS43OENTcorKqqraWjhJWXgUy2NTCzgARhLqnt6+vtry9ql7KOCvSwAGbv7ZuerB0284EYCocbC4Sfmd3bhFuE3VqLiXPfOdwdj01VugA)
|
|
126
127
|
|
|
127
|
-
The Value module
|
|
128
|
+
The Value module provides functions that perform typed operations on JavaScript values. It includes functions such as Check, Parse, Clone, Encode, Decode and as well as advanced functions to perform structural Diff and Patch on dynamic JavaScript values.
|
|
128
129
|
|
|
129
130
|
```typescript
|
|
130
131
|
import Value from 'typebox/value'
|
|
@@ -132,7 +133,7 @@ import Value from 'typebox/value'
|
|
|
132
133
|
|
|
133
134
|
### Example
|
|
134
135
|
|
|
135
|
-
The following uses the Value module to
|
|
136
|
+
The following uses the Value module to Parse a value.
|
|
136
137
|
|
|
137
138
|
```typescript
|
|
138
139
|
const T = Type.Object({
|
|
@@ -141,20 +142,10 @@ const T = Type.Object({
|
|
|
141
142
|
z: Type.Number()
|
|
142
143
|
})
|
|
143
144
|
|
|
144
|
-
//
|
|
145
|
-
|
|
146
|
-
const A = Value.Check(T, { // const A: boolean = true
|
|
147
|
-
x: 1,
|
|
148
|
-
y: 2,
|
|
149
|
-
z: 3
|
|
150
|
-
})
|
|
151
|
-
|
|
152
|
-
// Parse
|
|
153
|
-
|
|
154
|
-
const B = Value.Parse(T, { // const B: {
|
|
145
|
+
const A = Value.Parse(T, { // const A: {
|
|
155
146
|
x: 1, // x: number,
|
|
156
|
-
y:
|
|
157
|
-
z:
|
|
147
|
+
y: 0, // y: number,
|
|
148
|
+
z: 0 // z: number
|
|
158
149
|
}) // } = ...
|
|
159
150
|
```
|
|
160
151
|
|
|
@@ -163,11 +154,9 @@ const B = Value.Parse(T, { // const B: {
|
|
|
163
154
|
|
|
164
155
|
## Compile
|
|
165
156
|
|
|
166
|
-
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/compile/overview) | [Example](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgYQuYAbApnAvnAMyjTgHIYBPMLAIwgA8B6AYzTEy1IChRJY4AKlRxES5YXXrcurAHYBneMjgBeFGw4AKIdQB0AeRoArLMxiakcK9Zu3bjRnDmKUALjgA1AIYZgAEy8YaAAeBFwAGkFDEzNQrit6dx0sXQA5AFcQGiwoTQBKSLsi4pKrB2tEwQysnPD4uAok4TTM7NyC0s6u8qtGqtba+oAvJr1qtvyuqZKeuBH+
|
|
167
|
-
|
|
168
|
-
The TypeBox Compile module provides functions to convert types into high-performance validators. The compiler is tuned for fast compilation as well as fast validation.
|
|
157
|
+
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/compile/overview) | [Example](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgYQuYAbApnAvnAMyjTgHIYBPMLAIwgA8B6AYzTEy1IChRJY4AKlRxES5YXXrcurAHYBneMjgBeFGw4AKIdQB0AeRoArLMxiakcK9Zu3bjRnDmKUALjgA1AIYZgAEy8YaAAeBFwAGkFDEzNQrit6dx0sXQA5AFcQGiwoTQBKSLsi4pKrB2tEwQysnPD4uAok4TTM7NyC0s6u8qtGqtba+oAvJr1qtvyuqZKeuBH+mqguXDy86fWNm3LcAD4drhkIBXgAQVUUXQAFLyh5LAtNjfLnU-cEesqABkLH37tZyqyAZQOq9dwARh+f2hcFmfSBi1Bc3cnxhaNhjis8wRbWWa3RBO2510JKAA)
|
|
169
158
|
|
|
170
|
-
The Compile module is
|
|
159
|
+
The Compile module is a high-performance JIT compiler that transforms types into efficient runtime validators. The compiler is optimized for both fast compilation and validation.
|
|
171
160
|
|
|
172
161
|
```typescript
|
|
173
162
|
import { Compile } from 'typebox/compile'
|
|
@@ -175,7 +164,7 @@ import { Compile } from 'typebox/compile'
|
|
|
175
164
|
|
|
176
165
|
### Example
|
|
177
166
|
|
|
178
|
-
The following uses the
|
|
167
|
+
The following uses the compiler to Compile and Parse a value.
|
|
179
168
|
|
|
180
169
|
```typescript
|
|
181
170
|
const C = Compile(Type.Object({ // const C: Validator<{}, TObject<{
|
|
@@ -184,23 +173,100 @@ const C = Compile(Type.Object({ // const C: Validator<{}, TO
|
|
|
184
173
|
z: Type.Number() // z: TNumber
|
|
185
174
|
})) // }>>
|
|
186
175
|
|
|
187
|
-
//
|
|
176
|
+
const A = C.Parse({ // const A: {
|
|
177
|
+
x: 0, // x: number,
|
|
178
|
+
y: 1, // y: number,
|
|
179
|
+
z: 0 // z: number
|
|
180
|
+
}) // } = ...
|
|
181
|
+
```
|
|
188
182
|
|
|
189
|
-
|
|
190
|
-
x: 1,
|
|
191
|
-
y: 2,
|
|
192
|
-
z: 3
|
|
193
|
-
})
|
|
183
|
+
<a name="Schema"></a>
|
|
194
184
|
|
|
195
|
-
|
|
185
|
+
## Schema
|
|
196
186
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
187
|
+
[Example 1](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgYQuYAbApnAvnAMyjTgHIYBPMLAIwgA8B6AYzTEy1ICgfWA7AM7xkcALwo2HABQIucOJWoAuMhBoArLMxikANHLhQsARwCuwIwBMVAbVL09ZCo9IAvUgF198sMWqxgLAEVWXl5ehCFKiwVUj5TEBosKFI8bzCKSMUYsnjE5NTcdPlXLOjYvKSUvANcLlwASl4IQXgAQTEUADoABQBDKAEsGTDRsfGJycZGOH4hODaQgwi4AAZdSc2t7YnpsJXK5PTMtY2d84uxvfkTw6h00rgARkvXy+u4R7v6hre-95m+HEXRBQA) | [Example 2](https://tsplay.dev/mxrl7w)
|
|
188
|
+
|
|
189
|
+
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.
|
|
190
|
+
|
|
191
|
+
### Example
|
|
192
|
+
|
|
193
|
+
The following compiles Json Schema. Type inference is supported.
|
|
194
|
+
|
|
195
|
+
```typescript
|
|
196
|
+
const C = Compile({
|
|
197
|
+
type: 'object',
|
|
198
|
+
required: ['x', 'y', 'z'],
|
|
199
|
+
properties: {
|
|
200
|
+
x: { type: 'number' },
|
|
201
|
+
y: { type: 'number' },
|
|
202
|
+
z: { type: 'number' }
|
|
203
|
+
}
|
|
204
|
+
})
|
|
205
|
+
|
|
206
|
+
const A = C.Parse({ // const A: {
|
|
207
|
+
x: 0, // x: number,
|
|
208
|
+
y: 0, // y: number,
|
|
209
|
+
z: 1 // z: number
|
|
201
210
|
}) // } = ...
|
|
202
211
|
```
|
|
203
212
|
|
|
213
|
+
It can also be used to accelerate remote libraries via Json Schema translation.
|
|
214
|
+
|
|
215
|
+
```typescript
|
|
216
|
+
const C = Compile(x.toJsonSchema(x.object({
|
|
217
|
+
x: x.number(),
|
|
218
|
+
y: x.number(),
|
|
219
|
+
z: x.number()
|
|
220
|
+
})))
|
|
221
|
+
|
|
222
|
+
const A = C.Check(...) // high performance runtime checking
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
## Script
|
|
226
|
+
|
|
227
|
+
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/script/overview) | [Example 1](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAFQJ5gKZwGZQiOByGFVAIwgA88AoSgehrgGUBjKYMGVAE0SOqYgB2AZ3gI4AXh5oAdM1bsAFAAMA3nEpw4ZAFxwBAVxDFUUADTrNSXSNYCA5uY1wAXrtIQANqgCGA9QF8lAEpNULDwiMio6PC6OH5hUV0EAHliACtUJhgAHhUnGMKi4rC4zR1EADlDYzMCkobG0vpLZIYYWwd6pp7ispdkgCEITx8Bbt7JqLj-AD5qOIApIUFGJgALVBBvOABJAQ6IITRs4EFqBGlCNCnb0Li8CAysmCpLqFQAR31gD+47npxADaeAo5jwSDw4OceAAupRLmBsGhYMBUEIAY04moKtI8eYrHA8dJzK4iXi4P4FvQ5GwONxkDcFABhHBgfT0oJ8QQiRgSKSoWQsOkKNRifzmVROIEAaTgwD8AGtUEgIBhELDkrLYXAAD56fQeDyUQIhTF3OIJXkMZJpTLZPITc2TfoVBAAVQEZwEOSBCGqRhM5n9ho8sNmpidzsBLTghI9XsEvoQ7U6weqRvDkejAP6ZIT3uTw1GvnTofDUZzDRm81oNJgvk43ig3H2GBMqAETFQ1Gu6AY-MZgva3hgwCYOT7asYs1ucT7fMk+Src9jFQMgageoNRuzK5dscJNgVdm3Bl3lf3hTzbhGXl8Z9Dl6v0RmQA) | [Example 2](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAFQJ5gKZwGZQiOByGFVAIwgA88AoSgehrgAkIA3VKOGAC1QGd0NocYADsMbVMIDG6ABQ9OEAK4AbACZwQAQxiTOHbnB6SowMDFTrCaAJSVUZSLA5E4AMQgQ4AXkREAdADKMNrAkgA8VqgQGG4eAHx2DtDwkXAAQprsPshogcEwoRFE0emZCbT0AMIQYEhwmsLqYJo85ogBzmg8QsIwnjmogcamMDJ+49b1jXAA5qjwxJqSANadqAC0PJpicABSAQDyAHJwAbqoWomO8JIQwq1wAN6xEAA0pewAvt6+uWcmZhkAANKKl3J4fI9KHA4GQAFxwYSKEDENjQuBIBFIlFomEALyxyNRUEon2oqQyWSe6KgqE0qjuyjqAG0ANI9ODLVBIErg+rdACymjAYVZcQAugiAIKqZgNaSqUVxOAAHxebPFpPJLhlcqkFlF3PeADVMsAGjBuj48AANPCq-AATXtarwAC08MqfDJ0QAdIG+gAkjwAqmA0FBJC1UIakHFPkHHqaTBaeJ91qBIDweMBiMpUP7KLZQS4hSLWdyvXAwxGo3wwuiK3V7OZGt08BQ4AB+fCae1wxvcuAtiSqdtIe09vDEfuD5tkVtj-B4yf4SSzmHCVCsEnlIG2OhwA6KGBgE9rbqZfgqJlwWkYfOSYJ59D7Y6nc6XcF+VIwv---9DzwCBiAAK1QJ97TgShvzAbAIwKXgAL-Q9nnhOBxj8d5MQw8Z3gJXC-DgMlv1pABHRRgFpdRkJQ+hmQ7PB3jwCdmJXcUgA)
|
|
228
|
+
|
|
229
|
+
TypeBox includes a TypeScript scripting engine able to parse TypeScript types directly into Json Schema. The engine uses symmetric runtime and type-level parsing and returns typed safe schematics from TypeScript types (including computed types). The engine is designed for TypeScript 7 native compiler but is supported in TypeScript 5 and above.
|
|
230
|
+
|
|
231
|
+
```typescript
|
|
232
|
+
// Scripted Type
|
|
233
|
+
|
|
234
|
+
const T = Type.Script(`{
|
|
235
|
+
x: number,
|
|
236
|
+
y: string,
|
|
237
|
+
z: boolean
|
|
238
|
+
}`) // const T: TObject<{
|
|
239
|
+
// x: TNumber,
|
|
240
|
+
// y: TString,
|
|
241
|
+
// z: TBoolean
|
|
242
|
+
// }>
|
|
243
|
+
|
|
244
|
+
// Json Schema Introspection
|
|
245
|
+
|
|
246
|
+
T.type // 'object'
|
|
247
|
+
T.required // ['x', 'y', 'z']
|
|
248
|
+
T.properties // { x: ..., y: ..., z: ... }
|
|
249
|
+
|
|
250
|
+
// Scripted Type (Computed)
|
|
251
|
+
|
|
252
|
+
const S = Type.Script({ T }, `{
|
|
253
|
+
[K in keyof T]: T[K] | null
|
|
254
|
+
}`) // const S: TObject<{
|
|
255
|
+
// x: TUnion<[TNumber, TNull]>,
|
|
256
|
+
// y: TUnion<[TString, TNull]>,
|
|
257
|
+
// z: TUnion<[TBoolean, TNull]>
|
|
258
|
+
// }>
|
|
259
|
+
|
|
260
|
+
// Standard Inference
|
|
261
|
+
|
|
262
|
+
type S = Type.Static<typeof S> // type S = {
|
|
263
|
+
// x: number | null,
|
|
264
|
+
// y: string | null,
|
|
265
|
+
// z: boolean | null
|
|
266
|
+
// }
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
|
|
204
270
|
## Contribute
|
|
205
271
|
|
|
206
272
|
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.
|