@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/compiler/compiler.d.ts +8 -1
- package/compiler/compiler.js +119 -55
- package/compiler/index.d.ts +1 -1
- package/compiler/index.js +2 -2
- package/{error → errors}/errors.d.ts +4 -0
- package/{error → errors}/errors.js +77 -51
- package/errors/index.d.ts +1 -0
- package/{compiler/property.js → errors/index.js} +16 -36
- package/guard/guard.js +83 -17
- package/package.json +1 -1
- package/readme.md +57 -55
- package/value/cast.d.ts +4 -0
- package/value/cast.js +75 -52
- package/value/check.d.ts +4 -0
- package/value/check.js +76 -50
- package/value/create.d.ts +4 -0
- package/value/create.js +75 -53
- package/value/index.d.ts +1 -1
- package/value/index.js +2 -2
- 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,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(
|
|
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 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) &&
|
|
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' && (
|
|
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) &&
|
|
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) &&
|
|
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
|
|
106
|
-
if (!
|
|
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' &&
|
|
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' &&
|
|
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' &&
|
|
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
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/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
|
}
|