@sinclair/typebox 0.24.21 → 0.24.22
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.js +5 -3
- package/compiler/property.d.ts +4 -0
- package/compiler/property.js +64 -0
- package/package.json +1 -1
- package/readme.md +51 -51
package/compiler/compiler.js
CHANGED
|
@@ -29,6 +29,7 @@ THE SOFTWARE.
|
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
30
|
exports.TypeCompiler = exports.TypeCheck = void 0;
|
|
31
31
|
const errors_1 = require("../value/errors");
|
|
32
|
+
const property_1 = require("./property");
|
|
32
33
|
const Types = require("../typebox");
|
|
33
34
|
// -------------------------------------------------------------------
|
|
34
35
|
// TypeCheck
|
|
@@ -143,13 +144,14 @@ var TypeCompiler;
|
|
|
143
144
|
}
|
|
144
145
|
}
|
|
145
146
|
for (const propertyKey of propertyKeys) {
|
|
147
|
+
const memberExpression = property_1.Property.Check(propertyKey) ? `${value}.${propertyKey}` : `${value}['${propertyKey}']`;
|
|
146
148
|
const propertySchema = schema.properties[propertyKey];
|
|
147
149
|
if (schema.required && schema.required.includes(propertyKey)) {
|
|
148
|
-
yield* Visit(propertySchema,
|
|
150
|
+
yield* Visit(propertySchema, memberExpression);
|
|
149
151
|
}
|
|
150
152
|
else {
|
|
151
|
-
const expression = CreateExpression(propertySchema,
|
|
152
|
-
yield `(${
|
|
153
|
+
const expression = CreateExpression(propertySchema, memberExpression);
|
|
154
|
+
yield `(${memberExpression} === undefined ? true : (${expression}))`;
|
|
153
155
|
}
|
|
154
156
|
}
|
|
155
157
|
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*--------------------------------------------------------------------------
|
|
3
|
+
|
|
4
|
+
@sinclair/typebox/compiler
|
|
5
|
+
|
|
6
|
+
The MIT License (MIT)
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in
|
|
18
|
+
all copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
26
|
+
THE SOFTWARE.
|
|
27
|
+
|
|
28
|
+
---------------------------------------------------------------------------*/
|
|
29
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
+
exports.Property = void 0;
|
|
31
|
+
var Property;
|
|
32
|
+
(function (Property) {
|
|
33
|
+
function DollarSign(char) {
|
|
34
|
+
return char === 36;
|
|
35
|
+
}
|
|
36
|
+
function Underscore(char) {
|
|
37
|
+
return char === 95;
|
|
38
|
+
}
|
|
39
|
+
function Numeric(char) {
|
|
40
|
+
return char >= 48 && char <= 57;
|
|
41
|
+
}
|
|
42
|
+
function Alpha(char) {
|
|
43
|
+
return (char >= 65 && char <= 90) || (char >= 97 && char <= 122);
|
|
44
|
+
}
|
|
45
|
+
/** Tests if this property name can be used in a member expression */
|
|
46
|
+
function Check(propertyName) {
|
|
47
|
+
if (propertyName.length === 0)
|
|
48
|
+
return false;
|
|
49
|
+
{
|
|
50
|
+
const code = propertyName.charCodeAt(0);
|
|
51
|
+
if (!(DollarSign(code) || Underscore(code) || Alpha(code))) {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
for (let i = 1; i < propertyName.length; i++) {
|
|
56
|
+
const code = propertyName.charCodeAt(i);
|
|
57
|
+
if (!(DollarSign(code) || Underscore(code) || Alpha(code) || Numeric(code))) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
Property.Check = Check;
|
|
64
|
+
})(Property = exports.Property || (exports.Property = {}));
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -528,7 +528,7 @@ function test(node: Node) {
|
|
|
528
528
|
|
|
529
529
|
## Generic Types
|
|
530
530
|
|
|
531
|
-
|
|
531
|
+
Use functions to create generic types. The following creates a generic `Nullable<T>` type.
|
|
532
532
|
|
|
533
533
|
```typescript
|
|
534
534
|
import { Type, Static, TSchema } from '@sinclair/typebox'
|
|
@@ -594,12 +594,12 @@ type T = Static<typeof T> // type T = string | null
|
|
|
594
594
|
|
|
595
595
|
//--------------------------------------------------------------------------------------------
|
|
596
596
|
//
|
|
597
|
-
//
|
|
597
|
+
// StringEnum<string[]>
|
|
598
598
|
//
|
|
599
599
|
//--------------------------------------------------------------------------------------------
|
|
600
600
|
|
|
601
601
|
function StringEnum<T extends string[]>(values: [...T]) {
|
|
602
|
-
return Type.Unsafe<T[number]>({ enum: values })
|
|
602
|
+
return Type.Unsafe<T[number]>({ type: 'string', enum: values })
|
|
603
603
|
}
|
|
604
604
|
|
|
605
605
|
const T = StringEnum(['A', 'B', 'C']) // const T = {
|
|
@@ -886,31 +886,31 @@ This benchmark measures validation performance for varying types. You can review
|
|
|
886
886
|
┌──────────────────┬────────────┬──────────────┬──────────────┬──────────────┐
|
|
887
887
|
│ (index) │ Iterations │ Ajv │ TypeCompiler │ Performance │
|
|
888
888
|
├──────────────────┼────────────┼──────────────┼──────────────┼──────────────┤
|
|
889
|
-
│ Number │
|
|
890
|
-
│ String │
|
|
891
|
-
│ Boolean │
|
|
892
|
-
│ Null │
|
|
893
|
-
│ RegEx │
|
|
894
|
-
│ ObjectA │
|
|
895
|
-
│ ObjectB │
|
|
896
|
-
│ Tuple │
|
|
897
|
-
│ Union │
|
|
898
|
-
│ Recursive │
|
|
899
|
-
│ Vector4 │
|
|
900
|
-
│ Matrix4 │
|
|
901
|
-
│ Literal_String │
|
|
902
|
-
│ Literal_Number │
|
|
903
|
-
│ Literal_Boolean │
|
|
904
|
-
│ Array_Number │
|
|
905
|
-
│ Array_String │
|
|
906
|
-
│ Array_Boolean │
|
|
907
|
-
│ Array_ObjectA │
|
|
908
|
-
│ Array_ObjectB │
|
|
909
|
-
│ Array_Tuple │
|
|
910
|
-
│ Array_Union │
|
|
911
|
-
│ Array_Recursive │
|
|
912
|
-
│ Array_Vector4 │
|
|
913
|
-
│ Array_Matrix4 │
|
|
889
|
+
│ Number │ 4000000 │ ' 17 ms' │ ' 16 ms' │ ' 1.06 x' │
|
|
890
|
+
│ String │ 4000000 │ ' 74 ms' │ ' 37 ms' │ ' 2.00 x' │
|
|
891
|
+
│ Boolean │ 4000000 │ ' 68 ms' │ ' 36 ms' │ ' 1.89 x' │
|
|
892
|
+
│ Null │ 4000000 │ ' 68 ms' │ ' 36 ms' │ ' 1.89 x' │
|
|
893
|
+
│ RegEx │ 4000000 │ ' 170 ms' │ ' 143 ms' │ ' 1.19 x' │
|
|
894
|
+
│ ObjectA │ 4000000 │ ' 122 ms' │ ' 84 ms' │ ' 1.45 x' │
|
|
895
|
+
│ ObjectB │ 4000000 │ ' 182 ms' │ ' 137 ms' │ ' 1.33 x' │
|
|
896
|
+
│ Tuple │ 4000000 │ ' 82 ms' │ ' 48 ms' │ ' 1.71 x' │
|
|
897
|
+
│ Union │ 4000000 │ ' 84 ms' │ ' 52 ms' │ ' 1.62 x' │
|
|
898
|
+
│ Recursive │ 4000000 │ ' 1526 ms' │ ' 631 ms' │ ' 2.42 x' │
|
|
899
|
+
│ Vector4 │ 4000000 │ ' 80 ms' │ ' 42 ms' │ ' 1.90 x' │
|
|
900
|
+
│ Matrix4 │ 4000000 │ ' 157 ms' │ ' 113 ms' │ ' 1.39 x' │
|
|
901
|
+
│ Literal_String │ 4000000 │ ' 69 ms' │ ' 36 ms' │ ' 1.92 x' │
|
|
902
|
+
│ Literal_Number │ 4000000 │ ' 69 ms' │ ' 35 ms' │ ' 1.97 x' │
|
|
903
|
+
│ Literal_Boolean │ 4000000 │ ' 68 ms' │ ' 35 ms' │ ' 1.94 x' │
|
|
904
|
+
│ Array_Number │ 4000000 │ ' 119 ms' │ ' 60 ms' │ ' 1.98 x' │
|
|
905
|
+
│ Array_String │ 4000000 │ ' 119 ms' │ ' 76 ms' │ ' 1.57 x' │
|
|
906
|
+
│ Array_Boolean │ 4000000 │ ' 131 ms' │ ' 88 ms' │ ' 1.49 x' │
|
|
907
|
+
│ Array_ObjectA │ 4000000 │ ' 10615 ms' │ ' 7053 ms' │ ' 1.51 x' │
|
|
908
|
+
│ Array_ObjectB │ 4000000 │ ' 11305 ms' │ ' 8128 ms' │ ' 1.39 x' │
|
|
909
|
+
│ Array_Tuple │ 4000000 │ ' 355 ms' │ ' 271 ms' │ ' 1.31 x' │
|
|
910
|
+
│ Array_Union │ 4000000 │ ' 910 ms' │ ' 340 ms' │ ' 2.68 x' │
|
|
911
|
+
│ Array_Recursive │ 4000000 │ ' 29101 ms' │ ' 10837 ms' │ ' 2.69 x' │
|
|
912
|
+
│ Array_Vector4 │ 4000000 │ ' 381 ms' │ ' 212 ms' │ ' 1.80 x' │
|
|
913
|
+
│ Array_Matrix4 │ 4000000 │ ' 1534 ms' │ ' 1344 ms' │ ' 1.14 x' │
|
|
914
914
|
└──────────────────┴────────────┴──────────────┴──────────────┴──────────────┘
|
|
915
915
|
```
|
|
916
916
|
|
|
@@ -922,29 +922,29 @@ This benchmark measures compilation performance for varying types. You can revie
|
|
|
922
922
|
┌──────────────────┬────────────┬──────────────┬──────────────┬──────────────┐
|
|
923
923
|
│ (index) │ Iterations │ Ajv │ TypeCompiler │ Performance │
|
|
924
924
|
├──────────────────┼────────────┼──────────────┼──────────────┼──────────────┤
|
|
925
|
-
│ Number │ 2000 │ '
|
|
926
|
-
│ String │ 2000 │ '
|
|
927
|
-
│ Boolean │ 2000 │ '
|
|
928
|
-
│ Null │ 2000 │ '
|
|
929
|
-
│ RegEx │ 2000 │ '
|
|
930
|
-
│ ObjectA │ 2000 │ '
|
|
931
|
-
│ ObjectB │ 2000 │ '
|
|
932
|
-
│ Tuple │ 2000 │ '
|
|
933
|
-
│ Union │ 2000 │ '
|
|
934
|
-
│ Vector4 │ 2000 │ '
|
|
935
|
-
│ Matrix4 │ 2000 │ '
|
|
936
|
-
│ Literal_String │ 2000 │ '
|
|
937
|
-
│ Literal_Number │ 2000 │ '
|
|
938
|
-
│ Literal_Boolean │ 2000 │ '
|
|
939
|
-
│ Array_Number │ 2000 │ '
|
|
940
|
-
│ Array_String │ 2000 │ '
|
|
941
|
-
│ Array_Boolean │ 2000 │ '
|
|
942
|
-
│ Array_ObjectA │ 2000 │ '
|
|
943
|
-
│ Array_ObjectB │ 2000 │ '
|
|
944
|
-
│ Array_Tuple │ 2000 │ '
|
|
945
|
-
│ Array_Union │ 2000 │ '
|
|
946
|
-
│ Array_Vector4 │ 2000 │ '
|
|
947
|
-
│ Array_Matrix4 │ 2000 │ '
|
|
925
|
+
│ Number │ 2000 │ ' 384 ms' │ ' 7 ms' │ ' 54.86 x' │
|
|
926
|
+
│ String │ 2000 │ ' 311 ms' │ ' 6 ms' │ ' 51.83 x' │
|
|
927
|
+
│ Boolean │ 2000 │ ' 304 ms' │ ' 5 ms' │ ' 60.80 x' │
|
|
928
|
+
│ Null │ 2000 │ ' 252 ms' │ ' 5 ms' │ ' 50.40 x' │
|
|
929
|
+
│ RegEx │ 2000 │ ' 480 ms' │ ' 7 ms' │ ' 68.57 x' │
|
|
930
|
+
│ ObjectA │ 2000 │ ' 2786 ms' │ ' 27 ms' │ ' 103.19 x' │
|
|
931
|
+
│ ObjectB │ 2000 │ ' 2946 ms' │ ' 22 ms' │ ' 133.91 x' │
|
|
932
|
+
│ Tuple │ 2000 │ ' 1277 ms' │ ' 16 ms' │ ' 79.81 x' │
|
|
933
|
+
│ Union │ 2000 │ ' 1288 ms' │ ' 16 ms' │ ' 80.50 x' │
|
|
934
|
+
│ Vector4 │ 2000 │ ' 1628 ms' │ ' 12 ms' │ ' 135.67 x' │
|
|
935
|
+
│ Matrix4 │ 2000 │ ' 912 ms' │ ' 7 ms' │ ' 130.29 x' │
|
|
936
|
+
│ Literal_String │ 2000 │ ' 344 ms' │ ' 5 ms' │ ' 68.80 x' │
|
|
937
|
+
│ Literal_Number │ 2000 │ ' 432 ms' │ ' 5 ms' │ ' 86.40 x' │
|
|
938
|
+
│ Literal_Boolean │ 2000 │ ' 407 ms' │ ' 4 ms' │ ' 101.75 x' │
|
|
939
|
+
│ Array_Number │ 2000 │ ' 788 ms' │ ' 9 ms' │ ' 87.56 x' │
|
|
940
|
+
│ Array_String │ 2000 │ ' 823 ms' │ ' 7 ms' │ ' 117.57 x' │
|
|
941
|
+
│ Array_Boolean │ 2000 │ ' 816 ms' │ ' 5 ms' │ ' 163.20 x' │
|
|
942
|
+
│ Array_ObjectA │ 2000 │ ' 4270 ms' │ ' 24 ms' │ ' 177.92 x' │
|
|
943
|
+
│ Array_ObjectB │ 2000 │ ' 4008 ms' │ ' 28 ms' │ ' 143.14 x' │
|
|
944
|
+
│ Array_Tuple │ 2000 │ ' 2243 ms' │ ' 13 ms' │ ' 172.54 x' │
|
|
945
|
+
│ Array_Union │ 2000 │ ' 1734 ms' │ ' 16 ms' │ ' 108.38 x' │
|
|
946
|
+
│ Array_Vector4 │ 2000 │ ' 2348 ms' │ ' 14 ms' │ ' 167.71 x' │
|
|
947
|
+
│ Array_Matrix4 │ 2000 │ ' 1608 ms' │ ' 11 ms' │ ' 146.18 x' │
|
|
948
948
|
└──────────────────┴────────────┴──────────────┴──────────────┴──────────────┘
|
|
949
949
|
```
|
|
950
950
|
|