@sinclair/typebox 0.24.23 → 0.24.24

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/guard/guard.js CHANGED
@@ -32,11 +32,41 @@ const Types = require("../typebox");
32
32
  /** TypeGuard tests that values conform to a known TypeBox type specification */
33
33
  var TypeGuard;
34
34
  (function (TypeGuard) {
35
- function IsObject(schema) {
36
- return typeof schema === 'object' && schema !== null && !Array.isArray(schema);
35
+ function IsObject(value) {
36
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
37
37
  }
38
- function IsArray(schema) {
39
- return typeof schema === 'object' && schema !== null && Array.isArray(schema);
38
+ function IsArray(value) {
39
+ return typeof value === 'object' && value !== null && Array.isArray(value);
40
+ }
41
+ function IsPattern(value) {
42
+ try {
43
+ new RegExp(value);
44
+ return true;
45
+ }
46
+ catch {
47
+ return false;
48
+ }
49
+ }
50
+ function IsString(value) {
51
+ return typeof value === 'string';
52
+ }
53
+ function IsNumber(value) {
54
+ return typeof value === 'number';
55
+ }
56
+ function IsBoolean(value) {
57
+ return typeof value === 'boolean';
58
+ }
59
+ function IsOptionalNumber(value) {
60
+ return value === undefined || (value !== undefined && IsNumber(value));
61
+ }
62
+ function IsOptionalBoolean(value) {
63
+ return value === undefined || (value !== undefined && IsBoolean(value));
64
+ }
65
+ function IsOptionalString(value) {
66
+ return value === undefined || (value !== undefined && IsString(value));
67
+ }
68
+ function IsOptionalPattern(value) {
69
+ return value === undefined || (value !== undefined && IsPattern(value));
40
70
  }
41
71
  /** Returns true if the given schema is TAny */
42
72
  function TAny(schema) {
@@ -45,7 +75,12 @@ var TypeGuard;
45
75
  TypeGuard.TAny = TAny;
46
76
  /** Returns true if the given schema is TArray */
47
77
  function TArray(schema) {
48
- return IsObject(schema) && schema[Types.Kind] === 'Array' && schema.type === 'array' && TSchema(schema.items);
78
+ return (IsObject(schema) &&
79
+ schema[Types.Kind] === 'Array' &&
80
+ schema.type === 'array' &&
81
+ TSchema(schema.items) &&
82
+ IsOptionalNumber(schema.minItems) &&
83
+ IsOptionalNumber(schema.maxItems));
49
84
  }
50
85
  TypeGuard.TArray = TArray;
51
86
  /** Returns true if the given schema is TBoolean */
@@ -79,12 +114,19 @@ var TypeGuard;
79
114
  TypeGuard.TFunction = TFunction;
80
115
  /** Returns true if the given schema is TInteger */
81
116
  function TInteger(schema) {
82
- return IsObject(schema) && schema[Types.Kind] === 'Integer' && schema.type === 'integer';
117
+ return (IsObject(schema) &&
118
+ schema[Types.Kind] === 'Integer' &&
119
+ schema.type === 'integer' &&
120
+ IsOptionalNumber(schema.multipleOf) &&
121
+ IsOptionalNumber(schema.minimum) &&
122
+ IsOptionalNumber(schema.maximum) &&
123
+ IsOptionalNumber(schema.exclusiveMinimum) &&
124
+ IsOptionalNumber(schema.exclusiveMaximum));
83
125
  }
84
126
  TypeGuard.TInteger = TInteger;
85
127
  /** Returns true if the given schema is TLiteral */
86
128
  function TLiteral(schema) {
87
- return IsObject(schema) && schema[Types.Kind] === 'Literal' && (typeof schema.const === 'string' || typeof schema.const === 'number' || typeof schema.const === 'boolean');
129
+ return IsObject(schema) && schema[Types.Kind] === 'Literal' && (IsString(schema.const) || IsNumber(schema.const) || IsBoolean(schema.const));
88
130
  }
89
131
  TypeGuard.TLiteral = TLiteral;
90
132
  /** Returns true if the given schema is TNull */
@@ -94,12 +136,19 @@ var TypeGuard;
94
136
  TypeGuard.TNull = TNull;
95
137
  /** Returns true if the given schema is TNumber */
96
138
  function TNumber(schema) {
97
- return IsObject(schema) && schema[Types.Kind] === 'Number' && schema.type === 'number';
139
+ return (IsObject(schema) &&
140
+ schema[Types.Kind] === 'Number' &&
141
+ schema.type === 'number' &&
142
+ IsOptionalNumber(schema.multipleOf) &&
143
+ IsOptionalNumber(schema.minimum) &&
144
+ IsOptionalNumber(schema.maximum) &&
145
+ IsOptionalNumber(schema.exclusiveMinimum) &&
146
+ IsOptionalNumber(schema.exclusiveMaximum));
98
147
  }
99
148
  TypeGuard.TNumber = TNumber;
100
149
  /** Returns true if the given schema is TObject */
101
150
  function TObject(schema) {
102
- if (!(IsObject(schema) && schema[Types.Kind] === 'Object' && schema.type === 'object' && IsObject(schema.properties))) {
151
+ if (!(IsObject(schema) && schema[Types.Kind] === 'Object' && schema.type === 'object' && IsObject(schema.properties) && IsOptionalBoolean(schema.additionalProperties))) {
103
152
  return false;
104
153
  }
105
154
  for (const property of Object.values(schema.properties)) {
@@ -123,6 +172,9 @@ var TypeGuard;
123
172
  if (keys.length !== 1) {
124
173
  return false;
125
174
  }
175
+ if (!IsPattern(keys[0])) {
176
+ return false;
177
+ }
126
178
  if (!TSchema(schema.patternProperties[keys[0]])) {
127
179
  return false;
128
180
  }
@@ -131,22 +183,33 @@ var TypeGuard;
131
183
  TypeGuard.TRecord = TRecord;
132
184
  /** Returns true if the given schema is TSelf */
133
185
  function TSelf(schema) {
134
- return IsObject(schema) && schema[Types.Kind] === 'Self' && typeof schema.$ref === 'string';
186
+ return IsObject(schema) && schema[Types.Kind] === 'Self' && IsString(schema.$ref);
135
187
  }
136
188
  TypeGuard.TSelf = TSelf;
137
189
  /** Returns true if the given schema is TRef */
138
190
  function TRef(schema) {
139
- return IsObject(schema) && schema[Types.Kind] === 'Ref' && typeof schema.$ref === 'string';
191
+ return IsObject(schema) && schema[Types.Kind] === 'Ref' && IsString(schema.$ref);
140
192
  }
141
193
  TypeGuard.TRef = TRef;
142
194
  /** Returns true if the given schema is TString */
143
195
  function TString(schema) {
144
- return IsObject(schema) && schema[Types.Kind] === 'String' && schema.type === 'string';
196
+ return (IsObject(schema) &&
197
+ schema[Types.Kind] === 'String' &&
198
+ schema.type === 'string' &&
199
+ IsOptionalNumber(schema.minLength) &&
200
+ IsOptionalNumber(schema.maxLength) &&
201
+ IsOptionalPattern(schema.pattern) &&
202
+ IsOptionalString(schema.format));
145
203
  }
146
204
  TypeGuard.TString = TString;
147
205
  /** Returns true if the given schema is TTuple */
148
206
  function TTuple(schema) {
149
- if (!(IsObject(schema) && schema[Types.Kind] === 'Tuple' && schema.type === 'array' && typeof schema.minItems === 'number' && typeof schema.maxItems === 'number' && schema.minItems === schema.maxItems)) {
207
+ if (!(IsObject(schema) &&
208
+ schema[Types.Kind] === 'Tuple' &&
209
+ schema.type === 'array' &&
210
+ IsNumber(schema.minItems) &&
211
+ IsNumber(schema.maxItems) &&
212
+ schema.minItems === schema.maxItems)) {
150
213
  return false;
151
214
  }
152
215
  if (schema.items === undefined && schema.additionalItems === undefined && schema.minItems === 0) {
@@ -181,7 +244,12 @@ var TypeGuard;
181
244
  TypeGuard.TUnion = TUnion;
182
245
  /** Returns true if the given schema is TUint8Array */
183
246
  function TUint8Array(schema) {
184
- return IsObject(schema) && schema[Types.Kind] === 'Uint8Array' && schema.type === 'object' && schema.specialized === 'Uint8Array';
247
+ return (IsObject(schema) &&
248
+ schema[Types.Kind] === 'Uint8Array' &&
249
+ schema.type === 'object' &&
250
+ schema.specialized === 'Uint8Array' &&
251
+ IsOptionalNumber(schema.minByteLength) &&
252
+ IsOptionalNumber(schema.maxByteLength));
185
253
  }
186
254
  TypeGuard.TUint8Array = TUint8Array;
187
255
  /** Returns true if the given schema is TUnknown */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.24.23",
3
+ "version": "0.24.24",
4
4
  "description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",
package/readme.md CHANGED
@@ -818,19 +818,19 @@ const C = TypeCompiler.Compile(Type.Object({ // const C: TypeCheck<TObje
818
818
  const value = { }
819
819
 
820
820
  const errors = [...C.Errors(value)] // const errors = [{
821
- // type: ValueErrorType.Number,
821
+ // type: 14,
822
822
  // schema: { type: 'number' },
823
823
  // path: '/x',
824
824
  // value: undefined,
825
825
  // message: 'Expected number'
826
826
  // }, {
827
- // type: ValueErrorType.Number,
827
+ // type: 14,
828
828
  // schema: { type: 'number' },
829
829
  // path: '/y',
830
830
  // value: undefined,
831
831
  // message: 'Expected number'
832
832
  // }, {
833
- // type: ValueErrorType.Number,
833
+ // type: 14,
834
834
  // schema: { type: 'number' },
835
835
  // path: '/z',
836
836
  // value: undefined,
@@ -858,76 +858,78 @@ This project maintains a set of benchmarks that measure Ajv and TypeCompiler com
858
858
 
859
859
  For additional comparative benchmarks, please refer to [typescript-runtime-type-benchmarks](https://moltar.github.io/typescript-runtime-type-benchmarks/).
860
860
 
861
- ### Validate
861
+ ### Compile
862
862
 
863
- This benchmark measures validation performance for varying types. You can review this benchmark [here](https://github.com/sinclairzx81/typebox/blob/master/benchmark/check.ts).
863
+ This benchmark measures compilation performance for varying types. You can review this benchmark [here](https://github.com/sinclairzx81/typebox/blob/master/benchmark/compile.ts).
864
864
 
865
865
  ```typescript
866
866
  ┌──────────────────┬────────────┬──────────────┬──────────────┬──────────────┐
867
867
  │ (index) │ Iterations │ Ajv │ TypeCompiler │ Performance │
868
868
  ├──────────────────┼────────────┼──────────────┼──────────────┼──────────────┤
869
- │ Number │ 4000000 │ ' 17 ms' │ ' 16 ms' │ ' 1.06 x' │
870
- │ String │ 4000000 │ ' 74 ms' │ ' 37 ms' │ ' 2.00 x' │
871
- │ Boolean │ 4000000 │ ' 68 ms' │ ' 36 ms' │ ' 1.89 x' │
872
- │ Null │ 4000000 │ ' 68 ms' │ ' 36 ms' │ ' 1.89 x' │
873
- │ RegEx │ 4000000 │ ' 170 ms' │ ' 143 ms' │ ' 1.19 x' │
874
- │ ObjectA │ 4000000 │ ' 122 ms' │ ' 84 ms' │ ' 1.45 x' │
875
- │ ObjectB │ 4000000 │ ' 182 ms' │ ' 137 ms' │ ' 1.33 x' │
876
- │ Tuple │ 4000000 │ ' 82 ms' │ ' 48 ms' │ ' 1.71 x' │
877
- │ Union │ 4000000 │ ' 84 ms' │ ' 52 ms' │ ' 1.62 x' │
878
- Recursive 4000000 │ ' 1526 ms' │ ' 631 ms' │ ' 2.42 x' │
879
- Vector4 4000000 │ ' 80 ms' │ ' 42 ms' │ ' 1.90 x' │
880
- Matrix4 4000000 │ ' 157 ms' │ ' 113 ms' │ ' 1.39 x' │
881
- Literal_String 4000000 │ ' 69 ms' │ ' 36 ms' │ ' 1.92 x' │
882
- Literal_Number 4000000 │ ' 69 ms' │ ' 35 ms' │ ' 1.97 x' │
883
- Literal_Boolean 4000000 │ ' 68 ms' │ ' 35 ms' │ ' 1.94 x' │
884
- Array_Number 4000000 │ ' 119 ms' │ ' 60 ms' │ ' 1.98 x' │
885
- Array_String 4000000 │ ' 119 ms' │ ' 76 ms' │ ' 1.57 x' │
886
- Array_Boolean 4000000 │ ' 131 ms' │ ' 88 ms' │ ' 1.49 x' │
887
- Array_ObjectA 4000000 │ ' 10615 ms' │ ' 7053 ms' │ ' 1.51 x' │
888
- Array_ObjectB 4000000 │ ' 11305 ms' │ ' 8128 ms' │ ' 1.39 x' │
889
- Array_Tuple 4000000 │ ' 355 ms' │ ' 271 ms' │ ' 1.31 x' │
890
- Array_Union 4000000 │ ' 910 ms' │ ' 340 ms' │ ' 2.68 x' │
891
- Array_Recursive 4000000 │ ' 29101 ms' │ ' 10837 ms' │ ' 2.69 x' │
892
- │ Array_Vector4 │ 4000000 │ ' 381 ms' │ ' 212 ms' │ ' 1.80 x' │
893
- │ Array_Matrix4 │ 4000000 │ ' 1534 ms' │ ' 1344 ms' │ ' 1.14 x' │
869
+ │ Number │ 2000 │ ' 382 ms' │ ' 8 ms' │ ' 47.75 x' │
870
+ │ String │ 2000 │ ' 305 ms' │ ' 7 ms' │ ' 43.57 x' │
871
+ │ Boolean │ 2000 │ ' 305 ms' │ ' 5 ms' │ ' 61.00 x' │
872
+ │ Null │ 2000 │ ' 253 ms' │ ' 5 ms' │ ' 50.60 x' │
873
+ │ RegEx │ 2000 │ ' 475 ms' │ ' 10 ms' │ ' 47.50 x' │
874
+ │ ObjectA │ 2000 │ ' 2812 ms' │ ' 44 ms' │ ' 63.91 x' │
875
+ │ ObjectB │ 2000 │ ' 3045 ms' │ ' 34 ms' │ ' 89.56 x' │
876
+ │ Tuple │ 2000 │ ' 1324 ms' │ ' 19 ms' │ ' 69.68 x' │
877
+ │ Union │ 2000 │ ' 1347 ms' │ ' 26 ms' │ ' 51.81 x' │
878
+ Vector4 2000 │ ' 1635 ms' │ ' 18 ms' │ ' 90.83 x' │
879
+ Matrix4 2000 │ ' 928 ms' │ ' 10 ms' │ ' 92.80 x' │
880
+ Literal_String 2000 │ ' 349 ms' │ ' 5 ms' │ ' 69.80 x' │
881
+ Literal_Number 2000 │ ' 379 ms' │ ' 5 ms' │ ' 75.80 x' │
882
+ Literal_Boolean 2000 │ ' 376 ms' │ ' 6 ms' │ ' 62.67 x' │
883
+ Array_Number 2000 │ ' 733 ms' │ ' 10 ms' │ ' 73.30 x' │
884
+ Array_String 2000 │ ' 766 ms' │ ' 8 ms' │ ' 95.75 x' │
885
+ Array_Boolean 2000 │ ' 860 ms' │ ' 11 ms' │ ' 78.18 x' │
886
+ Array_ObjectA 2000 │ ' 3668 ms' │ ' 42 ms' │ ' 87.33 x' │
887
+ Array_ObjectB 2000 │ ' 3809 ms' │ ' 41 ms' │ ' 92.90 x' │
888
+ Array_Tuple 2000 │ ' 2219 ms' │ ' 17 ms' │ ' 130.53 x' │
889
+ Array_Union 2000 │ ' 1709 ms' │ ' 26 ms' │ ' 65.73 x' │
890
+ Array_Vector4 2000 │ ' 2301 ms' │ ' 18 ms' │ ' 127.83 x' │
891
+ Array_Matrix4 2000 │ ' 1594 ms' │ ' 12 ms' │ ' 132.83 x' │
894
892
  └──────────────────┴────────────┴──────────────┴──────────────┴──────────────┘
895
893
  ```
896
894
 
897
- ### Compile
895
+ ### Validate
898
896
 
899
- This benchmark measures compilation performance for varying types. You can review this benchmark [here](https://github.com/sinclairzx81/typebox/blob/master/benchmark/compile.ts).
897
+ This benchmark measures validation performance for varying types. You can review this benchmark [here](https://github.com/sinclairzx81/typebox/blob/master/benchmark/check.ts).
900
898
 
901
899
  ```typescript
902
900
  ┌──────────────────┬────────────┬──────────────┬──────────────┬──────────────┐
903
901
  │ (index) │ Iterations │ Ajv │ TypeCompiler │ Performance │
904
902
  ├──────────────────┼────────────┼──────────────┼──────────────┼──────────────┤
905
- │ Number │ 2000 │ ' 384 ms' │ ' 7 ms' │ ' 54.86 x' │
906
- │ String │ 2000 │ ' 311 ms' │ ' 6 ms' │ ' 51.83 x' │
907
- │ Boolean │ 2000 │ ' 304 ms' │ ' 5 ms' │ ' 60.80 x' │
908
- │ Null │ 2000 │ ' 252 ms' │ ' 5 ms' │ ' 50.40 x' │
909
- │ RegEx │ 2000 │ ' 480 ms' │ ' 7 ms' │ ' 68.57 x' │
910
- │ ObjectA │ 2000 │ ' 2786 ms' │ ' 27 ms' │ ' 103.19 x' │
911
- │ ObjectB │ 2000 │ ' 2946 ms' │ ' 22 ms' │ ' 133.91 x' │
912
- │ Tuple │ 2000 │ ' 1277 ms' │ ' 16 ms' │ ' 79.81 x' │
913
- │ Union │ 2000 │ ' 1288 ms' │ ' 16 ms' │ ' 80.50 x' │
914
- Vector4 2000 │ ' 1628 ms' │ ' 12 ms' │ ' 135.67 x' │
915
- Matrix4 2000 │ ' 912 ms' │ ' 7 ms' │ ' 130.29 x' │
916
- Literal_String 2000 │ ' 344 ms' │ ' 5 ms' │ ' 68.80 x' │
917
- Literal_Number 2000 │ ' 432 ms' │ ' 5 ms' │ ' 86.40 x' │
918
- Literal_Boolean 2000 │ ' 407 ms' │ ' 4 ms' │ ' 101.75 x' │
919
- Array_Number 2000 │ ' 788 ms' │ ' 9 ms' │ ' 87.56 x' │
920
- Array_String 2000 │ ' 823 ms' │ ' 7 ms' │ ' 117.57 x' │
921
- Array_Boolean 2000 │ ' 816 ms' │ ' 5 ms' │ ' 163.20 x' │
922
- Array_ObjectA 2000 │ ' 4270 ms' │ ' 24 ms' │ ' 177.92 x' │
923
- Array_ObjectB 2000 │ ' 4008 ms' │ ' 28 ms' │ ' 143.14 x' │
924
- Array_Tuple 2000 │ ' 2243 ms' │ ' 13 ms' │ ' 172.54 x' │
925
- Array_Union 2000 │ ' 1734 ms' │ ' 16 ms' │ ' 108.38 x' │
926
- Array_Vector4 2000 │ ' 2348 ms' │ ' 14 ms' │ ' 167.71 x' │
927
- Array_Matrix4 2000 │ ' 1608 ms' │ ' 11 ms' │ ' 146.18 x' │
903
+ │ Number │ 4000000 │ ' 18 ms' │ ' 16 ms' │ ' 1.13 x' │
904
+ │ String │ 4000000 │ ' 74 ms' │ ' 43 ms' │ ' 1.72 x' │
905
+ │ Boolean │ 4000000 │ ' 71 ms' │ ' 37 ms' │ ' 1.92 x' │
906
+ │ Null │ 4000000 │ ' 70 ms' │ ' 37 ms' │ ' 1.89 x' │
907
+ │ RegEx │ 4000000 │ ' 175 ms' │ ' 153 ms' │ ' 1.14 x' │
908
+ │ ObjectA │ 4000000 │ ' 124 ms' │ ' 87 ms' │ ' 1.43 x' │
909
+ │ ObjectB │ 4000000 │ ' 215 ms' │ ' 143 ms' │ ' 1.50 x' │
910
+ │ Tuple │ 4000000 │ ' 101 ms' │ ' 51 ms' │ ' 1.98 x' │
911
+ │ Union │ 4000000 │ ' 108 ms' │ ' 51 ms' │ ' 2.12 x' │
912
+ Recursive 4000000 │ ' 1647 ms' │ ' 626 ms' │ ' 2.63 x' │
913
+ Vector4 4000000 │ ' 83 ms' │ ' 41 ms' │ ' 2.02 x' │
914
+ Matrix4 4000000 │ ' 154 ms' │ ' 105 ms' │ ' 1.47 x' │
915
+ Literal_String 4000000 │ ' 101 ms' │ ' 37 ms' │ ' 2.73 x' │
916
+ Literal_Number 4000000 │ ' 69 ms' │ ' 35 ms' │ ' 1.97 x' │
917
+ Literal_Boolean 4000000 │ ' 71 ms' │ ' 35 ms' │ ' 2.03 x' │
918
+ Array_Number 4000000 │ ' 117 ms' │ ' 61 ms' │ ' 1.92 x' │
919
+ Array_String 4000000 │ ' 116 ms' │ ' 73 ms' │ ' 1.59 x' │
920
+ Array_Boolean 4000000 │ ' 129 ms' │ ' 89 ms' │ ' 1.45 x' │
921
+ Array_ObjectA 4000000 │ ' 10582 ms' │ ' 6854 ms' │ ' 1.54 x' │
922
+ Array_ObjectB 4000000 │ ' 11539 ms' │ ' 8224 ms' │ ' 1.40 x' │
923
+ Array_Tuple 4000000 │ ' 362 ms' │ ' 282 ms' │ ' 1.28 x' │
924
+ Array_Union 4000000 │ ' 950 ms' │ ' 355 ms' │ ' 2.68 x' │
925
+ Array_Recursive 4000000 │ ' 28047 ms' │ ' 9567 ms' │ ' 2.93 x' │
926
+ │ Array_Vector4 │ 4000000 │ ' 372 ms' │ ' 200 ms' │ ' 1.86 x' │
927
+ │ Array_Matrix4 │ 4000000 │ ' 1521 ms' │ ' 1244 ms' │ ' 1.22 x' │
928
928
  └──────────────────┴────────────┴──────────────┴──────────────┴──────────────┘
929
929
  ```
930
930
 
931
+
932
+
931
933
  ## Contribute
932
934
 
933
935
  TypeBox is open to community contribution. Please ensure you submit an open issue before submitting your pull request. The TypeBox project preferences open community discussion prior to accepting new features.
package/typebox.js CHANGED
@@ -91,7 +91,7 @@ class TypeBuilder {
91
91
  const values = Object.keys(item)
92
92
  .filter((key) => isNaN(key))
93
93
  .map((key) => item[key]);
94
- const anyOf = values.map((value) => (typeof value === 'string' ? { [exports.Kind]: 'Literal', type: 'string', const: value } : { [exports.Kind]: 'Literal', type: 'number', const: value }));
94
+ const anyOf = values.map((value) => typeof value === 'string' ? { [exports.Kind]: 'Literal', type: 'string', const: value } : { [exports.Kind]: 'Literal', type: 'number', const: value });
95
95
  return this.Create({ ...options, [exports.Kind]: 'Union', [exports.Hint]: 'Enum', anyOf });
96
96
  }
97
97
  /** Creates a function type */
@@ -308,7 +308,9 @@ class TypeBuilder {
308
308
  const additionalItems = false;
309
309
  const minItems = items.length;
310
310
  const maxItems = items.length;
311
- const schema = (items.length > 0 ? { ...options, [exports.Kind]: 'Tuple', type: 'array', items, additionalItems, minItems, maxItems } : { ...options, [exports.Kind]: 'Tuple', type: 'array', minItems, maxItems });
311
+ const schema = (items.length > 0
312
+ ? { ...options, [exports.Kind]: 'Tuple', type: 'array', items, additionalItems, minItems, maxItems }
313
+ : { ...options, [exports.Kind]: 'Tuple', type: 'array', minItems, maxItems });
312
314
  return this.Create(schema);
313
315
  }
314
316
  /** Creates a undefined type */
package/value/cast.d.ts CHANGED
@@ -1,4 +1,8 @@
1
1
  import * as Types from '../typebox';
2
+ export declare class ValueCastInvalidTypeError extends Error {
3
+ readonly schema: Types.TSchema;
4
+ constructor(schema: Types.TSchema);
5
+ }
2
6
  export declare namespace ValueCast {
3
7
  function Visit(schema: Types.TSchema, references: Types.TSchema[], value: any): any;
4
8
  function Cast<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R], value: any): Types.Static<T>;
package/value/cast.js CHANGED
@@ -27,8 +27,9 @@ THE SOFTWARE.
27
27
 
28
28
  ---------------------------------------------------------------------------*/
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.ValueCast = void 0;
30
+ exports.ValueCast = exports.ValueCastInvalidTypeError = void 0;
31
31
  const Types = require("../typebox");
32
+ const index_1 = require("../guard/index");
32
33
  const create_1 = require("./create");
33
34
  const check_1 = require("./check");
34
35
  // --------------------------------------------------------------------------
@@ -71,6 +72,13 @@ var UnionValueCast;
71
72
  }
72
73
  UnionValueCast.Create = Create;
73
74
  })(UnionValueCast || (UnionValueCast = {}));
75
+ class ValueCastInvalidTypeError extends Error {
76
+ constructor(schema) {
77
+ super('ValueCast: Invalid type');
78
+ this.schema = schema;
79
+ }
80
+ }
81
+ exports.ValueCastInvalidTypeError = ValueCastInvalidTypeError;
74
82
  var ValueCast;
75
83
  (function (ValueCast) {
76
84
  function Any(schema, references, value) {
@@ -189,57 +197,72 @@ var ValueCast;
189
197
  return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
190
198
  }
191
199
  function Visit(schema, references, value) {
192
- const anyReferences = schema.$id === undefined ? references : [schema, ...references];
193
- const anySchema = schema;
194
- switch (schema[Types.Kind]) {
195
- case 'Any':
196
- return Any(anySchema, anyReferences, value);
197
- case 'Array':
198
- return Array(anySchema, anyReferences, value);
199
- case 'Boolean':
200
- return Boolean(anySchema, anyReferences, value);
201
- case 'Constructor':
202
- return Constructor(anySchema, anyReferences, value);
203
- case 'Enum':
204
- return Enum(anySchema, anyReferences, value);
205
- case 'Function':
206
- return Function(anySchema, anyReferences, value);
207
- case 'Integer':
208
- return Integer(anySchema, anyReferences, value);
209
- case 'Literal':
210
- return Literal(anySchema, anyReferences, value);
211
- case 'Null':
212
- return Null(anySchema, anyReferences, value);
213
- case 'Number':
214
- return Number(anySchema, anyReferences, value);
215
- case 'Object':
216
- return Object(anySchema, anyReferences, value);
217
- case 'Promise':
218
- return Promise(anySchema, anyReferences, value);
219
- case 'Record':
220
- return Record(anySchema, anyReferences, value);
221
- case 'Rec':
222
- return Recursive(anySchema, anyReferences, value);
223
- case 'Ref':
224
- return Ref(anySchema, anyReferences, value);
225
- case 'Self':
226
- return Self(anySchema, anyReferences, value);
227
- case 'String':
228
- return String(anySchema, anyReferences, value);
229
- case 'Tuple':
230
- return Tuple(anySchema, anyReferences, value);
231
- case 'Undefined':
232
- return Undefined(anySchema, anyReferences, value);
233
- case 'Union':
234
- return Union(anySchema, anyReferences, value);
235
- case 'Uint8Array':
236
- return Uint8Array(anySchema, anyReferences, value);
237
- case 'Unknown':
238
- return Unknown(anySchema, anyReferences, value);
239
- case 'Void':
240
- return Void(anySchema, anyReferences, value);
241
- default:
242
- throw Error(`ValueCast.Visit: Unknown schema kind '${schema[Types.Kind]}'`);
200
+ const refs = schema.$id === undefined ? references : [schema, ...references];
201
+ if (index_1.TypeGuard.TAny(schema)) {
202
+ return Any(schema, refs, value);
203
+ }
204
+ else if (index_1.TypeGuard.TArray(schema)) {
205
+ return Array(schema, refs, value);
206
+ }
207
+ else if (index_1.TypeGuard.TBoolean(schema)) {
208
+ return Boolean(schema, refs, value);
209
+ }
210
+ else if (index_1.TypeGuard.TConstructor(schema)) {
211
+ return Constructor(schema, refs, value);
212
+ }
213
+ else if (index_1.TypeGuard.TFunction(schema)) {
214
+ return Function(schema, refs, value);
215
+ }
216
+ else if (index_1.TypeGuard.TInteger(schema)) {
217
+ return Integer(schema, refs, value);
218
+ }
219
+ else if (index_1.TypeGuard.TLiteral(schema)) {
220
+ return Literal(schema, refs, value);
221
+ }
222
+ else if (index_1.TypeGuard.TNull(schema)) {
223
+ return Null(schema, refs, value);
224
+ }
225
+ else if (index_1.TypeGuard.TNumber(schema)) {
226
+ return Number(schema, refs, value);
227
+ }
228
+ else if (index_1.TypeGuard.TObject(schema)) {
229
+ return Object(schema, refs, value);
230
+ }
231
+ else if (index_1.TypeGuard.TPromise(schema)) {
232
+ return Promise(schema, refs, value);
233
+ }
234
+ else if (index_1.TypeGuard.TRecord(schema)) {
235
+ return Record(schema, refs, value);
236
+ }
237
+ else if (index_1.TypeGuard.TRef(schema)) {
238
+ return Ref(schema, refs, value);
239
+ }
240
+ else if (index_1.TypeGuard.TSelf(schema)) {
241
+ return Self(schema, refs, value);
242
+ }
243
+ else if (index_1.TypeGuard.TString(schema)) {
244
+ return String(schema, refs, value);
245
+ }
246
+ else if (index_1.TypeGuard.TTuple(schema)) {
247
+ return Tuple(schema, refs, value);
248
+ }
249
+ else if (index_1.TypeGuard.TUndefined(schema)) {
250
+ return Undefined(schema, refs, value);
251
+ }
252
+ else if (index_1.TypeGuard.TUnion(schema)) {
253
+ return Union(schema, refs, value);
254
+ }
255
+ else if (index_1.TypeGuard.TUint8Array(schema)) {
256
+ return Uint8Array(schema, refs, value);
257
+ }
258
+ else if (index_1.TypeGuard.TUnknown(schema)) {
259
+ return Unknown(schema, refs, value);
260
+ }
261
+ else if (index_1.TypeGuard.TVoid(schema)) {
262
+ return Void(schema, refs, value);
263
+ }
264
+ else {
265
+ throw new ValueCastInvalidTypeError(schema);
243
266
  }
244
267
  }
245
268
  ValueCast.Visit = Visit;
package/value/check.d.ts CHANGED
@@ -1,4 +1,8 @@
1
1
  import * as Types from '../typebox';
2
+ export declare class ValueCheckInvalidTypeError extends Error {
3
+ readonly schema: Types.TSchema;
4
+ constructor(schema: Types.TSchema);
5
+ }
2
6
  export declare namespace ValueCheck {
3
7
  function Check<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R], value: any): boolean;
4
8
  }