@sinclair/typebox 0.33.15 → 0.33.16
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.
|
@@ -11,13 +11,14 @@ const function_1 = require("./function");
|
|
|
11
11
|
const index_4 = require("../type/error/index");
|
|
12
12
|
const index_5 = require("../value/deref/index");
|
|
13
13
|
const index_6 = require("../value/hash/index");
|
|
14
|
-
const index_7 = require("../
|
|
15
|
-
const index_8 = require("../type/
|
|
14
|
+
const index_7 = require("../value/check/index");
|
|
15
|
+
const index_8 = require("../type/symbols/index");
|
|
16
|
+
const index_9 = require("../type/never/index");
|
|
16
17
|
// ------------------------------------------------------------------
|
|
17
18
|
// ValueGuard
|
|
18
19
|
// ------------------------------------------------------------------
|
|
19
20
|
// prettier-ignore
|
|
20
|
-
const
|
|
21
|
+
const index_10 = require("../value/guard/index");
|
|
21
22
|
// ------------------------------------------------------------------
|
|
22
23
|
// ValueErrorType
|
|
23
24
|
// ------------------------------------------------------------------
|
|
@@ -130,15 +131,22 @@ exports.ValueErrorIterator = ValueErrorIterator;
|
|
|
130
131
|
// --------------------------------------------------------------------------
|
|
131
132
|
// Create
|
|
132
133
|
// --------------------------------------------------------------------------
|
|
133
|
-
function Create(errorType, schema, path, value) {
|
|
134
|
-
return {
|
|
134
|
+
function Create(errorType, schema, path, value, errors = []) {
|
|
135
|
+
return {
|
|
136
|
+
type: errorType,
|
|
137
|
+
schema,
|
|
138
|
+
path,
|
|
139
|
+
value,
|
|
140
|
+
message: (0, function_1.GetErrorFunction)()({ errorType, path, schema, value, errors }),
|
|
141
|
+
errors,
|
|
142
|
+
};
|
|
135
143
|
}
|
|
136
144
|
// --------------------------------------------------------------------------
|
|
137
145
|
// Types
|
|
138
146
|
// --------------------------------------------------------------------------
|
|
139
147
|
function* FromAny(schema, references, path, value) { }
|
|
140
148
|
function* FromArray(schema, references, path, value) {
|
|
141
|
-
if (!(0,
|
|
149
|
+
if (!(0, index_10.IsArray)(value)) {
|
|
142
150
|
return yield Create(ValueErrorType.Array, schema, path, value);
|
|
143
151
|
}
|
|
144
152
|
if (IsDefined(schema.minItems) && !(value.length >= schema.minItems)) {
|
|
@@ -166,24 +174,24 @@ function* FromArray(schema, references, path, value) {
|
|
|
166
174
|
if (!(IsDefined(schema.contains) || IsDefined(schema.minContains) || IsDefined(schema.maxContains))) {
|
|
167
175
|
return;
|
|
168
176
|
}
|
|
169
|
-
const containsSchema = IsDefined(schema.contains) ? schema.contains : (0,
|
|
177
|
+
const containsSchema = IsDefined(schema.contains) ? schema.contains : (0, index_9.Never)();
|
|
170
178
|
const containsCount = value.reduce((acc, value, index) => (Visit(containsSchema, references, `${path}${index}`, value).next().done === true ? acc + 1 : acc), 0);
|
|
171
179
|
if (containsCount === 0) {
|
|
172
180
|
yield Create(ValueErrorType.ArrayContains, schema, path, value);
|
|
173
181
|
}
|
|
174
|
-
if ((0,
|
|
182
|
+
if ((0, index_10.IsNumber)(schema.minContains) && containsCount < schema.minContains) {
|
|
175
183
|
yield Create(ValueErrorType.ArrayMinContains, schema, path, value);
|
|
176
184
|
}
|
|
177
|
-
if ((0,
|
|
185
|
+
if ((0, index_10.IsNumber)(schema.maxContains) && containsCount > schema.maxContains) {
|
|
178
186
|
yield Create(ValueErrorType.ArrayMaxContains, schema, path, value);
|
|
179
187
|
}
|
|
180
188
|
}
|
|
181
189
|
function* FromAsyncIterator(schema, references, path, value) {
|
|
182
|
-
if (!(0,
|
|
190
|
+
if (!(0, index_10.IsAsyncIterator)(value))
|
|
183
191
|
yield Create(ValueErrorType.AsyncIterator, schema, path, value);
|
|
184
192
|
}
|
|
185
193
|
function* FromBigInt(schema, references, path, value) {
|
|
186
|
-
if (!(0,
|
|
194
|
+
if (!(0, index_10.IsBigInt)(value))
|
|
187
195
|
return yield Create(ValueErrorType.BigInt, schema, path, value);
|
|
188
196
|
if (IsDefined(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
|
|
189
197
|
yield Create(ValueErrorType.BigIntExclusiveMaximum, schema, path, value);
|
|
@@ -202,14 +210,14 @@ function* FromBigInt(schema, references, path, value) {
|
|
|
202
210
|
}
|
|
203
211
|
}
|
|
204
212
|
function* FromBoolean(schema, references, path, value) {
|
|
205
|
-
if (!(0,
|
|
213
|
+
if (!(0, index_10.IsBoolean)(value))
|
|
206
214
|
yield Create(ValueErrorType.Boolean, schema, path, value);
|
|
207
215
|
}
|
|
208
216
|
function* FromConstructor(schema, references, path, value) {
|
|
209
217
|
yield* Visit(schema.returns, references, path, value.prototype);
|
|
210
218
|
}
|
|
211
219
|
function* FromDate(schema, references, path, value) {
|
|
212
|
-
if (!(0,
|
|
220
|
+
if (!(0, index_10.IsDate)(value))
|
|
213
221
|
return yield Create(ValueErrorType.Date, schema, path, value);
|
|
214
222
|
if (IsDefined(schema.exclusiveMaximumTimestamp) && !(value.getTime() < schema.exclusiveMaximumTimestamp)) {
|
|
215
223
|
yield Create(ValueErrorType.DateExclusiveMaximumTimestamp, schema, path, value);
|
|
@@ -228,11 +236,11 @@ function* FromDate(schema, references, path, value) {
|
|
|
228
236
|
}
|
|
229
237
|
}
|
|
230
238
|
function* FromFunction(schema, references, path, value) {
|
|
231
|
-
if (!(0,
|
|
239
|
+
if (!(0, index_10.IsFunction)(value))
|
|
232
240
|
yield Create(ValueErrorType.Function, schema, path, value);
|
|
233
241
|
}
|
|
234
242
|
function* FromInteger(schema, references, path, value) {
|
|
235
|
-
if (!(0,
|
|
243
|
+
if (!(0, index_10.IsInteger)(value))
|
|
236
244
|
return yield Create(ValueErrorType.Integer, schema, path, value);
|
|
237
245
|
if (IsDefined(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
|
|
238
246
|
yield Create(ValueErrorType.IntegerExclusiveMaximum, schema, path, value);
|
|
@@ -281,7 +289,7 @@ function* FromIntersect(schema, references, path, value) {
|
|
|
281
289
|
}
|
|
282
290
|
}
|
|
283
291
|
function* FromIterator(schema, references, path, value) {
|
|
284
|
-
if (!(0,
|
|
292
|
+
if (!(0, index_10.IsIterator)(value))
|
|
285
293
|
yield Create(ValueErrorType.Iterator, schema, path, value);
|
|
286
294
|
}
|
|
287
295
|
function* FromLiteral(schema, references, path, value) {
|
|
@@ -296,7 +304,7 @@ function* FromNot(schema, references, path, value) {
|
|
|
296
304
|
yield Create(ValueErrorType.Not, schema, path, value);
|
|
297
305
|
}
|
|
298
306
|
function* FromNull(schema, references, path, value) {
|
|
299
|
-
if (!(0,
|
|
307
|
+
if (!(0, index_10.IsNull)(value))
|
|
300
308
|
yield Create(ValueErrorType.Null, schema, path, value);
|
|
301
309
|
}
|
|
302
310
|
function* FromNumber(schema, references, path, value) {
|
|
@@ -365,7 +373,7 @@ function* FromObject(schema, references, path, value) {
|
|
|
365
373
|
}
|
|
366
374
|
}
|
|
367
375
|
function* FromPromise(schema, references, path, value) {
|
|
368
|
-
if (!(0,
|
|
376
|
+
if (!(0, index_10.IsPromise)(value))
|
|
369
377
|
yield Create(ValueErrorType.Promise, schema, path, value);
|
|
370
378
|
}
|
|
371
379
|
function* FromRecord(schema, references, path, value) {
|
|
@@ -401,7 +409,7 @@ function* FromRef(schema, references, path, value) {
|
|
|
401
409
|
yield* Visit((0, index_5.Deref)(schema, references), references, path, value);
|
|
402
410
|
}
|
|
403
411
|
function* FromRegExp(schema, references, path, value) {
|
|
404
|
-
if (!(0,
|
|
412
|
+
if (!(0, index_10.IsString)(value))
|
|
405
413
|
return yield Create(ValueErrorType.String, schema, path, value);
|
|
406
414
|
if (IsDefined(schema.minLength) && !(value.length >= schema.minLength)) {
|
|
407
415
|
yield Create(ValueErrorType.StringMinLength, schema, path, value);
|
|
@@ -415,7 +423,7 @@ function* FromRegExp(schema, references, path, value) {
|
|
|
415
423
|
}
|
|
416
424
|
}
|
|
417
425
|
function* FromString(schema, references, path, value) {
|
|
418
|
-
if (!(0,
|
|
426
|
+
if (!(0, index_10.IsString)(value))
|
|
419
427
|
return yield Create(ValueErrorType.String, schema, path, value);
|
|
420
428
|
if (IsDefined(schema.minLength) && !(value.length >= schema.minLength)) {
|
|
421
429
|
yield Create(ValueErrorType.StringMinLength, schema, path, value);
|
|
@@ -423,13 +431,13 @@ function* FromString(schema, references, path, value) {
|
|
|
423
431
|
if (IsDefined(schema.maxLength) && !(value.length <= schema.maxLength)) {
|
|
424
432
|
yield Create(ValueErrorType.StringMaxLength, schema, path, value);
|
|
425
433
|
}
|
|
426
|
-
if ((0,
|
|
434
|
+
if ((0, index_10.IsString)(schema.pattern)) {
|
|
427
435
|
const regex = new RegExp(schema.pattern);
|
|
428
436
|
if (!regex.test(value)) {
|
|
429
437
|
yield Create(ValueErrorType.StringPattern, schema, path, value);
|
|
430
438
|
}
|
|
431
439
|
}
|
|
432
|
-
if ((0,
|
|
440
|
+
if ((0, index_10.IsString)(schema.format)) {
|
|
433
441
|
if (!index_3.FormatRegistry.Has(schema.format)) {
|
|
434
442
|
yield Create(ValueErrorType.StringFormatUnknown, schema, path, value);
|
|
435
443
|
}
|
|
@@ -442,11 +450,11 @@ function* FromString(schema, references, path, value) {
|
|
|
442
450
|
}
|
|
443
451
|
}
|
|
444
452
|
function* FromSymbol(schema, references, path, value) {
|
|
445
|
-
if (!(0,
|
|
453
|
+
if (!(0, index_10.IsSymbol)(value))
|
|
446
454
|
yield Create(ValueErrorType.Symbol, schema, path, value);
|
|
447
455
|
}
|
|
448
456
|
function* FromTemplateLiteral(schema, references, path, value) {
|
|
449
|
-
if (!(0,
|
|
457
|
+
if (!(0, index_10.IsString)(value))
|
|
450
458
|
return yield Create(ValueErrorType.String, schema, path, value);
|
|
451
459
|
const regex = new RegExp(schema.pattern);
|
|
452
460
|
if (!regex.test(value)) {
|
|
@@ -457,7 +465,7 @@ function* FromThis(schema, references, path, value) {
|
|
|
457
465
|
yield* Visit((0, index_5.Deref)(schema, references), references, path, value);
|
|
458
466
|
}
|
|
459
467
|
function* FromTuple(schema, references, path, value) {
|
|
460
|
-
if (!(0,
|
|
468
|
+
if (!(0, index_10.IsArray)(value))
|
|
461
469
|
return yield Create(ValueErrorType.Tuple, schema, path, value);
|
|
462
470
|
if (schema.items === undefined && !(value.length === 0)) {
|
|
463
471
|
return yield Create(ValueErrorType.TupleLength, schema, path, value);
|
|
@@ -473,23 +481,17 @@ function* FromTuple(schema, references, path, value) {
|
|
|
473
481
|
}
|
|
474
482
|
}
|
|
475
483
|
function* FromUndefined(schema, references, path, value) {
|
|
476
|
-
if (!(0,
|
|
484
|
+
if (!(0, index_10.IsUndefined)(value))
|
|
477
485
|
yield Create(ValueErrorType.Undefined, schema, path, value);
|
|
478
486
|
}
|
|
479
487
|
function* FromUnion(schema, references, path, value) {
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
return; // matched
|
|
485
|
-
count += errors.length;
|
|
486
|
-
}
|
|
487
|
-
if (count > 0) {
|
|
488
|
-
yield Create(ValueErrorType.Union, schema, path, value);
|
|
489
|
-
}
|
|
488
|
+
if ((0, index_7.Check)(schema, references, value))
|
|
489
|
+
return;
|
|
490
|
+
const errors = schema.anyOf.map((variant) => new ValueErrorIterator(Visit(variant, references, path, value)));
|
|
491
|
+
yield Create(ValueErrorType.Union, schema, path, value, errors);
|
|
490
492
|
}
|
|
491
493
|
function* FromUint8Array(schema, references, path, value) {
|
|
492
|
-
if (!(0,
|
|
494
|
+
if (!(0, index_10.IsUint8Array)(value))
|
|
493
495
|
return yield Create(ValueErrorType.Uint8Array, schema, path, value);
|
|
494
496
|
if (IsDefined(schema.maxByteLength) && !(value.length <= schema.maxByteLength)) {
|
|
495
497
|
yield Create(ValueErrorType.Uint8ArrayMaxByteLength, schema, path, value);
|
|
@@ -504,14 +506,14 @@ function* FromVoid(schema, references, path, value) {
|
|
|
504
506
|
yield Create(ValueErrorType.Void, schema, path, value);
|
|
505
507
|
}
|
|
506
508
|
function* FromKind(schema, references, path, value) {
|
|
507
|
-
const check = index_3.TypeRegistry.Get(schema[
|
|
509
|
+
const check = index_3.TypeRegistry.Get(schema[index_8.Kind]);
|
|
508
510
|
if (!check(schema, value))
|
|
509
511
|
yield Create(ValueErrorType.Kind, schema, path, value);
|
|
510
512
|
}
|
|
511
513
|
function* Visit(schema, references, path, value) {
|
|
512
514
|
const references_ = IsDefined(schema.$id) ? [...references, schema] : references;
|
|
513
515
|
const schema_ = schema;
|
|
514
|
-
switch (schema_[
|
|
516
|
+
switch (schema_[index_8.Kind]) {
|
|
515
517
|
case 'Any':
|
|
516
518
|
return yield* FromAny(schema_, references_, path, value);
|
|
517
519
|
case 'Array':
|
|
@@ -575,7 +577,7 @@ function* Visit(schema, references, path, value) {
|
|
|
575
577
|
case 'Void':
|
|
576
578
|
return yield* FromVoid(schema_, references_, path, value);
|
|
577
579
|
default:
|
|
578
|
-
if (!index_3.TypeRegistry.Has(schema_[
|
|
580
|
+
if (!index_3.TypeRegistry.Has(schema_[index_8.Kind]))
|
|
579
581
|
throw new ValueErrorsUnknownTypeError(schema);
|
|
580
582
|
return yield* FromKind(schema_, references_, path, value);
|
|
581
583
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { TSchema } from '../type/schema/index';
|
|
2
|
-
import { ValueErrorType } from './errors';
|
|
2
|
+
import { ValueErrorIterator, ValueErrorType } from './errors';
|
|
3
3
|
/** Creates an error message using en-US as the default locale */
|
|
4
4
|
export declare function DefaultErrorFunction(error: ErrorFunctionParameter): string;
|
|
5
5
|
export type ErrorFunctionParameter = {
|
|
@@ -11,6 +11,8 @@ export type ErrorFunctionParameter = {
|
|
|
11
11
|
schema: TSchema;
|
|
12
12
|
/** The value associated with the error */
|
|
13
13
|
value: unknown;
|
|
14
|
+
/** Interior errors for this error */
|
|
15
|
+
errors: ValueErrorIterator[];
|
|
14
16
|
};
|
|
15
17
|
export type ErrorFunction = (parameter: ErrorFunctionParameter) => string;
|
|
16
18
|
/** Sets the error function used to generate error messages. */
|
|
@@ -6,6 +6,7 @@ import { GetErrorFunction } from './function.mjs';
|
|
|
6
6
|
import { TypeBoxError } from '../type/error/index.mjs';
|
|
7
7
|
import { Deref } from '../value/deref/index.mjs';
|
|
8
8
|
import { Hash } from '../value/hash/index.mjs';
|
|
9
|
+
import { Check } from '../value/check/index.mjs';
|
|
9
10
|
import { Kind } from '../type/symbols/index.mjs';
|
|
10
11
|
import { Never } from '../type/never/index.mjs';
|
|
11
12
|
// ------------------------------------------------------------------
|
|
@@ -123,8 +124,15 @@ export class ValueErrorIterator {
|
|
|
123
124
|
// --------------------------------------------------------------------------
|
|
124
125
|
// Create
|
|
125
126
|
// --------------------------------------------------------------------------
|
|
126
|
-
function Create(errorType, schema, path, value) {
|
|
127
|
-
return {
|
|
127
|
+
function Create(errorType, schema, path, value, errors = []) {
|
|
128
|
+
return {
|
|
129
|
+
type: errorType,
|
|
130
|
+
schema,
|
|
131
|
+
path,
|
|
132
|
+
value,
|
|
133
|
+
message: GetErrorFunction()({ errorType, path, schema, value, errors }),
|
|
134
|
+
errors,
|
|
135
|
+
};
|
|
128
136
|
}
|
|
129
137
|
// --------------------------------------------------------------------------
|
|
130
138
|
// Types
|
|
@@ -470,16 +478,10 @@ function* FromUndefined(schema, references, path, value) {
|
|
|
470
478
|
yield Create(ValueErrorType.Undefined, schema, path, value);
|
|
471
479
|
}
|
|
472
480
|
function* FromUnion(schema, references, path, value) {
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
return; // matched
|
|
478
|
-
count += errors.length;
|
|
479
|
-
}
|
|
480
|
-
if (count > 0) {
|
|
481
|
-
yield Create(ValueErrorType.Union, schema, path, value);
|
|
482
|
-
}
|
|
481
|
+
if (Check(schema, references, value))
|
|
482
|
+
return;
|
|
483
|
+
const errors = schema.anyOf.map((variant) => new ValueErrorIterator(Visit(variant, references, path, value)));
|
|
484
|
+
yield Create(ValueErrorType.Union, schema, path, value, errors);
|
|
483
485
|
}
|
|
484
486
|
function* FromUint8Array(schema, references, path, value) {
|
|
485
487
|
if (!IsUint8Array(value))
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { TSchema } from '../type/schema/index.mjs';
|
|
2
|
-
import { ValueErrorType } from './errors.mjs';
|
|
2
|
+
import { ValueErrorIterator, ValueErrorType } from './errors.mjs';
|
|
3
3
|
/** Creates an error message using en-US as the default locale */
|
|
4
4
|
export declare function DefaultErrorFunction(error: ErrorFunctionParameter): string;
|
|
5
5
|
export type ErrorFunctionParameter = {
|
|
@@ -11,6 +11,8 @@ export type ErrorFunctionParameter = {
|
|
|
11
11
|
schema: TSchema;
|
|
12
12
|
/** The value associated with the error */
|
|
13
13
|
value: unknown;
|
|
14
|
+
/** Interior errors for this error */
|
|
15
|
+
errors: ValueErrorIterator[];
|
|
14
16
|
};
|
|
15
17
|
export type ErrorFunction = (parameter: ErrorFunctionParameter) => string;
|
|
16
18
|
/** Sets the error function used to generate error messages. */
|