@sinclair/typebox 0.24.23 → 0.24.26

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,52 @@ 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 IsValidPropertyKey(value) {
51
+ if (typeof value !== 'string')
52
+ return false;
53
+ for (let i = 0; i < value.length; i++) {
54
+ const code = value.charCodeAt(i);
55
+ if ((code >= 7 && code <= 13) || code === 27 || code === 127) {
56
+ return false;
57
+ }
58
+ }
59
+ return true;
60
+ }
61
+ function IsString(value) {
62
+ return typeof value === 'string';
63
+ }
64
+ function IsNumber(value) {
65
+ return typeof value === 'number';
66
+ }
67
+ function IsBoolean(value) {
68
+ return typeof value === 'boolean';
69
+ }
70
+ function IsOptionalNumber(value) {
71
+ return value === undefined || (value !== undefined && IsNumber(value));
72
+ }
73
+ function IsOptionalBoolean(value) {
74
+ return value === undefined || (value !== undefined && IsBoolean(value));
75
+ }
76
+ function IsOptionalString(value) {
77
+ return value === undefined || (value !== undefined && IsString(value));
78
+ }
79
+ function IsOptionalPattern(value) {
80
+ return value === undefined || (value !== undefined && IsString(value) && IsPattern(value));
40
81
  }
41
82
  /** Returns true if the given schema is TAny */
