@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/compiler/compiler.d.ts +8 -1
- package/compiler/compiler.js +127 -54
- package/compiler/index.d.ts +1 -1
- package/compiler/index.js +1 -1
- package/{error → errors}/errors.d.ts +4 -0
- package/{error → errors}/errors.js +76 -50
- package/errors/index.d.ts +1 -0
- package/{compiler/property.js → errors/index.js} +16 -36
- package/guard/guard.js +82 -14
- package/package.json +1 -1
- package/readme.md +57 -55
- package/typebox.js +4 -2
- package/value/cast.d.ts +4 -0
- package/value/cast.js +75 -52
- package/value/check.d.ts +4 -0
- package/value/check.js +75 -49
- package/value/create.d.ts +4 -0
- package/value/create.js +75 -53
- package/value/index.d.ts +1 -1
- package/value/index.js +1 -1
- package/value/value.d.ts +1 -1
- package/value/value.js +2 -2
- package/compiler/property.d.ts +0 -4
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(
|
|
36
|
-
return typeof
|
|
35
|
+
function IsObject(value) {
|
|
36
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
37
37
|
}
|
|
38
|
-
function IsArray(
|
|
39
|
-
return typeof
|
|
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) &&
|
|
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) &&
|
|
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' && (
|
|
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) &&
|
|
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' &&
|
|
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' &&
|
|
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) &&
|
|
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) &&
|
|
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) &&
|
|
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
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:
|
|
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:
|
|
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:
|
|
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
|
-
###
|
|
861
|
+
### Compile
|
|
862
862
|
|
|
863
|
-
This benchmark measures
|
|
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 │
|
|
870
|
-
│ String │
|
|
871
|
-
│ Boolean │
|
|
872
|
-
│ Null │
|
|
873
|
-
│ RegEx │
|
|
874
|
-
│ ObjectA │
|
|
875
|
-
│ ObjectB │
|
|
876
|
-
│ Tuple │
|
|
877
|
-
│ Union │
|
|
878
|
-
│
|
|
879
|
-
│
|
|
880
|
-
│
|
|
881
|
-
│
|
|
882
|
-
│
|
|
883
|
-
│
|
|
884
|
-
│
|
|
885
|
-
│
|
|
886
|
-
│
|
|
887
|
-
│
|
|
888
|
-
│
|
|
889
|
-
│
|
|
890
|
-
│
|
|
891
|
-
│
|
|
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
|
-
###
|
|
895
|
+
### Validate
|
|
898
896
|
|
|
899
|
-
This benchmark measures
|
|
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 │
|
|
906
|
-
│ String │
|
|
907
|
-
│ Boolean │
|
|
908
|
-
│ Null │
|
|
909
|
-
│ RegEx │
|
|
910
|
-
│ ObjectA │
|
|
911
|
-
│ ObjectB │
|
|
912
|
-
│ Tuple │
|
|
913
|
-
│ Union │
|
|
914
|
-
│
|
|
915
|
-
│
|
|
916
|
-
│
|
|
917
|
-
│
|
|
918
|
-
│
|
|
919
|
-
│
|
|
920
|
-
│
|
|
921
|
-
│
|
|
922
|
-
│
|
|
923
|
-
│
|
|
924
|
-
│
|
|
925
|
-
│
|
|
926
|
-
│
|
|
927
|
-
│
|
|
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) =>
|
|
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
|
|
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
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
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
|
}
|