@sinclair/typebox 0.29.6 → 0.30.0-dev-2

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/errors/errors.js CHANGED
@@ -1,6 +1,4 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ValueErrors = exports.ValueErrorsDereferenceError = exports.ValueErrorsUnknownTypeError = exports.ValueErrorIterator = exports.ValueErrorType = void 0;
4
2
  /*--------------------------------------------------------------------------
5
3
 
6
4
  @sinclair/typebox/errors
@@ -28,77 +26,85 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28
26
  THE SOFTWARE.
29
27
 
30
28
  ---------------------------------------------------------------------------*/
31
- const Types = require("../typebox");
29
+ Object.defineProperty(exports, "__esModule", { value: true });
30
+ exports.Errors = exports.ValueErrorsDereferenceError = exports.ValueErrorsUnknownTypeError = exports.ValueErrorIterator = exports.ValueErrorType = void 0;
32
31
  const index_1 = require("../system/index");
33
- const hash_1 = require("../value/hash");
34
- // -------------------------------------------------------------------
32
+ const Types = require("../typebox");
33
+ const ValueHash = require("../value/hash");
34
+ const ValueGuard = require("../value/guard");
35
+ // --------------------------------------------------------------------------
35
36
  // ValueErrorType
36
- // -------------------------------------------------------------------
37
+ // --------------------------------------------------------------------------
37
38
  var ValueErrorType;
38
39
  (function (ValueErrorType) {
39
40
  ValueErrorType[ValueErrorType["Array"] = 0] = "Array";
40
41
  ValueErrorType[ValueErrorType["ArrayMinItems"] = 1] = "ArrayMinItems";
41
42
  ValueErrorType[ValueErrorType["ArrayMaxItems"] = 2] = "ArrayMaxItems";
42
- ValueErrorType[ValueErrorType["ArrayUniqueItems"] = 3] = "ArrayUniqueItems";
43
- ValueErrorType[ValueErrorType["BigInt"] = 4] = "BigInt";
44
- ValueErrorType[ValueErrorType["BigIntMultipleOf"] = 5] = "BigIntMultipleOf";
45
- ValueErrorType[ValueErrorType["BigIntExclusiveMinimum"] = 6] = "BigIntExclusiveMinimum";
46
- ValueErrorType[ValueErrorType["BigIntExclusiveMaximum"] = 7] = "BigIntExclusiveMaximum";
47
- ValueErrorType[ValueErrorType["BigIntMinimum"] = 8] = "BigIntMinimum";
48
- ValueErrorType[ValueErrorType["BigIntMaximum"] = 9] = "BigIntMaximum";
49
- ValueErrorType[ValueErrorType["Boolean"] = 10] = "Boolean";
50
- ValueErrorType[ValueErrorType["Date"] = 11] = "Date";
51
- ValueErrorType[ValueErrorType["DateExclusiveMinimumTimestamp"] = 12] = "DateExclusiveMinimumTimestamp";
52
- ValueErrorType[ValueErrorType["DateExclusiveMaximumTimestamp"] = 13] = "DateExclusiveMaximumTimestamp";
53
- ValueErrorType[ValueErrorType["DateMinimumTimestamp"] = 14] = "DateMinimumTimestamp";
54
- ValueErrorType[ValueErrorType["DateMaximumTimestamp"] = 15] = "DateMaximumTimestamp";
55
- ValueErrorType[ValueErrorType["Function"] = 16] = "Function";
56
- ValueErrorType[ValueErrorType["Integer"] = 17] = "Integer";
57
- ValueErrorType[ValueErrorType["IntegerMultipleOf"] = 18] = "IntegerMultipleOf";
58
- ValueErrorType[ValueErrorType["IntegerExclusiveMinimum"] = 19] = "IntegerExclusiveMinimum";
59
- ValueErrorType[ValueErrorType["IntegerExclusiveMaximum"] = 20] = "IntegerExclusiveMaximum";
60
- ValueErrorType[ValueErrorType["IntegerMinimum"] = 21] = "IntegerMinimum";
61
- ValueErrorType[ValueErrorType["IntegerMaximum"] = 22] = "IntegerMaximum";
62
- ValueErrorType[ValueErrorType["Intersect"] = 23] = "Intersect";
63
- ValueErrorType[ValueErrorType["IntersectUnevaluatedProperties"] = 24] = "IntersectUnevaluatedProperties";
64
- ValueErrorType[ValueErrorType["Literal"] = 25] = "Literal";
65
- ValueErrorType[ValueErrorType["Never"] = 26] = "Never";
66
- ValueErrorType[ValueErrorType["Not"] = 27] = "Not";
67
- ValueErrorType[ValueErrorType["Null"] = 28] = "Null";
68
- ValueErrorType[ValueErrorType["Number"] = 29] = "Number";
69
- ValueErrorType[ValueErrorType["NumberMultipleOf"] = 30] = "NumberMultipleOf";
70
- ValueErrorType[ValueErrorType["NumberExclusiveMinimum"] = 31] = "NumberExclusiveMinimum";
71
- ValueErrorType[ValueErrorType["NumberExclusiveMaximum"] = 32] = "NumberExclusiveMaximum";
72
- ValueErrorType[ValueErrorType["NumberMinimum"] = 33] = "NumberMinimum";
73
- ValueErrorType[ValueErrorType["NumberMaximum"] = 34] = "NumberMaximum";
74
- ValueErrorType[ValueErrorType["Object"] = 35] = "Object";
75
- ValueErrorType[ValueErrorType["ObjectMinProperties"] = 36] = "ObjectMinProperties";
76
- ValueErrorType[ValueErrorType["ObjectMaxProperties"] = 37] = "ObjectMaxProperties";
77
- ValueErrorType[ValueErrorType["ObjectAdditionalProperties"] = 38] = "ObjectAdditionalProperties";
78
- ValueErrorType[ValueErrorType["ObjectRequiredProperties"] = 39] = "ObjectRequiredProperties";
79
- ValueErrorType[ValueErrorType["Promise"] = 40] = "Promise";
80
- ValueErrorType[ValueErrorType["RecordKeyNumeric"] = 41] = "RecordKeyNumeric";
81
- ValueErrorType[ValueErrorType["RecordKeyString"] = 42] = "RecordKeyString";
82
- ValueErrorType[ValueErrorType["String"] = 43] = "String";
83
- ValueErrorType[ValueErrorType["StringMinLength"] = 44] = "StringMinLength";
84
- ValueErrorType[ValueErrorType["StringMaxLength"] = 45] = "StringMaxLength";
85
- ValueErrorType[ValueErrorType["StringPattern"] = 46] = "StringPattern";
86
- ValueErrorType[ValueErrorType["StringFormatUnknown"] = 47] = "StringFormatUnknown";
87
- ValueErrorType[ValueErrorType["StringFormat"] = 48] = "StringFormat";
88
- ValueErrorType[ValueErrorType["Symbol"] = 49] = "Symbol";
89
- ValueErrorType[ValueErrorType["TupleZeroLength"] = 50] = "TupleZeroLength";
90
- ValueErrorType[ValueErrorType["TupleLength"] = 51] = "TupleLength";
91
- ValueErrorType[ValueErrorType["Undefined"] = 52] = "Undefined";
92
- ValueErrorType[ValueErrorType["Union"] = 53] = "Union";
93
- ValueErrorType[ValueErrorType["Uint8Array"] = 54] = "Uint8Array";
94
- ValueErrorType[ValueErrorType["Uint8ArrayMinByteLength"] = 55] = "Uint8ArrayMinByteLength";
95
- ValueErrorType[ValueErrorType["Uint8ArrayMaxByteLength"] = 56] = "Uint8ArrayMaxByteLength";
96
- ValueErrorType[ValueErrorType["Void"] = 57] = "Void";
97
- ValueErrorType[ValueErrorType["Custom"] = 58] = "Custom";
43
+ ValueErrorType[ValueErrorType["ArrayContains"] = 3] = "ArrayContains";
44
+ ValueErrorType[ValueErrorType["ArrayMinContains"] = 4] = "ArrayMinContains";
45
+ ValueErrorType[ValueErrorType["ArrayMaxContains"] = 5] = "ArrayMaxContains";
46
+ ValueErrorType[ValueErrorType["ArrayUniqueItems"] = 6] = "ArrayUniqueItems";
47
+ ValueErrorType[ValueErrorType["AsyncIterator"] = 7] = "AsyncIterator";
48
+ ValueErrorType[ValueErrorType["BigInt"] = 8] = "BigInt";
49
+ ValueErrorType[ValueErrorType["BigIntMultipleOf"] = 9] = "BigIntMultipleOf";
50
+ ValueErrorType[ValueErrorType["BigIntExclusiveMinimum"] = 10] = "BigIntExclusiveMinimum";
51
+ ValueErrorType[ValueErrorType["BigIntExclusiveMaximum"] = 11] = "BigIntExclusiveMaximum";
52
+ ValueErrorType[ValueErrorType["BigIntMinimum"] = 12] = "BigIntMinimum";
53
+ ValueErrorType[ValueErrorType["BigIntMaximum"] = 13] = "BigIntMaximum";
54
+ ValueErrorType[ValueErrorType["Boolean"] = 14] = "Boolean";
55
+ ValueErrorType[ValueErrorType["Date"] = 15] = "Date";
56
+ ValueErrorType[ValueErrorType["DateExclusiveMinimumTimestamp"] = 16] = "DateExclusiveMinimumTimestamp";
57
+ ValueErrorType[ValueErrorType["DateExclusiveMaximumTimestamp"] = 17] = "DateExclusiveMaximumTimestamp";
58
+ ValueErrorType[ValueErrorType["DateMinimumTimestamp"] = 18] = "DateMinimumTimestamp";
59
+ ValueErrorType[ValueErrorType["DateMaximumTimestamp"] = 19] = "DateMaximumTimestamp";
60
+ ValueErrorType[ValueErrorType["Function"] = 20] = "Function";
61
+ ValueErrorType[ValueErrorType["Integer"] = 21] = "Integer";
62
+ ValueErrorType[ValueErrorType["IntegerMultipleOf"] = 22] = "IntegerMultipleOf";
63
+ ValueErrorType[ValueErrorType["IntegerExclusiveMinimum"] = 23] = "IntegerExclusiveMinimum";
64
+ ValueErrorType[ValueErrorType["IntegerExclusiveMaximum"] = 24] = "IntegerExclusiveMaximum";
65
+ ValueErrorType[ValueErrorType["IntegerMinimum"] = 25] = "IntegerMinimum";
66
+ ValueErrorType[ValueErrorType["IntegerMaximum"] = 26] = "IntegerMaximum";
67
+ ValueErrorType[ValueErrorType["Intersect"] = 27] = "Intersect";
68
+ ValueErrorType[ValueErrorType["IntersectUnevaluatedProperties"] = 28] = "IntersectUnevaluatedProperties";
69
+ ValueErrorType[ValueErrorType["Iterator"] = 29] = "Iterator";
70
+ ValueErrorType[ValueErrorType["Literal"] = 30] = "Literal";
71
+ ValueErrorType[ValueErrorType["Never"] = 31] = "Never";
72
+ ValueErrorType[ValueErrorType["Not"] = 32] = "Not";
73
+ ValueErrorType[ValueErrorType["Null"] = 33] = "Null";
74
+ ValueErrorType[ValueErrorType["Number"] = 34] = "Number";
75
+ ValueErrorType[ValueErrorType["NumberMultipleOf"] = 35] = "NumberMultipleOf";
76
+ ValueErrorType[ValueErrorType["NumberExclusiveMinimum"] = 36] = "NumberExclusiveMinimum";
77
+ ValueErrorType[ValueErrorType["NumberExclusiveMaximum"] = 37] = "NumberExclusiveMaximum";
78
+ ValueErrorType[ValueErrorType["NumberMinimum"] = 38] = "NumberMinimum";
79
+ ValueErrorType[ValueErrorType["NumberMaximum"] = 39] = "NumberMaximum";
80
+ ValueErrorType[ValueErrorType["Object"] = 40] = "Object";
81
+ ValueErrorType[ValueErrorType["ObjectMinProperties"] = 41] = "ObjectMinProperties";
82
+ ValueErrorType[ValueErrorType["ObjectMaxProperties"] = 42] = "ObjectMaxProperties";
83
+ ValueErrorType[ValueErrorType["ObjectAdditionalProperties"] = 43] = "ObjectAdditionalProperties";
84
+ ValueErrorType[ValueErrorType["ObjectRequiredProperties"] = 44] = "ObjectRequiredProperties";
85
+ ValueErrorType[ValueErrorType["Promise"] = 45] = "Promise";
86
+ ValueErrorType[ValueErrorType["RecordKeyNumeric"] = 46] = "RecordKeyNumeric";
87
+ ValueErrorType[ValueErrorType["RecordKeyString"] = 47] = "RecordKeyString";
88
+ ValueErrorType[ValueErrorType["String"] = 48] = "String";
89
+ ValueErrorType[ValueErrorType["StringMinLength"] = 49] = "StringMinLength";
90
+ ValueErrorType[ValueErrorType["StringMaxLength"] = 50] = "StringMaxLength";
91
+ ValueErrorType[ValueErrorType["StringPattern"] = 51] = "StringPattern";
92
+ ValueErrorType[ValueErrorType["StringFormatUnknown"] = 52] = "StringFormatUnknown";
93
+ ValueErrorType[ValueErrorType["StringFormat"] = 53] = "StringFormat";
94
+ ValueErrorType[ValueErrorType["Symbol"] = 54] = "Symbol";
95
+ ValueErrorType[ValueErrorType["TupleZeroLength"] = 55] = "TupleZeroLength";
96
+ ValueErrorType[ValueErrorType["TupleLength"] = 56] = "TupleLength";
97
+ ValueErrorType[ValueErrorType["Undefined"] = 57] = "Undefined";
98
+ ValueErrorType[ValueErrorType["Union"] = 58] = "Union";
99
+ ValueErrorType[ValueErrorType["Uint8Array"] = 59] = "Uint8Array";
100
+ ValueErrorType[ValueErrorType["Uint8ArrayMinByteLength"] = 60] = "Uint8ArrayMinByteLength";
101
+ ValueErrorType[ValueErrorType["Uint8ArrayMaxByteLength"] = 61] = "Uint8ArrayMaxByteLength";
102
+ ValueErrorType[ValueErrorType["Void"] = 62] = "Void";
103
+ ValueErrorType[ValueErrorType["Kind"] = 63] = "Kind";
98
104
  })(ValueErrorType || (exports.ValueErrorType = ValueErrorType = {}));
