typebox 1.0.66 → 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/build/guard/emit.d.mts +2 -1
- package/build/guard/emit.mjs +6 -3
- package/build/guard/guard.d.mts +6 -2
- package/build/guard/guard.mjs +16 -81
- package/build/guard/string.d.mts +10 -0
- package/build/guard/string.mjs +154 -0
- package/build/schema/engine/maxLength.mjs +2 -2
- package/build/schema/engine/minLength.mjs +2 -2
- 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/build/guard/emit.d.mts
CHANGED
|
@@ -34,7 +34,8 @@ export declare function IsGreaterThan(left: string, right: string): string;
|
|
|
34
34
|
export declare function IsLessThan(left: string, right: string): string;
|
|
35
35
|
export declare function IsLessEqualThan(left: string, right: string): string;
|
|
36
36
|
export declare function IsGreaterEqualThan(left: string, right: string): string;
|
|
37
|
-
export declare function
|
|
37
|
+
export declare function IsMinLength(value: string, length: string): string;
|
|
38
|
+
export declare function IsMaxLength(value: string, length: string): string;
|
|
38
39
|
export declare function Every(value: string, offset: string, params: [value: string, index: string], expression: string): string;
|
|
39
40
|
export declare function Entries(value: string): string;
|
|
40
41
|
export declare function Keys(value: string): string;
|
package/build/guard/emit.mjs
CHANGED
|
@@ -104,8 +104,11 @@ export function IsGreaterEqualThan(left, right) {
|
|
|
104
104
|
// --------------------------------------------------------------------------
|
|
105
105
|
// String
|
|
106
106
|
// --------------------------------------------------------------------------
|
|
107
|
-
export function
|
|
108
|
-
return `Guard.
|
|
107
|
+
export function IsMinLength(value, length) {
|
|
108
|
+
return `Guard.IsMinLength(${value}, ${length})`;
|
|
109
|
+
}
|
|
110
|
+
export function IsMaxLength(value, length) {
|
|
111
|
+
return `Guard.IsMaxLength(${value}, ${length})`;
|
|
109
112
|
}
|
|
110
113
|
// --------------------------------------------------------------------------
|
|
111
114
|
// Array
|
|
@@ -181,7 +184,7 @@ export function ReduceOr(operands) {
|
|
|
181
184
|
return G.IsEqual(operands.length, 0) ? 'false' : operands.reduce((left, right) => Or(left, right));
|
|
182
185
|
}
|
|
183
186
|
// --------------------------------------------------------------------------
|
|
184
|
-
//
|
|
187
|
+
// Arithmetic
|
|
185
188
|
// --------------------------------------------------------------------------
|
|
186
189
|
export function PrefixIncrement(expression) {
|
|
187
190
|
return `++${expression}`;
|
package/build/guard/guard.d.mts
CHANGED
|
@@ -37,6 +37,12 @@ export declare function IsMultipleOf(dividend: bigint | number, divisor: bigint
|
|
|
37
37
|
/** Returns true if the value appears to be an instance of a class. */
|
|
38
38
|
export declare function IsClassInstance(value: unknown): boolean;
|
|
39
39
|
export declare function IsValueLike(value: unknown): value is bigint | boolean | null | number | string | undefined;
|
|
40
|
+
/** Returns the number of grapheme clusters in the string */
|
|
41
|
+
export declare function GraphemeCount(value: string): number;
|
|
42
|
+
/** Returns true if the string has at most the given number of graphemes */
|
|
43
|
+
export declare function IsMaxLength(value: string, length: number): boolean;
|
|
44
|
+
/** Returns true if the string has at least the given number of graphemes */
|
|
45
|
+
export declare function IsMinLength(value: string, length: number): boolean;
|
|
40
46
|
export declare function Every<T>(value: T[], offset: number, callback: (value: T, index: number) => boolean): boolean;
|
|
41
47
|
export declare function EveryAll<T>(value: T[], offset: number, callback: (value: T, index: number) => boolean): boolean;
|
|
42
48
|
/** Returns true if this value has this property key */
|
|
@@ -55,5 +61,3 @@ export declare function Symbols(value: Record<PropertyKey, unknown>): symbol[];
|
|
|
55
61
|
export declare function Values(value: Record<PropertyKey, unknown>): unknown[];
|
|
56
62
|
/** Tests values for deep equality */
|
|
57
63
|
export declare function IsDeepEqual(left: unknown, right: unknown): boolean;
|
|
58
|
-
/** Returns the number of Unicode Grapheme Clusters */
|
|
59
|
-
export declare function StringGraphemeCount(value: string): number;
|
package/build/guard/guard.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import * as String from './string.mjs';
|
|
1
2
|
// --------------------------------------------------------------------------
|
|
2
3
|
// Guards
|
|
3
4
|
// --------------------------------------------------------------------------
|
|
@@ -127,6 +128,21 @@ export function IsValueLike(value) {
|
|
|
127
128
|
IsUndefined(value);
|
|
128
129
|
}
|
|
129
130
|
// --------------------------------------------------------------------------
|
|
131
|
+
// String
|
|
132
|
+
// --------------------------------------------------------------------------
|
|
133
|
+
/** Returns the number of grapheme clusters in the string */
|
|
134
|
+
export function GraphemeCount(value) {
|
|
135
|
+
return String.GraphemeCount(value);
|
|
136
|
+
}
|
|
137
|
+
/** Returns true if the string has at most the given number of graphemes */
|
|
138
|
+
export function IsMaxLength(value, length) {
|
|
139
|
+
return String.IsMaxLengthFast(value, length);
|
|
140
|
+
}
|
|
141
|
+
/** Returns true if the string has at least the given number of graphemes */
|
|
142
|
+
export function IsMinLength(value, length) {
|
|
143
|
+
return String.IsMinLengthFast(value, length);
|
|
144
|
+
}
|
|
145
|
+
// --------------------------------------------------------------------------
|
|
130
146
|
// Array
|
|
131
147
|
// --------------------------------------------------------------------------
|
|
132
148
|
export function Every(value, offset, callback) {
|
|
@@ -190,84 +206,3 @@ function DeepEqualArray(left, right) {
|
|
|
190
206
|
export function IsDeepEqual(left, right) {
|
|
191
207
|
return (IsArray(left) ? DeepEqualArray(left, right) : IsObject(left) ? DeepEqualObject(left, right) : IsEqual(left, right));
|
|
192
208
|
}
|
|
193
|
-
// --------------------------------------------------------------------------
|
|
194
|
-
// StringGraphemeCountIntl - Intl.Segmenter Polyfill
|
|
195
|
-
// --------------------------------------------------------------------------
|
|
196
|
-
//
|
|
197
|
-
// const segmenter = new Intl.Segmenter(undefined, { granularity: 'grapheme' })
|
|
198
|
-
// function StringGraphemeCountIntl(value: string): number {
|
|
199
|
-
// const iterator = segmenter.segment(value)[Symbol.iterator]()
|
|
200
|
-
// let length = 0
|
|
201
|
-
// while (!iterator.next().done) length++
|
|
202
|
-
// return length
|
|
203
|
-
// }
|
|
204
|
-
//
|
|
205
|
-
// --------------------------------------------------------------------------
|
|
206
|
-
function IsRegionalIndicator(value) {
|
|
207
|
-
return value >= 0x1F1E6 && value <= 0x1F1FF;
|
|
208
|
-
}
|
|
209
|
-
function IsVariationSelector(value) {
|
|
210
|
-
return value >= 0xFE00 && value <= 0xFE0F;
|
|
211
|
-
}
|
|
212
|
-
function IsCombiningMark(value) {
|
|
213
|
-
return ((value >= 0x0300 && value <= 0x036F) ||
|
|
214
|
-
(value >= 0x1AB0 && value <= 0x1AFF) ||
|
|
215
|
-
(value >= 0x1DC0 && value <= 0x1DFF) ||
|
|
216
|
-
(value >= 0xFE20 && value <= 0xFE2F));
|
|
217
|
-
}
|
|
218
|
-
function CodePointLength(value) {
|
|
219
|
-
return value > 0xFFFF ? 2 : 1;
|
|
220
|
-
}
|
|
221
|
-
function StringGraphemeCountIntl(value) {
|
|
222
|
-
let count = 0;
|
|
223
|
-
let index = 0;
|
|
224
|
-
while (index < value.length) {
|
|
225
|
-
const start = value.codePointAt(index);
|
|
226
|
-
let clusterEnd = index + CodePointLength(start);
|
|
227
|
-
// Combining marks & variation selectors
|
|
228
|
-
while (clusterEnd < value.length) {
|
|
229
|
-
const next = value.codePointAt(clusterEnd);
|
|
230
|
-
if (IsCombiningMark(next) || IsVariationSelector(next)) {
|
|
231
|
-
clusterEnd += CodePointLength(next);
|
|
232
|
-
}
|
|
233
|
-
else {
|
|
234
|
-
break;
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
// ZWJ sequences
|
|
238
|
-
while (clusterEnd < value.length - 1 && value[clusterEnd] === '\u200D') {
|
|
239
|
-
const next = value.codePointAt(clusterEnd + 1);
|
|
240
|
-
clusterEnd += 1 + CodePointLength(next);
|
|
241
|
-
}
|
|
242
|
-
// Regional indicator pairs (flags)
|
|
243
|
-
const first = IsRegionalIndicator(start);
|
|
244
|
-
const second = clusterEnd < value.length && IsRegionalIndicator(value.codePointAt(clusterEnd));
|
|
245
|
-
if (first && second) {
|
|
246
|
-
const next = value.codePointAt(clusterEnd);
|
|
247
|
-
clusterEnd += CodePointLength(next);
|
|
248
|
-
}
|
|
249
|
-
count++;
|
|
250
|
-
index = clusterEnd;
|
|
251
|
-
}
|
|
252
|
-
return count;
|
|
253
|
-
}
|
|
254
|
-
// --------------------------------------------------------------------------
|
|
255
|
-
// StringGraphemeCount
|
|
256
|
-
// --------------------------------------------------------------------------
|
|
257
|
-
function IsComplexGraphemeCodeUnit(value) {
|
|
258
|
-
return ((value >= 0xD800 && value <= 0xDBFF) || // High surrogate
|
|
259
|
-
(value >= 0x0300 && value <= 0x036F) || // Combining diacritical marks
|
|
260
|
-
(value === 0x200D) // Zero-width joiner
|
|
261
|
-
);
|
|
262
|
-
}
|
|
263
|
-
/** Returns the number of Unicode Grapheme Clusters */
|
|
264
|
-
export function StringGraphemeCount(value) {
|
|
265
|
-
let count = 0;
|
|
266
|
-
for (let index = 0; index < value.length; index++) {
|
|
267
|
-
if (IsComplexGraphemeCodeUnit(value.charCodeAt(index))) {
|
|
268
|
-
return StringGraphemeCountIntl(value);
|
|
269
|
-
}
|
|
270
|
-
count++;
|
|
271
|
-
}
|
|
272
|
-
return count;
|
|
273
|
-
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** Returns the number of grapheme clusters in a string */
|
|
2
|
+
export declare function GraphemeCount(value: string): number;
|
|
3
|
+
/** Checks if a string has at least a minimum number of grapheme clusters */
|
|
4
|
+
export declare function IsMinLength(value: string, minLength: number): boolean;
|
|
5
|
+
/** Checks if a string has at most a maximum number of grapheme clusters */
|
|
6
|
+
export declare function IsMaxLength(value: string, maxLength: number): boolean;
|
|
7
|
+
/** Fast check for minimum grapheme length, falls back to full check if needed */
|
|
8
|
+
export declare function IsMinLengthFast(value: string, minLength: number): boolean;
|
|
9
|
+
/** Fast check for maximum grapheme length, falls back to full check if needed */
|
|
10
|
+
export declare function IsMaxLengthFast(value: string, maxLength: number): boolean;
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
// --------------------------------------------------------------------------
|
|
2
|
+
// IsBetween
|
|
3
|
+
// --------------------------------------------------------------------------
|
|
4
|
+
function IsBetween(value, min, max) {
|
|
5
|
+
return value >= min && value <= max;
|
|
6
|
+
}
|
|
7
|
+
// --------------------------------------------------------------------------
|
|
8
|
+
// IsRegionalIndicator
|
|
9
|
+
// --------------------------------------------------------------------------
|
|
10
|
+
function IsRegionalIndicator(value) {
|
|
11
|
+
return IsBetween(value, 0x1F1E6, 0x1F1FF);
|
|
12
|
+
}
|
|
13
|
+
// --------------------------------------------------------------------------
|
|
14
|
+
// IsVariationSelector
|
|
15
|
+
// --------------------------------------------------------------------------
|
|
16
|
+
function IsVariationSelector(value) {
|
|
17
|
+
return IsBetween(value, 0xFE00, 0xFE0F);
|
|
18
|
+
}
|
|
19
|
+
// --------------------------------------------------------------------------
|
|
20
|
+
// IsCombiningMark
|
|
21
|
+
// --------------------------------------------------------------------------
|
|
22
|
+
function IsCombiningMark(value) {
|
|
23
|
+
return (IsBetween(value, 0x0300, 0x036F) ||
|
|
24
|
+
IsBetween(value, 0x1AB0, 0x1AFF) ||
|
|
25
|
+
IsBetween(value, 0x1DC0, 0x1DFF) ||
|
|
26
|
+
IsBetween(value, 0xFE20, 0xFE2F));
|
|
27
|
+
}
|
|
28
|
+
// --------------------------------------------------------------------------
|
|
29
|
+
// CodePointLength
|
|
30
|
+
// --------------------------------------------------------------------------
|
|
31
|
+
function CodePointLength(value) {
|
|
32
|
+
return value > 0xFFFF ? 2 : 1;
|
|
33
|
+
}
|
|
34
|
+
// --------------------------------------------------------------------------
|
|
35
|
+
// ConsumeModifiers (helper)
|
|
36
|
+
// --------------------------------------------------------------------------
|
|
37
|
+
function ConsumeModifiers(value, index) {
|
|
38
|
+
while (index < value.length) {
|
|
39
|
+
const point = value.codePointAt(index);
|
|
40
|
+
if (IsCombiningMark(point) || IsVariationSelector(point)) {
|
|
41
|
+
index += CodePointLength(point);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return index;
|
|
48
|
+
}
|
|
49
|
+
// --------------------------------------------------------------------------
|
|
50
|
+
// NextGraphemeClusterIndex
|
|
51
|
+
// --------------------------------------------------------------------------
|
|
52
|
+
function NextGraphemeClusterIndex(value, clusterStart) {
|
|
53
|
+
const startCP = value.codePointAt(clusterStart);
|
|
54
|
+
let clusterEnd = clusterStart + CodePointLength(startCP);
|
|
55
|
+
// Consume combining marks & variation selectors
|
|
56
|
+
clusterEnd = ConsumeModifiers(value, clusterEnd);
|
|
57
|
+
// Handle multi-ZWJ sequences
|
|
58
|
+
while (clusterEnd < value.length - 1 && value[clusterEnd] === '\u200D') {
|
|
59
|
+
const nextCP = value.codePointAt(clusterEnd + 1);
|
|
60
|
+
clusterEnd += 1 + CodePointLength(nextCP);
|
|
61
|
+
clusterEnd = ConsumeModifiers(value, clusterEnd);
|
|
62
|
+
}
|
|
63
|
+
// Handle regional indicator pairs (flags)
|
|
64
|
+
if (IsRegionalIndicator(startCP) &&
|
|
65
|
+
clusterEnd < value.length &&
|
|
66
|
+
IsRegionalIndicator(value.codePointAt(clusterEnd))) {
|
|
67
|
+
clusterEnd += CodePointLength(value.codePointAt(clusterEnd));
|
|
68
|
+
}
|
|
69
|
+
return clusterEnd;
|
|
70
|
+
}
|
|
71
|
+
// --------------------------------------------------------------------------
|
|
72
|
+
// IsGraphemeCodePoint
|
|
73
|
+
// --------------------------------------------------------------------------
|
|
74
|
+
function IsGraphemeCodePoint(value) {
|
|
75
|
+
return (IsBetween(value, 0xD800, 0xDBFF) || // High surrogate
|
|
76
|
+
IsBetween(value, 0x0300, 0x036F) || // Combining diacritical marks
|
|
77
|
+
(value === 0x200D) // Zero-width joiner
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
// --------------------------------------------------------------------------
|
|
81
|
+
// GraphemeCount
|
|
82
|
+
// --------------------------------------------------------------------------
|
|
83
|
+
/** Returns the number of grapheme clusters in a string */
|
|
84
|
+
export function GraphemeCount(value) {
|
|
85
|
+
let count = 0;
|
|
86
|
+
let index = 0;
|
|
87
|
+
while (index < value.length) {
|
|
88
|
+
index = NextGraphemeClusterIndex(value, index);
|
|
89
|
+
count++;
|
|
90
|
+
}
|
|
91
|
+
return count;
|
|
92
|
+
}
|
|
93
|
+
// --------------------------------------------------------------------------
|
|
94
|
+
// IsMinLength
|
|
95
|
+
// --------------------------------------------------------------------------
|
|
96
|
+
/** Checks if a string has at least a minimum number of grapheme clusters */
|
|
97
|
+
export function IsMinLength(value, minLength) {
|
|
98
|
+
let count = 0;
|
|
99
|
+
let index = 0;
|
|
100
|
+
while (index < value.length) {
|
|
101
|
+
index = NextGraphemeClusterIndex(value, index);
|
|
102
|
+
count++;
|
|
103
|
+
if (count >= minLength)
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
// --------------------------------------------------------------------------
|
|
109
|
+
// IsMaxLength
|
|
110
|
+
// --------------------------------------------------------------------------
|
|
111
|
+
/** Checks if a string has at most a maximum number of grapheme clusters */
|
|
112
|
+
export function IsMaxLength(value, maxLength) {
|
|
113
|
+
let count = 0;
|
|
114
|
+
let index = 0;
|
|
115
|
+
while (index < value.length) {
|
|
116
|
+
index = NextGraphemeClusterIndex(value, index);
|
|
117
|
+
count++;
|
|
118
|
+
if (count > maxLength)
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
// --------------------------------------------------------------------------
|
|
124
|
+
// IsMinLengthFast
|
|
125
|
+
// --------------------------------------------------------------------------
|
|
126
|
+
/** Fast check for minimum grapheme length, falls back to full check if needed */
|
|
127
|
+
export function IsMinLengthFast(value, minLength) {
|
|
128
|
+
let index = 0;
|
|
129
|
+
while (index < value.length) {
|
|
130
|
+
if (IsGraphemeCodePoint(value.charCodeAt(index))) {
|
|
131
|
+
return IsMinLength(value, minLength);
|
|
132
|
+
}
|
|
133
|
+
index++;
|
|
134
|
+
if (index >= minLength)
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
// --------------------------------------------------------------------------
|
|
140
|
+
// IsMaxLengthFast
|
|
141
|
+
// --------------------------------------------------------------------------
|
|
142
|
+
/** Fast check for maximum grapheme length, falls back to full check if needed */
|
|
143
|
+
export function IsMaxLengthFast(value, maxLength) {
|
|
144
|
+
let index = 0;
|
|
145
|
+
while (index < value.length) {
|
|
146
|
+
if (IsGraphemeCodePoint(value.charCodeAt(index))) {
|
|
147
|
+
return IsMaxLength(value, maxLength);
|
|
148
|
+
}
|
|
149
|
+
index++;
|
|
150
|
+
if (index > maxLength)
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
return true;
|
|
154
|
+
}
|
|
@@ -4,13 +4,13 @@ import { Guard as G, EmitGuard as E } from '../../guard/index.mjs';
|
|
|
4
4
|
// Build
|
|
5
5
|
// ------------------------------------------------------------------
|
|
6
6
|
export function BuildMaxLength(stack, context, schema, value) {
|
|
7
|
-
return E.
|
|
7
|
+
return E.IsMaxLength(value, E.Constant(schema.maxLength));
|
|
8
8
|
}
|
|
9
9
|
// ------------------------------------------------------------------
|
|
10
10
|
// Check
|
|
11
11
|
// ------------------------------------------------------------------
|
|
12
12
|
export function CheckMaxLength(stack, context, schema, value) {
|
|
13
|
-
return G.
|
|
13
|
+
return G.IsMaxLength(value, schema.maxLength);
|
|
14
14
|
}
|
|
15
15
|
// ------------------------------------------------------------------
|
|
16
16
|
// Error
|
|
@@ -4,13 +4,13 @@ import { Guard as G, EmitGuard as E } from '../../guard/index.mjs';
|
|
|
4
4
|
// Build
|
|
5
5
|
// ------------------------------------------------------------------
|
|
6
6
|
export function BuildMinLength(stack, context, schema, value) {
|
|
7
|
-
return E.
|
|
7
|
+
return E.IsMinLength(value, E.Constant(schema.minLength));
|
|
8
8
|
}
|
|
9
9
|
// ------------------------------------------------------------------
|
|
10
10
|
// Check
|
|
11
11
|
// ------------------------------------------------------------------
|
|
12
12
|
export function CheckMinLength(stack, context, schema, value) {
|
|
13
|
-
return G.
|
|
13
|
+
return G.IsMinLength(value, schema.minLength);
|
|
14
14
|
}
|
|
15
15
|
// ------------------------------------------------------------------
|
|
16
16
|
// Error
|
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.
|