42
83
  function TAny(schema) {
@@ -45,7 +86,7 @@ var TypeGuard;
45
86
  TypeGuard.TAny = TAny;
46
87
  /** Returns true if the given schema is TArray */
47
88
  function TArray(schema) {
48
- return IsObject(schema) && schema[Types.Kind] === 'Array' && schema.type === 'array' && TSchema(schema.items);
89
+ return IsObject(schema) && schema[Types.Kind] === 'Array' && schema.type === 'array' && TSchema(schema.items) && IsOptionalNumber(schema.minItems) && IsOptionalNumber(schema.maxItems) && IsOptionalBoolean(schema.uniqueItems);
49
90
  }
50
91
  TypeGuard.TArray = TArray;
51
92
  /** Returns true if the given schema is TBoolean */
@@ -79,12 +120,19 @@ var TypeGuard;
79
120
  TypeGuard.TFunction = TFunction;
80
121
  /** Returns true if the given schema is TInteger */
81
122
  function TInteger(schema) {
82
- return IsObject(schema) && schema[Types.Kind] === 'Integer' && schema.type === 'integer';
123
+ return (IsObject(schema) &&
124
+ schema[Types.Kind] === 'Integer' &&
125
+ schema.type === 'integer' &&
126
+ IsOptionalNumber(schema.multipleOf) &&
127
+ IsOptionalNumber(schema.minimum) &&
128
+ IsOptionalNumber(schema.maximum) &&
129
+ IsOptionalNumber(schema.exclusiveMinimum) &&
130
+ IsOptionalNumber(schema.exclusiveMaximum));
83
131
  }
84
132
  TypeGuard.TInteger = TInteger;
85
133
  /** Returns true if the given schema is TLiteral */
86
134
  function TLiteral(schema) {
87
- return IsObject(schema) && schema[Types.Kind] === 'Literal' && (typeof schema.const === 'string' || typeof schema.const === 'number' || typeof schema.const === 'boolean');
135
+ return IsObject(schema) && schema[Types.Kind] === 'Literal' && (IsString(schema.const) || IsNumber(schema.const) || IsBoolean(schema.const));
88
136
  }
89
137
  TypeGuard.TLiteral = TLiteral;
90
138
  /** Returns true if the given schema is TNull */
@@ -94,16 +142,31 @@ var TypeGuard;
94
142
  TypeGuard.TNull = TNull;
95
143
  /** Returns true if the given schema is TNumber */
96
144
  function TNumber(schema) {
97
- return IsObject(schema) && schema[Types.Kind] === 'Number' && schema.type === 'number';
145
+ return (IsObject(schema) &&
146
+ schema[Types.Kind] === 'Number' &&
147
+ schema.type === 'number' &&
148
+ IsOptionalNumber(schema.multipleOf) &&
149
+ IsOptionalNumber(schema.minimum) &&
150
+ IsOptionalNumber(schema.maximum) &&
151
+ IsOptionalNumber(schema.exclusiveMinimum) &&
152
+ IsOptionalNumber(schema.exclusiveMaximum));
98
153
  }
99
154
  TypeGuard.TNumber = TNumber;
100
155
  /** Returns true if the given schema is TObject */
101
156
  function TObject(schema) {
102
- if (!(IsObject(schema) && schema[Types.Kind] === 'Object' && schema.type === 'object' && IsObject(schema.properties))) {
157
+ if (!(IsObject(schema) &&
158
+ schema[Types.Kind] === 'Object' &&
159
+ schema.type === 'object' &&
160
+ IsObject(schema.properties) &&
161
+ IsOptionalBoolean(schema.additionalProperties) &&
162
+ IsOptionalNumber(schema.minProperties) &&
163
+ IsOptionalNumber(schema.maxProperties))) {
103
164
  return false;
104
165
  }
105
- for (const property of Object.values(schema.properties)) {
106
- if (!TSchema(property))
166
+ for (const [key, value] of Object.entries(schema.properties)) {
167
+ if (!IsValidPropertyKey(key))
168
+ return false;
169
+ if (!TSchema(value))
107
170
  return false;
108
171
  }
109
172
  return true;
@@ -116,13 +179,16 @@ var TypeGuard;
116
179
  TypeGuard.TPromise = TPromise;
117
180
  /** Returns true if the given schema is TRecord */
118
181
  function TRecord(schema) {
119
- if (!(IsObject(schema) && schema[Types.Kind] === 'Record' && schema.type === 'object' && IsObject(schema.patternProperties))) {
182
+ if (!(IsObject(schema) && schema[Types.Kind] === 'Record' && schema.type === 'object' && schema.additionalProperties === false && IsObject(schema.patternProperties))) {
120
183
  return false;
121
184
  }
122
185
  const keys = Object.keys(schema.patternProperties);
123
186
  if (keys.length !== 1) {
124
187
  return false;
125
188
  }
189
+ if (!IsPattern(keys[0])) {
190
+ return false;
191
+ }
126
192
  if (!TSchema(schema.patternProperties[keys[0]])) {
127
193
  return false;
128
194
  }
@@ -131,22 +197,22 @@ var TypeGuard;
131
197
  TypeGuard.TRecord = TRecord;
132
198
  /** Returns true if the given schema is TSelf */
133
199
  function TSelf(schema) {
134
- return IsObject(schema) && schema[Types.Kind] === 'Self' && typeof schema.$ref === 'string';
200
+ return IsObject(schema) && schema[Types.Kind] === 'Self' && IsString(schema.$ref);
135
201
  }
136
202
  TypeGuard.TSelf = TSelf;
137
203
  /** Returns true if the given schema is TRef */
138
204
  function TRef(schema) {
139
- return IsObject(schema) && schema[Types.Kind] === 'Ref' && typeof schema.$ref === 'string';
205
+ return IsObject(schema) && schema[Types.Kind] === 'Ref' && IsString(schema.$ref);
140
206
  }
141
207
  TypeGuard.TRef = TRef;
142
208
  /** Returns true if the given schema is TString */
143
209
  function TString(schema) {
144
- return IsObject(schema) && schema[Types.Kind] === 'String' && schema.type === 'string';
210
+ return IsObject(schema) && schema[Types.Kind] === 'String' && schema.type === 'string' && IsOptionalNumber(schema.minLength) && IsOptionalNumber(schema.maxLength) && IsOptionalPattern(schema.pattern) && IsOptionalString(schema.format);
145
211
  }
146
212
  TypeGuard.TString = TString;
147
213
  /** Returns true if the given schema is TTuple */
148
214
  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)) {
215
+ if (!(IsObject(schema) && schema[Types.Kind] === 'Tuple' && schema.type === 'array' && IsNumber(schema.minItems) && IsNumber(schema.maxItems) && schema.minItems === schema.maxItems)) {
150
216
  return false;
151
217
  }
152
218
  if (schema.items === undefined && schema.additionalItems === undefined && schema.minItems === 0) {
@@ -181,7 +247,7 @@ var TypeGuard;
181
247
  TypeGuard.TUnion = TUnion;
182
248
  /** Returns true if the given schema is TUint8Array */
183
249
  function TUint8Array(schema) {
184
- return IsObject(schema) && schema[Types.Kind] === 'Uint8Array' && schema.type === 'object' && schema.specialized === 'Uint8Array';
250
+ return IsObject(schema) && schema[Types.Kind] === 'Uint8Array' && schema.type === 'object' && schema.specialized === 'Uint8Array' && IsOptionalNumber(schema.minByteLength) && IsOptionalNumber(schema.maxByteLength);
185
251
  }
186
252
  TypeGuard.TUint8Array = TUint8Array;
187
253
  /** 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.26",
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/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
  }