@sinclair/typebox 0.25.24 → 0.26.0-dev.1
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 +10 -5
- package/compiler/compiler.js +161 -123
- package/errors/errors.d.ts +56 -46
- package/errors/errors.js +234 -153
- package/package.json +1 -6
- package/readme.md +294 -207
- package/system/system.d.ts +9 -6
- package/system/system.js +17 -17
- package/typebox.d.ts +388 -162
- package/typebox.js +1716 -229
- package/value/cast.d.ts +2 -2
- package/value/cast.js +121 -188
- package/value/check.d.ts +1 -1
- package/value/check.js +156 -111
- package/value/convert.d.ts +13 -0
- package/value/convert.js +345 -0
- package/value/create.d.ts +6 -2
- package/value/create.js +149 -97
- package/{hash → value}/hash.js +39 -14
- package/value/index.d.ts +1 -0
- package/value/index.js +3 -1
- package/value/value.d.ts +2 -8
- package/value/value.js +20 -14
- package/conditional/conditional.d.ts +0 -17
- package/conditional/conditional.js +0 -91
- package/conditional/index.d.ts +0 -2
- package/conditional/index.js +0 -45
- package/conditional/structural.d.ts +0 -11
- package/conditional/structural.js +0 -685
- package/custom/custom.d.ts +0 -12
- package/custom/custom.js +0 -55
- package/custom/index.d.ts +0 -1
- package/custom/index.js +0 -44
- package/format/format.d.ts +0 -12
- package/format/format.js +0 -55
- package/format/index.d.ts +0 -1
- package/format/index.js +0 -44
- package/guard/extends.d.ts +0 -10
- package/guard/extends.js +0 -50
- package/guard/guard.d.ts +0 -60
- package/guard/guard.js +0 -440
- package/guard/index.d.ts +0 -2
- package/guard/index.js +0 -45
- package/hash/index.d.ts +0 -1
- package/hash/index.js +0 -44
- /package/{hash → value}/hash.d.ts +0 -0
package/value/cast.d.ts
CHANGED
|
@@ -21,6 +21,6 @@ export declare class ValueCastUnknownTypeError extends Error {
|
|
|
21
21
|
constructor(schema: Types.TSchema);
|
|
22
22
|
}
|
|
23
23
|
export declare namespace ValueCast {
|
|
24
|
-
function Visit(schema: Types.TSchema,
|
|
25
|
-
function Cast<T extends Types.TSchema
|
|
24
|
+
function Visit(schema: Types.TSchema, value: any): any;
|
|
25
|
+
function Cast<T extends Types.TSchema>(schema: T, value: any): Types.Static<T>;
|
|
26
26
|
}
|
package/value/cast.js
CHANGED
|
@@ -32,7 +32,6 @@ const Types = require("../typebox");
|
|
|
32
32
|
const create_1 = require("./create");
|
|
33
33
|
const check_1 = require("./check");
|
|
34
34
|
const clone_1 = require("./clone");
|
|
35
|
-
const index_1 = require("../custom/index");
|
|
36
35
|
// ----------------------------------------------------------------------------------------------
|
|
37
36
|
// Errors
|
|
38
37
|
// ----------------------------------------------------------------------------------------------
|
|
@@ -80,7 +79,7 @@ exports.ValueCastUnknownTypeError = ValueCastUnknownTypeError;
|
|
|
80
79
|
// ----------------------------------------------------------------------------------------------
|
|
81
80
|
var UnionCastCreate;
|
|
82
81
|
(function (UnionCastCreate) {
|
|
83
|
-
function Score(schema,
|
|
82
|
+
function Score(schema, value) {
|
|
84
83
|
if (schema[Types.Kind] === 'Object' && typeof value === 'object' && value !== null) {
|
|
85
84
|
const object = schema;
|
|
86
85
|
const keys = Object.keys(value);
|
|
@@ -88,19 +87,19 @@ var UnionCastCreate;
|
|
|
88
87
|
const [point, max] = [1 / entries.length, entries.length];
|
|
89
88
|
return entries.reduce((acc, [key, schema]) => {
|
|
90
89
|
const literal = schema[Types.Kind] === 'Literal' && schema.const === value[key] ? max : 0;
|
|
91
|
-
const checks = check_1.ValueCheck.Check(schema,
|
|
90
|
+
const checks = check_1.ValueCheck.Check(schema, value[key]) ? point : 0;
|
|
92
91
|
const exists = keys.includes(key) ? point : 0;
|
|
93
92
|
return acc + (literal + checks + exists);
|
|
94
93
|
}, 0);
|
|
95
94
|
}
|
|
96
95
|
else {
|
|
97
|
-
return check_1.ValueCheck.Check(schema,
|
|
96
|
+
return check_1.ValueCheck.Check(schema, value) ? 1 : 0;
|
|
98
97
|
}
|
|
99
98
|
}
|
|
100
|
-
function Select(union,
|
|
99
|
+
function Select(union, value) {
|
|
101
100
|
let [select, best] = [union.anyOf[0], 0];
|
|
102
101
|
for (const schema of union.anyOf) {
|
|
103
|
-
const score = Score(schema,
|
|
102
|
+
const score = Score(schema, value);
|
|
104
103
|
if (score > best) {
|
|
105
104
|
select = schema;
|
|
106
105
|
best = score;
|
|
@@ -108,13 +107,13 @@ var UnionCastCreate;
|
|
|
108
107
|
}
|
|
109
108
|
return select;
|
|
110
109
|
}
|
|
111
|
-
function Create(union,
|
|
110
|
+
function Create(union, value) {
|
|
112
111
|
if (union.default !== undefined) {
|
|
113
112
|
return union.default;
|
|
114
113
|
}
|
|
115
114
|
else {
|
|
116
|
-
const schema = Select(union,
|
|
117
|
-
return ValueCast.Cast(schema,
|
|
115
|
+
const schema = Select(union, value);
|
|
116
|
+
return ValueCast.Cast(schema, value);
|
|
118
117
|
}
|
|
119
118
|
}
|
|
120
119
|
UnionCastCreate.Create = Create;
|
|
@@ -124,162 +123,93 @@ var ValueCast;
|
|
|
124
123
|
// ----------------------------------------------------------------------------------------------
|
|
125
124
|
// Guards
|
|
126
125
|
// ----------------------------------------------------------------------------------------------
|
|
126
|
+
function IsObject(value) {
|
|
127
|
+
return typeof value === 'object' && value !== null && !globalThis.Array.isArray(value);
|
|
128
|
+
}
|
|
127
129
|
function IsArray(value) {
|
|
128
130
|
return typeof value === 'object' && globalThis.Array.isArray(value);
|
|
129
131
|
}
|
|
130
|
-
function IsDate(value) {
|
|
131
|
-
return typeof value === 'object' && value instanceof globalThis.Date;
|
|
132
|
-
}
|
|
133
|
-
function IsString(value) {
|
|
134
|
-
return typeof value === 'string';
|
|
135
|
-
}
|
|
136
|
-
function IsBoolean(value) {
|
|
137
|
-
return typeof value === 'boolean';
|
|
138
|
-
}
|
|
139
|
-
function IsBigInt(value) {
|
|
140
|
-
return typeof value === 'bigint';
|
|
141
|
-
}
|
|
142
132
|
function IsNumber(value) {
|
|
143
133
|
return typeof value === 'number' && !isNaN(value);
|
|
144
134
|
}
|
|
145
|
-
function IsStringNumeric(value) {
|
|
146
|
-
return IsString(value) && !isNaN(value) && !isNaN(parseFloat(value));
|
|
147
|
-
}
|
|
148
|
-
function IsValueToString(value) {
|
|
149
|
-
return IsBigInt(value) || IsBoolean(value) || IsNumber(value);
|
|
150
|
-
}
|
|
151
|
-
function IsValueTrue(value) {
|
|
152
|
-
return value === true || (IsNumber(value) && value === 1) || (IsBigInt(value) && value === globalThis.BigInt('1')) || (IsString(value) && (value.toLowerCase() === 'true' || value === '1'));
|
|
153
|
-
}
|
|
154
|
-
function IsValueFalse(value) {
|
|
155
|
-
return value === false || (IsNumber(value) && value === 0) || (IsBigInt(value) && value === globalThis.BigInt('0')) || (IsString(value) && (value.toLowerCase() === 'false' || value === '0'));
|
|
156
|
-
}
|
|
157
|
-
function IsTimeStringWithTimeZone(value) {
|
|
158
|
-
return IsString(value) && /^(?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)(?:\.\d+)?(?:z|[+-]\d\d(?::?\d\d)?)$/i.test(value);
|
|
159
|
-
}
|
|
160
|
-
function IsTimeStringWithoutTimeZone(value) {
|
|
161
|
-
return IsString(value) && /^(?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)?$/i.test(value);
|
|
162
|
-
}
|
|
163
|
-
function IsDateTimeStringWithTimeZone(value) {
|
|
164
|
-
return IsString(value) && /^\d\d\d\d-[0-1]\d-[0-3]\dt(?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)(?:\.\d+)?(?:z|[+-]\d\d(?::?\d\d)?)$/i.test(value);
|
|
165
|
-
}
|
|
166
|
-
function IsDateTimeStringWithoutTimeZone(value) {
|
|
167
|
-
return IsString(value) && /^\d\d\d\d-[0-1]\d-[0-3]\dt(?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)?$/i.test(value);
|
|
168
|
-
}
|
|
169
|
-
function IsDateString(value) {
|
|
170
|
-
return IsString(value) && /^\d\d\d\d-[0-1]\d-[0-3]\d$/i.test(value);
|
|
171
|
-
}
|
|
172
|
-
// ----------------------------------------------------------------------------------------------
|
|
173
|
-
// Convert
|
|
174
|
-
// ----------------------------------------------------------------------------------------------
|
|
175
|
-
function TryConvertString(value) {
|
|
176
|
-
return IsValueToString(value) ? value.toString() : value;
|
|
177
|
-
}
|
|
178
|
-
function TryConvertNumber(value) {
|
|
179
|
-
return IsStringNumeric(value) ? parseFloat(value) : IsValueTrue(value) ? 1 : value;
|
|
180
|
-
}
|
|
181
|
-
function TryConvertInteger(value) {
|
|
182
|
-
return IsStringNumeric(value) ? parseInt(value) : IsValueTrue(value) ? 1 : value;
|
|
183
|
-
}
|
|
184
|
-
function TryConvertBoolean(value) {
|
|
185
|
-
return IsValueTrue(value) ? true : IsValueFalse(value) ? false : value;
|
|
186
|
-
}
|
|
187
|
-
function TryConvertDate(value) {
|
|
188
|
-
// note: this function may return an invalid dates for the regex tests
|
|
189
|
-
// above. Invalid dates will however be checked during the casting
|
|
190
|
-
// function and will return a epoch date if invalid. Consider better
|
|
191
|
-
// string parsing for the iso dates in future revisions.
|
|
192
|
-
return IsDate(value)
|
|
193
|
-
? value
|
|
194
|
-
: IsNumber(value)
|
|
195
|
-
? new globalThis.Date(value)
|
|
196
|
-
: IsValueTrue(value)
|
|
197
|
-
? new globalThis.Date(1)
|
|
198
|
-
: IsStringNumeric(value)
|
|
199
|
-
? new globalThis.Date(parseInt(value))
|
|
200
|
-
: IsTimeStringWithoutTimeZone(value)
|
|
201
|
-
? new globalThis.Date(`1970-01-01T${value}.000Z`)
|
|
202
|
-
: IsTimeStringWithTimeZone(value)
|
|
203
|
-
? new globalThis.Date(`1970-01-01T${value}`)
|
|
204
|
-
: IsDateTimeStringWithoutTimeZone(value)
|
|
205
|
-
? new globalThis.Date(`${value}.000Z`)
|
|
206
|
-
: IsDateTimeStringWithTimeZone(value)
|
|
207
|
-
? new globalThis.Date(value)
|
|
208
|
-
: IsDateString(value)
|
|
209
|
-
? new globalThis.Date(`${value}T00:00:00.000Z`)
|
|
210
|
-
: value;
|
|
211
|
-
}
|
|
212
135
|
// ----------------------------------------------------------------------------------------------
|
|
213
136
|
// Cast
|
|
214
137
|
// ----------------------------------------------------------------------------------------------
|
|
215
|
-
function Any(schema,
|
|
216
|
-
return check_1.ValueCheck.Check(schema,
|
|
138
|
+
function Any(schema, value) {
|
|
139
|
+
return check_1.ValueCheck.Check(schema, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema);
|
|
217
140
|
}
|
|
218
|
-
function Array(schema,
|
|
219
|
-
if (check_1.ValueCheck.Check(schema,
|
|
141
|
+
function Array(schema, value) {
|
|
142
|
+
if (check_1.ValueCheck.Check(schema, value))
|
|
220
143
|
return clone_1.ValueClone.Clone(value);
|
|
221
|
-
const created = IsArray(value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema
|
|
144
|
+
const created = IsArray(value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema);
|
|
222
145
|
const minimum = IsNumber(schema.minItems) && created.length < schema.minItems ? [...created, ...globalThis.Array.from({ length: schema.minItems - created.length }, () => null)] : created;
|
|
223
146
|
const maximum = IsNumber(schema.maxItems) && minimum.length > schema.maxItems ? minimum.slice(0, schema.maxItems) : minimum;
|
|
224
|
-
const casted = maximum.map((value) => Visit(schema.items,
|
|
147
|
+
const casted = maximum.map((value) => Visit(schema.items, value));
|
|
225
148
|
if (schema.uniqueItems !== true)
|
|
226
149
|
return casted;
|
|
227
150
|
const unique = [...new Set(casted)];
|
|
228
|
-
if (!check_1.ValueCheck.Check(schema,
|
|
151
|
+
if (!check_1.ValueCheck.Check(schema, unique))
|
|
229
152
|
throw new ValueCastArrayUniqueItemsTypeError(schema, unique);
|
|
230
153
|
return unique;
|
|
231
154
|
}
|
|
232
|
-
function
|
|
233
|
-
|
|
234
|
-
return check_1.ValueCheck.Check(schema, references, conversion) ? conversion : create_1.ValueCreate.Create(schema, references);
|
|
155
|
+
function BigInt(schema, value) {
|
|
156
|
+
return check_1.ValueCheck.Check(schema, value) ? value : create_1.ValueCreate.Create(schema);
|
|
235
157
|
}
|
|
236
|
-
function
|
|
237
|
-
|
|
238
|
-
|
|
158
|
+
function Boolean(schema, value) {
|
|
159
|
+
return check_1.ValueCheck.Check(schema, value) ? value : create_1.ValueCreate.Create(schema);
|
|
160
|
+
}
|
|
161
|
+
function Constructor(schema, value) {
|
|
162
|
+
if (check_1.ValueCheck.Check(schema, value))
|
|
163
|
+
return create_1.ValueCreate.Create(schema);
|
|
239
164
|
const required = new Set(schema.returns.required || []);
|
|
240
165
|
const result = function () { };
|
|
241
166
|
for (const [key, property] of globalThis.Object.entries(schema.returns.properties)) {
|
|
242
167
|
if (!required.has(key) && value.prototype[key] === undefined)
|
|
243
168
|
continue;
|
|
244
|
-
result.prototype[key] = Visit(property,
|
|
169
|
+
result.prototype[key] = Visit(property, value.prototype[key]);
|
|
245
170
|
}
|
|
246
171
|
return result;
|
|
247
172
|
}
|
|
248
|
-
function Date(schema,
|
|
249
|
-
|
|
250
|
-
|
|
173
|
+
function Date(schema, value) {
|
|
174
|
+
return check_1.ValueCheck.Check(schema, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema);
|
|
175
|
+
}
|
|
176
|
+
function Function(schema, value) {
|
|
177
|
+
return check_1.ValueCheck.Check(schema, value) ? value : create_1.ValueCreate.Create(schema);
|
|
251
178
|
}
|
|
252
|
-
function
|
|
253
|
-
return check_1.ValueCheck.Check(schema,
|
|
179
|
+
function Integer(schema, value) {
|
|
180
|
+
return check_1.ValueCheck.Check(schema, value) ? value : create_1.ValueCreate.Create(schema);
|
|
254
181
|
}
|
|
255
|
-
function
|
|
256
|
-
const
|
|
257
|
-
|
|
182
|
+
function Intersect(schema, value) {
|
|
183
|
+
const created = create_1.ValueCreate.Create(schema);
|
|
184
|
+
const mapped = IsObject(created) && IsObject(value) ? { ...created, ...value } : value;
|
|
185
|
+
return check_1.ValueCheck.Check(schema, mapped) ? mapped : create_1.ValueCreate.Create(schema);
|
|
258
186
|
}
|
|
259
|
-
function Literal(schema,
|
|
260
|
-
return check_1.ValueCheck.Check(schema,
|
|
187
|
+
function Literal(schema, value) {
|
|
188
|
+
return check_1.ValueCheck.Check(schema, value) ? value : create_1.ValueCreate.Create(schema);
|
|
261
189
|
}
|
|
262
|
-
function Never(schema,
|
|
190
|
+
function Never(schema, value) {
|
|
263
191
|
throw new ValueCastNeverTypeError(schema);
|
|
264
192
|
}
|
|
265
|
-
function
|
|
266
|
-
return check_1.ValueCheck.Check(schema,
|
|
193
|
+
function Not(schema, value) {
|
|
194
|
+
return check_1.ValueCheck.Check(schema, value) ? value : create_1.ValueCreate.Create(schema.allOf[1]);
|
|
267
195
|
}
|
|
268
|
-
function
|
|
269
|
-
|
|
270
|
-
return check_1.ValueCheck.Check(schema, references, conversion) ? conversion : create_1.ValueCreate.Create(schema, references);
|
|
196
|
+
function Null(schema, value) {
|
|
197
|
+
return check_1.ValueCheck.Check(schema, value) ? value : create_1.ValueCreate.Create(schema);
|
|
271
198
|
}
|
|
272
|
-
function
|
|
273
|
-
|
|
274
|
-
|
|
199
|
+
function Number(schema, value) {
|
|
200
|
+
return check_1.ValueCheck.Check(schema, value) ? value : create_1.ValueCreate.Create(schema);
|
|
201
|
+
}
|
|
202
|
+
function Object(schema, value) {
|
|
203
|
+
if (check_1.ValueCheck.Check(schema, value))
|
|
204
|
+
return value;
|
|
275
205
|
if (value === null || typeof value !== 'object')
|
|
276
|
-
return create_1.ValueCreate.Create(schema
|
|
206
|
+
return create_1.ValueCreate.Create(schema);
|
|
277
207
|
const required = new Set(schema.required || []);
|
|
278
208
|
const result = {};
|
|
279
209
|
for (const [key, property] of globalThis.Object.entries(schema.properties)) {
|
|
280
210
|
if (!required.has(key) && value[key] === undefined)
|
|
281
211
|
continue;
|
|
282
|
-
result[key] = Visit(property,
|
|
212
|
+
result[key] = Visit(property, value[key]);
|
|
283
213
|
}
|
|
284
214
|
// additional schema properties
|
|
285
215
|
if (typeof schema.additionalProperties === 'object') {
|
|
@@ -287,129 +217,132 @@ var ValueCast;
|
|
|
287
217
|
for (const propertyName of globalThis.Object.getOwnPropertyNames(value)) {
|
|
288
218
|
if (propertyNames.includes(propertyName))
|
|
289
219
|
continue;
|
|
290
|
-
result[propertyName] = Visit(schema.additionalProperties,
|
|
220
|
+
result[propertyName] = Visit(schema.additionalProperties, value[propertyName]);
|
|
291
221
|
}
|
|
292
222
|
}
|
|
293
223
|
return result;
|
|
294
224
|
}
|
|
295
|
-
function Promise(schema,
|
|
296
|
-
return check_1.ValueCheck.Check(schema,
|
|
225
|
+
function Promise(schema, value) {
|
|
226
|
+
return check_1.ValueCheck.Check(schema, value) ? value : create_1.ValueCreate.Create(schema);
|
|
297
227
|
}
|
|
298
|
-
function Record(schema,
|
|
299
|
-
if (check_1.ValueCheck.Check(schema,
|
|
228
|
+
function Record(schema, value) {
|
|
229
|
+
if (check_1.ValueCheck.Check(schema, value))
|
|
300
230
|
return clone_1.ValueClone.Clone(value);
|
|
301
231
|
if (value === null || typeof value !== 'object' || globalThis.Array.isArray(value) || value instanceof globalThis.Date)
|
|
302
|
-
return create_1.ValueCreate.Create(schema
|
|
232
|
+
return create_1.ValueCreate.Create(schema);
|
|
303
233
|
const subschemaPropertyName = globalThis.Object.getOwnPropertyNames(schema.patternProperties)[0];
|
|
304
234
|
const subschema = schema.patternProperties[subschemaPropertyName];
|
|
305
235
|
const result = {};
|
|
306
236
|
for (const [propKey, propValue] of globalThis.Object.entries(value)) {
|
|
307
|
-
result[propKey] = Visit(subschema,
|
|
237
|
+
result[propKey] = Visit(subschema, propValue);
|
|
308
238
|
}
|
|
309
239
|
return result;
|
|
310
240
|
}
|
|
311
|
-
function Ref(schema,
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
return Visit(
|
|
241
|
+
function Ref(schema, value) {
|
|
242
|
+
return Visit(Types.ReferenceRegistry.DerefOne(schema), value);
|
|
243
|
+
}
|
|
244
|
+
function Self(schema, value) {
|
|
245
|
+
return Visit(Types.ReferenceRegistry.DerefOne(schema), value);
|
|
316
246
|
}
|
|
317
|
-
function
|
|
318
|
-
|
|
319
|
-
if (reference === undefined)
|
|
320
|
-
throw new ValueCastReferenceTypeError(schema);
|
|
321
|
-
return Visit(reference, references, value);
|
|
247
|
+
function String(schema, value) {
|
|
248
|
+
return check_1.ValueCheck.Check(schema, value) ? value : create_1.ValueCreate.Create(schema);
|
|
322
249
|
}
|
|
323
|
-
function
|
|
324
|
-
|
|
325
|
-
return check_1.ValueCheck.Check(schema, references, conversion) ? conversion : create_1.ValueCreate.Create(schema, references);
|
|
250
|
+
function Symbol(schema, value) {
|
|
251
|
+
return check_1.ValueCheck.Check(schema, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema);
|
|
326
252
|
}
|
|
327
|
-
function Tuple(schema,
|
|
328
|
-
if (check_1.ValueCheck.Check(schema,
|
|
253
|
+
function Tuple(schema, value) {
|
|
254
|
+
if (check_1.ValueCheck.Check(schema, value))
|
|
329
255
|
return clone_1.ValueClone.Clone(value);
|
|
330
256
|
if (!globalThis.Array.isArray(value))
|
|
331
|
-
return create_1.ValueCreate.Create(schema
|
|
257
|
+
return create_1.ValueCreate.Create(schema);
|
|
332
258
|
if (schema.items === undefined)
|
|
333
259
|
return [];
|
|
334
|
-
return schema.items.map((schema, index) => Visit(schema,
|
|
260
|
+
return schema.items.map((schema, index) => Visit(schema, value[index]));
|
|
335
261
|
}
|
|
336
|
-
function Undefined(schema,
|
|
337
|
-
return check_1.ValueCheck.Check(schema,
|
|
262
|
+
function Undefined(schema, value) {
|
|
263
|
+
return check_1.ValueCheck.Check(schema, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema);
|
|
338
264
|
}
|
|
339
|
-
function Union(schema,
|
|
340
|
-
return check_1.ValueCheck.Check(schema,
|
|
265
|
+
function Union(schema, value) {
|
|
266
|
+
return check_1.ValueCheck.Check(schema, value) ? clone_1.ValueClone.Clone(value) : UnionCastCreate.Create(schema, value);
|
|
341
267
|
}
|
|
342
|
-
function Uint8Array(schema,
|
|
343
|
-
return check_1.ValueCheck.Check(schema,
|
|
268
|
+
function Uint8Array(schema, value) {
|
|
269
|
+
return check_1.ValueCheck.Check(schema, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema);
|
|
344
270
|
}
|
|
345
|
-
function Unknown(schema,
|
|
346
|
-
return check_1.ValueCheck.Check(schema,
|
|
271
|
+
function Unknown(schema, value) {
|
|
272
|
+
return check_1.ValueCheck.Check(schema, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema);
|
|
347
273
|
}
|
|
348
|
-
function Void(schema,
|
|
349
|
-
return check_1.ValueCheck.Check(schema,
|
|
274
|
+
function Void(schema, value) {
|
|
275
|
+
return check_1.ValueCheck.Check(schema, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema);
|
|
350
276
|
}
|
|
351
|
-
function UserDefined(schema,
|
|
352
|
-
return check_1.ValueCheck.Check(schema,
|
|
277
|
+
function UserDefined(schema, value) {
|
|
278
|
+
return check_1.ValueCheck.Check(schema, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema);
|
|
353
279
|
}
|
|
354
|
-
function Visit(schema,
|
|
355
|
-
const anyReferences = schema.$id === undefined ? references : [schema, ...references];
|
|
280
|
+
function Visit(schema, value) {
|
|
356
281
|
const anySchema = schema;
|
|
357
282
|
switch (schema[Types.Kind]) {
|
|
358
283
|
case 'Any':
|
|
359
|
-
return Any(anySchema,
|
|
284
|
+
return Any(anySchema, value);
|
|
360
285
|
case 'Array':
|
|
361
|
-
return Array(anySchema,
|
|
286
|
+
return Array(anySchema, value);
|
|
287
|
+
case 'BigInt':
|
|
288
|
+
return BigInt(anySchema, value);
|
|
362
289
|
case 'Boolean':
|
|
363
|
-
return Boolean(anySchema,
|
|
290
|
+
return Boolean(anySchema, value);
|
|
364
291
|
case 'Constructor':
|
|
365
|
-
return Constructor(anySchema,
|
|
292
|
+
return Constructor(anySchema, value);
|
|
366
293
|
case 'Date':
|
|
367
|
-
return Date(anySchema,
|
|
294
|
+
return Date(anySchema, value);
|
|
368
295
|
case 'Function':
|
|
369
|
-
return Function(anySchema,
|
|
296
|
+
return Function(anySchema, value);
|
|
370
297
|
case 'Integer':
|
|
371
|
-
return Integer(anySchema,
|
|
298
|
+
return Integer(anySchema, value);
|
|
299
|
+
case 'Intersect':
|
|
300
|
+
return Intersect(anySchema, value);
|
|
372
301
|
case 'Literal':
|
|
373
|
-
return Literal(anySchema,
|
|
302
|
+
return Literal(anySchema, value);
|
|
374
303
|
case 'Never':
|
|
375
|
-
return Never(anySchema,
|
|
304
|
+
return Never(anySchema, value);
|
|
305
|
+
case 'Not':
|
|
306
|
+
return Not(anySchema, value);
|
|
376
307
|
case 'Null':
|
|
377
|
-
return Null(anySchema,
|
|
308
|
+
return Null(anySchema, value);
|
|
378
309
|
case 'Number':
|
|
379
|
-
return Number(anySchema,
|
|
310
|
+
return Number(anySchema, value);
|
|
380
311
|
case 'Object':
|
|
381
|
-
return Object(anySchema,
|
|
312
|
+
return Object(anySchema, value);
|
|
382
313
|
case 'Promise':
|
|
383
|
-
return Promise(anySchema,
|
|
314
|
+
return Promise(anySchema, value);
|
|
384
315
|
case 'Record':
|
|
385
|
-
return Record(anySchema,
|
|
316
|
+
return Record(anySchema, value);
|
|
386
317
|
case 'Ref':
|
|
387
|
-
return Ref(anySchema,
|
|
318
|
+
return Ref(anySchema, value);
|
|
388
319
|
case 'Self':
|
|
389
|
-
return Self(anySchema,
|
|
320
|
+
return Self(anySchema, value);
|
|
390
321
|
case 'String':
|
|
391
|
-
return String(anySchema,
|
|
322
|
+
return String(anySchema, value);
|
|
323
|
+
case 'Symbol':
|
|
324
|
+
return Symbol(anySchema, value);
|
|
392
325
|
case 'Tuple':
|
|
393
|
-
return Tuple(anySchema,
|
|
326
|
+
return Tuple(anySchema, value);
|
|
394
327
|
case 'Undefined':
|
|
395
|
-
return Undefined(anySchema,
|
|
328
|
+
return Undefined(anySchema, value);
|
|
396
329
|
case 'Union':
|
|
397
|
-
return Union(anySchema,
|
|
330
|
+
return Union(anySchema, value);
|
|
398
331
|
case 'Uint8Array':
|
|
399
|
-
return Uint8Array(anySchema,
|
|
332
|
+
return Uint8Array(anySchema, value);
|
|
400
333
|
case 'Unknown':
|
|
401
|
-
return Unknown(anySchema,
|
|
334
|
+
return Unknown(anySchema, value);
|
|
402
335
|
case 'Void':
|
|
403
|
-
return Void(anySchema,
|
|
336
|
+
return Void(anySchema, value);
|
|
404
337
|
default:
|
|
405
|
-
if (!
|
|
338
|
+
if (!Types.TypeRegistry.Has(anySchema[Types.Kind]))
|
|
406
339
|
throw new ValueCastUnknownTypeError(anySchema);
|
|
407
|
-
return UserDefined(anySchema,
|
|
340
|
+
return UserDefined(anySchema, value);
|
|
408
341
|
}
|
|
409
342
|
}
|
|
410
343
|
ValueCast.Visit = Visit;
|
|
411
|
-
function Cast(schema,
|
|
412
|
-
return
|
|
344
|
+
function Cast(schema, value) {
|
|
345
|
+
return Visit(schema, clone_1.ValueClone.Clone(value));
|
|
413
346
|
}
|
|
414
347
|
ValueCast.Cast = Cast;
|
|
415
348
|
})(ValueCast = exports.ValueCast || (exports.ValueCast = {}));
|
package/value/check.d.ts
CHANGED
|
@@ -4,5 +4,5 @@ export declare class ValueCheckUnknownTypeError extends Error {
|
|
|
4
4
|
constructor(schema: Types.TSchema);
|
|
5
5
|
}
|
|
6
6
|
export declare namespace ValueCheck {
|
|
7
|
-
function Check<T extends Types.TSchema
|
|
7
|
+
function Check<T extends Types.TSchema>(schema: T, value: any): boolean;
|
|
8
8
|
}
|