typebox 1.1.13 → 1.1.15
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/schema/engine/pattern.mjs +2 -2
- package/build/schema/engine/patternProperties.mjs +3 -3
- package/build/type/extends/constructor.d.mts +1 -1
- package/build/type/extends/extends-right.mjs +1 -4
- package/build/type/extends/function.d.mts +1 -1
- package/build/type/extends/parameters.mjs +2 -5
- package/build/type/extends/tuple.mjs +1 -5
- package/package.json +1 -1
- package/readme.md +138 -147
|
@@ -5,14 +5,14 @@ import { Guard as G, EmitGuard as E } from '../../guard/index.mjs';
|
|
|
5
5
|
// Build
|
|
6
6
|
// ------------------------------------------------------------------
|
|
7
7
|
export function BuildPattern(stack, context, schema, value) {
|
|
8
|
-
const regexp = Externals.CreateVariable(G.IsString(schema.pattern) ? new RegExp(schema.pattern) : schema.pattern);
|
|
8
|
+
const regexp = Externals.CreateVariable(G.IsString(schema.pattern) ? new RegExp(schema.pattern, 'u') : schema.pattern);
|
|
9
9
|
return E.Call(E.Member(regexp, 'test'), [value]);
|
|
10
10
|
}
|
|
11
11
|
// ------------------------------------------------------------------
|
|
12
12
|
// Check
|
|
13
13
|
// ------------------------------------------------------------------
|
|
14
14
|
export function CheckPattern(stack, context, schema, value) {
|
|
15
|
-
const regexp = G.IsString(schema.pattern) ? new RegExp(schema.pattern) : schema.pattern;
|
|
15
|
+
const regexp = G.IsString(schema.pattern) ? new RegExp(schema.pattern, 'u') : schema.pattern;
|
|
16
16
|
return regexp.test(value);
|
|
17
17
|
}
|
|
18
18
|
// ------------------------------------------------------------------
|
|
@@ -9,7 +9,7 @@ import { BuildSchema, CheckSchema, ErrorSchema } from './schema.mjs';
|
|
|
9
9
|
export function BuildPatternProperties(stack, context, schema, value) {
|
|
10
10
|
return E.ReduceAnd(G.Entries(schema.patternProperties).map(([pattern, schema]) => {
|
|
11
11
|
const [key, prop] = [Unique(), Unique()];
|
|
12
|
-
const regexp = Externals.CreateVariable(new RegExp(pattern));
|
|
12
|
+
const regexp = Externals.CreateVariable(new RegExp(pattern, 'u'));
|
|
13
13
|
const notKey = E.Not(E.Call(E.Member(regexp, 'test'), [key]));
|
|
14
14
|
const isSchema = BuildSchema(stack, context, schema, prop);
|
|
15
15
|
const addKey = context.AddKey(key);
|
|
@@ -22,7 +22,7 @@ export function BuildPatternProperties(stack, context, schema, value) {
|
|
|
22
22
|
// ------------------------------------------------------------------
|
|
23
23
|
export function CheckPatternProperties(stack, context, schema, value) {
|
|
24
24
|
return G.Every(G.Entries(schema.patternProperties), 0, ([pattern, schema]) => {
|
|
25
|
-
const regexp = new RegExp(pattern);
|
|
25
|
+
const regexp = new RegExp(pattern, 'u');
|
|
26
26
|
return G.Every(G.Entries(value), 0, ([key, prop]) => {
|
|
27
27
|
return !regexp.test(key) || CheckSchema(stack, context, schema, prop) && context.AddKey(key);
|
|
28
28
|
});
|
|
@@ -34,7 +34,7 @@ export function CheckPatternProperties(stack, context, schema, value) {
|
|
|
34
34
|
export function ErrorPatternProperties(stack, context, schemaPath, instancePath, schema, value) {
|
|
35
35
|
return G.EveryAll(G.Entries(schema.patternProperties), 0, ([pattern, schema]) => {
|
|
36
36
|
const nextSchemaPath = `${schemaPath}/patternProperties/${pattern}`;
|
|
37
|
-
const regexp = new RegExp(pattern);
|
|
37
|
+
const regexp = new RegExp(pattern, 'u');
|
|
38
38
|
return G.EveryAll(G.Entries(value), 0, ([key, value]) => {
|
|
39
39
|
const nextInstancePath = `${instancePath}/${key}`;
|
|
40
40
|
const notKey = !regexp.test(key);
|
|
@@ -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,164 @@ 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 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/JYWwDg9gTgLgBAFQJ5gKZwGZQiOByGFVAIwgA88AoSgehrgFonmXW32POvueHb6AygGMowMDH6Ne0mbLnNqQiADsAzvADecAKqrUUADQ69UbWAAmAQxjoAvnAC8iIgDpho8QAoABtThxgZRsoDEshdABRIOBCOA1Kf39gcwAuOHVRZQBzBLhbPwCg-VDw4304VDIbZXNVOCiYGKQ4uFz-ZUsQVDSMwKyjNoqQS2AAGx6YTKzW-3zcwjQy0wtrdCcIgDdLUYBXVYAeQYAFYCEAa33dfSMz1CQIDHrowgA+OAAyGcSjy1hgbf2AHkQDFLiYbncHk9Gq8Xrk4ZRvABKah0KTyDGYzGSABKqAwo1QQgkaKxZPJvEUKlUEEJLlGECyniuUCRiXZHM5XK5aPi3P5AsFgrR-gW3XwEGIACsiTA8AZBkKlcrOSK4GBsGg-qhVGk+SqDYa1UlUi0xWk8L1sng8grDfblca4B0unq4Ob8FasjbbHaHf7+U7UMMxm6PZbJn0fYqA7G4GrfTG4-61VBUABHHbANOmgDaSeT9qdeGS8q+hdjxZdqDLBYrKuLwZGoyo9cr9H8AF0622hWi5ko1LTUPTGcyTGYrDY2YXeT3ewK1eHJTLifL5wvuWqNRAtY0dXqN5vVR3Eskw0QLV6fX7jw3T+1OuKtOHr7aj3fEkGQ+MzZfPZG1p5B+n7xqeiagY6p5ppm2aoHmJbmHg3aQUq-aovQFJYdhLCSAIMDWKckg4SRFLUGKSyOM4aBuARjRCPsYpQiybxofQFEslR+qocKp7nukgH9CBoFqtWExTLePGBqeTahgJUzCZ+6GUBxE4rDYVHIDR+GEQxTGPCyk6rKxaKqfoRkaU43FSVufGml6kk2SeiTVgA-OJfSOU57JqrJozufJfSKXe6FAA) | [Example 2](https://www.typescriptlang.org/play/#code/JYWwDg9gTgLgBAFQJ5gKZwGZQiOByGFVAIwgA88AoSgehrgFonmXW32POvnb7k1ejbsJGj21AMYQAdgGd4AUWkxghOAF5ERAHQBJZaigYAhhNQAKANoBdADRwA3pThxgAEwBcWtNoDKMKGBpAHNzAEpKAF8Iyik5eABVWUMNb1Q9AyNTC0slFUI7R2c4aWMQVC9+dP9AkPDbYtQQY2AAG0qdGqDQiOjJGXk4JMMEsDdjGHRNKu0FADdjVoBXCYsZ-UmoZIkYK2KZgAVgCQBrc2GoexmAaVQkAHkMczzVJDCwhpdD41hgRfMZvcQKpzslLmltLcHk8XoR3hFrO9qHQhGI0eiMdxBAAlVAYVqoHaCTEk0mY-pyCAE7StCChC5hFxM5ks1lslkopzs7k83m8lEuQhoLx4CDEABWhJgeE+fLl8u5ArgYGwaF+qFkXi5Cp1uqVLncWrgQoq+HktWCeDgkVlurtfP1JTKpocxqIIvN3StNuK9r97MdTRa7UcbuFZoCXutvv9saZSp9caTLiVUFQAEclsA0544JYY8m-Y68O4ZXAC4W7cXSuUyxXKzri0G2lQG-6ldZ6225SjIhTZFT0rT6WDRuNJozk5yu93+fRBe78GLJTsZTPZ4r58rVYYVBqteuN2zHYbQyaPZGQt7bUee1uXDWXWHTXhPVfrTfb3OWc2Q67zxGFreoeX7xluiagQ6W5ppm2aoLmlglm4eCdpBUHRsSZJYdhnCCP4EzHJhOHEcR1AmkMYKpDM+EqBIAA8JoQBgFGGAAfPKKLkRcqTamhPJKqeb7BJ+fGskqj5eEJImicySq-pJl7BCBaG9mRRAsVAY6rFRnQwAR9GMcxFxaZM7FwJx6nGWM2maLxMliVugmKdJ9nmVuj4APwKRaLn2XJzRtF5cBCcpkG9kAA)
|
|
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 function to parse TypeScript interfaces into JSON Schema.
|
|
122
123
|
|
|
123
124
|
```typescript
|
|
124
|
-
import
|
|
125
|
-
```
|
|
125
|
+
import Type from 'typebox'
|
|
126
126
|
|
|
127
|
-
|
|
127
|
+
// -------------------------------------------------------------------------------
|
|
128
|
+
// Script
|
|
129
|
+
// -------------------------------------------------------------------------------
|
|
128
130
|
|
|
129
|
-
|
|
131
|
+
const { User, UserUpdate } = Type.Script(`
|
|
130
132
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
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
|
-
```
|
|
133
|
+
interface Entity {
|
|
134
|
+
id: string
|
|
135
|
+
}
|
|
144
136
|
|
|
145
|
-
|
|
137
|
+
interface User extends Entity {
|
|
138
|
+
name: string,
|
|
139
|
+
email: string
|
|
140
|
+
}
|
|
146
141
|
|
|
147
|
-
|
|
142
|
+
type UserUpdate = Evaluate<
|
|
143
|
+
Pick<User, keyof Entity> &
|
|
144
|
+
Partial<Omit<User, keyof Entity>>
|
|
145
|
+
>
|
|
148
146
|
|
|
149
|
-
|
|
147
|
+
`)
|
|
150
148
|
|
|
151
|
-
|
|
152
|
-
// ----------------------------------------------------------
|
|
153
|
-
// Script
|
|
154
|
-
// ----------------------------------------------------------
|
|
155
|
-
const T = Type.Script(`{
|
|
156
|
-
x: number,
|
|
157
|
-
y: string,
|
|
158
|
-
z: boolean
|
|
159
|
-
}`)
|
|
160
|
-
|
|
161
|
-
// ----------------------------------------------------------
|
|
149
|
+
// -------------------------------------------------------------------------------
|
|
162
150
|
// Reflect
|
|
163
|
-
//
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
//
|
|
169
|
-
//
|
|
170
|
-
//
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
//
|
|
176
|
-
//
|
|
177
|
-
//
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
151
|
+
// -------------------------------------------------------------------------------
|
|
152
|
+
|
|
153
|
+
console.log(User) // {
|
|
154
|
+
// type: 'object',
|
|
155
|
+
// properties: {
|
|
156
|
+
// id: { type: 'string' },
|
|
157
|
+
// name: { type: 'string' },
|
|
158
|
+
// email: { type: 'string' }
|
|
159
|
+
// },
|
|
160
|
+
// required: [
|
|
161
|
+
// 'id',
|
|
162
|
+
// 'name',
|
|
163
|
+
// 'email'
|
|
164
|
+
// ]
|
|
165
|
+
// }
|
|
166
|
+
|
|
167
|
+
console.log(UserUpdate) // {
|
|
168
|
+
// type: 'object',
|
|
169
|
+
// properties: {
|
|
170
|
+
// id: { type: 'string' },
|
|
171
|
+
// name: { type: 'string' },
|
|
172
|
+
// email: { type: 'string' }
|
|
173
|
+
// },
|
|
174
|
+
// required: ['id']
|
|
175
|
+
// }
|
|
176
|
+
|
|
177
|
+
// -------------------------------------------------------------------------------
|
|
178
|
+
// Static
|
|
179
|
+
// -------------------------------------------------------------------------------
|
|
180
|
+
|
|
181
|
+
type User = Type.Static<typeof User> // type User = {
|
|
182
|
+
// id: string,
|
|
183
|
+
// name: string,
|
|
184
|
+
// email: string
|
|
185
|
+
// }
|
|
186
|
+
|
|
187
|
+
type UserUpdate = Type.Static<typeof UserUpdate> // type UserUpdate = {
|
|
188
|
+
// id: string,
|
|
189
|
+
// name?: string,
|
|
190
|
+
// email?: string
|
|
191
|
+
// }
|
|
192
|
+
|
|
183
193
|
```
|
|
184
194
|
|
|
185
195
|
<a name="Schema"></a>
|
|
186
196
|
|
|
187
197
|
## Schema
|
|
188
198
|
|
|
189
|
-
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/schema/overview)
|
|
199
|
+
[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-YkLissrqtJYlbJ9Ggpgi0sjcaCwYfN7FcoroquioNABHAFdgZay4AG1oryJMsf3iOobjmqIZ9i8AXTYKgEoOLjVtd4+P1QAFTCgzVSfIHAyS6AxGOBrMwWaymcx2X7-VzNGqotHouCvPSGeBQ8wpaKZfIANgArDgAIyYABMlAAzEgACwZUloYk4ADsmAAHJQAJxIAAMGQpYwx6Ne6W27VK3TOgzxUDF4pVqq8ktq9UaMpK3Rm+UVAAEMgRMMU7HoQOU1SqNfq4DqHo8bS7XajXhUrHA7D6gA) | [Specification](https://sinclairzx81.github.io/typebox/#/docs/schema/6_specification)
|
|
190
200
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
```typescript
|
|
194
|
-
import Schema from 'typebox/schema'
|
|
195
|
-
```
|
|
201
|
+
TypeBox includes a JSON Schema compiler designed for high-performance JIT validation. It also provides automatic fallback to dynamic interpreted checking for JIT restrictive environments such as Cloudflare Workers. The compiler is designed to be a lightweight, spec compliant alternative to Ajv for high-throughput applications based on the JSON Schema standard.
|
|
196
202
|
|
|
197
203
|
### Example
|
|
198
204
|
|
|
199
|
-
The following uses the Schema submodule to compile
|
|
205
|
+
The following uses the Schema submodule to compile a TypeBox type.
|
|
200
206
|
|
|
201
207
|
```typescript
|
|
202
|
-
|
|
208
|
+
import Schema from 'typebox/schema'
|
|
209
|
+
|
|
210
|
+
// -------------------------------------------------------------------------------
|
|
203
211
|
// Compile
|
|
204
|
-
//
|
|
205
|
-
const C = Schema.Compile({
|
|
206
|
-
type: 'object',
|
|
207
|
-
required: ['x', 'y', 'z'],
|
|
208
|
-
properties: {
|
|
209
|
-
x: { type: 'number' },
|
|
210
|
-
y: { type: 'number' },
|
|
211
|
-
z: { type: 'number' }
|
|
212
|
-
}
|
|
213
|
-
})
|
|
212
|
+
// -------------------------------------------------------------------------------
|
|
214
213
|
|
|
215
|
-
|
|
214
|
+
const User = Schema.Compile(Type.Object({
|
|
215
|
+
id: Type.String(),
|
|
216
|
+
name: Type.String(),
|
|
217
|
+
email: Type.String({ format: 'email' })
|
|
218
|
+
}))
|
|
219
|
+
|
|
220
|
+
// -------------------------------------------------------------------------------
|
|
216
221
|
// Parse
|
|
217
|
-
//
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
222
|
+
// -------------------------------------------------------------------------------
|
|
223
|
+
|
|
224
|
+
const user = User.Parse({ // const user: {
|
|
225
|
+
id: '65f1a2b3c4d5e6f7a8b9c0d1', // id: string,
|
|
226
|
+
name: 'user', // name: string,
|
|
227
|
+
email: 'user@domain.com' // email: string
|
|
228
|
+
}) // } = ...
|
|
223
229
|
```
|
|
224
230
|
|
|
225
231
|
<a name="Versions"></a>
|
|
@@ -228,25 +234,10 @@ const R = C.Parse({ x: 0, y: 0, z: 0 }) // const R: {
|
|
|
228
234
|
|
|
229
235
|
TypeBox provides two distinct versions that span two generations of the TypeScript compiler.
|
|
230
236
|
|
|
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.
|
|
237
|
+
| TypeBox | TypeScript | Description |
|
|
238
|
+
| :--- | :--- | :--- |
|
|
239
|
+
| 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.** |
|
|
240
|
+
| 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
241
|
|
|
251
242
|
## Contribute
|
|
252
243
|
|