@sinclair/typebox 0.25.23 → 0.25.25

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.
Files changed (61) hide show
  1. package/compiler/compiler.d.ts +25 -28
  2. package/compiler/compiler.js +495 -495
  3. package/compiler/index.d.ts +2 -2
  4. package/compiler/index.js +47 -47
  5. package/conditional/conditional.d.ts +17 -17
  6. package/conditional/conditional.js +91 -91
  7. package/conditional/index.d.ts +2 -2
  8. package/conditional/index.js +45 -45
  9. package/conditional/structural.d.ts +11 -11
  10. package/conditional/structural.js +685 -685
  11. package/custom/custom.d.ts +12 -12
  12. package/custom/custom.js +55 -55
  13. package/custom/index.d.ts +1 -1
  14. package/custom/index.js +44 -44
  15. package/errors/errors.d.ts +67 -67
  16. package/errors/errors.js +472 -468
  17. package/errors/index.d.ts +1 -1
  18. package/errors/index.js +44 -44
  19. package/format/format.d.ts +12 -12
  20. package/format/format.js +55 -55
  21. package/format/index.d.ts +1 -1
  22. package/format/index.js +44 -44
  23. package/guard/extends.d.ts +10 -0
  24. package/guard/extends.js +50 -0
  25. package/guard/guard.d.ts +60 -60
  26. package/guard/guard.js +440 -440
  27. package/guard/index.d.ts +2 -1
  28. package/guard/index.js +45 -44
  29. package/hash/hash.d.ts +8 -8
  30. package/hash/hash.js +183 -183
  31. package/hash/index.d.ts +1 -1
  32. package/hash/index.js +44 -44
  33. package/license +22 -22
  34. package/package.json +55 -52
  35. package/readme.md +1237 -1325
  36. package/system/index.d.ts +1 -1
  37. package/system/index.js +44 -44
  38. package/system/system.d.ts +17 -17
  39. package/system/system.js +69 -69
  40. package/typebox.d.ts +422 -422
  41. package/typebox.js +388 -388
  42. package/value/cast.d.ts +26 -26
  43. package/value/cast.js +415 -420
  44. package/value/check.d.ts +8 -8
  45. package/value/check.js +405 -395
  46. package/value/clone.d.ts +3 -3
  47. package/value/clone.js +71 -71
  48. package/value/create.d.ts +14 -14
  49. package/value/create.js +378 -388
  50. package/value/delta.d.ts +43 -43
  51. package/value/delta.js +204 -204
  52. package/value/equal.d.ts +3 -3
  53. package/value/equal.js +80 -80
  54. package/value/index.d.ts +4 -4
  55. package/value/index.js +53 -53
  56. package/value/is.d.ts +11 -11
  57. package/value/is.js +53 -53
  58. package/value/pointer.d.ts +24 -24
  59. package/value/pointer.js +142 -142
  60. package/value/value.d.ts +32 -32
  61. package/value/value.js +87 -87
