@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.
@@ -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, `${value}.${propertyKey}`);
150
+ yield* Visit(propertySchema, memberExpression);
149
151
  }
150
152
  else {
151
- const expression = CreateExpression(propertySchema, `${value}.${propertyKey}`);
152
- yield `(${value}.${propertyKey} === undefined ? true : (${expression}))`;
153
+ const expression = CreateExpression(propertySchema, memberExpression);
154
+ yield `(${memberExpression} === undefined ? true : (${expression}))`;
153
155
  }
154
156
  }
155
157
  }
@@ -0,0 +1,4 @@
1
+ export declare namespace Property {
2
+ /** Tests if this property name can be used in a member expression */
3
+ function Check(propertyName: string): boolean;
4
+ }
@@ -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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.24.21",
3
+ "version": "0.24.22",
4
4
  "description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",
package/readme.md CHANGED
@@ -528,7 +528,7 @@ function test(node: Node) {
528
528
 
529
529
  ## Generic Types
530
530
 
531
- Generic types can be created using functions. The following creates a generic `Nullable<T>` type.
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
- // StringUnion<[...]>
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 │ 16000000 │ ' 73 ms' │ ' 65 ms' │ ' 1.12 x' │
890
- │ String │ 16000000 │ ' 281 ms' │ ' 161 ms' │ ' 1.75 x' │
891
- │ Boolean │ 16000000 │ ' 302 ms' │ ' 151 ms' │ ' 2.00 x' │
892
- │ Null │ 16000000 │ ' 281 ms' │ ' 149 ms' │ ' 1.89 x' │
893
- │ RegEx │ 16000000 │ ' 689 ms' │ ' 550 ms' │ ' 1.25 x' │
894
- │ ObjectA │ 16000000 │ ' 470 ms' │ ' 311 ms' │ ' 1.51 x' │
895
- │ ObjectB │ 16000000 │ ' 667 ms' │ ' 556 ms' │ ' 1.20 x' │
896
- │ Tuple │ 16000000 │ ' 362 ms' │ ' 190 ms' │ ' 1.91 x' │
897
- │ Union │ 16000000 │ ' 387 ms' │ ' 209 ms' │ ' 1.85 x' │
898
- │ Recursive │ 16000000 │ ' 6327 ms' │ ' 2773 ms' │ ' 2.28 x' │
899
- │ Vector4 │ 16000000 │ ' 362 ms' │ ' 172 ms' │ ' 2.10 x' │
900
- │ Matrix4 │ 16000000 │ ' 607 ms' │ ' 418 ms' │ ' 1.45 x' │
901
- │ Literal_String │ 16000000 │ ' 314 ms' │ ' 151 ms' │ ' 2.08 x' │
902
- │ Literal_Number │ 16000000 │ ' 281 ms' │ ' 147 ms' │ ' 1.91 x' │
903
- │ Literal_Boolean │ 16000000 │ ' 289 ms' │ ' 148 ms' │ ' 1.95 x' │
904
- │ Array_Number │ 16000000 │ ' 475 ms' │ ' 230 ms' │ ' 2.07 x' │
905
- │ Array_String │ 16000000 │ ' 463 ms' │ ' 297 ms' │ ' 1.56 x' │
906
- │ Array_Boolean │ 16000000 │ ' 521 ms' │ ' 348 ms' │ ' 1.50 x' │
907
- │ Array_ObjectA │ 16000000 │ ' 43842 ms' │ ' 28375 ms' │ ' 1.55 x' │
908
- │ Array_ObjectB │ 16000000 │ ' 45055 ms' │ ' 32882 ms' │ ' 1.37 x' │
909
- │ Array_Tuple │ 16000000 │ ' 1424 ms' │ ' 1105 ms' │ ' 1.29 x' │
910
- │ Array_Union │ 16000000 │ ' 3505 ms' │ ' 1350 ms' │ ' 2.60 x' │
911
- │ Array_Recursive │ 16000000 │ ' 117087 ms' │ ' 42982 ms' │ ' 2.72 x' │
912
- │ Array_Vector4 │ 16000000 │ ' 1500 ms' │ ' 817 ms' │ ' 1.84 x' │
913
- │ Array_Matrix4 │ 16000000 │ ' 6126 ms' │ ' 4965 ms' │ ' 1.23 x' │
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 │ ' 387 ms' │ ' 7 ms' │ ' 55.29 x' │
926
- │ String │ 2000 │ ' 340 ms' │ ' 6 ms' │ ' 56.67 x' │
927
- │ Boolean │ 2000 │ ' 314 ms' │ ' 5 ms' │ ' 62.80 x' │
928
- │ Null │ 2000 │ ' 259 ms' │ ' 5 ms' │ ' 51.80 x' │
929
- │ RegEx │ 2000 │ ' 491 ms' │ ' 7 ms' │ ' 70.14 x' │
930
- │ ObjectA │ 2000 │ ' 2850 ms' │ ' 26 ms' │ ' 109.62 x' │
931
- │ ObjectB │ 2000 │ ' 2989 ms' │ ' 22 ms' │ ' 135.86 x' │
932
- │ Tuple │ 2000 │ ' 1283 ms' │ ' 17 ms' │ ' 75.47 x' │
933
- │ Union │ 2000 │ ' 1292 ms' │ ' 16 ms' │ ' 80.75 x' │
934
- │ Vector4 │ 2000 │ ' 1588 ms' │ ' 12 ms' │ ' 132.33 x' │
935
- │ Matrix4 │ 2000 │ ' 934 ms' │ ' 7 ms' │ ' 133.43 x' │
936
- │ Literal_String │ 2000 │ ' 340 ms' │ ' 5 ms' │ ' 68.00 x' │
937
- │ Literal_Number │ 2000 │ ' 402 ms' │ ' 4 ms' │ ' 100.50 x' │
938
- │ Literal_Boolean │ 2000 │ ' 381 ms' │ ' 6 ms' │ ' 63.50 x' │
939
- │ Array_Number │ 2000 │ ' 750 ms' │ ' 8 ms' │ ' 93.75 x' │
940
- │ Array_String │ 2000 │ ' 760 ms' │ ' 6 ms' │ ' 126.67 x' │
941
- │ Array_Boolean │ 2000 │ ' 796 ms' │ ' 7 ms' │ ' 113.71 x' │
942
- │ Array_ObjectA │ 2000 │ ' 3617 ms' │ ' 26 ms' │ ' 139.12 x' │
943
- │ Array_ObjectB │ 2000 │ ' 3823 ms' │ ' 26 ms' │ ' 147.04 x' │
944
- │ Array_Tuple │ 2000 │ ' 2263 ms' │ ' 13 ms' │ ' 174.08 x' │
945
- │ Array_Union │ 2000 │ ' 1725 ms' │ ' 16 ms' │ ' 107.81 x' │
946
- │ Array_Vector4 │ 2000 │ ' 2445 ms' │ ' 14 ms' │ ' 174.64 x' │
947
- │ Array_Matrix4 │ 2000 │ ' 1638 ms' │ ' 11 ms' │ ' 148.91 x' │
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