typebox 1.0.26 → 1.0.27
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.
|
@@ -7,7 +7,7 @@ import { BuildSchema, CheckSchema, ErrorSchema } from './schema.mjs';
|
|
|
7
7
|
// ------------------------------------------------------------------
|
|
8
8
|
function BuildItemsSized(context, schema, value) {
|
|
9
9
|
return E.ReduceAnd(schema.items.map((schema, index) => {
|
|
10
|
-
const isLength = E.
|
|
10
|
+
const isLength = E.IsLessEqualThan(E.Member(value, 'length'), E.Constant(index));
|
|
11
11
|
const isSchema = BuildSchema(context, schema, `${value}[${index}]`);
|
|
12
12
|
const addIndex = context.AddIndex(E.Constant(index));
|
|
13
13
|
const guarded = context.UseUnevaluated() ? E.And(isSchema, addIndex) : isSchema;
|
|
@@ -16,7 +16,7 @@ function BuildItemsSized(context, schema, value) {
|
|
|
16
16
|
}
|
|
17
17
|
function CheckItemsSized(context, schema, value) {
|
|
18
18
|
return G.Every(schema.items, 0, (schema, index) => {
|
|
19
|
-
return G.
|
|
19
|
+
return G.IsLessEqualThan(value.length, index)
|
|
20
20
|
|| (CheckSchema(context, schema, value[index]) && context.AddIndex(index));
|
|
21
21
|
});
|
|
22
22
|
}
|
|
@@ -24,7 +24,7 @@ function ErrorItemsSized(context, schemaPath, instancePath, schema, value) {
|
|
|
24
24
|
return G.EveryAll(schema.items, 0, (schema, index) => {
|
|
25
25
|
const nextSchemaPath = `${schemaPath}/items/${index}`;
|
|
26
26
|
const nextInstancePath = `${instancePath}/${index}`;
|
|
27
|
-
return G.
|
|
27
|
+
return G.IsLessEqualThan(value.length, index)
|
|
28
28
|
|| (ErrorSchema(context, nextSchemaPath, nextInstancePath, schema, value[index]) && context.AddIndex(index));
|
|
29
29
|
});
|
|
30
30
|
}
|
|
@@ -6,7 +6,7 @@ import { BuildSchema, CheckSchema, ErrorSchema } from './schema.mjs';
|
|
|
6
6
|
// ------------------------------------------------------------------
|
|
7
7
|
export function BuildPrefixItems(context, schema, value) {
|
|
8
8
|
return E.ReduceAnd(schema.prefixItems.map((schema, index) => {
|
|
9
|
-
const isLength = E.
|
|
9
|
+
const isLength = E.IsLessEqualThan(E.Member(value, 'length'), E.Constant(index));
|
|
10
10
|
const isSchema = BuildSchema(context, schema, `${value}[${index}]`);
|
|
11
11
|
const addIndex = context.AddIndex(E.Constant(index));
|
|
12
12
|
const guarded = context.UseUnevaluated() ? E.And(isSchema, addIndex) : isSchema;
|
|
@@ -18,7 +18,7 @@ export function BuildPrefixItems(context, schema, value) {
|
|
|
18
18
|
// ------------------------------------------------------------------
|
|
19
19
|
export function CheckPrefixItems(context, schema, value) {
|
|
20
20
|
return G.IsEqual(value.length, 0) || G.Every(schema.prefixItems, 0, (schema, index) => {
|
|
21
|
-
return G.
|
|
21
|
+
return G.IsLessEqualThan(value.length, index)
|
|
22
22
|
|| (CheckSchema(context, schema, value[index]) && context.AddIndex(index));
|
|
23
23
|
});
|
|
24
24
|
}
|
|
@@ -29,7 +29,7 @@ export function ErrorPrefixItems(context, schemaPath, instancePath, schema, valu
|
|
|
29
29
|
return G.IsEqual(value.length, 0) || G.EveryAll(schema.prefixItems, 0, (schema, index) => {
|
|
30
30
|
const nextSchemaPath = `${schemaPath}/prefixItems/${index}`;
|
|
31
31
|
const nextInstancePath = `${instancePath}/${index}`;
|
|
32
|
-
return G.
|
|
32
|
+
return G.IsLessEqualThan(value.length, index)
|
|
33
33
|
|| (ErrorSchema(context, nextSchemaPath, nextInstancePath, schema, value[index]) && context.AddIndex(index));
|
|
34
34
|
});
|
|
35
35
|
}
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -19,16 +19,14 @@
|
|
|
19
19
|
## Install
|
|
20
20
|
|
|
21
21
|
```bash
|
|
22
|
-
$ npm install typebox
|
|
23
|
-
|
|
24
|
-
$ npm install @sinclair/typebox --save # 0.34.x
|
|
22
|
+
$ npm install typebox
|
|
25
23
|
```
|
|
26
24
|
|
|
27
25
|
|
|
28
26
|
## Usage
|
|
29
27
|
|
|
30
28
|
```typescript
|
|
31
|
-
import Type
|
|
29
|
+
import Type from 'typebox'
|
|
32
30
|
|
|
33
31
|
const T = Type.Object({ // const T = {
|
|
34
32
|
x: Type.Number(), // type: 'object',
|
|
@@ -40,7 +38,7 @@ const T = Type.Object({ // const T = {
|
|
|
40
38
|
// }
|
|
41
39
|
// }
|
|
42
40
|
|
|
43
|
-
type T = Static<typeof T>
|
|
41
|
+
type T = Type.Static<typeof T> // type T = {
|
|
44
42
|
// x: number,
|
|
45
43
|
// y: number,
|
|
46
44
|
// z: number
|
|
@@ -68,7 +66,7 @@ License: MIT
|
|
|
68
66
|
|
|
69
67
|
## Upgrade
|
|
70
68
|
|
|
71
|
-
|
|
69
|
+
If upgrading from `@sinclair/typebox` refer to the 1.0 migration guide at the following URL.
|
|
72
70
|
|
|
73
71
|
[Migration Guide](https://github.com/sinclairzx81/typebox/blob/main/changelog/1.0.0-migration.md)
|
|
74
72
|
|
|
@@ -76,7 +74,7 @@ Refer to the following URL for information on upgrading from 0.34.x to 1.0.
|
|
|
76
74
|
|
|
77
75
|
## Type
|
|
78
76
|
|
|
79
|
-
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/type/overview) | [Example](https://www.typescriptlang.org/play
|
|
77
|
+
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/type/overview) | [Example](https://www.typescriptlang.org/play/#code/JYWwDg9gTgLgBAFQJ5gKZwGZQiOByGFVAIwgA88AoSgYwgDsBneBOAXkSIDoB5YgK1Q0YACgDecSVOkzZcuAHoFcOkxbs4YypLIAuTmi4A5AK4hiqKCICUAGnkPHspVMJp9eCAKEw8t7XBI+siGpuaWNvZO0Q4uklCoAI4mwAkAJvoA2ngU9nhIfvgAXngAuv6SRcHcYRZW1jGNcnFwYNhosMCojPpaAL4NTUPDisrSeppwbqge9GZ1eHB9FSOrjS2SQZPTs-OWi8sBa8exY1JV20S74VAHRycP0i1994+PLi+U04gaIahcAGUYABDGDAGgAHmmEAwiAAfCcXN9WBwtG90aMpBM5jcVhiHi0tji6nj8ccWhdiZZXmSRh8gA)
|
|
80
78
|
|
|
81
79
|
TypeBox includes many functions to create Json Schema types. Each function returns a small Json Schema fragment that corresponds to a TypeScript type. TypeBox uses function composition to combine schema fragments into more complex types. It provides a set of functions that are used to model Json Schema schematics as well as a set of functions that model constructs native to JavaScript and TypeScript.
|
|
82
80
|
|
|
@@ -85,7 +83,7 @@ TypeBox includes many functions to create Json Schema types. Each function retur
|
|
|
85
83
|
The following creates a Json Schema type and infers with Static.
|
|
86
84
|
|
|
87
85
|
```typescript
|
|
88
|
-
import Type
|
|
86
|
+
import Type from 'typebox'
|
|
89
87
|
|
|
90
88
|
const T = Type.Object({ // const T = {
|
|
91
89
|
x: Type.Number(), // type: 'object',
|
|
@@ -97,7 +95,7 @@ const T = Type.Object({ // const T = {
|
|
|
97
95
|
// }
|
|
98
96
|
// }
|
|
99
97
|
|
|
100
|
-
type T = Static<typeof T>
|
|
98
|
+
type T = Type.Static<typeof T> // type T = {
|
|
101
99
|
// x: number,
|
|
102
100
|
// y: number,
|
|
103
101
|
// z: number
|
|
@@ -110,16 +108,16 @@ type T = Static<typeof T> // type T = {
|
|
|
110
108
|
|
|
111
109
|
## Script
|
|
112
110
|
|
|
113
|
-
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/script/overview) | [Example](https://www.typescriptlang.org/play/?moduleResolution=99&target=99&jsx=0&module=199#code/
|
|
111
|
+
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/script/overview) | [Example](https://www.typescriptlang.org/play/?moduleResolution=99&target=99&jsx=0&module=199#code/JYWwDg9gTgLgBAFQJ5gKZwGZQiOByGFVAIwgA88AoSgYwgDsBneBOAXkSIDoBlGqYGBgAKAAYBvOJThwyALjj0AriGKooAGikykC5avVbpcAF56VaqFIC+ogJQzHT5y9dv3zgPSe4dJi3Y4cWMPULDwp29HQjQFPAhiACtUGhg8DRCIrOzInxkoVABHJWACgBMFAG08Ci08JHT8EzwAXQycjpyomTBsNFhgVEYFYM6x8O7HeSC4GNQ4-Us8OGt28fW3SZ0R2aIFi3Vl1cyN07gt0x25-YMoI5Oz9cnrB8exqJfaBmY4HkDkNC8fiCESSVirOASYyVADScGA9DgAGtUEgIBhEC0FAhYS04AAfRRKAA2xMotgcb3GUT8Pz+HFGVI2k2u+ASyVS6VeTOykwKxVKqAqcGqtXwDTqzTa3J5EUmvQg-Rgg2GQRlsrCF2mknVGo8FxkAEN6EgAPIYKq6vWbPKuSSsvCLQ4rIzWjoGxz2vb4ZSk+5urq2px4q0B3LOY5hrIXXQzUNR85BxzGs0WkXxqMemRe2I+g53F3aBOapNOHPzPN+lYZsMekPFksRtYN9wXMxxlv60twFPmy2d1vd7O7XOO-NHV0D1xZmYO33E-1Tlx1otL8NOSNr9fVrfhz5zX7-bg8GCG5U0AA8c3RvwAfE8fAf6Wrd45JtMnVZCfPm7vJrHPwJIlSV-LdJnbQDvxJMlXxkD4gA)
|
|
114
112
|
|
|
115
|
-
TypeBox
|
|
113
|
+
TypeBox can translate TypeScript syntax into Json Schema. The Script function is a fully type-safe, syntactic frontend to the TypeBox type builder API, allowing Json Schema to be constructed and mapped using TypeScript type expressions encoded as strings.
|
|
116
114
|
|
|
117
115
|
### Example
|
|
118
116
|
|
|
119
117
|
The following uses Script to construct and map Json Schema.
|
|
120
118
|
|
|
121
119
|
```typescript
|
|
122
|
-
import Type
|
|
120
|
+
import Type from 'typebox'
|
|
123
121
|
|
|
124
122
|
const T = Type.Script(`{
|
|
125
123
|
x: number,
|
|
@@ -162,7 +160,7 @@ const S = Type.Script({ T }, `{
|
|
|
162
160
|
// }
|
|
163
161
|
// }
|
|
164
162
|
|
|
165
|
-
type S = Static<typeof S>
|
|
163
|
+
type S = Type.Static<typeof S> // type S = {
|
|
166
164
|
// x: number | null,
|
|
167
165
|
// y: number | null,
|
|
168
166
|
// z: number | null
|
|
@@ -175,7 +173,7 @@ type S = Static<typeof S> // type S = {
|
|
|
175
173
|
|
|
176
174
|
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/value/overview) | [Example](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAFQJ5gKZwGZQiOByGFVAIwgA88AoUSWOANQEMAbAV3Sx30LVLIHoAbi3ZVKAYwgA7AM7wEcALyIiAOgDyxAFapxMABQBvSnDhkAXCrSqAcqxDFUUfQEoANCbhJLya3YdOrh6mAF4+av6Ozi6UAL4xlPz8cADCABa6ANaUEtJycACCSgwiqKrpWfoIbnCGpvUNjU1NSXCSsvAFlqQQzKiMUsUwUOyeFnAAjDXNM7Nzs57ecABMwXBhcADMcQmtAAqMUDKoOe35AELFTGxlB0eoVTV18y+mrWfw55bGpuNTrwDAfVWr9LFJ7FE1ktVkDYS8QV4wRCnGsNps4RjZgiNuCAlAdpjCVjkrFiqpyUA)
|
|
177
175
|
|
|
178
|
-
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.
|
|
176
|
+
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.
|
|
179
177
|
|
|
180
178
|
The Value module is available via optional import.
|
|
181
179
|
|
|
@@ -218,7 +216,7 @@ const B = Value.Parse(T, { // const B: {
|
|
|
218
216
|
|
|
219
217
|
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/compile/overview) | [Example](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgYQuYAbApnAvnAMyjTgHIYBPMLAIwgA8B6AYzTEy1IChRJY4AKlRxES5YXXrcurAHYBneMjgBeFGw4AKIdQB0AeRoArLMxiakcK9Zu3bjRnDmKUALjgA1AIYZgAEy8YaAAeBFwAGkFDEzNQrit6dx0sXQA5AFcQGiwoTQBKSLsi4pKrB2tEwQysnPD4uAok4TTM7NyC0s6u8qtGqtba+oAvJr1qtvyuqZKeuBH+mqguXDy86fWNm3LcAD4dri5y5AALUwBrA+d4AEFVFF0T84tNjfKruGv3OghsL1k7mBQdJYeqVACMhReUNs9T6ACY6lZ5gBmZZ5A7lAAKXig8hBMggCngACE7shdNjcVhntDuo53sT3AhQe4IbT2WVHAl3LIBlBEQ13AiOezZn1eYsBSiRaKuXMeXy0TKZds7rp1UA)
|
|
220
218
|
|
|
221
|
-
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.
|
|
219
|
+
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.
|
|
222
220
|
|
|
223
221
|
The Compile module is available via optional import.
|
|
224
222
|
|