package/errors/errors.js CHANGED
@@ -1,468 +1,472 @@
1
- "use strict";
2
- /*--------------------------------------------------------------------------
3
-
4
- @sinclair/typebox/errors
5
-
6
- The MIT License (MIT)
7
-
8
- Copyright (c) 2017-2023 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
9
-
10
- Permission is hereby granted, free of charge, to any person obtaining a copy
11
- of this software and associated documentation files (the "Software"), to deal
12
- in the Software without restriction, including without limitation the rights
13
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
- copies of the Software, and to permit persons to whom the Software is
15
- furnished to do so, subject to the following conditions:
16
-
17
- The above copyright notice and this permission notice shall be included in
18
- all copies or substantial portions of the Software.
19
-
20
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26
- THE SOFTWARE.
27
-
28
- ---------------------------------------------------------------------------*/
29
- Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.ValueErrors = exports.ValueErrorsUnknownTypeError = exports.ValueErrorType = void 0;
31
- const Types = require("../typebox");
32
- const index_1 = require("../system/index");
33
- const index_2 = require("../format/index");
34
- const index_3 = require("../custom/index");
35
- const index_4 = require("../hash/index");
36
- // -------------------------------------------------------------------
37
- // ValueErrorType
38
- // -------------------------------------------------------------------
39
- var ValueErrorType;
40
- (function (ValueErrorType) {
41
- ValueErrorType[ValueErrorType["Array"] = 0] = "Array";
42
- ValueErrorType[ValueErrorType["ArrayMinItems"] = 1] = "ArrayMinItems";
43
- ValueErrorType[ValueErrorType["ArrayMaxItems"] = 2] = "ArrayMaxItems";
44
- ValueErrorType[ValueErrorType["ArrayUniqueItems"] = 3] = "ArrayUniqueItems";
45
- ValueErrorType[ValueErrorType["Boolean"] = 4] = "Boolean";
46
- ValueErrorType[ValueErrorType["Date"] = 5] = "Date";
47
- ValueErrorType[ValueErrorType["DateExclusiveMinimumTimestamp"] = 6] = "DateExclusiveMinimumTimestamp";
48
- ValueErrorType[ValueErrorType["DateExclusiveMaximumTimestamp"] = 7] = "DateExclusiveMaximumTimestamp";
49
- ValueErrorType[ValueErrorType["DateMinimumTimestamp"] = 8] = "DateMinimumTimestamp";
50
- ValueErrorType[ValueErrorType["DateMaximumTimestamp"] = 9] = "DateMaximumTimestamp";
51
- ValueErrorType[ValueErrorType["Function"] = 10] = "Function";
52
- ValueErrorType[ValueErrorType["Integer"] = 11] = "Integer";
53
- ValueErrorType[ValueErrorType["IntegerMultipleOf"] = 12] = "IntegerMultipleOf";
54
- ValueErrorType[ValueErrorType["IntegerExclusiveMinimum"] = 13] = "IntegerExclusiveMinimum";
55
- ValueErrorType[ValueErrorType["IntegerExclusiveMaximum"] = 14] = "IntegerExclusiveMaximum";
56
- ValueErrorType[ValueErrorType["IntegerMinimum"] = 15] = "IntegerMinimum";
57
- ValueErrorType[ValueErrorType["IntegerMaximum"] = 16] = "IntegerMaximum";
58
- ValueErrorType[ValueErrorType["Literal"] = 17] = "Literal";
59
- ValueErrorType[ValueErrorType["Never"] = 18] = "Never";
60
- ValueErrorType[ValueErrorType["Null"] = 19] = "Null";
61
- ValueErrorType[ValueErrorType["Number"] = 20] = "Number";
62
- ValueErrorType[ValueErrorType["NumberMultipleOf"] = 21] = "NumberMultipleOf";
63
- ValueErrorType[ValueErrorType["NumberExclusiveMinimum"] = 22] = "NumberExclusiveMinimum";
64
- ValueErrorType[ValueErrorType["NumberExclusiveMaximum"] = 23] = "NumberExclusiveMaximum";
65
- ValueErrorType[ValueErrorType["NumberMinumum"] = 24] = "NumberMinumum";
66
- ValueErrorType[ValueErrorType["NumberMaximum"] = 25] = "NumberMaximum";
67
- ValueErrorType[ValueErrorType["Object"] = 26] = "Object";
68
- ValueErrorType[ValueErrorType["ObjectMinProperties"] = 27] = "ObjectMinProperties";
69
- ValueErrorType[ValueErrorType["ObjectMaxProperties"] = 28] = "ObjectMaxProperties";
70
- ValueErrorType[ValueErrorType["ObjectAdditionalProperties"] = 29] = "ObjectAdditionalProperties";
71
- ValueErrorType[ValueErrorType["ObjectRequiredProperties"] = 30] = "ObjectRequiredProperties";
72
- ValueErrorType[ValueErrorType["Promise"] = 31] = "Promise";
73
- ValueErrorType[ValueErrorType["RecordKeyNumeric"] = 32] = "RecordKeyNumeric";
74
- ValueErrorType[ValueErrorType["RecordKeyString"] = 33] = "RecordKeyString";
75
- ValueErrorType[ValueErrorType["String"] = 34] = "String";
76
- ValueErrorType[ValueErrorType["StringMinLength"] = 35] = "StringMinLength";
77
- ValueErrorType[ValueErrorType["StringMaxLength"] = 36] = "StringMaxLength";
78
- ValueErrorType[ValueErrorType["StringPattern"] = 37] = "StringPattern";
79
- ValueErrorType[ValueErrorType["StringFormatUnknown"] = 38] = "StringFormatUnknown";
80
- ValueErrorType[ValueErrorType["StringFormat"] = 39] = "StringFormat";
81
- ValueErrorType[ValueErrorType["TupleZeroLength"] = 40] = "TupleZeroLength";
82
- ValueErrorType[ValueErrorType["TupleLength"] = 41] = "TupleLength";
83
- ValueErrorType[ValueErrorType["Undefined"] = 42] = "Undefined";
84
- ValueErrorType[ValueErrorType["Union"] = 43] = "Union";
85
- ValueErrorType[ValueErrorType["Uint8Array"] = 44] = "Uint8Array";
86
- ValueErrorType[ValueErrorType["Uint8ArrayMinByteLength"] = 45] = "Uint8ArrayMinByteLength";
87
- ValueErrorType[ValueErrorType["Uint8ArrayMaxByteLength"] = 46] = "Uint8ArrayMaxByteLength";
88
- ValueErrorType[ValueErrorType["Void"] = 47] = "Void";
89
- ValueErrorType[ValueErrorType["Custom"] = 48] = "Custom";
90
- })(ValueErrorType = exports.ValueErrorType || (exports.ValueErrorType = {}));
91
- // -------------------------------------------------------------------
92
- // ValueErrors
93
- // -------------------------------------------------------------------
94
- class ValueErrorsUnknownTypeError extends Error {
95
- constructor(schema) {
96
- super('ValueErrors: Unknown type');
97
- this.schema = schema;
98
- }
99
- }
100
- exports.ValueErrorsUnknownTypeError = ValueErrorsUnknownTypeError;
101
- /** Provides functionality to generate a sequence of errors against a TypeBox type. */
102
- var ValueErrors;
103
- (function (ValueErrors) {
104
- function IsNumber(value) {
105
- return typeof value === 'number' && !isNaN(value);
106
- }
107
- function* Any(schema, references, path, value) { }
108
- function* Array(schema, references, path, value) {
109
- if (!globalThis.Array.isArray(value)) {
110
- return yield { type: ValueErrorType.Array, schema, path, value, message: `Expected array` };
111
- }
112
- if (IsNumber(schema.minItems) && !(value.length >= schema.minItems)) {
113
- yield { type: ValueErrorType.ArrayMinItems, schema, path, value, message: `Expected array length to be greater or equal to ${schema.minItems}` };
114
- }
115
- if (IsNumber(schema.maxItems) && !(value.length <= schema.maxItems)) {
116
- yield { type: ValueErrorType.ArrayMinItems, schema, path, value, message: `Expected array length to be less or equal to ${schema.maxItems}` };
117
- }
118
- // prettier-ignore
119
- if (schema.uniqueItems === true && !((function () { const set = new Set(); for (const element of value) {
120
- const hashed = index_4.ValueHash.Create(element);
121
- if (set.has(hashed)) {
122
- return false;
123
- }
124
- else {
125
- set.add(hashed);
126
- }
127
- } return true; })())) {
128
- yield { type: ValueErrorType.ArrayUniqueItems, schema, path, value, message: `Expected array elements to be unique` };
129
- }
130
- for (let i = 0; i < value.length; i++) {
131
- yield* Visit(schema.items, references, `${path}/${i}`, value[i]);
132
- }
133
- }
134
- function* Boolean(schema, references, path, value) {
135
- if (!(typeof value === 'boolean')) {
136
- return yield { type: ValueErrorType.Boolean, schema, path, value, message: `Expected boolean` };
137
- }
138
- }
139
- function* Constructor(schema, references, path, value) {
140
- yield* Visit(schema.returns, references, path, value.prototype);
141
- }
142
- function* Date(schema, references, path, value) {
143
- if (!(value instanceof globalThis.Date)) {
144
- return yield { type: ValueErrorType.Date, schema, path, value, message: `Expected Date object` };
145
- }
146
- if (isNaN(value.getTime())) {
147
- return yield { type: ValueErrorType.Date, schema, path, value, message: `Invalid Date` };
148
- }
149
- if (IsNumber(schema.exclusiveMinimumTimestamp) && !(value.getTime() > schema.exclusiveMinimumTimestamp)) {
150
- yield { type: ValueErrorType.DateExclusiveMinimumTimestamp, schema, path, value, message: `Expected Date timestamp to be greater than ${schema.exclusiveMinimum}` };
151
- }
152
- if (IsNumber(schema.exclusiveMaximumTimestamp) && !(value.getTime() < schema.exclusiveMaximumTimestamp)) {
153
- yield { type: ValueErrorType.DateExclusiveMaximumTimestamp, schema, path, value, message: `Expected Date timestamp to be less than ${schema.exclusiveMaximum}` };
154
- }
155
- if (IsNumber(schema.minimumTimestamp) && !(value.getTime() >= schema.minimumTimestamp)) {
156
- yield { type: ValueErrorType.DateMinimumTimestamp, schema, path, value, message: `Expected Date timestamp to be greater or equal to ${schema.minimum}` };
157
- }
158
- if (IsNumber(schema.maximumTimestamp) && !(value.getTime() <= schema.maximumTimestamp)) {
159
- yield { type: ValueErrorType.DateMaximumTimestamp, schema, path, value, message: `Expected Date timestamp to be less or equal to ${schema.maximum}` };
160
- }
161
- }
162
- function* Function(schema, references, path, value) {
163
- if (!(typeof value === 'function')) {
164
- return yield { type: ValueErrorType.Function, schema, path, value, message: `Expected function` };
165
- }
166
- }
167
- function* Integer(schema, references, path, value) {
168
- if (!(typeof value === 'number' && globalThis.Number.isInteger(value))) {
169
- return yield { type: ValueErrorType.Integer, schema, path, value, message: `Expected integer` };
170
- }
171
- if (IsNumber(schema.multipleOf) && !(value % schema.multipleOf === 0)) {
172
- yield { type: ValueErrorType.IntegerMultipleOf, schema, path, value, message: `Expected integer to be a multiple of ${schema.multipleOf}` };
173
- }
174
- if (IsNumber(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) {
175
- yield { type: ValueErrorType.IntegerExclusiveMinimum, schema, path, value, message: `Expected integer to be greater than ${schema.exclusiveMinimum}` };
176
- }
177
- if (IsNumber(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
178
- yield { type: ValueErrorType.IntegerExclusiveMaximum, schema, path, value, message: `Expected integer to be less than ${schema.exclusiveMaximum}` };
179
- }
180
- if (IsNumber(schema.minimum) && !(value >= schema.minimum)) {
181
- yield { type: ValueErrorType.IntegerMinimum, schema, path, value, message: `Expected integer to be greater or equal to ${schema.minimum}` };
182
- }
183
- if (IsNumber(schema.maximum) && !(value <= schema.maximum)) {
184
- yield { type: ValueErrorType.IntegerMaximum, schema, path, value, message: `Expected integer to be less or equal to ${schema.maximum}` };
185
- }
186
- }
187
- function* Literal(schema, references, path, value) {
188
- if (!(value === schema.const)) {
189
- const error = typeof schema.const === 'string' ? `'${schema.const}'` : schema.const;
190
- return yield { type: ValueErrorType.Literal, schema, path, value, message: `Expected ${error}` };
191
- }
192
- }
193
- function* Never(schema, references, path, value) {
194
- yield { type: ValueErrorType.Never, schema, path, value, message: `Value cannot be validated` };
195
- }
196
- function* Null(schema, references, path, value) {
197
- if (!(value === null)) {
198
- return yield { type: ValueErrorType.Null, schema, path, value, message: `Expected null` };
199
- }
200
- }
201
- function* Number(schema, references, path, value) {
202
- if (index_1.TypeSystem.AllowNaN) {
203
- if (!(typeof value === 'number')) {
204
- return yield { type: ValueErrorType.Number, schema, path, value, message: `Expected number` };
205
- }
206
- }
207
- else {
208
- if (!(typeof value === 'number' && !isNaN(value))) {
209
- return yield { type: ValueErrorType.Number, schema, path, value, message: `Expected number` };
210
- }
211
- }
212
- if (IsNumber(schema.multipleOf) && !(value % schema.multipleOf === 0)) {
213
- yield { type: ValueErrorType.NumberMultipleOf, schema, path, value, message: `Expected number to be a multiple of ${schema.multipleOf}` };
214
- }
215
- if (IsNumber(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) {
216
- yield { type: ValueErrorType.NumberExclusiveMinimum, schema, path, value, message: `Expected number to be greater than ${schema.exclusiveMinimum}` };
217
- }
218
- if (IsNumber(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
219
- yield { type: ValueErrorType.NumberExclusiveMaximum, schema, path, value, message: `Expected number to be less than ${schema.exclusiveMaximum}` };
220
- }
221
- if (IsNumber(schema.minimum) && !(value >= schema.minimum)) {
222
- yield { type: ValueErrorType.NumberMaximum, schema, path, value, message: `Expected number to be greater or equal to ${schema.minimum}` };
223
- }
224
- if (IsNumber(schema.maximum) && !(value <= schema.maximum)) {
225
- yield { type: ValueErrorType.NumberMinumum, schema, path, value, message: `Expected number to be less or equal to ${schema.maximum}` };
226
- }
227
- }
228
- function* Object(schema, references, path, value) {
229
- if (index_1.TypeSystem.AllowArrayObjects) {
230
- if (!(typeof value === 'object' && value !== null)) {
231
- return yield { type: ValueErrorType.Object, schema, path, value, message: `Expected object` };
232
- }
233
- }
234
- else {
235
- if (!(typeof value === 'object' && value !== null && !globalThis.Array.isArray(value))) {
236
- return yield { type: ValueErrorType.Object, schema, path, value, message: `Expected object` };
237
- }
238
- }
239
- if (IsNumber(schema.minProperties) && !(globalThis.Object.getOwnPropertyNames(value).length >= schema.minProperties)) {
240
- yield { type: ValueErrorType.ObjectMinProperties, schema, path, value, message: `Expected object to have at least ${schema.minProperties} properties` };
241
- }
242
- if (IsNumber(schema.maxProperties) && !(globalThis.Object.getOwnPropertyNames(value).length <= schema.maxProperties)) {
243
- yield { type: ValueErrorType.ObjectMaxProperties, schema, path, value, message: `Expected object to have less than ${schema.minProperties} properties` };
244
- }
245
- const propertyNames = globalThis.Object.getOwnPropertyNames(schema.properties);
246
- if (schema.additionalProperties === false) {
247
- for (const propertyName of globalThis.Object.getOwnPropertyNames(value)) {
248
- if (!propertyNames.includes(propertyName)) {
249
- yield { type: ValueErrorType.ObjectAdditionalProperties, schema, path: `${path}/${propertyName}`, value: value[propertyName], message: `Unexpected property` };
250
- }
251
- }
252
- }
253
- if (schema.required && schema.required.length > 0) {
254
- const propertyNames = globalThis.Object.getOwnPropertyNames(value);
255
- for (const requiredKey of schema.required) {
256
- if (propertyNames.includes(requiredKey))
257
- continue;
258
- yield { type: ValueErrorType.ObjectRequiredProperties, schema: schema.properties[requiredKey], path: `${path}/${requiredKey}`, value: undefined, message: `Expected required property` };
259
- }
260
- }
261
- if (typeof schema.additionalProperties === 'object') {
262
- for (const propertyName of globalThis.Object.getOwnPropertyNames(value)) {
263
- if (propertyNames.includes(propertyName))
264
- continue;
265
- yield* Visit(schema.additionalProperties, references, `${path}/${propertyName}`, value[propertyName]);
266
- }
267
- }
268
- for (const propertyKey of propertyNames) {
269
- const propertySchema = schema.properties[propertyKey];
270
- if (schema.required && schema.required.includes(propertyKey)) {
271
- yield* Visit(propertySchema, references, `${path}/${propertyKey}`, value[propertyKey]);
272
- }
273
- else {
274
- if (value[propertyKey] !== undefined) {
275
- yield* Visit(propertySchema, references, `${path}/${propertyKey}`, value[propertyKey]);
276
- }
277
- }
278
- }
279
- }
280
- function* Promise(schema, references, path, value) {
281
- if (!(typeof value === 'object' && typeof value.then === 'function')) {
282
- yield { type: ValueErrorType.Promise, schema, path, value, message: `Expected Promise` };
283
- }
284
- }
285
- function* Record(schema, references, path, value) {
286
- if (index_1.TypeSystem.AllowArrayObjects) {
287
- if (!(typeof value === 'object' && value !== null && !(value instanceof globalThis.Date))) {
288
- return yield { type: ValueErrorType.Object, schema, path, value, message: `Expected object` };
289
- }
290
- }
291
- else {
292
- if (!(typeof value === 'object' && value !== null && !(value instanceof globalThis.Date) && !globalThis.Array.isArray(value))) {
293
- return yield { type: ValueErrorType.Object, schema, path, value, message: `Expected object` };
294
- }
295
- }
296
- const [keyPattern, valueSchema] = globalThis.Object.entries(schema.patternProperties)[0];
297
- const regex = new RegExp(keyPattern);
298
- if (!globalThis.Object.getOwnPropertyNames(value).every((key) => regex.test(key))) {
299
- const numeric = keyPattern === '^(0|[1-9][0-9]*)$';
300
- const type = numeric ? ValueErrorType.RecordKeyNumeric : ValueErrorType.RecordKeyString;
301
- const message = numeric ? 'Expected all object property keys to be numeric' : 'Expected all object property keys to be strings';
302
- return yield { type, schema, path, value, message };
303
- }
304
- for (const [propKey, propValue] of globalThis.Object.entries(value)) {
305
- yield* Visit(valueSchema, references, `${path}/${propKey}`, propValue);
306
- }
307
- }
308
- function* Ref(schema, references, path, value) {
309
- const reference = references.find((reference) => reference.$id === schema.$ref);
310
- if (reference === undefined)
311
- throw new Error(`ValueErrors.Ref: Cannot find schema with $id '${schema.$ref}'.`);
312
- yield* Visit(reference, references, path, value);
313
- }
314
- function* Self(schema, references, path, value) {
315
- const reference = references.find((reference) => reference.$id === schema.$ref);
316
- if (reference === undefined)
317
- throw new Error(`ValueErrors.Self: Cannot find schema with $id '${schema.$ref}'.`);
318
- yield* Visit(reference, references, path, value);
319
- }
320
- function* String(schema, references, path, value) {
321
- if (!(typeof value === 'string')) {
322
- return yield { type: ValueErrorType.String, schema, path, value, message: 'Expected string' };
323
- }
324
- if (IsNumber(schema.minLength) && !(value.length >= schema.minLength)) {
325
- yield { type: ValueErrorType.StringMinLength, schema, path, value, message: `Expected string length greater or equal to ${schema.minLength}` };
326
- }
327
- if (IsNumber(schema.maxLength) && !(value.length <= schema.maxLength)) {
328
- yield { type: ValueErrorType.StringMaxLength, schema, path, value, message: `Expected string length less or equal to ${schema.maxLength}` };
329
- }
330
- if (schema.pattern !== undefined) {
331
- const regex = new RegExp(schema.pattern);
332
- if (!regex.test(value)) {
333
- yield { type: ValueErrorType.StringPattern, schema, path, value, message: `Expected string to match pattern ${schema.pattern}` };
334
- }
335
- }
336
- if (schema.format !== undefined) {
337
- if (!index_2.Format.Has(schema.format)) {
338
- yield { type: ValueErrorType.StringFormatUnknown, schema, path, value, message: `Unknown string format '${schema.format}'` };
339
- }
340
- else {
341
- const format = index_2.Format.Get(schema.format);
342
- if (!format(value)) {
343
- yield { type: ValueErrorType.StringFormat, schema, path, value, message: `Expected string to match format '${schema.format}'` };
344
- }
345
- }
346
- }
347
- }
348
- function* Tuple(schema, references, path, value) {
349
- if (!globalThis.Array.isArray(value)) {
350
- return yield { type: ValueErrorType.Array, schema, path, value, message: 'Expected Array' };
351
- }
352
- if (schema.items === undefined && !(value.length === 0)) {
353
- return yield { type: ValueErrorType.TupleZeroLength, schema, path, value, message: 'Expected tuple to have 0 elements' };
354
- }
355
- if (!(value.length === schema.maxItems)) {
356
- yield { type: ValueErrorType.TupleLength, schema, path, value, message: `Expected tuple to have ${schema.maxItems} elements` };
357
- }
358
- if (!schema.items) {
359
- return;
360
- }
361
- for (let i = 0; i < schema.items.length; i++) {
362
- yield* Visit(schema.items[i], references, `${path}/${i}`, value[i]);
363
- }
364
- }
365
- function* Undefined(schema, references, path, value) {
366
- if (!(value === undefined)) {
367
- yield { type: ValueErrorType.Undefined, schema, path, value, message: `Expected undefined` };
368
- }
369
- }
370
- function* Union(schema, references, path, value) {
371
- const errors = [];
372
- for (const inner of schema.anyOf) {
373
- const variantErrors = [...Visit(inner, references, path, value)];
374
- if (variantErrors.length === 0)
375
- return;
376
- errors.push(...variantErrors);
377
- }
378
- for (const error of errors) {
379
- yield error;
380
- }
381
- if (errors.length > 0) {
382
- yield { type: ValueErrorType.Union, schema, path, value, message: 'Expected value of union' };
383
- }
384
- }
385
- function* Uint8Array(schema, references, path, value) {
386
- if (!(value instanceof globalThis.Uint8Array)) {
387
- return yield { type: ValueErrorType.Uint8Array, schema, path, value, message: `Expected Uint8Array` };
388
- }
389
- if (IsNumber(schema.maxByteLength) && !(value.length <= schema.maxByteLength)) {
390
- yield { type: ValueErrorType.Uint8ArrayMaxByteLength, schema, path, value, message: `Expected Uint8Array to have a byte length less or equal to ${schema.maxByteLength}` };
391
- }
392
- if (IsNumber(schema.minByteLength) && !(value.length >= schema.minByteLength)) {
393
- yield { type: ValueErrorType.Uint8ArrayMinByteLength, schema, path, value, message: `Expected Uint8Array to have a byte length greater or equal to ${schema.maxByteLength}` };
394
- }
395
- }
396
- function* Unknown(schema, references, path, value) { }
397
- function* Void(schema, references, path, value) {
398
- if (!(value === null)) {
399
- return yield { type: ValueErrorType.Void, schema, path, value, message: `Expected null` };
400
- }
401
- }
402
- function* UserDefined(schema, references, path, value) {
403
- const func = index_3.Custom.Get(schema[Types.Kind]);
404
- if (!func(schema, value)) {
405
- return yield { type: ValueErrorType.Custom, schema, path, value, message: `Expected kind ${schema[Types.Kind]}` };
406
- }
407
- }
408
- function* Visit(schema, references, path, value) {
409
- const anyReferences = schema.$id === undefined ? references : [schema, ...references];
410
- const anySchema = schema;
411
- switch (anySchema[Types.Kind]) {
412
- case 'Any':
413
- return yield* Any(anySchema, anyReferences, path, value);
414
- case 'Array':
415
- return yield* Array(anySchema, anyReferences, path, value);
416
- case 'Boolean':
417
- return yield* Boolean(anySchema, anyReferences, path, value);
418
- case 'Constructor':
419
- return yield* Constructor(anySchema, anyReferences, path, value);
420
- case 'Date':
421
- return yield* Date(anySchema, anyReferences, path, value);
422
- case 'Function':
423
- return yield* Function(anySchema, anyReferences, path, value);
424
- case 'Integer':
425
- return yield* Integer(anySchema, anyReferences, path, value);
426
- case 'Literal':
427
- return yield* Literal(anySchema, anyReferences, path, value);
428
- case 'Never':
429
- return yield* Never(anySchema, anyReferences, path, value);
430
- case 'Null':
431
- return yield* Null(anySchema, anyReferences, path, value);
432
- case 'Number':
433
- return yield* Number(anySchema, anyReferences, path, value);
434
- case 'Object':
435
- return yield* Object(anySchema, anyReferences, path, value);
436
- case 'Promise':
437
- return yield* Promise(anySchema, anyReferences, path, value);
438
- case 'Record':
439
- return yield* Record(anySchema, anyReferences, path, value);
440
- case 'Ref':
441
- return yield* Ref(anySchema, anyReferences, path, value);
442
- case 'Self':
443
- return yield* Self(anySchema, anyReferences, path, value);
444
- case 'String':
445
- return yield* String(anySchema, anyReferences, path, value);
446
- case 'Tuple':
447
- return yield* Tuple(anySchema, anyReferences, path, value);
448
- case 'Undefined':
449
- return yield* Undefined(anySchema, anyReferences, path, value);
450
- case 'Union':
451
- return yield* Union(anySchema, anyReferences, path, value);
452
- case 'Uint8Array':
453
- return yield* Uint8Array(anySchema, anyReferences, path, value);
454
- case 'Unknown':
455
- return yield* Unknown(anySchema, anyReferences, path, value);
456
- case 'Void':
457
- return yield* Void(anySchema, anyReferences, path, value);
458
- default:
459
- if (!index_3.Custom.Has(anySchema[Types.Kind]))
460
- throw new ValueErrorsUnknownTypeError(schema);
461
- return yield* UserDefined(anySchema, anyReferences, path, value);
462
- }
463
- }
464
- function* Errors(schema, references, value) {
465
- yield* Visit(schema, references, '', value);
466
- }
467
- ValueErrors.Errors = Errors;
468
- })(ValueErrors = exports.ValueErrors || (exports.ValueErrors = {}));
1
+ "use strict";
2
+ /*--------------------------------------------------------------------------
3
+
4
+ @sinclair/typebox/errors
5
+
6
+ The MIT License (MIT)
7
+
8
+ Copyright (c) 2017-2023 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in
18
+ all copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26
+ THE SOFTWARE.
27
+
28
+ ---------------------------------------------------------------------------*/
29
+ Object.defineProperty(exports, "__esModule", { value: true });
30
+ exports.ValueErrors = exports.ValueErrorsUnknownTypeError = exports.ValueErrorType = void 0;
31
+ const Types = require("../typebox");
32
+ const index_1 = require("../system/index");
33
+ const extends_1 = require("../guard/extends");
34
+ const index_2 = require("../format/index");
35
+ const index_3 = require("../custom/index");
36
+ const index_4 = require("../hash/index");
37
+ // -------------------------------------------------------------------
38
+ // ValueErrorType
39
+ // -------------------------------------------------------------------
40
+ var ValueErrorType;
41
+ (function (ValueErrorType) {
42
+ ValueErrorType[ValueErrorType["Array"] = 0] = "Array";
43
+ ValueErrorType[ValueErrorType["ArrayMinItems"] = 1] = "ArrayMinItems";
44
+ ValueErrorType[ValueErrorType["ArrayMaxItems"] = 2] = "ArrayMaxItems";
45
+ ValueErrorType[ValueErrorType["ArrayUniqueItems"] = 3] = "ArrayUniqueItems";
46
+ ValueErrorType[ValueErrorType["Boolean"] = 4] = "Boolean";
47
+ ValueErrorType[ValueErrorType["Date"] = 5] = "Date";
48
+ ValueErrorType[ValueErrorType["DateExclusiveMinimumTimestamp"] = 6] = "DateExclusiveMinimumTimestamp";
49
+ ValueErrorType[ValueErrorType["DateExclusiveMaximumTimestamp"] = 7] = "DateExclusiveMaximumTimestamp";
50
+ ValueErrorType[ValueErrorType["DateMinimumTimestamp"] = 8] = "DateMinimumTimestamp";
51
+ ValueErrorType[ValueErrorType["DateMaximumTimestamp"] = 9] = "DateMaximumTimestamp";
52
+ ValueErrorType[ValueErrorType["Function"] = 10] = "Function";
53
+ ValueErrorType[ValueErrorType["Integer"] = 11] = "Integer";
54
+ ValueErrorType[ValueErrorType["IntegerMultipleOf"] = 12] = "IntegerMultipleOf";
55
+ ValueErrorType[ValueErrorType["IntegerExclusiveMinimum"] = 13] = "IntegerExclusiveMinimum";
56
+ ValueErrorType[ValueErrorType["IntegerExclusiveMaximum"] = 14] = "IntegerExclusiveMaximum";
57
+ ValueErrorType[ValueErrorType["IntegerMinimum"] = 15] = "IntegerMinimum";
58
+ ValueErrorType[ValueErrorType["IntegerMaximum"] = 16] = "IntegerMaximum";
59
+ ValueErrorType[ValueErrorType["Literal"] = 17] = "Literal";
60
+ ValueErrorType[ValueErrorType["Never"] = 18] = "Never";
61
+ ValueErrorType[ValueErrorType["Null"] = 19] = "Null";
62
+ ValueErrorType[ValueErrorType["Number"] = 20] = "Number";
63
+ ValueErrorType[ValueErrorType["NumberMultipleOf"] = 21] = "NumberMultipleOf";
64
+ ValueErrorType[ValueErrorType["NumberExclusiveMinimum"] = 22] = "NumberExclusiveMinimum";
65
+ ValueErrorType[ValueErrorType["NumberExclusiveMaximum"] = 23] = "NumberExclusiveMaximum";
66
+ ValueErrorType[ValueErrorType["NumberMinumum"] = 24] = "NumberMinumum";
67
+ ValueErrorType[ValueErrorType["NumberMaximum"] = 25] = "NumberMaximum";
68
+ ValueErrorType[ValueErrorType["Object"] = 26] = "Object";
69
+ ValueErrorType[ValueErrorType["ObjectMinProperties"] = 27] = "ObjectMinProperties";
70
+ ValueErrorType[ValueErrorType["ObjectMaxProperties"] = 28] = "ObjectMaxProperties";
71
+ ValueErrorType[ValueErrorType["ObjectAdditionalProperties"] = 29] = "ObjectAdditionalProperties";
72
+ ValueErrorType[ValueErrorType["ObjectRequiredProperties"] = 30] = "ObjectRequiredProperties";
73
+ ValueErrorType[ValueErrorType["Promise"] = 31] = "Promise";
74
+ ValueErrorType[ValueErrorType["RecordKeyNumeric"] = 32] = "RecordKeyNumeric";
75
+ ValueErrorType[ValueErrorType["RecordKeyString"] = 33] = "RecordKeyString";
76
+ ValueErrorType[ValueErrorType["String"] = 34] = "String";
77
+ ValueErrorType[ValueErrorType["StringMinLength"] = 35] = "StringMinLength";
78
+ ValueErrorType[ValueErrorType["StringMaxLength"] = 36] = "StringMaxLength";
79
+ ValueErrorType[ValueErrorType["StringPattern"] = 37] = "StringPattern";
80
+ ValueErrorType[ValueErrorType["StringFormatUnknown"] = 38] = "StringFormatUnknown";
81
+ ValueErrorType[ValueErrorType["StringFormat"] = 39] = "StringFormat";
82
+ ValueErrorType[ValueErrorType["TupleZeroLength"] = 40] = "TupleZeroLength";
83
+ ValueErrorType[ValueErrorType["TupleLength"] = 41] = "TupleLength";
84
+ ValueErrorType[ValueErrorType["Undefined"] = 42] = "Undefined";
85
+ ValueErrorType[ValueErrorType["Union"] = 43] = "Union";
86
+ ValueErrorType[ValueErrorType["Uint8Array"] = 44] = "Uint8Array";
87
+ ValueErrorType[ValueErrorType["Uint8ArrayMinByteLength"] = 45] = "Uint8ArrayMinByteLength";
88
+ ValueErrorType[ValueErrorType["Uint8ArrayMaxByteLength"] = 46] = "Uint8ArrayMaxByteLength";
89
+ ValueErrorType[ValueErrorType["Void"] = 47] = "Void";
90
+ ValueErrorType[ValueErrorType["Custom"] = 48] = "Custom";
91
+ })(ValueErrorType = exports.ValueErrorType || (exports.ValueErrorType = {}));
92
+ // -------------------------------------------------------------------
93
+ // ValueErrors
94
+ // -------------------------------------------------------------------
95
+ class ValueErrorsUnknownTypeError extends Error {
96
+ constructor(schema) {
97
+ super('ValueErrors: Unknown type');
98
+ this.schema = schema;
99
+ }
100
+ }
101
+ exports.ValueErrorsUnknownTypeError = ValueErrorsUnknownTypeError;
102
+ /** Provides functionality to generate a sequence of errors against a TypeBox type. */
103
+ var ValueErrors;
104
+ (function (ValueErrors) {
105
+ function IsNumber(value) {
106
+ return typeof value === 'number' && !isNaN(value);
107
+ }
108
+ function* Any(schema, references, path, value) { }
109
+ function* Array(schema, references, path, value) {
110
+ if (!globalThis.Array.isArray(value)) {
111
+ return yield { type: ValueErrorType.Array, schema, path, value, message: `Expected array` };
112
+ }
113
+ if (IsNumber(schema.minItems) && !(value.length >= schema.minItems)) {
114
+ yield { type: ValueErrorType.ArrayMinItems, schema, path, value, message: `Expected array length to be greater or equal to ${schema.minItems}` };
115
+ }
116
+ if (IsNumber(schema.maxItems) && !(value.length <= schema.maxItems)) {
117
+ yield { type: ValueErrorType.ArrayMinItems, schema, path, value, message: `Expected array length to be less or equal to ${schema.maxItems}` };
118
+ }
119
+ // prettier-ignore
120
+ if (schema.uniqueItems === true && !((function () { const set = new Set(); for (const element of value) {
121
+ const hashed = index_4.ValueHash.Create(element);
122
+ if (set.has(hashed)) {
123
+ return false;
124
+ }
125
+ else {
126
+ set.add(hashed);
127
+ }
128
+ } return true; })())) {
129
+ yield { type: ValueErrorType.ArrayUniqueItems, schema, path, value, message: `Expected array elements to be unique` };
130
+ }
131
+ for (let i = 0; i < value.length; i++) {
132
+ yield* Visit(schema.items, references, `${path}/${i}`, value[i]);
133
+ }
134
+ }
135
+ function* Boolean(schema, references, path, value) {
136
+ if (!(typeof value === 'boolean')) {
137
+ return yield { type: ValueErrorType.Boolean, schema, path, value, message: `Expected boolean` };
138
+ }
139
+ }
140
+ function* Constructor(schema, references, path, value) {
141
+ yield* Visit(schema.returns, references, path, value.prototype);
142
+ }
143
+ function* Date(schema, references, path, value) {
144
+ if (!(value instanceof globalThis.Date)) {
145
+ return yield { type: ValueErrorType.Date, schema, path, value, message: `Expected Date object` };
146
+ }
147
+ if (isNaN(value.getTime())) {
148
+ return yield { type: ValueErrorType.Date, schema, path, value, message: `Invalid Date` };
149
+ }
150
+ if (IsNumber(schema.exclusiveMinimumTimestamp) && !(value.getTime() > schema.exclusiveMinimumTimestamp)) {
151
+ yield { type: ValueErrorType.DateExclusiveMinimumTimestamp, schema, path, value, message: `Expected Date timestamp to be greater than ${schema.exclusiveMinimum}` };
152
+ }
153
+ if (IsNumber(schema.exclusiveMaximumTimestamp) && !(value.getTime() < schema.exclusiveMaximumTimestamp)) {
154
+ yield { type: ValueErrorType.DateExclusiveMaximumTimestamp, schema, path, value, message: `Expected Date timestamp to be less than ${schema.exclusiveMaximum}` };
155
+ }
156
+ if (IsNumber(schema.minimumTimestamp) && !(value.getTime() >= schema.minimumTimestamp)) {
157
+ yield { type: ValueErrorType.DateMinimumTimestamp, schema, path, value, message: `Expected Date timestamp to be greater or equal to ${schema.minimum}` };
158
+ }
159
+ if (IsNumber(schema.maximumTimestamp) && !(value.getTime() <= schema.maximumTimestamp)) {
160
+ yield { type: ValueErrorType.DateMaximumTimestamp, schema, path, value, message: `Expected Date timestamp to be less or equal to ${schema.maximum}` };
161
+ }
162
+ }
163
+ function* Function(schema, references, path, value) {
164
+ if (!(typeof value === 'function')) {
165
+ return yield { type: ValueErrorType.Function, schema, path, value, message: `Expected function` };
166
+ }
167
+ }
168
+ function* Integer(schema, references, path, value) {
169
+ if (!(typeof value === 'number' && globalThis.Number.isInteger(value))) {
170
+ return yield { type: ValueErrorType.Integer, schema, path, value, message: `Expected integer` };
171
+ }
172
+ if (IsNumber(schema.multipleOf) && !(value % schema.multipleOf === 0)) {
173
+ yield { type: ValueErrorType.IntegerMultipleOf, schema, path, value, message: `Expected integer to be a multiple of ${schema.multipleOf}` };
174
+ }
175
+ if (IsNumber(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) {
176
+ yield { type: ValueErrorType.IntegerExclusiveMinimum, schema, path, value, message: `Expected integer to be greater than ${schema.exclusiveMinimum}` };
177
+ }
178
+ if (IsNumber(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
179
+ yield { type: ValueErrorType.IntegerExclusiveMaximum, schema, path, value, message: `Expected integer to be less than ${schema.exclusiveMaximum}` };
180
+ }
181
+ if (IsNumber(schema.minimum) && !(value >= schema.minimum)) {
182
+ yield { type: ValueErrorType.IntegerMinimum, schema, path, value, message: `Expected integer to be greater or equal to ${schema.minimum}` };
183
+ }
184
+ if (IsNumber(schema.maximum) && !(value <= schema.maximum)) {
185
+ yield { type: ValueErrorType.IntegerMaximum, schema, path, value, message: `Expected integer to be less or equal to ${schema.maximum}` };
186
+ }
187
+ }
188
+ function* Literal(schema, references, path, value) {
189
+ if (!(value === schema.const)) {
190
+ const error = typeof schema.const === 'string' ? `'${schema.const}'` : schema.const;
191
+ return yield { type: ValueErrorType.Literal, schema, path, value, message: `Expected ${error}` };
192
+ }
193
+ }
194
+ function* Never(schema, references, path, value) {
195
+ yield { type: ValueErrorType.Never, schema, path, value, message: `Value cannot be validated` };
196
+ }
197
+ function* Null(schema, references, path, value) {
198
+ if (!(value === null)) {
199
+ return yield { type: ValueErrorType.Null, schema, path, value, message: `Expected null` };
200
+ }
201
+ }
202
+ function* Number(schema, references, path, value) {
203
+ if (index_1.TypeSystem.AllowNaN) {
204
+ if (!(typeof value === 'number')) {
205
+ return yield { type: ValueErrorType.Number, schema, path, value, message: `Expected number` };
206
+ }
207
+ }
208
+ else {
209
+ if (!(typeof value === 'number' && !isNaN(value))) {
210
+ return yield { type: ValueErrorType.Number, schema, path, value, message: `Expected number` };
211
+ }
212
+ }
213
+ if (IsNumber(schema.multipleOf) && !(value % schema.multipleOf === 0)) {
214
+ yield { type: ValueErrorType.NumberMultipleOf, schema, path, value, message: `Expected number to be a multiple of ${schema.multipleOf}` };
215
+ }
216
+ if (IsNumber(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) {
217
+ yield { type: ValueErrorType.NumberExclusiveMinimum, schema, path, value, message: `Expected number to be greater than ${schema.exclusiveMinimum}` };
218
+ }
219
+ if (IsNumber(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
220
+ yield { type: ValueErrorType.NumberExclusiveMaximum, schema, path, value, message: `Expected number to be less than ${schema.exclusiveMaximum}` };
221
+ }
222
+ if (IsNumber(schema.minimum) && !(value >= schema.minimum)) {
223
+ yield { type: ValueErrorType.NumberMaximum, schema, path, value, message: `Expected number to be greater or equal to ${schema.minimum}` };
224
+ }
225
+ if (IsNumber(schema.maximum) && !(value <= schema.maximum)) {
226
+ yield { type: ValueErrorType.NumberMinumum, schema, path, value, message: `Expected number to be less or equal to ${schema.maximum}` };
227
+ }
228
+ }
229
+ function* Object(schema, references, path, value) {
230
+ if (index_1.TypeSystem.AllowArrayObjects) {
231
+ if (!(typeof value === 'object' && value !== null)) {
232
+ return yield { type: ValueErrorType.Object, schema, path, value, message: `Expected object` };
233
+ }
234
+ }
235
+ else {
236
+ if (!(typeof value === 'object' && value !== null && !globalThis.Array.isArray(value))) {
237
+ return yield { type: ValueErrorType.Object, schema, path, value, message: `Expected object` };
238
+ }
239
+ }
240
+ if (IsNumber(schema.minProperties) && !(globalThis.Object.getOwnPropertyNames(value).length >= schema.minProperties)) {
241
+ yield { type: ValueErrorType.ObjectMinProperties, schema, path, value, message: `Expected object to have at least ${schema.minProperties} properties` };
242
+ }
243
+ if (IsNumber(schema.maxProperties) && !(globalThis.Object.getOwnPropertyNames(value).length <= schema.maxProperties)) {
244
+ yield { type: ValueErrorType.ObjectMaxProperties, schema, path, value, message: `Expected object to have less than ${schema.minProperties} properties` };
245
+ }
246
+ const propertyNames = globalThis.Object.getOwnPropertyNames(schema.properties);
247
+ if (schema.additionalProperties === false) {
248
+ for (const propertyName of globalThis.Object.getOwnPropertyNames(value)) {
249
+ if (!propertyNames.includes(propertyName)) {
250
+ yield { type: ValueErrorType.ObjectAdditionalProperties, schema, path: `${path}/${propertyName}`, value: value[propertyName], message: `Unexpected property` };
251
+ }
252
+ }
253
+ }
254
+ if (schema.required && schema.required.length > 0) {
255
+ const propertyNames = globalThis.Object.getOwnPropertyNames(value);
256
+ for (const requiredKey of schema.required) {
257
+ if (propertyNames.includes(requiredKey))
258
+ continue;
259
+ yield { type: ValueErrorType.ObjectRequiredProperties, schema: schema.properties[requiredKey], path: `${path}/${requiredKey}`, value: undefined, message: `Expected required property` };
260
+ }
261
+ }
262
+ if (typeof schema.additionalProperties === 'object') {
263
+ for (const propertyName of globalThis.Object.getOwnPropertyNames(value)) {
264
+ if (propertyNames.includes(propertyName))
265
+ continue;
266
+ yield* Visit(schema.additionalProperties, references, `${path}/${propertyName}`, value[propertyName]);
267
+ }
268
+ }
269
+ for (const propertyKey of propertyNames) {
270
+ const propertySchema = schema.properties[propertyKey];
271
+ if (schema.required && schema.required.includes(propertyKey)) {
272
+ yield* Visit(propertySchema, references, `${path}/${propertyKey}`, value[propertyKey]);
273
+ if (extends_1.TypeExtends.Undefined(schema) && !(propertyKey in value)) {
274
+ yield { type: ValueErrorType.ObjectRequiredProperties, schema: propertySchema, path: `${path}/${propertyKey}`, value: undefined, message: `Expected required property` };
275
+ }
276
+ }
277
+ else {
278
+ if (value[propertyKey] !== undefined) {
279
+ yield* Visit(propertySchema, references, `${path}/${propertyKey}`, value[propertyKey]);
280
+ }
281
+ }
282
+ }
283
+ }
284
+ function* Promise(schema, references, path, value) {
285
+ if (!(typeof value === 'object' && typeof value.then === 'function')) {
286
+ yield { type: ValueErrorType.Promise, schema, path, value, message: `Expected Promise` };
287
+ }
288
+ }
289
+ function* Record(schema, references, path, value) {
290
+ if (index_1.TypeSystem.AllowArrayObjects) {
291
+ if (!(typeof value === 'object' && value !== null && !(value instanceof globalThis.Date))) {
292
+ return yield { type: ValueErrorType.Object, schema, path, value, message: `Expected object` };
293
+ }
294
+ }
295
+ else {
296
+ if (!(typeof value === 'object' && value !== null && !(value instanceof globalThis.Date) && !globalThis.Array.isArray(value))) {
297
+ return yield { type: ValueErrorType.Object, schema, path, value, message: `Expected object` };
298
+ }
299
+ }
300
+ const [keyPattern, valueSchema] = globalThis.Object.entries(schema.patternProperties)[0];
301
+ const regex = new RegExp(keyPattern);
302
+ if (!globalThis.Object.getOwnPropertyNames(value).every((key) => regex.test(key))) {
303
+ const numeric = keyPattern === '^(0|[1-9][0-9]*)$';
304
+ const type = numeric ? ValueErrorType.RecordKeyNumeric : ValueErrorType.RecordKeyString;
305
+ const message = numeric ? 'Expected all object property keys to be numeric' : 'Expected all object property keys to be strings';
306
+ return yield { type, schema, path, value, message };
307
+ }
308
+ for (const [propKey, propValue] of globalThis.Object.entries(value)) {
309
+ yield* Visit(valueSchema, references, `${path}/${propKey}`, propValue);
310
+ }
311
+ }
312
+ function* Ref(schema, references, path, value) {
313
+ const reference = references.find((reference) => reference.$id === schema.$ref);
314
+ if (reference === undefined)
315
+ throw new Error(`ValueErrors.Ref: Cannot find schema with $id '${schema.$ref}'.`);
316
+ yield* Visit(reference, references, path, value);
317
+ }
318
+ function* Self(schema, references, path, value) {
319
+ const reference = references.find((reference) => reference.$id === schema.$ref);
320
+ if (reference === undefined)
321
+ throw new Error(`ValueErrors.Self: Cannot find schema with $id '${schema.$ref}'.`);
322
+ yield* Visit(reference, references, path, value);
323
+ }
324
+ function* String(schema, references, path, value) {
325
+ if (!(typeof value === 'string')) {
326
+ return yield { type: ValueErrorType.String, schema, path, value, message: 'Expected string' };
327
+ }
328
+ if (IsNumber(schema.minLength) && !(value.length >= schema.minLength)) {
329
+ yield { type: ValueErrorType.StringMinLength, schema, path, value, message: `Expected string length greater or equal to ${schema.minLength}` };
330
+ }
331
+ if (IsNumber(schema.maxLength) && !(value.length <= schema.maxLength)) {
332
+ yield { type: ValueErrorType.StringMaxLength, schema, path, value, message: `Expected string length less or equal to ${schema.maxLength}` };
333
+ }
334
+ if (schema.pattern !== undefined) {
335
+ const regex = new RegExp(schema.pattern);
336
+ if (!regex.test(value)) {
337
+ yield { type: ValueErrorType.StringPattern, schema, path, value, message: `Expected string to match pattern ${schema.pattern}` };
338
+ }
339
+ }
340
+ if (schema.format !== undefined) {
341
+ if (!index_2.Format.Has(schema.format)) {
342
+ yield { type: ValueErrorType.StringFormatUnknown, schema, path, value, message: `Unknown string format '${schema.format}'` };
343
+ }
344
+ else {
345
+ const format = index_2.Format.Get(schema.format);
346
+ if (!format(value)) {
347
+ yield { type: ValueErrorType.StringFormat, schema, path, value, message: `Expected string to match format '${schema.format}'` };
348
+ }
349
+ }
350
+ }
351
+ }
352
+ function* Tuple(schema, references, path, value) {
353
+ if (!globalThis.Array.isArray(value)) {
354
+ return yield { type: ValueErrorType.Array, schema, path, value, message: 'Expected Array' };
355
+ }
356
+ if (schema.items === undefined && !(value.length === 0)) {
357
+ return yield { type: ValueErrorType.TupleZeroLength, schema, path, value, message: 'Expected tuple to have 0 elements' };
358
+ }
359
+ if (!(value.length === schema.maxItems)) {
360
+ yield { type: ValueErrorType.TupleLength, schema, path, value, message: `Expected tuple to have ${schema.maxItems} elements` };
361
+ }
362
+ if (!schema.items) {
363
+ return;
364
+ }
365
+ for (let i = 0; i < schema.items.length; i++) {
366
+ yield* Visit(schema.items[i], references, `${path}/${i}`, value[i]);
367
+ }
368
+ }
369
+ function* Undefined(schema, references, path, value) {
370
+ if (!(value === undefined)) {
371
+ yield { type: ValueErrorType.Undefined, schema, path, value, message: `Expected undefined` };
372
+ }
373
+ }
374
+ function* Union(schema, references, path, value) {
375
+ const errors = [];
376
+ for (const inner of schema.anyOf) {
377
+ const variantErrors = [...Visit(inner, references, path, value)];
378
+ if (variantErrors.length === 0)
379
+ return;
380
+ errors.push(...variantErrors);
381
+ }
382
+ for (const error of errors) {
383
+ yield error;
384
+ }
385
+ if (errors.length > 0) {
386
+ yield { type: ValueErrorType.Union, schema, path, value, message: 'Expected value of union' };
387
+ }
388
+ }
389
+ function* Uint8Array(schema, references, path, value) {
390
+ if (!(value instanceof globalThis.Uint8Array)) {
391
+ return yield { type: ValueErrorType.Uint8Array, schema, path, value, message: `Expected Uint8Array` };
392
+ }
393
+ if (IsNumber(schema.maxByteLength) && !(value.length <= schema.maxByteLength)) {
394
+ yield { type: ValueErrorType.Uint8ArrayMaxByteLength, schema, path, value, message: `Expected Uint8Array to have a byte length less or equal to ${schema.maxByteLength}` };
395
+ }
396
+ if (IsNumber(schema.minByteLength) && !(value.length >= schema.minByteLength)) {
397
+ yield { type: ValueErrorType.Uint8ArrayMinByteLength, schema, path, value, message: `Expected Uint8Array to have a byte length greater or equal to ${schema.maxByteLength}` };
398
+ }
399
+ }
400
+ function* Unknown(schema, references, path, value) { }
401
+ function* Void(schema, references, path, value) {
402
+ if (!(value === null)) {
403
+ return yield { type: ValueErrorType.Void, schema, path, value, message: `Expected null` };
404
+ }
405
+ }
406
+ function* UserDefined(schema, references, path, value) {
407
+ const func = index_3.Custom.Get(schema[Types.Kind]);
408
+ if (!func(schema, value)) {
409
+ return yield { type: ValueErrorType.Custom, schema, path, value, message: `Expected kind ${schema[Types.Kind]}` };
410
+ }
411
+ }
412
+ function* Visit(schema, references, path, value) {
413
+ const anyReferences = schema.$id === undefined ? references : [schema, ...references];
414
+ const anySchema = schema;
415
+ switch (anySchema[Types.Kind]) {
416
+ case 'Any':
417
+ return yield* Any(anySchema, anyReferences, path, value);
418
+ case 'Array':
419
+ return yield* Array(anySchema, anyReferences, path, value);
420
+ case 'Boolean':
421
+ return yield* Boolean(anySchema, anyReferences, path, value);
422
+ case 'Constructor':
423
+ return yield* Constructor(anySchema, anyReferences, path, value);
424
+ case 'Date':
425
+ return yield* Date(anySchema, anyReferences, path, value);
426
+ case 'Function':
427
+ return yield* Function(anySchema, anyReferences, path, value);
428
+ case 'Integer':
429
+ return yield* Integer(anySchema, anyReferences, path, value);
430
+ case 'Literal':
431
+ return yield* Literal(anySchema, anyReferences, path, value);
432
+ case 'Never':
433
+ return yield* Never(anySchema, anyReferences, path, value);
434
+ case 'Null':
435
+ return yield* Null(anySchema, anyReferences, path, value);
436
+ case 'Number':
437
+ return yield* Number(anySchema, anyReferences, path, value);
438
+ case 'Object':
439
+ return yield* Object(anySchema, anyReferences, path, value);
440
+ case 'Promise':
441
+ return yield* Promise(anySchema, anyReferences, path, value);
442
+ case 'Record':
443
+ return yield* Record(anySchema, anyReferences, path, value);
444
+ case 'Ref':
445
+ return yield* Ref(anySchema, anyReferences, path, value);
446
+ case 'Self':
447
+ return yield* Self(anySchema, anyReferences, path, value);
448
+ case 'String':
449
+ return yield* String(anySchema, anyReferences, path, value);
450
+ case 'Tuple':
451
+ return yield* Tuple(anySchema, anyReferences, path, value);
452
+ case 'Undefined':
453
+ return yield* Undefined(anySchema, anyReferences, path, value);
454
+ case 'Union':
455
+ return yield* Union(anySchema, anyReferences, path, value);
456
+ case 'Uint8Array':
457
+ return yield* Uint8Array(anySchema, anyReferences, path, value);
458
+ case 'Unknown':
459
+ return yield* Unknown(anySchema, anyReferences, path, value);
460
+ case 'Void':
461
+ return yield* Void(anySchema, anyReferences, path, value);
462
+ default:
463
+ if (!index_3.Custom.Has(anySchema[Types.Kind]))
464
+ throw new ValueErrorsUnknownTypeError(schema);
465
+ return yield* UserDefined(anySchema, anyReferences, path, value);
466
+ }
467
+ }
468
+ function* Errors(schema, references, value) {
469
+ yield* Visit(schema, references, '', value);
470
+ }
471
+ ValueErrors.Errors = Errors;
472
+ })(ValueErrors = exports.ValueErrors || (exports.ValueErrors = {}));