@sinclair/typebox 0.25.21 → 0.25.23
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/compiler/compiler.d.ts +2 -2
- package/compiler/compiler.js +46 -10
- package/package.json +1 -1
- package/readme.md +51 -51
- package/typebox.d.ts +5 -10
package/compiler/compiler.d.ts
CHANGED
|
@@ -14,8 +14,8 @@ export declare class TypeCheck<T extends Types.TSchema> {
|
|
|
14
14
|
/** Returns true if the value matches the compiled type. */
|
|
15
15
|
Check(value: unknown): value is Types.Static<T>;
|
|
16
16
|
}
|
|
17
|
-
export declare namespace
|
|
18
|
-
function
|
|
17
|
+
export declare namespace MemberExpression {
|
|
18
|
+
function Encode(object: string, key: string): string;
|
|
19
19
|
}
|
|
20
20
|
export declare class TypeCompilerUnknownTypeError extends Error {
|
|
21
21
|
readonly schema: Types.TSchema;
|
package/compiler/compiler.js
CHANGED
|
@@ -27,7 +27,7 @@ THE SOFTWARE.
|
|
|
27
27
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.TypeCompiler = exports.TypeCompilerUnknownTypeError = exports.
|
|
30
|
+
exports.TypeCompiler = exports.TypeCompilerUnknownTypeError = exports.MemberExpression = exports.TypeCheck = void 0;
|
|
31
31
|
const index_1 = require("../errors/index");
|
|
32
32
|
const index_2 = require("../system/index");
|
|
33
33
|
const index_3 = require("../guard/index");
|
|
@@ -60,41 +60,74 @@ class TypeCheck {
|
|
|
60
60
|
}
|
|
61
61
|
exports.TypeCheck = TypeCheck;
|
|
62
62
|
// -------------------------------------------------------------------
|
|
63
|
-
//
|
|
63
|
+
// Character
|
|
64
64
|
// -------------------------------------------------------------------
|
|
65
|
-
var
|
|
66
|
-
(function (
|
|
65
|
+
var Character;
|
|
66
|
+
(function (Character) {
|
|
67
67
|
function DollarSign(code) {
|
|
68
68
|
return code === 36;
|
|
69
69
|
}
|
|
70
|
+
Character.DollarSign = DollarSign;
|
|
70
71
|
function Underscore(code) {
|
|
71
72
|
return code === 95;
|
|
72
73
|
}
|
|
74
|
+
Character.Underscore = Underscore;
|
|
73
75
|
function Numeric(code) {
|
|
74
76
|
return code >= 48 && code <= 57;
|
|
75
77
|
}
|
|
78
|
+
Character.Numeric = Numeric;
|
|
76
79
|
function Alpha(code) {
|
|
77
80
|
return (code >= 65 && code <= 90) || (code >= 97 && code <= 122);
|
|
78
81
|
}
|
|
82
|
+
Character.Alpha = Alpha;
|
|
83
|
+
})(Character || (Character = {}));
|
|
84
|
+
// -------------------------------------------------------------------
|
|
85
|
+
// Identifier
|
|
86
|
+
// -------------------------------------------------------------------
|
|
87
|
+
var Identifier;
|
|
88
|
+
(function (Identifier) {
|
|
89
|
+
function Encode($id) {
|
|
90
|
+
const buffer = [];
|
|
91
|
+
for (let i = 0; i < $id.length; i++) {
|
|
92
|
+
const code = $id.charCodeAt(i);
|
|
93
|
+
if (Character.Numeric(code) || Character.Alpha(code)) {
|
|
94
|
+
buffer.push($id.charAt(i));
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
buffer.push(`_${code}_`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return buffer.join('').replace(/__/g, '_');
|
|
101
|
+
}
|
|
102
|
+
Identifier.Encode = Encode;
|
|
103
|
+
})(Identifier || (Identifier = {}));
|
|
104
|
+
// -------------------------------------------------------------------
|
|
105
|
+
// MemberExpression
|
|
106
|
+
// -------------------------------------------------------------------
|
|
107
|
+
var MemberExpression;
|
|
108
|
+
(function (MemberExpression) {
|
|
79
109
|
function Check(propertyName) {
|
|
80
110
|
if (propertyName.length === 0)
|
|
81
111
|
return false;
|
|
82
112
|
{
|
|
83
113
|
const code = propertyName.charCodeAt(0);
|
|
84
|
-
if (!(DollarSign(code) || Underscore(code) || Alpha(code))) {
|
|
114
|
+
if (!(Character.DollarSign(code) || Character.Underscore(code) || Character.Alpha(code))) {
|
|
85
115
|
return false;
|
|
86
116
|
}
|
|
87
117
|
}
|
|
88
118
|
for (let i = 1; i < propertyName.length; i++) {
|
|
89
119
|
const code = propertyName.charCodeAt(i);
|
|
90
|
-
if (!(DollarSign(code) || Underscore(code) || Alpha(code) || Numeric(code))) {
|
|
120
|
+
if (!(Character.DollarSign(code) || Character.Underscore(code) || Character.Alpha(code) || Character.Numeric(code))) {
|
|
91
121
|
return false;
|
|
92
122
|
}
|
|
93
123
|
}
|
|
94
124
|
return true;
|
|
95
125
|
}
|
|
96
|
-
|
|
97
|
-
|
|
126
|
+
function Encode(object, key) {
|
|
127
|
+
return !Check(key) ? `${object}['${key}']` : `${object}.${key}`;
|
|
128
|
+
}
|
|
129
|
+
MemberExpression.Encode = Encode;
|
|
130
|
+
})(MemberExpression = exports.MemberExpression || (exports.MemberExpression = {}));
|
|
98
131
|
// -------------------------------------------------------------------
|
|
99
132
|
// TypeCompiler
|
|
100
133
|
// -------------------------------------------------------------------
|
|
@@ -108,6 +141,9 @@ exports.TypeCompilerUnknownTypeError = TypeCompilerUnknownTypeError;
|
|
|
108
141
|
/** Compiles Types for Runtime Type Checking */
|
|
109
142
|
var TypeCompiler;
|
|
110
143
|
(function (TypeCompiler) {
|
|
144
|
+
// -------------------------------------------------------------------
|
|
145
|
+
// Guards
|
|
146
|
+
// -------------------------------------------------------------------
|
|
111
147
|
function IsNumber(value) {
|
|
112
148
|
return typeof value === 'number' && !globalThis.isNaN(value);
|
|
113
149
|
}
|
|
@@ -223,7 +259,7 @@ var TypeCompiler;
|
|
|
223
259
|
yield `(Object.getOwnPropertyNames(${value}).every(key => ${keys}.includes(key) || ${expression}))`;
|
|
224
260
|
}
|
|
225
261
|
for (const propertyKey of propertyKeys) {
|
|
226
|
-
const memberExpression =
|
|
262
|
+
const memberExpression = MemberExpression.Encode(value, propertyKey);
|
|
227
263
|
const propertySchema = schema.properties[propertyKey];
|
|
228
264
|
if (schema.required && schema.required.includes(propertyKey)) {
|
|
229
265
|
yield* Visit(propertySchema, memberExpression);
|
|
@@ -406,7 +442,7 @@ var TypeCompiler;
|
|
|
406
442
|
return `(${[...Visit(schema, value)].join(' && ')})`;
|
|
407
443
|
}
|
|
408
444
|
function CreateFunctionName($id) {
|
|
409
|
-
return `check_${$id
|
|
445
|
+
return `check_${Identifier.Encode($id)}`;
|
|
410
446
|
}
|
|
411
447
|
function CreateFunction(name, schema, value) {
|
|
412
448
|
const expression = [...Visit(schema, value)].map((condition) => ` ${condition}`).join(' &&\n');
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -1233,29 +1233,29 @@ This benchmark measures compilation performance for varying types. You can revie
|
|
|
1233
1233
|
┌──────────────────┬────────────┬──────────────┬──────────────┬──────────────┐
|
|
1234
1234
|
│ (index) │ Iterations │ Ajv │ TypeCompiler │ Performance │
|
|
1235
1235
|
├──────────────────┼────────────┼──────────────┼──────────────┼──────────────┤
|
|
1236
|
-
│ Number │ 2000 │ '
|
|
1237
|
-
│ String │ 2000 │ '
|
|
1238
|
-
│ Boolean │ 2000 │ '
|
|
1239
|
-
│ Null │ 2000 │ '
|
|
1240
|
-
│ RegEx │ 2000 │ '
|
|
1241
|
-
│ ObjectA │ 2000 │ '
|
|
1242
|
-
│ ObjectB │ 2000 │ '
|
|
1243
|
-
│ Tuple │ 2000 │ '
|
|
1244
|
-
│ Union │ 2000 │ '
|
|
1245
|
-
│ Vector4 │ 2000 │ '
|
|
1246
|
-
│ Matrix4 │ 2000 │ '
|
|
1247
|
-
│ Literal_String │ 2000 │ '
|
|
1248
|
-
│ Literal_Number │ 2000 │ '
|
|
1249
|
-
│ Literal_Boolean │ 2000 │ '
|
|
1250
|
-
│ Array_Number │ 2000 │ '
|
|
1251
|
-
│ Array_String │ 2000 │ '
|
|
1252
|
-
│ Array_Boolean │ 2000 │ '
|
|
1253
|
-
│ Array_ObjectA │ 2000 │ '
|
|
1254
|
-
│ Array_ObjectB │ 2000 │ '
|
|
1255
|
-
│ Array_Tuple │ 2000 │ '
|
|
1256
|
-
│ Array_Union │ 2000 │ '
|
|
1257
|
-
│ Array_Vector4 │ 2000 │ '
|
|
1258
|
-
│ Array_Matrix4 │ 2000 │ '
|
|
1236
|
+
│ Number │ 2000 │ ' 418 ms' │ ' 14 ms' │ ' 29.86 x' │
|
|
1237
|
+
│ String │ 2000 │ ' 331 ms' │ ' 12 ms' │ ' 27.58 x' │
|
|
1238
|
+
│ Boolean │ 2000 │ ' 290 ms' │ ' 13 ms' │ ' 22.31 x' │
|
|
1239
|
+
│ Null │ 2000 │ ' 253 ms' │ ' 8 ms' │ ' 31.63 x' │
|
|
1240
|
+
│ RegEx │ 2000 │ ' 481 ms' │ ' 18 ms' │ ' 26.72 x' │
|
|
1241
|
+
│ ObjectA │ 2000 │ ' 2675 ms' │ ' 54 ms' │ ' 49.54 x' │
|
|
1242
|
+
│ ObjectB │ 2000 │ ' 2849 ms' │ ' 39 ms' │ ' 73.05 x' │
|
|
1243
|
+
│ Tuple │ 2000 │ ' 1224 ms' │ ' 22 ms' │ ' 55.64 x' │
|
|
1244
|
+
│ Union │ 2000 │ ' 1225 ms' │ ' 26 ms' │ ' 47.12 x' │
|
|
1245
|
+
│ Vector4 │ 2000 │ ' 1777 ms' │ ' 24 ms' │ ' 74.04 x' │
|
|
1246
|
+
│ Matrix4 │ 2000 │ ' 825 ms' │ ' 12 ms' │ ' 68.75 x' │
|
|
1247
|
+
│ Literal_String │ 2000 │ ' 345 ms' │ ' 9 ms' │ ' 38.33 x' │
|
|
1248
|
+
│ Literal_Number │ 2000 │ ' 363 ms' │ ' 7 ms' │ ' 51.86 x' │
|
|
1249
|
+
│ Literal_Boolean │ 2000 │ ' 358 ms' │ ' 6 ms' │ ' 59.67 x' │
|
|
1250
|
+
│ Array_Number │ 2000 │ ' 687 ms' │ ' 8 ms' │ ' 85.88 x' │
|
|
1251
|
+
│ Array_String │ 2000 │ ' 726 ms' │ ' 8 ms' │ ' 90.75 x' │
|
|
1252
|
+
│ Array_Boolean │ 2000 │ ' 703 ms' │ ' 8 ms' │ ' 87.88 x' │
|
|
1253
|
+
│ Array_ObjectA │ 2000 │ ' 3686 ms' │ ' 40 ms' │ ' 92.15 x' │
|
|
1254
|
+
│ Array_ObjectB │ 2000 │ ' 3821 ms' │ ' 40 ms' │ ' 95.53 x' │
|
|
1255
|
+
│ Array_Tuple │ 2000 │ ' 2070 ms' │ ' 17 ms' │ ' 121.76 x' │
|
|
1256
|
+
│ Array_Union │ 2000 │ ' 1503 ms' │ ' 21 ms' │ ' 71.57 x' │
|
|
1257
|
+
│ Array_Vector4 │ 2000 │ ' 2185 ms' │ ' 21 ms' │ ' 104.05 x' │
|
|
1258
|
+
│ Array_Matrix4 │ 2000 │ ' 1502 ms' │ ' 16 ms' │ ' 93.88 x' │
|
|
1259
1259
|
└──────────────────┴────────────┴──────────────┴──────────────┴──────────────┘
|
|
1260
1260
|
```
|
|
1261
1261
|
|
|
@@ -1269,31 +1269,31 @@ This benchmark measures validation performance for varying types. You can review
|
|
|
1269
1269
|
┌──────────────────┬────────────┬──────────────┬──────────────┬──────────────┬──────────────┐
|
|
1270
1270
|
│ (index) │ Iterations │ ValueCheck │ Ajv │ TypeCompiler │ Performance │
|
|
1271
1271
|
├──────────────────┼────────────┼──────────────┼──────────────┼──────────────┼──────────────┤
|
|
1272
|
-
│ Number │ 1000000 │ '
|
|
1273
|
-
│ String │ 1000000 │ '
|
|
1274
|
-
│ Boolean │ 1000000 │ '
|
|
1275
|
-
│ Null │ 1000000 │ '
|
|
1276
|
-
│ RegEx │ 1000000 │ '
|
|
1277
|
-
│ ObjectA │ 1000000 │ '
|
|
1278
|
-
│ ObjectB │ 1000000 │ '
|
|
1279
|
-
│ Tuple │ 1000000 │ '
|
|
1280
|
-
│ Union │ 1000000 │ '
|
|
1281
|
-
│ Recursive │ 1000000 │ '
|
|
1282
|
-
│ Vector4 │ 1000000 │ '
|
|
1283
|
-
│ Matrix4 │ 1000000 │ '
|
|
1284
|
-
│ Literal_String │ 1000000 │ '
|
|
1285
|
-
│ Literal_Number │ 1000000 │ ' 47 ms' │ '
|
|
1286
|
-
│ Literal_Boolean │ 1000000 │ ' 48 ms' │ '
|
|
1287
|
-
│ Array_Number │ 1000000 │ '
|
|
1288
|
-
│ Array_String │ 1000000 │ '
|
|
1289
|
-
│ Array_Boolean │ 1000000 │ '
|
|
1290
|
-
│ Array_ObjectA │ 1000000 │ '
|
|
1291
|
-
│ Array_ObjectB │ 1000000 │ '
|
|
1292
|
-
│ Array_Tuple │ 1000000 │ '
|
|
1293
|
-
│ Array_Union │ 1000000 │ '
|
|
1294
|
-
│ Array_Recursive │ 1000000 │ '
|
|
1295
|
-
│ Array_Vector4 │ 1000000 │ '
|
|
1296
|
-
│ Array_Matrix4 │ 1000000 │ '
|
|
1272
|
+
│ Number │ 1000000 │ ' 28 ms' │ ' 6 ms' │ ' 6 ms' │ ' 1.00 x' │
|
|
1273
|
+
│ String │ 1000000 │ ' 25 ms' │ ' 23 ms' │ ' 11 ms' │ ' 2.09 x' │
|
|
1274
|
+
│ Boolean │ 1000000 │ ' 24 ms' │ ' 21 ms' │ ' 11 ms' │ ' 1.91 x' │
|
|
1275
|
+
│ Null │ 1000000 │ ' 25 ms' │ ' 22 ms' │ ' 10 ms' │ ' 2.20 x' │
|
|
1276
|
+
│ RegEx │ 1000000 │ ' 164 ms' │ ' 53 ms' │ ' 37 ms' │ ' 1.43 x' │
|
|
1277
|
+
│ ObjectA │ 1000000 │ ' 593 ms' │ ' 47 ms' │ ' 25 ms' │ ' 1.88 x' │
|
|
1278
|
+
│ ObjectB │ 1000000 │ ' 1053 ms' │ ' 54 ms' │ ' 40 ms' │ ' 1.35 x' │
|
|
1279
|
+
│ Tuple │ 1000000 │ ' 129 ms' │ ' 25 ms' │ ' 16 ms' │ ' 1.56 x' │
|
|
1280
|
+
│ Union │ 1000000 │ ' 334 ms' │ ' 25 ms' │ ' 16 ms' │ ' 1.56 x' │
|
|
1281
|
+
│ Recursive │ 1000000 │ ' 3127 ms' │ ' 424 ms' │ ' 98 ms' │ ' 4.33 x' │
|
|
1282
|
+
│ Vector4 │ 1000000 │ ' 152 ms' │ ' 24 ms' │ ' 12 ms' │ ' 2.00 x' │
|
|
1283
|
+
│ Matrix4 │ 1000000 │ ' 593 ms' │ ' 41 ms' │ ' 27 ms' │ ' 1.52 x' │
|
|
1284
|
+
│ Literal_String │ 1000000 │ ' 48 ms' │ ' 20 ms' │ ' 11 ms' │ ' 1.82 x' │
|
|
1285
|
+
│ Literal_Number │ 1000000 │ ' 47 ms' │ ' 22 ms' │ ' 10 ms' │ ' 2.20 x' │
|
|
1286
|
+
│ Literal_Boolean │ 1000000 │ ' 48 ms' │ ' 21 ms' │ ' 11 ms' │ ' 1.91 x' │
|
|
1287
|
+
│ Array_Number │ 1000000 │ ' 495 ms' │ ' 32 ms' │ ' 21 ms' │ ' 1.52 x' │
|
|
1288
|
+
│ Array_String │ 1000000 │ ' 481 ms' │ ' 31 ms' │ ' 21 ms' │ ' 1.48 x' │
|
|
1289
|
+
│ Array_Boolean │ 1000000 │ ' 446 ms' │ ' 32 ms' │ ' 27 ms' │ ' 1.19 x' │
|
|
1290
|
+
│ Array_ObjectA │ 1000000 │ ' 14314 ms' │ ' 2341 ms' │ ' 1969 ms' │ ' 1.19 x' │
|
|
1291
|
+
│ Array_ObjectB │ 1000000 │ ' 16883 ms' │ ' 2661 ms' │ ' 2606 ms' │ ' 1.02 x' │
|
|
1292
|
+
│ Array_Tuple │ 1000000 │ ' 1834 ms' │ ' 98 ms' │ ' 77 ms' │ ' 1.27 x' │
|
|
1293
|
+
│ Array_Union │ 1000000 │ ' 4960 ms' │ ' 240 ms' │ ' 87 ms' │ ' 2.76 x' │
|
|
1294
|
+
│ Array_Recursive │ 1000000 │ ' 56273 ms' │ ' 7118 ms' │ ' 1122 ms' │ ' 6.34 x' │
|
|
1295
|
+
│ Array_Vector4 │ 1000000 │ ' 2498 ms' │ ' 99 ms' │ ' 48 ms' │ ' 2.06 x' │
|
|
1296
|
+
│ Array_Matrix4 │ 1000000 │ ' 12487 ms' │ ' 383 ms' │ ' 246 ms' │ ' 1.56 x' │
|
|
1297
1297
|
└──────────────────┴────────────┴──────────────┴──────────────┴──────────────┴──────────────┘
|
|
1298
1298
|
```
|
|
1299
1299
|
|
|
@@ -1307,13 +1307,13 @@ The following table lists esbuild compiled and minified sizes for each TypeBox m
|
|
|
1307
1307
|
┌──────────────────────┬────────────┬────────────┬─────────────┐
|
|
1308
1308
|
│ (index) │ Compiled │ Minified │ Compression │
|
|
1309
1309
|
├──────────────────────┼────────────┼────────────┼─────────────┤
|
|
1310
|
-
│ typebox/compiler │ '
|
|
1310
|
+
│ typebox/compiler │ ' 64 kb' │ ' 31 kb' │ '2.02 x' │
|
|
1311
1311
|
│ typebox/conditional │ ' 45 kb' │ ' 18 kb' │ '2.44 x' │
|
|
1312
1312
|
│ typebox/custom │ ' 0 kb' │ ' 0 kb' │ '2.61 x' │
|
|
1313
1313
|
│ typebox/format │ ' 0 kb' │ ' 0 kb' │ '2.66 x' │
|
|
1314
1314
|
│ typebox/guard │ ' 23 kb' │ ' 11 kb' │ '2.07 x' │
|
|
1315
|
-
│ typebox/hash │ ' 4 kb' │ ' 1 kb' │ '2.
|
|
1316
|
-
│ typebox/value │ '
|
|
1315
|
+
│ typebox/hash │ ' 4 kb' │ ' 1 kb' │ '2.30 x' │
|
|
1316
|
+
│ typebox/value │ ' 89 kb' │ ' 41 kb' │ '2.15 x' │
|
|
1317
1317
|
│ typebox │ ' 12 kb' │ ' 6 kb' │ '1.89 x' │
|
|
1318
1318
|
└──────────────────────┴────────────┴────────────┴─────────────┘
|
|
1319
1319
|
```
|
package/typebox.d.ts
CHANGED
|
@@ -172,17 +172,12 @@ export type OptionalPropertyKeys<T extends TProperties> = {
|
|
|
172
172
|
[K in keyof T]: T[K] extends TOptional<TSchema> ? K : never;
|
|
173
173
|
}[keyof T];
|
|
174
174
|
export type RequiredPropertyKeys<T extends TProperties> = keyof Omit<T, ReadonlyOptionalPropertyKeys<T> | ReadonlyPropertyKeys<T> | OptionalPropertyKeys<T>>;
|
|
175
|
-
export type
|
|
176
|
-
|
|
177
|
-
} & {
|
|
178
|
-
readonly [K in ReadonlyPropertyKeys<T>]: Static<T[K], P>;
|
|
179
|
-
} & {
|
|
180
|
-
[K in OptionalPropertyKeys<T>]?: Static<T[K], P>;
|
|
181
|
-
} & {
|
|
182
|
-
[K in RequiredPropertyKeys<T>]: Static<T[K], P>;
|
|
183
|
-
} extends infer R ? {
|
|
184
|
-
[K in keyof R]: R[K];
|
|
175
|
+
export type PropertiesReducer<T extends TProperties, R extends Record<keyof any, unknown>> = (Readonly<Partial<Pick<R, ReadonlyOptionalPropertyKeys<T>>>> & Readonly<Pick<R, ReadonlyPropertyKeys<T>>> & Partial<Pick<R, OptionalPropertyKeys<T>>> & Required<Pick<R, RequiredPropertyKeys<T>>>) extends infer O ? {
|
|
176
|
+
[K in keyof O]: O[K];
|
|
185
177
|
} : never;
|
|
178
|
+
export type PropertiesReduce<T extends TProperties, P extends unknown[]> = PropertiesReducer<T, {
|
|
179
|
+
[K in keyof T]: Static<T[K], P>;
|
|
180
|
+
}>;
|
|
186
181
|
export type TRecordProperties<K extends TUnion<TLiteral[]>, T extends TSchema> = Static<K> extends string ? {
|
|
187
182
|
[X in Static<K>]: T;
|
|
188
183
|
} : never;
|