99
- // -------------------------------------------------------------------
105
+ // --------------------------------------------------------------------------
100
106
  // ValueErrorIterator
101
- // -------------------------------------------------------------------
107
+ // --------------------------------------------------------------------------
102
108
  class ValueErrorIterator {
103
109
  constructor(iterator) {
104
110
  this.iterator = iterator;
@@ -113,9 +119,9 @@ class ValueErrorIterator {
113
119
  }
114
120
  }
115
121
  exports.ValueErrorIterator = ValueErrorIterator;
116
- // -------------------------------------------------------------------
122
+ // --------------------------------------------------------------------------
117
123
  // ValueErrors
118
- // -------------------------------------------------------------------
124
+ // --------------------------------------------------------------------------
119
125
  class ValueErrorsUnknownTypeError extends Error {
120
126
  constructor(schema) {
121
127
  super('ValueErrors: Unknown type');
@@ -130,482 +136,499 @@ class ValueErrorsDereferenceError extends Error {
130
136
  }
131
137
  }
132
138
  exports.ValueErrorsDereferenceError = ValueErrorsDereferenceError;
133
- /** Provides functionality to generate a sequence of errors against a TypeBox type. */
134
- var ValueErrors;
135
- (function (ValueErrors) {
136
- // ----------------------------------------------------------------------
137
- // Guards
138
- // ----------------------------------------------------------------------
139
- function IsBigInt(value) {
140
- return typeof value === 'bigint';
141
- }
142
- function IsInteger(value) {
143
- return globalThis.Number.isInteger(value);
144
- }
145
- function IsString(value) {
146
- return typeof value === 'string';
147
- }
148
- function IsDefined(value) {
149
- return value !== undefined;
150
- }
151
- // ----------------------------------------------------------------------
152
- // Policies
153
- // ----------------------------------------------------------------------
154
- function IsExactOptionalProperty(value, key) {
155
- return index_1.TypeSystem.ExactOptionalPropertyTypes ? key in value : value[key] !== undefined;
156
- }
157
- function IsObject(value) {
158
- const result = typeof value === 'object' && value !== null;
159
- return index_1.TypeSystem.AllowArrayObjects ? result : result && !globalThis.Array.isArray(value);
160
- }
161
- function IsRecordObject(value) {
162
- return IsObject(value) && !(value instanceof globalThis.Date) && !(value instanceof globalThis.Uint8Array);
163
- }
164
- function IsNumber(value) {
165
- const result = typeof value === 'number';
166
- return index_1.TypeSystem.AllowNaN ? result : result && globalThis.Number.isFinite(value);
167
- }
168
- function IsVoid(value) {
169
- const result = value === undefined;
170
- return index_1.TypeSystem.AllowVoidNull ? result || value === null : result;
171
- }
172
- // ----------------------------------------------------------------------
173
- // Types
174
- // ----------------------------------------------------------------------
175
- function* Any(schema, references, path, value) { }
176
- function* Array(schema, references, path, value) {
177
- if (!globalThis.Array.isArray(value)) {
178
- return yield { type: ValueErrorType.Array, schema, path, value, message: `Expected array` };
179
- }
180
- if (IsDefined(schema.minItems) && !(value.length >= schema.minItems)) {
181
- yield { type: ValueErrorType.ArrayMinItems, schema, path, value, message: `Expected array length to be greater or equal to ${schema.minItems}` };
182
- }
183
- if (IsDefined(schema.maxItems) && !(value.length <= schema.maxItems)) {
184
- yield { type: ValueErrorType.ArrayMinItems, schema, path, value, message: `Expected array length to be less or equal to ${schema.maxItems}` };
185
- }
186
- // prettier-ignore
187
- if (schema.uniqueItems === true && !((function () { const set = new Set(); for (const element of value) {
188
- const hashed = hash_1.ValueHash.Create(element);
189
- if (set.has(hashed)) {
190
- return false;
191
- }
192
- else {
193
- set.add(hashed);
194
- }
195
- } return true; })())) {
196
- yield { type: ValueErrorType.ArrayUniqueItems, schema, path, value, message: `Expected array elements to be unique` };
197
- }
198
- for (let i = 0; i < value.length; i++) {
199
- yield* Visit(schema.items, references, `${path}/${i}`, value[i]);
200
- }
139
+ // --------------------------------------------------------------------------
140
+ // Guards
141
+ // --------------------------------------------------------------------------
142
+ function IsDefined(value) {
143
+ return value !== undefined;
144
+ }
145
+ // --------------------------------------------------------------------------
146
+ // Policies
147
+ // --------------------------------------------------------------------------
148
+ function IsExactOptionalProperty(value, key) {
149
+ return index_1.TypeSystem.ExactOptionalPropertyTypes ? key in value : value[key] !== undefined;
150
+ }
151
+ function IsObject(value) {
152
+ const isObject = ValueGuard.IsObject(value);
153
+ return index_1.TypeSystem.AllowArrayObjects ? isObject : isObject && !ValueGuard.IsArray(value);
154
+ }
155
+ function IsRecordObject(value) {
156
+ return IsObject(value) && !(value instanceof Date) && !(value instanceof Uint8Array);
157
+ }
158
+ function IsNumber(value) {
159
+ const isNumber = ValueGuard.IsNumber(value);
160
+ return index_1.TypeSystem.AllowNaN ? isNumber : isNumber && Number.isFinite(value);
161
+ }
162
+ function IsVoid(value) {
163
+ const isUndefined = ValueGuard.IsUndefined(value);
164
+ return index_1.TypeSystem.AllowVoidNull ? isUndefined || value === null : isUndefined;
165
+ }
166
+ // --------------------------------------------------------------------------
167
+ // Types
168
+ // --------------------------------------------------------------------------
169
+ function* TAny(schema, references, path, value) { }
170
+ function* TArray(schema, references, path, value) {
171
+ if (!ValueGuard.IsArray(value)) {
172
+ return yield { type: ValueErrorType.Array, schema, path, value, message: `Expected array` };
201
173
  }
202
- function* BigInt(schema, references, path, value) {
203
- if (!IsBigInt(value)) {
204
- return yield { type: ValueErrorType.BigInt, schema, path, value, message: `Expected bigint` };
205
- }
206
- if (IsDefined(schema.multipleOf) && !(value % schema.multipleOf === globalThis.BigInt(0))) {
207
- yield { type: ValueErrorType.BigIntMultipleOf, schema, path, value, message: `Expected bigint to be a multiple of ${schema.multipleOf}` };
208
- }
209
- if (IsDefined(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) {
210
- yield { type: ValueErrorType.BigIntExclusiveMinimum, schema, path, value, message: `Expected bigint to be greater than ${schema.exclusiveMinimum}` };
211
- }
212
- if (IsDefined(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
213
- yield { type: ValueErrorType.BigIntExclusiveMaximum, schema, path, value, message: `Expected bigint to be less than ${schema.exclusiveMaximum}` };
214
- }
215
- if (IsDefined(schema.minimum) && !(value >= schema.minimum)) {
216
- yield { type: ValueErrorType.BigIntMinimum, schema, path, value, message: `Expected bigint to be greater or equal to ${schema.minimum}` };
217
- }
218
- if (IsDefined(schema.maximum) && !(value <= schema.maximum)) {
219
- yield { type: ValueErrorType.BigIntMaximum, schema, path, value, message: `Expected bigint to be less or equal to ${schema.maximum}` };
220
- }
174
+ if (IsDefined(schema.minItems) && !(value.length >= schema.minItems)) {
175
+ yield { type: ValueErrorType.ArrayMinItems, schema, path, value, message: `Expected array length to be greater or equal to ${schema.minItems}` };
221
176
  }
222
- function* Boolean(schema, references, path, value) {
223
- if (!(typeof value === 'boolean')) {
224
- return yield { type: ValueErrorType.Boolean, schema, path, value, message: `Expected boolean` };
225
- }
177
+ if (IsDefined(schema.maxItems) && !(value.length <= schema.maxItems)) {
178
+ yield { type: ValueErrorType.ArrayMinItems, schema, path, value, message: `Expected array length to be less or equal to ${schema.maxItems}` };
226
179
  }
227
- function* Constructor(schema, references, path, value) {
228
- yield* Visit(schema.returns, references, path, value.prototype);
180
+ for (let i = 0; i < value.length; i++) {
181
+ yield* Visit(schema.items, references, `${path}/${i}`, value[i]);
229
182
  }
230
- function* Date(schema, references, path, value) {
231
- if (!(value instanceof globalThis.Date)) {
232
- return yield { type: ValueErrorType.Date, schema, path, value, message: `Expected Date object` };
233
- }
234
- if (!globalThis.isFinite(value.getTime())) {
235
- return yield { type: ValueErrorType.Date, schema, path, value, message: `Invalid Date` };
236
- }
237
- if (IsDefined(schema.exclusiveMinimumTimestamp) && !(value.getTime() > schema.exclusiveMinimumTimestamp)) {
238
- yield { type: ValueErrorType.DateExclusiveMinimumTimestamp, schema, path, value, message: `Expected Date timestamp to be greater than ${schema.exclusiveMinimum}` };
239
- }
240
- if (IsDefined(schema.exclusiveMaximumTimestamp) && !(value.getTime() < schema.exclusiveMaximumTimestamp)) {
241
- yield { type: ValueErrorType.DateExclusiveMaximumTimestamp, schema, path, value, message: `Expected Date timestamp to be less than ${schema.exclusiveMaximum}` };
242
- }
243
- if (IsDefined(schema.minimumTimestamp) && !(value.getTime() >= schema.minimumTimestamp)) {
244
- yield { type: ValueErrorType.DateMinimumTimestamp, schema, path, value, message: `Expected Date timestamp to be greater or equal to ${schema.minimum}` };
183
+ // prettier-ignore
184
+ if (schema.uniqueItems === true && !((function () { const set = new Set(); for (const element of value) {
185
+ const hashed = ValueHash.Hash(element);
186
+ if (set.has(hashed)) {
187
+ return false;
245
188
  }
246
- if (IsDefined(schema.maximumTimestamp) && !(value.getTime() <= schema.maximumTimestamp)) {
247
- yield { type: ValueErrorType.DateMaximumTimestamp, schema, path, value, message: `Expected Date timestamp to be less or equal to ${schema.maximum}` };
189
+ else {
190
+ set.add(hashed);
248
191
  }
192
+ } return true; })())) {
193
+ yield { type: ValueErrorType.ArrayUniqueItems, schema, path, value, message: `Expected array elements to be unique` };
249
194
  }
250
- function* Function(schema, references, path, value) {
251
- if (!(typeof value === 'function')) {
252
- return yield { type: ValueErrorType.Function, schema, path, value, message: `Expected function` };
253
- }
195
+ // contains
196
+ if (!(IsDefined(schema.contains) || IsNumber(schema.minContains) || IsNumber(schema.maxContains))) {
197
+ return;
254
198
  }
255
- function* Integer(schema, references, path, value) {
256
- if (!IsInteger(value)) {
257
- return yield { type: ValueErrorType.Integer, schema, path, value, message: `Expected integer` };
258
- }
259
- if (IsDefined(schema.multipleOf) && !(value % schema.multipleOf === 0)) {
260
- yield { type: ValueErrorType.IntegerMultipleOf, schema, path, value, message: `Expected integer to be a multiple of ${schema.multipleOf}` };
261
- }
262
- if (IsDefined(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) {
263
- yield { type: ValueErrorType.IntegerExclusiveMinimum, schema, path, value, message: `Expected integer to be greater than ${schema.exclusiveMinimum}` };
264
- }
265
- if (IsDefined(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
266
- yield { type: ValueErrorType.IntegerExclusiveMaximum, schema, path, value, message: `Expected integer to be less than ${schema.exclusiveMaximum}` };
267
- }
268
- if (IsDefined(schema.minimum) && !(value >= schema.minimum)) {
269
- yield { type: ValueErrorType.IntegerMinimum, schema, path, value, message: `Expected integer to be greater or equal to ${schema.minimum}` };
270
- }
271
- if (IsDefined(schema.maximum) && !(value <= schema.maximum)) {
272
- yield { type: ValueErrorType.IntegerMaximum, schema, path, value, message: `Expected integer to be less or equal to ${schema.maximum}` };
273
- }
199
+ const containsSchema = IsDefined(schema.contains) ? schema.contains : Types.Type.Never();
200
+ const containsCount = value.reduce((acc, value, index) => (Visit(containsSchema, references, `${path}${index}`, value).next().done === true ? acc + 1 : acc), 0);
201
+ if (containsCount === 0) {
202
+ yield { type: ValueErrorType.ArrayContains, schema, path, value, message: `Expected array to contain at least one matching type` };
274
203
  }
275
- function* Intersect(schema, references, path, value) {
276
- for (const inner of schema.allOf) {
277
- const next = Visit(inner, references, path, value).next();
278
- if (!next.done) {
279
- yield next.value;
280
- yield { type: ValueErrorType.Intersect, schema, path, value, message: `Expected all sub schemas to be valid` };
281
- return;
282
- }
204
+ if (ValueGuard.IsNumber(schema.minContains) && containsCount < schema.minContains) {
205
+ yield { type: ValueErrorType.ArrayMinContains, schema, path, value, message: `Expected array to contain at least ${schema.minContains} matching types` };
206
+ }
207
+ if (ValueGuard.IsNumber(schema.maxContains) && containsCount > schema.maxContains) {
208
+ yield { type: ValueErrorType.ArrayMaxContains, schema, path, value, message: `Expected array to contain no more than ${schema.maxContains} matching types` };
209
+ }
210
+ }
211
+ function* TAsyncIterator(schema, references, path, value) {
212
+ if (!ValueGuard.IsAsyncIterator(value)) {
213
+ yield { type: ValueErrorType.AsyncIterator, schema, path, value, message: `Expected value to be an async iterator` };
214
+ }
215
+ }
216
+ function* TBigInt(schema, references, path, value) {
217
+ if (!ValueGuard.IsBigInt(value)) {
218
+ return yield { type: ValueErrorType.BigInt, schema, path, value, message: `Expected bigint` };
219
+ }
220
+ if (IsDefined(schema.multipleOf) && !(value % schema.multipleOf === BigInt(0))) {
221
+ yield { type: ValueErrorType.BigIntMultipleOf, schema, path, value, message: `Expected bigint to be a multiple of ${schema.multipleOf}` };
222
+ }
223
+ if (IsDefined(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) {
224
+ yield { type: ValueErrorType.BigIntExclusiveMinimum, schema, path, value, message: `Expected bigint to be greater than ${schema.exclusiveMinimum}` };
225
+ }
226
+ if (IsDefined(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
227
+ yield { type: ValueErrorType.BigIntExclusiveMaximum, schema, path, value, message: `Expected bigint to be less than ${schema.exclusiveMaximum}` };
228
+ }
229
+ if (IsDefined(schema.minimum) && !(value >= schema.minimum)) {
230
+ yield { type: ValueErrorType.BigIntMinimum, schema, path, value, message: `Expected bigint to be greater or equal to ${schema.minimum}` };
231
+ }
232
+ if (IsDefined(schema.maximum) && !(value <= schema.maximum)) {
233
+ yield { type: ValueErrorType.BigIntMaximum, schema, path, value, message: `Expected bigint to be less or equal to ${schema.maximum}` };
234
+ }
235
+ }
236
+ function* TBoolean(schema, references, path, value) {
237
+ if (!ValueGuard.IsBoolean(value)) {
238
+ return yield { type: ValueErrorType.Boolean, schema, path, value, message: `Expected boolean` };
239
+ }
240
+ }
241
+ function* TConstructor(schema, references, path, value) {
242
+ yield* Visit(schema.returns, references, path, value.prototype);
243
+ }
244
+ function* TDate(schema, references, path, value) {
245
+ if (!ValueGuard.IsDate(value)) {
246
+ return yield { type: ValueErrorType.Date, schema, path, value, message: `Expected Date object` };
247
+ }
248
+ if (!isFinite(value.getTime())) {
249
+ return yield { type: ValueErrorType.Date, schema, path, value, message: `Invalid Date` };
250
+ }
251
+ if (IsDefined(schema.exclusiveMinimumTimestamp) && !(value.getTime() > schema.exclusiveMinimumTimestamp)) {
252
+ yield { type: ValueErrorType.DateExclusiveMinimumTimestamp, schema, path, value, message: `Expected Date timestamp to be greater than ${schema.exclusiveMinimum}` };
253
+ }
254
+ if (IsDefined(schema.exclusiveMaximumTimestamp) && !(value.getTime() < schema.exclusiveMaximumTimestamp)) {
255
+ yield { type: ValueErrorType.DateExclusiveMaximumTimestamp, schema, path, value, message: `Expected Date timestamp to be less than ${schema.exclusiveMaximum}` };
256
+ }
257
+ if (IsDefined(schema.minimumTimestamp) && !(value.getTime() >= schema.minimumTimestamp)) {
258
+ yield { type: ValueErrorType.DateMinimumTimestamp, schema, path, value, message: `Expected Date timestamp to be greater or equal to ${schema.minimum}` };
259
+ }
260
+ if (IsDefined(schema.maximumTimestamp) && !(value.getTime() <= schema.maximumTimestamp)) {
261
+ yield { type: ValueErrorType.DateMaximumTimestamp, schema, path, value, message: `Expected Date timestamp to be less or equal to ${schema.maximum}` };
262
+ }
263
+ }
264
+ function* TFunction(schema, references, path, value) {
265
+ if (!ValueGuard.IsFunction(value)) {
266
+ return yield { type: ValueErrorType.Function, schema, path, value, message: `Expected function` };
267
+ }
268
+ }
269
+ function* TInteger(schema, references, path, value) {
270
+ if (!ValueGuard.IsInteger(value)) {
271
+ return yield { type: ValueErrorType.Integer, schema, path, value, message: `Expected integer` };
272
+ }
273
+ if (IsDefined(schema.multipleOf) && !(value % schema.multipleOf === 0)) {
274
+ yield { type: ValueErrorType.IntegerMultipleOf, schema, path, value, message: `Expected integer to be a multiple of ${schema.multipleOf}` };
275
+ }
276
+ if (IsDefined(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) {
277
+ yield { type: ValueErrorType.IntegerExclusiveMinimum, schema, path, value, message: `Expected integer to be greater than ${schema.exclusiveMinimum}` };
278
+ }
279
+ if (IsDefined(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
280
+ yield { type: ValueErrorType.IntegerExclusiveMaximum, schema, path, value, message: `Expected integer to be less than ${schema.exclusiveMaximum}` };
281
+ }
282
+ if (IsDefined(schema.minimum) && !(value >= schema.minimum)) {
283
+ yield { type: ValueErrorType.IntegerMinimum, schema, path, value, message: `Expected integer to be greater or equal to ${schema.minimum}` };
284
+ }
285
+ if (IsDefined(schema.maximum) && !(value <= schema.maximum)) {
286
+ yield { type: ValueErrorType.IntegerMaximum, schema, path, value, message: `Expected integer to be less or equal to ${schema.maximum}` };
287
+ }
288
+ }
289
+ function* TIntersect(schema, references, path, value) {
290
+ for (const inner of schema.allOf) {
291
+ const next = Visit(inner, references, path, value).next();
292
+ if (!next.done) {
293
+ yield next.value;
294
+ yield { type: ValueErrorType.Intersect, schema, path, value, message: `Expected all sub schemas to be valid` };
295
+ return;
283
296
  }
284
- if (schema.unevaluatedProperties === false) {
285
- const keyCheck = new RegExp(Types.KeyResolver.ResolvePattern(schema));
286
- for (const valueKey of globalThis.Object.getOwnPropertyNames(value)) {
287
- if (!keyCheck.test(valueKey)) {
288
- yield { type: ValueErrorType.IntersectUnevaluatedProperties, schema, path: `${path}/${valueKey}`, value, message: `Unexpected property` };
289
- }
297
+ }
298
+ if (schema.unevaluatedProperties === false) {
299
+ const keyCheck = new RegExp(Types.KeyResolver.ResolvePattern(schema));
300
+ for (const valueKey of Object.getOwnPropertyNames(value)) {
301
+ if (!keyCheck.test(valueKey)) {
302
+ yield { type: ValueErrorType.IntersectUnevaluatedProperties, schema, path: `${path}/${valueKey}`, value, message: `Unexpected property` };
290
303
  }
291
304
  }
292
- if (typeof schema.unevaluatedProperties === 'object') {
293
- const keyCheck = new RegExp(Types.KeyResolver.ResolvePattern(schema));
294
- for (const valueKey of globalThis.Object.getOwnPropertyNames(value)) {
295
- if (!keyCheck.test(valueKey)) {
296
- const next = Visit(schema.unevaluatedProperties, references, `${path}/${valueKey}`, value[valueKey]).next();
297
- if (!next.done) {
298
- yield next.value;
299
- yield { type: ValueErrorType.IntersectUnevaluatedProperties, schema, path: `${path}/${valueKey}`, value, message: `Invalid additional property` };
300
- return;
301
- }
305
+ }
306
+ if (typeof schema.unevaluatedProperties === 'object') {
307
+ const keyCheck = new RegExp(Types.KeyResolver.ResolvePattern(schema));
308
+ for (const valueKey of Object.getOwnPropertyNames(value)) {
309
+ if (!keyCheck.test(valueKey)) {
310
+ const next = Visit(schema.unevaluatedProperties, references, `${path}/${valueKey}`, value[valueKey]).next();
311
+ if (!next.done) {
312
+ yield next.value;
313
+ yield { type: ValueErrorType.IntersectUnevaluatedProperties, schema, path: `${path}/${valueKey}`, value, message: `Invalid additional property` };
314
+ return;
302
315
  }
303
316
  }
304
317
  }
305
318
  }
306
- function* Literal(schema, references, path, value) {
307
- if (!(value === schema.const)) {
308
- const error = typeof schema.const === 'string' ? `'${schema.const}'` : schema.const;
309
- return yield { type: ValueErrorType.Literal, schema, path, value, message: `Expected ${error}` };
310
- }
319
+ }
320
+ function* TIterator(schema, references, path, value) {
321
+ if (!(IsObject(value) && Symbol.iterator in value)) {
322
+ yield { type: ValueErrorType.Iterator, schema, path, value, message: `Expected value to be an iterator` };
311
323
  }
312
- function* Never(schema, references, path, value) {
313
- yield { type: ValueErrorType.Never, schema, path, value, message: `Value cannot be validated` };
324
+ }
325
+ function* TLiteral(schema, references, path, value) {
326
+ if (!(value === schema.const)) {
327
+ const error = typeof schema.const === 'string' ? `'${schema.const}'` : schema.const;
328
+ return yield { type: ValueErrorType.Literal, schema, path, value, message: `Expected ${error}` };
314
329
  }
315
- function* Not(schema, references, path, value) {
316
- if (Visit(schema.not, references, path, value).next().done === true) {
317
- yield { type: ValueErrorType.Not, schema, path, value, message: `Value should not validate` };
318
- }
330
+ }
331
+ function* TNever(schema, references, path, value) {
332
+ yield { type: ValueErrorType.Never, schema, path, value, message: `Value cannot be validated` };
333
+ }
334
+ function* TNot(schema, references, path, value) {
335
+ if (Visit(schema.not, references, path, value).next().done === true) {
336
+ yield { type: ValueErrorType.Not, schema, path, value, message: `Value should not validate` };
319
337
  }
320
- function* Null(schema, references, path, value) {
321
- if (!(value === null)) {
322
- return yield { type: ValueErrorType.Null, schema, path, value, message: `Expected null` };
323
- }
338
+ }
339
+ function* TNull(schema, references, path, value) {
340
+ if (!ValueGuard.IsNull(value)) {
341
+ return yield { type: ValueErrorType.Null, schema, path, value, message: `Expected null` };
324
342
  }
325
- function* Number(schema, references, path, value) {
326
- if (!IsNumber(value)) {
327
- return yield { type: ValueErrorType.Number, schema, path, value, message: `Expected number` };
328
- }
329
- if (IsDefined(schema.multipleOf) && !(value % schema.multipleOf === 0)) {
330
- yield { type: ValueErrorType.NumberMultipleOf, schema, path, value, message: `Expected number to be a multiple of ${schema.multipleOf}` };
331
- }
332
- if (IsDefined(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) {
333
- yield { type: ValueErrorType.NumberExclusiveMinimum, schema, path, value, message: `Expected number to be greater than ${schema.exclusiveMinimum}` };
334
- }
335
- if (IsDefined(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
336
- yield { type: ValueErrorType.NumberExclusiveMaximum, schema, path, value, message: `Expected number to be less than ${schema.exclusiveMaximum}` };
337
- }
338
- if (IsDefined(schema.minimum) && !(value >= schema.minimum)) {
339
- yield { type: ValueErrorType.NumberMinimum, schema, path, value, message: `Expected number to be greater or equal to ${schema.minimum}` };
340
- }
341
- if (IsDefined(schema.maximum) && !(value <= schema.maximum)) {
342
- yield { type: ValueErrorType.NumberMaximum, schema, path, value, message: `Expected number to be less or equal to ${schema.maximum}` };
343
- }
343
+ }
344
+ function* TNumber(schema, references, path, value) {
345
+ if (!IsNumber(value)) {
346
+ return yield { type: ValueErrorType.Number, schema, path, value, message: `Expected number` };
344
347
  }
345
- function* Object(schema, references, path, value) {
346
- if (!IsObject(value)) {
347
- return yield { type: ValueErrorType.Object, schema, path, value, message: `Expected object` };
348
- }
349
- if (IsDefined(schema.minProperties) && !(globalThis.Object.getOwnPropertyNames(value).length >= schema.minProperties)) {
350
- yield { type: ValueErrorType.ObjectMinProperties, schema, path, value, message: `Expected object to have at least ${schema.minProperties} properties` };
351
- }
352
- if (IsDefined(schema.maxProperties) && !(globalThis.Object.getOwnPropertyNames(value).length <= schema.maxProperties)) {
353
- yield { type: ValueErrorType.ObjectMaxProperties, schema, path, value, message: `Expected object to have no more than ${schema.maxProperties} properties` };
354
- }
355
- const requiredKeys = globalThis.Array.isArray(schema.required) ? schema.required : [];
356
- const knownKeys = globalThis.Object.getOwnPropertyNames(schema.properties);
357
- const unknownKeys = globalThis.Object.getOwnPropertyNames(value);
358
- for (const knownKey of knownKeys) {
359
- const property = schema.properties[knownKey];
360
- if (schema.required && schema.required.includes(knownKey)) {
361
- yield* Visit(property, references, `${path}/${knownKey}`, value[knownKey]);
362
- if (Types.ExtendsUndefined.Check(schema) && !(knownKey in value)) {
363
- yield { type: ValueErrorType.ObjectRequiredProperties, schema: property, path: `${path}/${knownKey}`, value: undefined, message: `Expected required property` };
364
- }
365
- }
366
- else {
367
- if (IsExactOptionalProperty(value, knownKey)) {
368
- yield* Visit(property, references, `${path}/${knownKey}`, value[knownKey]);
369
- }
370
- }
371
- }
372
- for (const requiredKey of requiredKeys) {
373
- if (unknownKeys.includes(requiredKey))
374
- continue;
375
- yield { type: ValueErrorType.ObjectRequiredProperties, schema: schema.properties[requiredKey], path: `${path}/${requiredKey}`, value: undefined, message: `Expected required property` };
376
- }
377
- if (schema.additionalProperties === false) {
378
- for (const valueKey of unknownKeys) {
379
- if (!knownKeys.includes(valueKey)) {
380
- yield { type: ValueErrorType.ObjectAdditionalProperties, schema, path: `${path}/${valueKey}`, value: value[valueKey], message: `Unexpected property` };
381
- }
348
+ if (IsDefined(schema.multipleOf) && !(value % schema.multipleOf === 0)) {
349
+ yield { type: ValueErrorType.NumberMultipleOf, schema, path, value, message: `Expected number to be a multiple of ${schema.multipleOf}` };
350
+ }
351
+ if (IsDefined(schema.exclusiveMinimum) && !(value > schema.exclusiveMinimum)) {
352
+ yield { type: ValueErrorType.NumberExclusiveMinimum, schema, path, value, message: `Expected number to be greater than ${schema.exclusiveMinimum}` };
353
+ }
354
+ if (IsDefined(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
355
+ yield { type: ValueErrorType.NumberExclusiveMaximum, schema, path, value, message: `Expected number to be less than ${schema.exclusiveMaximum}` };
356
+ }
357
+ if (IsDefined(schema.minimum) && !(value >= schema.minimum)) {
358
+ yield { type: ValueErrorType.NumberMinimum, schema, path, value, message: `Expected number to be greater or equal to ${schema.minimum}` };
359
+ }
360
+ if (IsDefined(schema.maximum) && !(value <= schema.maximum)) {
361
+ yield { type: ValueErrorType.NumberMaximum, schema, path, value, message: `Expected number to be less or equal to ${schema.maximum}` };
362
+ }
363
+ }
364
+ function* TObject(schema, references, path, value) {
365
+ if (!IsObject(value)) {
366
+ return yield { type: ValueErrorType.Object, schema, path, value, message: `Expected object` };
367
+ }
368
+ if (IsDefined(schema.minProperties) && !(Object.getOwnPropertyNames(value).length >= schema.minProperties)) {
369
+ yield { type: ValueErrorType.ObjectMinProperties, schema, path, value, message: `Expected object to have at least ${schema.minProperties} properties` };
370
+ }
371
+ if (IsDefined(schema.maxProperties) && !(Object.getOwnPropertyNames(value).length <= schema.maxProperties)) {
372
+ yield { type: ValueErrorType.ObjectMaxProperties, schema, path, value, message: `Expected object to have no more than ${schema.maxProperties} properties` };
373
+ }
374
+ const requiredKeys = Array.isArray(schema.required) ? schema.required : [];
375
+ const knownKeys = Object.getOwnPropertyNames(schema.properties);
376
+ const unknownKeys = Object.getOwnPropertyNames(value);
377
+ for (const knownKey of knownKeys) {
378
+ const property = schema.properties[knownKey];
379
+ if (schema.required && schema.required.includes(knownKey)) {
380
+ yield* Visit(property, references, `${path}/${knownKey}`, value[knownKey]);
381
+ if (Types.ExtendsUndefined.Check(schema) && !(knownKey in value)) {
382
+ yield { type: ValueErrorType.ObjectRequiredProperties, schema: property, path: `${path}/${knownKey}`, value: undefined, message: `Expected required property` };
382
383
  }
383
384
  }
384
- if (typeof schema.additionalProperties === 'object') {
385
- for (const valueKey of unknownKeys) {
386
- if (knownKeys.includes(valueKey))
387
- continue;
388
- yield* Visit(schema.additionalProperties, references, `${path}/${valueKey}`, value[valueKey]);
385
+ else {
386
+ if (IsExactOptionalProperty(value, knownKey)) {
387
+ yield* Visit(property, references, `${path}/${knownKey}`, value[knownKey]);
389
388
  }
390
389
  }
391
390
  }
392
- function* Promise(schema, references, path, value) {
393
- if (!(typeof value === 'object' && typeof value.then === 'function')) {
394
- yield { type: ValueErrorType.Promise, schema, path, value, message: `Expected Promise` };
395
- }
391
+ for (const requiredKey of requiredKeys) {
392
+ if (unknownKeys.includes(requiredKey))
393
+ continue;
394
+ yield { type: ValueErrorType.ObjectRequiredProperties, schema: schema.properties[requiredKey], path: `${path}/${requiredKey}`, value: undefined, message: `Expected required property` };
396
395
  }
397
- function* Record(schema, references, path, value) {
398
- if (!IsRecordObject(value)) {
399
- return yield { type: ValueErrorType.Object, schema, path, value, message: `Expected record object` };
400
- }
401
- if (IsDefined(schema.minProperties) && !(globalThis.Object.getOwnPropertyNames(value).length >= schema.minProperties)) {
402
- yield { type: ValueErrorType.ObjectMinProperties, schema, path, value, message: `Expected object to have at least ${schema.minProperties} properties` };
403
- }
404
- if (IsDefined(schema.maxProperties) && !(globalThis.Object.getOwnPropertyNames(value).length <= schema.maxProperties)) {
405
- yield { type: ValueErrorType.ObjectMaxProperties, schema, path, value, message: `Expected object to have no more than ${schema.maxProperties} properties` };
396
+ if (schema.additionalProperties === false) {
397
+ for (const valueKey of unknownKeys) {
398
+ if (!knownKeys.includes(valueKey)) {
399
+ yield { type: ValueErrorType.ObjectAdditionalProperties, schema, path: `${path}/${valueKey}`, value: value[valueKey], message: `Unexpected property` };
400
+ }
406
401
  }
407
- const [patternKey, patternSchema] = globalThis.Object.entries(schema.patternProperties)[0];
408
- const regex = new RegExp(patternKey);
409
- for (const [propertyKey, propertyValue] of globalThis.Object.entries(value)) {
410
- if (regex.test(propertyKey)) {
411
- yield* Visit(patternSchema, references, `${path}/${propertyKey}`, propertyValue);
402
+ }
403
+ if (typeof schema.additionalProperties === 'object') {
404
+ for (const valueKey of unknownKeys) {
405
+ if (knownKeys.includes(valueKey))
412
406
  continue;
413
- }
414
- if (typeof schema.additionalProperties === 'object') {
415
- yield* Visit(schema.additionalProperties, references, `${path}/${propertyKey}`, propertyValue);
416
- }
417
- if (schema.additionalProperties === false) {
418
- const propertyPath = `${path}/${propertyKey}`;
419
- const message = `Unexpected property '${propertyPath}'`;
420
- return yield { type: ValueErrorType.ObjectAdditionalProperties, schema, path: propertyPath, value: propertyValue, message };
421
- }
407
+ yield* Visit(schema.additionalProperties, references, `${path}/${valueKey}`, value[valueKey]);
422
408
  }
423
409
  }
424
- function* Ref(schema, references, path, value) {
425
- const index = references.findIndex((foreign) => foreign.$id === schema.$ref);
426
- if (index === -1)
427
- throw new ValueErrorsDereferenceError(schema);
428
- const target = references[index];
429
- yield* Visit(target, references, path, value);
410
+ }
411
+ function* TPromise(schema, references, path, value) {
412
+ if (!ValueGuard.IsPromise(value)) {
413
+ yield { type: ValueErrorType.Promise, schema, path, value, message: `Expected Promise` };
430
414
  }
431
- function* String(schema, references, path, value) {
432
- if (!IsString(value)) {
433
- return yield { type: ValueErrorType.String, schema, path, value, message: 'Expected string' };
434
- }
435
- if (IsDefined(schema.minLength) && !(value.length >= schema.minLength)) {
436
- yield { type: ValueErrorType.StringMinLength, schema, path, value, message: `Expected string length greater or equal to ${schema.minLength}` };
437
- }
438
- if (IsDefined(schema.maxLength) && !(value.length <= schema.maxLength)) {
439
- yield { type: ValueErrorType.StringMaxLength, schema, path, value, message: `Expected string length less or equal to ${schema.maxLength}` };
415
+ }
416
+ function* TRecord(schema, references, path, value) {
417
+ if (!IsRecordObject(value)) {
418
+ return yield { type: ValueErrorType.Object, schema, path, value, message: `Expected record object` };
419
+ }
420
+ if (IsDefined(schema.minProperties) && !(Object.getOwnPropertyNames(value).length >= schema.minProperties)) {
421
+ yield { type: ValueErrorType.ObjectMinProperties, schema, path, value, message: `Expected object to have at least ${schema.minProperties} properties` };
422
+ }
423
+ if (IsDefined(schema.maxProperties) && !(Object.getOwnPropertyNames(value).length <= schema.maxProperties)) {
424
+ yield { type: ValueErrorType.ObjectMaxProperties, schema, path, value, message: `Expected object to have no more than ${schema.maxProperties} properties` };
425
+ }
426
+ const [patternKey, patternSchema] = Object.entries(schema.patternProperties)[0];
427
+ const regex = new RegExp(patternKey);
428
+ for (const [propertyKey, propertyValue] of Object.entries(value)) {
429
+ if (regex.test(propertyKey)) {
430
+ yield* Visit(patternSchema, references, `${path}/${propertyKey}`, propertyValue);
431
+ continue;
440
432
  }
441
- if (schema.pattern !== undefined) {
442
- const regex = new RegExp(schema.pattern);
443
- if (!regex.test(value)) {
444
- yield { type: ValueErrorType.StringPattern, schema, path, value, message: `Expected string to match pattern ${schema.pattern}` };
445
- }
433
+ if (typeof schema.additionalProperties === 'object') {
434
+ yield* Visit(schema.additionalProperties, references, `${path}/${propertyKey}`, propertyValue);
446
435
  }
447
- if (schema.format !== undefined) {
448
- if (!Types.FormatRegistry.Has(schema.format)) {
449
- yield { type: ValueErrorType.StringFormatUnknown, schema, path, value, message: `Unknown string format '${schema.format}'` };
450
- }
451
- else {
452
- const format = Types.FormatRegistry.Get(schema.format);
453
- if (!format(value)) {
454
- yield { type: ValueErrorType.StringFormat, schema, path, value, message: `Expected string to match format '${schema.format}'` };
455
- }
456
- }
436
+ if (schema.additionalProperties === false) {
437
+ const propertyPath = `${path}/${propertyKey}`;
438
+ const message = `Unexpected property '${propertyPath}'`;
439
+ return yield { type: ValueErrorType.ObjectAdditionalProperties, schema, path: propertyPath, value: propertyValue, message };
457
440
  }
458
441
  }
459
- function* Symbol(schema, references, path, value) {
460
- if (!(typeof value === 'symbol')) {
461
- return yield { type: ValueErrorType.Symbol, schema, path, value, message: 'Expected symbol' };
462
- }
442
+ }
443
+ function* TRef(schema, references, path, value) {
444
+ const index = references.findIndex((foreign) => foreign.$id === schema.$ref);
445
+ if (index === -1)
446
+ throw new ValueErrorsDereferenceError(schema);
447
+ const target = references[index];
448
+ yield* Visit(target, references, path, value);
449
+ }
450
+ function* TString(schema, references, path, value) {
451
+ if (!ValueGuard.IsString(value)) {
452
+ return yield { type: ValueErrorType.String, schema, path, value, message: 'Expected string' };
463
453
  }
464
- function* TemplateLiteral(schema, references, path, value) {
465
- if (!IsString(value)) {
466
- return yield { type: ValueErrorType.String, schema, path, value, message: 'Expected string' };
467
- }
454
+ if (IsDefined(schema.minLength) && !(value.length >= schema.minLength)) {
455
+ yield { type: ValueErrorType.StringMinLength, schema, path, value, message: `Expected string length greater or equal to ${schema.minLength}` };
456
+ }
457
+ if (IsDefined(schema.maxLength) && !(value.length <= schema.maxLength)) {
458
+ yield { type: ValueErrorType.StringMaxLength, schema, path, value, message: `Expected string length less or equal to ${schema.maxLength}` };
459
+ }
460
+ if (ValueGuard.IsString(schema.pattern)) {
468
461
  const regex = new RegExp(schema.pattern);
469
462
  if (!regex.test(value)) {
470
463
  yield { type: ValueErrorType.StringPattern, schema, path, value, message: `Expected string to match pattern ${schema.pattern}` };
471
464
  }
472
465
  }
473
- function* This(schema, references, path, value) {
474
- const index = references.findIndex((foreign) => foreign.$id === schema.$ref);
475
- if (index === -1)
476
- throw new ValueErrorsDereferenceError(schema);
477
- const target = references[index];
478
- yield* Visit(target, references, path, value);
479
- }
480
- function* Tuple(schema, references, path, value) {
481
- if (!globalThis.Array.isArray(value)) {
482
- return yield { type: ValueErrorType.Array, schema, path, value, message: 'Expected Array' };
466
+ if (ValueGuard.IsString(schema.format)) {
467
+ if (!Types.FormatRegistry.Has(schema.format)) {
468
+ yield { type: ValueErrorType.StringFormatUnknown, schema, path, value, message: `Unknown string format '${schema.format}'` };
483
469
  }
484
- if (schema.items === undefined && !(value.length === 0)) {
485
- return yield { type: ValueErrorType.TupleZeroLength, schema, path, value, message: 'Expected tuple to have 0 elements' };
486
- }
487
- if (!(value.length === schema.maxItems)) {
488
- yield { type: ValueErrorType.TupleLength, schema, path, value, message: `Expected tuple to have ${schema.maxItems} elements` };
470
+ else {
471
+ const format = Types.FormatRegistry.Get(schema.format);
472
+ if (!format(value)) {
473
+ yield { type: ValueErrorType.StringFormat, schema, path, value, message: `Expected string to match format '${schema.format}'` };
474
+ }
489
475
  }
490
- if (!schema.items) {
476
+ }
477
+ }
478
+ function* TSymbol(schema, references, path, value) {
479
+ if (!ValueGuard.IsSymbol(value)) {
480
+ return yield { type: ValueErrorType.Symbol, schema, path, value, message: 'Expected symbol' };
481
+ }
482
+ }
483
+ function* TTemplateLiteral(schema, references, path, value) {
484
+ if (!ValueGuard.IsString(value)) {
485
+ return yield { type: ValueErrorType.String, schema, path, value, message: 'Expected string' };
486
+ }
487
+ const regex = new RegExp(schema.pattern);
488
+ if (!regex.test(value)) {
489
+ yield { type: ValueErrorType.StringPattern, schema, path, value, message: `Expected string to match pattern ${schema.pattern}` };
490
+ }
491
+ }
492
+ function* TThis(schema, references, path, value) {
493
+ const index = references.findIndex((foreign) => foreign.$id === schema.$ref);
494
+ if (index === -1)
495
+ throw new ValueErrorsDereferenceError(schema);
496
+ const target = references[index];
497
+ yield* Visit(target, references, path, value);
498
+ }
499
+ function* TTuple(schema, references, path, value) {
500
+ if (!ValueGuard.IsArray(value)) {
501
+ return yield { type: ValueErrorType.Array, schema, path, value, message: 'Expected Array' };
502
+ }
503
+ if (schema.items === undefined && !(value.length === 0)) {
504
+ return yield { type: ValueErrorType.TupleZeroLength, schema, path, value, message: 'Expected tuple to have 0 elements' };
505
+ }
506
+ if (!(value.length === schema.maxItems)) {
507
+ yield { type: ValueErrorType.TupleLength, schema, path, value, message: `Expected tuple to have ${schema.maxItems} elements` };
508
+ }
509
+ if (!schema.items) {
510
+ return;
511
+ }
512
+ for (let i = 0; i < schema.items.length; i++) {
513
+ yield* Visit(schema.items[i], references, `${path}/${i}`, value[i]);
514
+ }
515
+ }
516
+ function* TUndefined(schema, references, path, value) {
517
+ if (!(value === undefined)) {
518
+ yield { type: ValueErrorType.Undefined, schema, path, value, message: `Expected undefined` };
519
+ }
520
+ }
521
+ function* TUnion(schema, references, path, value) {
522
+ const errors = [];
523
+ for (const inner of schema.anyOf) {
524
+ const variantErrors = [...Visit(inner, references, path, value)];
525
+ if (variantErrors.length === 0)
491
526
  return;
492
- }
493
- for (let i = 0; i < schema.items.length; i++) {
494
- yield* Visit(schema.items[i], references, `${path}/${i}`, value[i]);
495
- }
527
+ errors.push(...variantErrors);
496
528
  }
497
- function* Undefined(schema, references, path, value) {
498
- if (!(value === undefined)) {
499
- yield { type: ValueErrorType.Undefined, schema, path, value, message: `Expected undefined` };
500
- }
529
+ if (errors.length > 0) {
530
+ yield { type: ValueErrorType.Union, schema, path, value, message: 'Expected value of union' };
501
531
  }
502
- function* Union(schema, references, path, value) {
503
- const errors = [];
504
- for (const inner of schema.anyOf) {
505
- const variantErrors = [...Visit(inner, references, path, value)];
506
- if (variantErrors.length === 0)
507
- return;
508
- errors.push(...variantErrors);
509
- }
510
- if (errors.length > 0) {
511
- yield { type: ValueErrorType.Union, schema, path, value, message: 'Expected value of union' };
512
- }
513
- for (const error of errors) {
514
- yield error;
515
- }
532
+ for (const error of errors) {
533
+ yield error;
516
534
  }
517
- function* Uint8Array(schema, references, path, value) {
518
- if (!(value instanceof globalThis.Uint8Array)) {
519
- return yield { type: ValueErrorType.Uint8Array, schema, path, value, message: `Expected Uint8Array` };
520
- }
521
- if (IsDefined(schema.maxByteLength) && !(value.length <= schema.maxByteLength)) {
522
- yield { type: ValueErrorType.Uint8ArrayMaxByteLength, schema, path, value, message: `Expected Uint8Array to have a byte length less or equal to ${schema.maxByteLength}` };
523
- }
524
- if (IsDefined(schema.minByteLength) && !(value.length >= schema.minByteLength)) {
525
- yield { type: ValueErrorType.Uint8ArrayMinByteLength, schema, path, value, message: `Expected Uint8Array to have a byte length greater or equal to ${schema.maxByteLength}` };
526
- }
535
+ }
536
+ function* TUint8Array(schema, references, path, value) {
537
+ if (!ValueGuard.IsUint8Array(value)) {
538
+ return yield { type: ValueErrorType.Uint8Array, schema, path, value, message: `Expected Uint8Array` };
527
539
  }
528
- function* Unknown(schema, references, path, value) { }
529
- function* Void(schema, references, path, value) {
530
- if (!IsVoid(value)) {
531
- return yield { type: ValueErrorType.Void, schema, path, value, message: `Expected void` };
532
- }
540
+ if (IsDefined(schema.maxByteLength) && !(value.length <= schema.maxByteLength)) {
541
+ yield { type: ValueErrorType.Uint8ArrayMaxByteLength, schema, path, value, message: `Expected Uint8Array to have a byte length less or equal to ${schema.maxByteLength}` };
533
542
  }
534
- function* UserDefined(schema, references, path, value) {
535
- const check = Types.TypeRegistry.Get(schema[Types.Kind]);
536
- if (!check(schema, value)) {
537
- return yield { type: ValueErrorType.Custom, schema, path, value, message: `Expected kind ${schema[Types.Kind]}` };
538
- }
543
+ if (IsDefined(schema.minByteLength) && !(value.length >= schema.minByteLength)) {
544
+ yield { type: ValueErrorType.Uint8ArrayMinByteLength, schema, path, value, message: `Expected Uint8Array to have a byte length greater or equal to ${schema.maxByteLength}` };
539
545
  }
540
- function* Visit(schema, references, path, value) {
541
- const references_ = IsDefined(schema.$id) ? [...references, schema] : references;
542
- const schema_ = schema;
543
- switch (schema_[Types.Kind]) {
544
- case 'Any':
545
- return yield* Any(schema_, references_, path, value);
546
- case 'Array':
547
- return yield* Array(schema_, references_, path, value);
548
- case 'BigInt':
549
- return yield* BigInt(schema_, references_, path, value);
550
- case 'Boolean':
551
- return yield* Boolean(schema_, references_, path, value);
552
- case 'Constructor':
553
- return yield* Constructor(schema_, references_, path, value);
554
- case 'Date':
555
- return yield* Date(schema_, references_, path, value);
556
- case 'Function':
557
- return yield* Function(schema_, references_, path, value);
558
- case 'Integer':
559
- return yield* Integer(schema_, references_, path, value);
560
- case 'Intersect':
561
- return yield* Intersect(schema_, references_, path, value);
562
- case 'Literal':
563
- return yield* Literal(schema_, references_, path, value);
564
- case 'Never':
565
- return yield* Never(schema_, references_, path, value);
566
- case 'Not':
567
- return yield* Not(schema_, references_, path, value);
568
- case 'Null':
569
- return yield* Null(schema_, references_, path, value);
570
- case 'Number':
571
- return yield* Number(schema_, references_, path, value);
572
- case 'Object':
573
- return yield* Object(schema_, references_, path, value);
574
- case 'Promise':
575
- return yield* Promise(schema_, references_, path, value);
576
- case 'Record':
577
- return yield* Record(schema_, references_, path, value);
578
- case 'Ref':
579
- return yield* Ref(schema_, references_, path, value);
580
- case 'String':
581
- return yield* String(schema_, references_, path, value);
582
- case 'Symbol':
583
- return yield* Symbol(schema_, references_, path, value);
584
- case 'TemplateLiteral':
585
- return yield* TemplateLiteral(schema_, references_, path, value);
586
- case 'This':
587
- return yield* This(schema_, references_, path, value);
588
- case 'Tuple':
589
- return yield* Tuple(schema_, references_, path, value);
590
- case 'Undefined':
591
- return yield* Undefined(schema_, references_, path, value);
592
- case 'Union':
593
- return yield* Union(schema_, references_, path, value);
594
- case 'Uint8Array':
595
- return yield* Uint8Array(schema_, references_, path, value);
596
- case 'Unknown':
597
- return yield* Unknown(schema_, references_, path, value);
598
- case 'Void':
599
- return yield* Void(schema_, references_, path, value);
600
- default:
601
- if (!Types.TypeRegistry.Has(schema_[Types.Kind]))
602
- throw new ValueErrorsUnknownTypeError(schema);
603
- return yield* UserDefined(schema_, references_, path, value);
604
- }
546
+ }
547
+ function* TUnknown(schema, references, path, value) { }
548
+ function* TVoid(schema, references, path, value) {
549
+ if (!IsVoid(value)) {
550
+ return yield { type: ValueErrorType.Void, schema, path, value, message: `Expected void` };
605
551
  }
606
- function Errors(schema, references, value) {
607
- const iterator = Visit(schema, references, '', value);
608
- return new ValueErrorIterator(iterator);
552
+ }
553
+ function* TKind(schema, references, path, value) {
554
+ const check = Types.TypeRegistry.Get(schema[Types.Kind]);
555
+ if (!check(schema, value)) {
556
+ return yield { type: ValueErrorType.Kind, schema, path, value, message: `Expected kind ${schema[Types.Kind]}` };
557
+ }
558
+ }
559
+ function* Visit(schema, references, path, value) {
560
+ const references_ = IsDefined(schema.$id) ? [...references, schema] : references;
561
+ const schema_ = schema;
562
+ switch (schema_[Types.Kind]) {
563
+ case 'Any':
564
+ return yield* TAny(schema_, references_, path, value);
565
+ case 'Array':
566
+ return yield* TArray(schema_, references_, path, value);
567
+ case 'AsyncIterator':
568
+ return yield* TAsyncIterator(schema_, references_, path, value);
569
+ case 'BigInt':
570
+ return yield* TBigInt(schema_, references_, path, value);
571
+ case 'Boolean':
572
+ return yield* TBoolean(schema_, references_, path, value);
573
+ case 'Constructor':
574
+ return yield* TConstructor(schema_, references_, path, value);
575
+ case 'Date':
576
+ return yield* TDate(schema_, references_, path, value);
577
+ case 'Function':
578
+ return yield* TFunction(schema_, references_, path, value);
579
+ case 'Integer':
580
+ return yield* TInteger(schema_, references_, path, value);
581
+ case 'Intersect':
582
+ return yield* TIntersect(schema_, references_, path, value);
583
+ case 'Iterator':
584
+ return yield* TIterator(schema_, references_, path, value);
585
+ case 'Literal':
586
+ return yield* TLiteral(schema_, references_, path, value);
587
+ case 'Never':
588
+ return yield* TNever(schema_, references_, path, value);
589
+ case 'Not':
590
+ return yield* TNot(schema_, references_, path, value);
591
+ case 'Null':
592
+ return yield* TNull(schema_, references_, path, value);
593
+ case 'Number':
594
+ return yield* TNumber(schema_, references_, path, value);
595
+ case 'Object':
596
+ return yield* TObject(schema_, references_, path, value);
597
+ case 'Promise':
598
+ return yield* TPromise(schema_, references_, path, value);
599
+ case 'Record':
600
+ return yield* TRecord(schema_, references_, path, value);
601
+ case 'Ref':
602
+ return yield* TRef(schema_, references_, path, value);
603
+ case 'String':
604
+ return yield* TString(schema_, references_, path, value);
605
+ case 'Symbol':
606
+ return yield* TSymbol(schema_, references_, path, value);
607
+ case 'TemplateLiteral':
608
+ return yield* TTemplateLiteral(schema_, references_, path, value);
609
+ case 'This':
610
+ return yield* TThis(schema_, references_, path, value);
611
+ case 'Tuple':
612
+ return yield* TTuple(schema_, references_, path, value);
613
+ case 'Undefined':
614
+ return yield* TUndefined(schema_, references_, path, value);
615
+ case 'Union':
616
+ return yield* TUnion(schema_, references_, path, value);
617
+ case 'Uint8Array':
618
+ return yield* TUint8Array(schema_, references_, path, value);
619
+ case 'Unknown':
620
+ return yield* TUnknown(schema_, references_, path, value);
621
+ case 'Void':
622
+ return yield* TVoid(schema_, references_, path, value);
623
+ default:
624
+ if (!Types.TypeRegistry.Has(schema_[Types.Kind]))
625
+ throw new ValueErrorsUnknownTypeError(schema);
626
+ return yield* TKind(schema_, references_, path, value);
609
627
  }
610
- ValueErrors.Errors = Errors;
611
- })(ValueErrors || (exports.ValueErrors = ValueErrors = {}));
628
+ }
629
+ /** Returns an iterator for each error in this value. */
630
+ function Errors(...args) {
631
+ const iterator = args.length === 3 ? Visit(args[0], args[1], '', args[2]) : Visit(args[0], [], '', args[1]);
632
+ return new ValueErrorIterator(iterator);
633
+ }
634
+ exports.Errors = Errors;