@jarenjs/validate 0.8.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/dist/index.js ADDED
@@ -0,0 +1,1802 @@
1
+ // ../core/src/index.js
2
+ function isFn(data) {
3
+ return typeof data === "function";
4
+ }
5
+ function isScalarTypeEx(typeName) {
6
+ switch (typeName) {
7
+ case "string":
8
+ case "number":
9
+ case "boolean":
10
+ case "integer":
11
+ case "bigint":
12
+ return true;
13
+ default:
14
+ return false;
15
+ }
16
+ }
17
+ function isScalarType(data) {
18
+ return data != null && isScalarTypeEx(typeof data);
19
+ }
20
+ function isStringType(data) {
21
+ return typeof data === "string";
22
+ }
23
+ function isBooleanType(data) {
24
+ return data === true || data === false;
25
+ }
26
+ function isNumberType(data) {
27
+ return data != null && typeof data === "number";
28
+ }
29
+ function isIntegerType(data) {
30
+ return Number.isInteger(data);
31
+ }
32
+ function isBigIntType(data) {
33
+ return typeof data === "bigint";
34
+ }
35
+ function getStringType(obj, def = void 0) {
36
+ return isStringType(obj) ? obj : def;
37
+ }
38
+ function getBigIntType(obj, def = void 0) {
39
+ return isBigIntType(obj) ? obj : def;
40
+ }
41
+ function isNullValue(data) {
42
+ return data === null;
43
+ }
44
+ function isObjectOfClass(data, type) {
45
+ return data != null && data.constructor === type;
46
+ }
47
+ function isObjectClass(data) {
48
+ return isObjectOfClass(data, Object);
49
+ }
50
+ function isMapClass(data) {
51
+ return isObjectOfClass(data, Map);
52
+ }
53
+ function isObjectType(data) {
54
+ return data != null && typeof data === "object" && data.constructor !== Array;
55
+ }
56
+ function isArrayClass(data) {
57
+ return isObjectOfClass(data, Array);
58
+ }
59
+ function isSetClass(data) {
60
+ return isObjectOfClass(data, Set);
61
+ }
62
+ var TypedArray = Object.getPrototypeOf(Uint8Array);
63
+ function isTypedArray(data) {
64
+ if (data == null)
65
+ return false;
66
+ const proto = data.__proto__.__proto__;
67
+ return proto != null && proto.constructor === TypedArray;
68
+ }
69
+ function getObjectType(obj, def = void 0) {
70
+ return isObjectClass(obj) ? obj : def;
71
+ }
72
+ function getInclusiveExclusiveBounds(getType, inclusive, exclusive) {
73
+ const includes = getType(inclusive);
74
+ const excludes = exclusive === true ? includes : getType(exclusive);
75
+ return excludes === void 0 ? [includes, void 0] : [void 0, excludes];
76
+ }
77
+
78
+ // ../core/src/array.js
79
+ function isArrayish(data) {
80
+ return data != null && (data instanceof Array || data instanceof Set || data instanceof TypedArray);
81
+ }
82
+ function getUniqueArray(arr, def = void 0) {
83
+ if (arr == null)
84
+ return def;
85
+ return arr.constructor === Array ? arr.filter((el, index, a) => index === a.indexOf(el)) : arr.constructor === Set ? Array.from(arr) : def;
86
+ }
87
+ function isUniqueArray(arr) {
88
+ const unique = getUniqueArray(arr);
89
+ return unique != null && unique.length == arr.length;
90
+ }
91
+ function includesAll(arr, values) {
92
+ return values.every((v) => arr.includes(v));
93
+ }
94
+
95
+ // ../core/src/number.js
96
+ function isBoolishType(data) {
97
+ return data === true || data === false || data === "true" || data === "false";
98
+ }
99
+ function getBoolishType(obj, def = void 0) {
100
+ return isBoolishType(obj) || obj === "true" || obj === "false" ? obj : def;
101
+ }
102
+ function isNumbishType(data) {
103
+ return typeof data !== "bigint" && !Number.isNaN(Number(data));
104
+ }
105
+ function isIntishType(data) {
106
+ return Number.isInteger(Number(data));
107
+ }
108
+ function getNumbishType(obj, def = void 0) {
109
+ return isNumbishType(obj) ? Number(obj) : def;
110
+ }
111
+ function getIntishType(obj, def = void 0) {
112
+ return isIntishType(obj) ? Number(obj) : def;
113
+ }
114
+
115
+ // ../core/src/function.js
116
+ function trueThat(whatever, _path = void 0, _root = void 0, _key = void 0) {
117
+ const that = true;
118
+ return whatever === true || that;
119
+ }
120
+ function falseThat(whatever, _path = void 0, _root = void 0, _key = void 0) {
121
+ return false;
122
+ }
123
+ function fallbackFn(compiled, fallback = trueThat) {
124
+ if (isFn(compiled)) return compiled;
125
+ return isFn(fallback) ? fallback : trueThat;
126
+ }
127
+ function addFunctionToArray(arr = [], fn) {
128
+ if (fn == null) return arr;
129
+ if (isFn(fn))
130
+ arr.push(fn);
131
+ else if (fn.constructor === Array) {
132
+ for (let i = 0; i < fn.length; ++i) {
133
+ if (isFn(fn[i]))
134
+ arr.push(fn[i]);
135
+ }
136
+ }
137
+ return arr;
138
+ }
139
+
140
+ // ../core/src/string.js
141
+ function isStringWhiteSpace(data) {
142
+ return data == null || /^\s*$/.test(data);
143
+ }
144
+ function isRegExpType(data) {
145
+ return isObjectOfClass(data, RegExp);
146
+ }
147
+ function createRegExp(pattern) {
148
+ if (pattern == null)
149
+ return void 0;
150
+ if (isRegExpType(pattern))
151
+ return pattern;
152
+ if (isStringType(pattern)) {
153
+ if (pattern[0] === "/") {
154
+ const e = pattern.lastIndexOf("/");
155
+ if (e >= 0) {
156
+ const r = pattern.substring(1, e);
157
+ const g = pattern.substring(e + 1);
158
+ return new RegExp(r, g);
159
+ }
160
+ } else
161
+ return new RegExp(pattern);
162
+ }
163
+ throw new Error(`Unknown Regular Expression Pattern Type: ${pattern}`);
164
+ }
165
+
166
+ // src/tools.js
167
+ function isBoolOrObjectClass(obj) {
168
+ return isBooleanType(obj) || isObjectClass(obj);
169
+ }
170
+ function getBoolOrObjectClass(obj, def = void 0) {
171
+ return isBoolOrObjectClass(obj) ? obj : def;
172
+ }
173
+ function getArrayClassMinItems(obj, len = 1, def = void 0) {
174
+ return isArrayClass(obj) && obj.length >= len && obj || def;
175
+ }
176
+ function isOfSchemaType(schema, type) {
177
+ const stype = schema.type;
178
+ if (stype == null) return false;
179
+ if (stype === type) return true;
180
+ if (stype.constructor === Array) {
181
+ return stype.includes(type);
182
+ }
183
+ if (stype.constructor === Set) {
184
+ return stype.has(type);
185
+ }
186
+ return false;
187
+ }
188
+ function hasSchemaRef(schema) {
189
+ return isObjectClass(schema) && isStringType(schema.$ref) && !isStringWhiteSpace(schema.$ref);
190
+ }
191
+ function createIsSchemaTypeHandler(type, isStrict = false) {
192
+ switch (type) {
193
+ case "null":
194
+ return isNullValue;
195
+ case "boolean":
196
+ return isBooleanType;
197
+ case "integer":
198
+ return isIntegerType;
199
+ case "bigint":
200
+ return isBigIntType;
201
+ case "number":
202
+ return isNumberType;
203
+ case "string":
204
+ return isStringType;
205
+ case "object":
206
+ return isStrict ? isObjectClass : isObjectType;
207
+ case "array":
208
+ return isStrict ? isArrayish : isArrayClass;
209
+ case "set":
210
+ return isSetClass;
211
+ case "map":
212
+ return isMapClass;
213
+ case "tuple":
214
+ return isArrayClass;
215
+ case "regex":
216
+ return isRegExpType;
217
+ default:
218
+ break;
219
+ }
220
+ if (type === null)
221
+ return isNullValue;
222
+ if (typeof type === "function")
223
+ throw new Error("This is interesting!");
224
+ return void 0;
225
+ }
226
+ var ValidationResult = class _ValidationResult {
227
+ static undefThat() {
228
+ return new _ValidationResult();
229
+ }
230
+ constructor(match = false, errors = 0) {
231
+ this.match = match;
232
+ this.errors = Number(errors);
233
+ }
234
+ addValid(valid = true) {
235
+ if (valid === false)
236
+ this.errors++;
237
+ return this;
238
+ }
239
+ addMatch(valid = true) {
240
+ this.match = true;
241
+ if (valid === false)
242
+ this.errors++;
243
+ return this;
244
+ }
245
+ addResult(result = new _ValidationResult()) {
246
+ this.match = this.match || result.match;
247
+ this.errors += result.errors;
248
+ return this;
249
+ }
250
+ isValid() {
251
+ return this.errors === 0;
252
+ }
253
+ };
254
+
255
+ // src/format.js
256
+ function registerFormatCompiler(registered, name, formatCompiler) {
257
+ if (registered[name] == null) {
258
+ if (isFn(formatCompiler)) {
259
+ registered[name] = formatCompiler;
260
+ return true;
261
+ }
262
+ }
263
+ return false;
264
+ }
265
+ function registerFormatCompilers(registered, formatCompilers) {
266
+ const keys = Object.keys(formatCompilers);
267
+ for (let i = 0; i < keys.length; ++i) {
268
+ const key = keys[i];
269
+ const item = formatCompilers[key];
270
+ registerFormatCompiler(registered, key, item);
271
+ }
272
+ return registered;
273
+ }
274
+ function getSchemaFormatCompiler(registered, name) {
275
+ if (isStringType(name))
276
+ return registered[name];
277
+ else
278
+ return void 0;
279
+ }
280
+ function compileFormatBasic(schemaObj, jsonSchema) {
281
+ if (!isStringType(jsonSchema.format))
282
+ return void 0;
283
+ const compiler = getSchemaFormatCompiler(
284
+ schemaObj.formats,
285
+ jsonSchema.format
286
+ );
287
+ if (compiler)
288
+ return compiler(schemaObj, jsonSchema);
289
+ else
290
+ throw new Error(`Unknown format ${jsonSchema.format}`);
291
+ }
292
+
293
+ // ../core/src/object.js
294
+ function equalsDeep(target, source) {
295
+ if (target === source) return true;
296
+ if (target == null) return false;
297
+ if (source == null) return false;
298
+ if (isBooleanType(target)) return false;
299
+ if (isBooleanType(source)) return false;
300
+ if (isFn(target))
301
+ return target.toString() === source.toString();
302
+ if (isScalarType(target))
303
+ return false;
304
+ if (target.constructor !== source.constructor)
305
+ return false;
306
+ if (target.constructor === Object) {
307
+ const tks = Object.keys(target);
308
+ const sks = Object.keys(source);
309
+ if (tks.length !== sks.length)
310
+ return false;
311
+ for (let i = 0; i < tks.length; ++i) {
312
+ const key = tks[i];
313
+ if (!equalsDeep(target[key], source[key]))
314
+ return false;
315
+ }
316
+ return true;
317
+ }
318
+ if (target.constructor === Map) {
319
+ if (target.size !== source.size)
320
+ return false;
321
+ for (const [key, value] of target) {
322
+ if (source.has(key) === false)
323
+ return false;
324
+ if (!equalsDeep(value, source.get(key)))
325
+ return false;
326
+ }
327
+ return true;
328
+ }
329
+ if (target.constructor === Array) {
330
+ if (target.length !== source.length)
331
+ return false;
332
+ for (let i = 0; i < target.length; ++i) {
333
+ if (!equalsDeep(target[i], source[i]))
334
+ return false;
335
+ }
336
+ return true;
337
+ }
338
+ if (target.constructor === Set) {
339
+ if (target.size !== source.size)
340
+ return false;
341
+ for (const value of target) {
342
+ if (source.has(value) === false)
343
+ return false;
344
+ }
345
+ return true;
346
+ }
347
+ if (target.constructor === RegExp) {
348
+ return target.toString() === source.toString();
349
+ }
350
+ if (isTypedArray(target)) {
351
+ if (target.length !== source.length)
352
+ return false;
353
+ for (let i = 0; i < target.length; ++i) {
354
+ if (target[i] !== source[i])
355
+ return false;
356
+ }
357
+ return true;
358
+ }
359
+ const tkeys = Object.keys(target);
360
+ const skeys = Object.keys(source);
361
+ if (tkeys.length !== skeys.length) return false;
362
+ if (tkeys.length === 0) return true;
363
+ for (let i = 0; i < tkeys.length; ++i) {
364
+ const key = tkeys[i];
365
+ if (!equalsDeep(target[key], source[key]))
366
+ return false;
367
+ }
368
+ return true;
369
+ }
370
+
371
+ // src/enum.js
372
+ function compileConst(schemaObj, jsonSchema) {
373
+ const constant = jsonSchema.const;
374
+ if (constant === void 0) return void 0;
375
+ const addError = schemaObj.createErrorHandler(constant, "const");
376
+ if (constant === null || isScalarType(constant)) {
377
+ return function validatePrimitiveConst(data, dataPath) {
378
+ return constant === data || addError(data, dataPath);
379
+ };
380
+ } else {
381
+ return function validateComplexConst(data, dataPath) {
382
+ return equalsDeep(constant, data) || addError(data, dataPath);
383
+ };
384
+ }
385
+ }
386
+ function compileEnum(schemaObj, jsonSchema) {
387
+ const enums = getArrayClassMinItems(jsonSchema.enum, 1);
388
+ if (enums == null) return void 0;
389
+ let hasObjects = false;
390
+ for (let i = 0; i < enums.length; ++i) {
391
+ const e = enums[i];
392
+ if (e != null && typeof e === "object") {
393
+ hasObjects = true;
394
+ break;
395
+ }
396
+ }
397
+ const addError = schemaObj.createErrorHandler(enums, "enum");
398
+ if (hasObjects === false) {
399
+ return function validateEnumSimple(data, dataPath) {
400
+ return data === void 0 ? true : enums.includes(data) ? true : addError(data, dataPath);
401
+ };
402
+ } else {
403
+ return function validateEnumDeep(data, dataPath) {
404
+ if (data === void 0) return true;
405
+ if (data === null || typeof data !== "object")
406
+ return enums.includes(data) ? true : addError(data, dataPath);
407
+ for (let i = 0; i < enums.length; ++i) {
408
+ if (equalsDeep(enums[i], data) === true)
409
+ return true;
410
+ }
411
+ return addError(data, dataPath);
412
+ };
413
+ }
414
+ }
415
+ function compileEnumBasic(schemaObj, jsonSchema) {
416
+ return [
417
+ compileConst(schemaObj, jsonSchema),
418
+ compileEnum(schemaObj, jsonSchema)
419
+ ];
420
+ }
421
+
422
+ // src/number.js
423
+ function compileNumberMaximum(schemaObj, jsonSchema) {
424
+ const [max, emax] = getInclusiveExclusiveBounds(
425
+ getNumbishType,
426
+ jsonSchema.maximum,
427
+ jsonSchema.exclusiveMaximum
428
+ );
429
+ if (emax != null) {
430
+ const addError = schemaObj.createErrorHandler(emax, "exclusiveMaximum");
431
+ return function validateExclusiveMaximum(data, dataPath) {
432
+ return data < emax || addError(data, dataPath);
433
+ };
434
+ } else if (max != null) {
435
+ const addError = schemaObj.createErrorHandler(max, "maximum");
436
+ return function validateMaximum(data, dataPath) {
437
+ return data <= max || addError(data, dataPath);
438
+ };
439
+ }
440
+ return void 0;
441
+ }
442
+ function compileNumberMinimum(schemaObj, jsonSchema) {
443
+ const [min, emin] = getInclusiveExclusiveBounds(
444
+ getNumbishType,
445
+ jsonSchema.minimum,
446
+ jsonSchema.exclusiveMinimum
447
+ );
448
+ if (emin != null) {
449
+ const addError = schemaObj.createErrorHandler(emin, "exclusiveMinimum");
450
+ return function validateExclusiveMinimum(data, dataPath) {
451
+ return data > emin || addError(data, dataPath);
452
+ };
453
+ } else if (min != null) {
454
+ const addError = schemaObj.createErrorHandler(min, "minimum");
455
+ return function validateMinimum(data, dataPath) {
456
+ return data >= min || addError(data, dataPath);
457
+ };
458
+ }
459
+ return void 0;
460
+ }
461
+ function compileNumberMultipleOf(schemaObj, jsonSchema) {
462
+ const mulOf = getNumbishType(jsonSchema.multipleOf);
463
+ if (mulOf == null) return void 0;
464
+ const addError = schemaObj.createErrorHandler(mulOf, "multipleOf");
465
+ return function validateMultipleOf(data, dataPath) {
466
+ return data % mulOf === 0 || addError(data, dataPath);
467
+ };
468
+ }
469
+ function compileNumberIntern(schemaObj, jsonSchema) {
470
+ const maximum = compileNumberMaximum(schemaObj, jsonSchema);
471
+ const minimum = compileNumberMinimum(schemaObj, jsonSchema);
472
+ const multipleOf = compileNumberMultipleOf(schemaObj, jsonSchema);
473
+ if (maximum == null && minimum == null && multipleOf == null)
474
+ return void 0;
475
+ const isMax = maximum || trueThat;
476
+ const isMin = minimum || trueThat;
477
+ const isMul = multipleOf || trueThat;
478
+ return function validateNumberIntern(data, dataPath) {
479
+ return isMax(data, dataPath) && isMin(data, dataPath) && isMul(data, dataPath);
480
+ };
481
+ }
482
+ function compileNumberBasic(schemaObj, jsonSchema) {
483
+ const intern = compileNumberIntern(schemaObj, jsonSchema);
484
+ if (intern == null) return void 0;
485
+ return function validateNumber(data, dataPath) {
486
+ return isNumberType(data) ? intern(data, dataPath) : true;
487
+ };
488
+ }
489
+
490
+ // src/bigint.js
491
+ function compileBigIntMaximum(schemaObj, jsonSchema) {
492
+ const [max, emax] = getInclusiveExclusiveBounds(
493
+ getBigIntType,
494
+ jsonSchema.maximum,
495
+ jsonSchema.exclusiveMaximum
496
+ );
497
+ if (emax != null) {
498
+ const addError = schemaObj.createErrorHandler(emax, "exclusiveMaximum");
499
+ return function validateExclusiveMaximumBigInt(data, dataPath) {
500
+ return data < emax || addError(data, dataPath);
501
+ };
502
+ } else if (max != null) {
503
+ const addError = schemaObj.createErrorHandler(max, "maximum");
504
+ return function validateMaximumBigInt(data, dataPath) {
505
+ return data <= max || addError(data, dataPath);
506
+ };
507
+ }
508
+ return void 0;
509
+ }
510
+ function compileBigIntMinimum(schemaObj, jsonSchema) {
511
+ const [min, emin] = getInclusiveExclusiveBounds(
512
+ getBigIntType,
513
+ jsonSchema.minimum,
514
+ jsonSchema.exclusiveMinimum
515
+ );
516
+ if (emin != null) {
517
+ const addError = schemaObj.createErrorHandler(emin, "exclusiveMinimum");
518
+ return function validateExclusiveMinimumBigInt(data, dataPath) {
519
+ return data > emin || addError(data, dataPath);
520
+ };
521
+ } else if (min != null) {
522
+ const addError = schemaObj.createErrorHandler(min, "minimum");
523
+ return function validateMinimumBigInt(data, dataPath) {
524
+ return data >= min || addError(data, dataPath);
525
+ };
526
+ }
527
+ return void 0;
528
+ }
529
+ function compileBigIntMultipleOf(schemaObj, jsonSchema) {
530
+ const mulOf = getBigIntType(jsonSchema.multipleOf);
531
+ if (mulOf == null) return void 0;
532
+ const addError = schemaObj.createErrorHandler(mulOf, "multipleOf");
533
+ return function validateMultipleOfBigInt(data, dataPath) {
534
+ return data % mulOf === BigInt(0) || addError(data, dataPath);
535
+ };
536
+ }
537
+ function compileBigIntBasic(schemaObj, jsonSchema) {
538
+ const maximum = compileBigIntMaximum(schemaObj, jsonSchema);
539
+ const minimum = compileBigIntMinimum(schemaObj, jsonSchema);
540
+ const multipleOf = compileBigIntMultipleOf(schemaObj, jsonSchema);
541
+ if (maximum == null && minimum == null && multipleOf == null) return void 0;
542
+ const isMax = maximum || trueThat;
543
+ const isMin = minimum || trueThat;
544
+ const isMul = multipleOf || trueThat;
545
+ return function validateBigIntSchema(data, dataPath) {
546
+ if (isBigIntType(data)) {
547
+ return isMax(data, dataPath) && isMin(data, dataPath) && isMul(data, dataPath);
548
+ }
549
+ return true;
550
+ };
551
+ }
552
+
553
+ // src/string.js
554
+ function compileMinLength(schemaObj, jsonSchema) {
555
+ const min = getIntishType(jsonSchema.minLength) || 0;
556
+ if (min < 1) return void 0;
557
+ const addError = schemaObj.createErrorHandler(min, "minLength");
558
+ return function validateIsMinLength(len = 0, dataPath) {
559
+ return len >= min || addError(len, dataPath);
560
+ };
561
+ }
562
+ function compileMaxLength(schemaObj, jsonSchema) {
563
+ const max = getIntishType(jsonSchema.maxLength) || -1;
564
+ if (max < 0) return void 0;
565
+ const addError = schemaObj.createErrorHandler(max, "maxlength");
566
+ return function validateIsMaxLength(len = 0, dataPath) {
567
+ return len <= max || addError(len, dataPath);
568
+ };
569
+ }
570
+ function compilePattern(schemaObj, jsonSchema) {
571
+ const pattern = createRegExp(jsonSchema.pattern);
572
+ if (pattern == null) return void 0;
573
+ const addError = schemaObj.createErrorHandler(pattern, "pattern");
574
+ return function validateIsMatch(str = "", dataPath) {
575
+ return pattern.test(str) || addError(str, dataPath);
576
+ };
577
+ }
578
+ function compileStringIntern(schemaObj, jsonSchema) {
579
+ const minLength = compileMinLength(schemaObj, jsonSchema);
580
+ const maxLength = compileMaxLength(schemaObj, jsonSchema);
581
+ const pattern = compilePattern(schemaObj, jsonSchema);
582
+ if ((minLength || maxLength || pattern) == null) return void 0;
583
+ const isMinLength = minLength || trueThat;
584
+ const isMaxLength = maxLength || trueThat;
585
+ const isMatch = pattern || trueThat;
586
+ return function validateStringIntern(data, dataPath) {
587
+ const len = data.length;
588
+ return isMinLength(len, dataPath) && isMaxLength(len, dataPath) && isMatch(data, dataPath);
589
+ };
590
+ }
591
+ function compileStringBasic(schemaObj, jsonSchema) {
592
+ const intern = compileStringIntern(schemaObj, jsonSchema);
593
+ if (intern == null) return void 0;
594
+ return function validateStringBasic(data, dataPath) {
595
+ return isStringType(data) && intern(data, dataPath);
596
+ };
597
+ }
598
+
599
+ // src/object.js
600
+ function compileMinProperties(schemaObj, jsonSchema) {
601
+ const min = getIntishType(jsonSchema.minProperties) || 0;
602
+ if (min < 1) return void 0;
603
+ const addError = schemaObj.createErrorHandler(min, "minProperties");
604
+ return function validateMinProperties(len = 0, dataPath = "") {
605
+ return len >= min || addError(len, dataPath);
606
+ };
607
+ }
608
+ function compileMaxProperties(schemaObj, jsonSchema) {
609
+ const max = getIntishType(jsonSchema.maxProperties) || -1;
610
+ if (max < 0) return void 0;
611
+ const min = getIntishType(jsonSchema.minProperties) || 0;
612
+ if (max < min) throw new Error("maxProperties must be greater then minProperties");
613
+ const addError = schemaObj.createErrorHandler(max, "maxProperties");
614
+ return function validateMaxProperties(len = 0, dataPath = "") {
615
+ return len <= max || addError(len, dataPath);
616
+ };
617
+ }
618
+ function compileRequiredProperties(schemaObj, jsonSchema) {
619
+ const required = getArrayClassMinItems(jsonSchema.required, 1);
620
+ if (required == null) return void 0;
621
+ const rlength = required.length;
622
+ const addError = schemaObj.createErrorHandler(required, "requiredProperties");
623
+ return function validateRequiredProperties(dataKeys = [], dataPath = "") {
624
+ let valid = true;
625
+ for (let i = 0; i < rlength; ++i) {
626
+ const key = required[i];
627
+ const idx = dataKeys.indexOf(key);
628
+ if (idx === -1)
629
+ valid &&= addError(key, dataPath);
630
+ }
631
+ return valid;
632
+ };
633
+ }
634
+ function compilePropertyNames(schemaObj, jsonSchema) {
635
+ const propNames = getBoolOrObjectClass(jsonSchema.propertyNames);
636
+ if (propNames == null) return void 0;
637
+ return schemaObj.createValidator(propNames, "propertyNames");
638
+ }
639
+ function compileProperties(schemaObj, jsonSchema) {
640
+ const properties = getObjectType(jsonSchema.properties);
641
+ if (properties == null) return void 0;
642
+ const keys = Object.keys(properties);
643
+ if (keys.length === 0) return void 0;
644
+ const validators = {};
645
+ for (let i = 0; i < keys.length; i++) {
646
+ const key = keys[i];
647
+ const schemas = properties[key];
648
+ const validator = schemaObj.createValidator(schemas, "properties", key);
649
+ if (validator != null)
650
+ validators[key] = validator;
651
+ }
652
+ if (Object.keys(validators).length === 0)
653
+ return void 0;
654
+ return function validatePropertyItem(data, dataPath, dataRoot, dataKey) {
655
+ const result = new ValidationResult();
656
+ const validator = validators[dataKey];
657
+ if (validator == null)
658
+ return result;
659
+ else {
660
+ return result.addMatch(
661
+ validator(data[dataKey], dataPath, dataRoot, dataKey)
662
+ );
663
+ }
664
+ };
665
+ }
666
+ function compilePatternProperties(schemaObj, jsonSchema) {
667
+ const entries = getObjectType(jsonSchema.patternProperties);
668
+ if (entries == null) return void 0;
669
+ const entryKeys = Object.keys(entries);
670
+ if (entryKeys.length === 0) return void 0;
671
+ const patterns = {};
672
+ for (let i = 0; i < entryKeys.length; ++i) {
673
+ const key = entryKeys[i];
674
+ const pattern = createRegExp(key);
675
+ if (pattern != null)
676
+ patterns[key] = pattern;
677
+ }
678
+ const patternKeys = Object.keys(patterns);
679
+ if (patternKeys.length === 0) return void 0;
680
+ const validators = {};
681
+ for (let i = 0; i < patternKeys.length; ++i) {
682
+ const key = patternKeys[i];
683
+ const schema = entries[key];
684
+ const validator = schemaObj.createValidator(schema, "patternProperties", key);
685
+ if (validator != null)
686
+ validators[key] = validator;
687
+ }
688
+ const validatorKeys = Object.keys(validators);
689
+ if (validatorKeys.length === 0) return void 0;
690
+ return function validatePatternPropertiesItem(data, dataPath, dataRoot, dataKey) {
691
+ const result = new ValidationResult();
692
+ for (let i = 0; i < validatorKeys.length; ++i) {
693
+ const key = validatorKeys[i];
694
+ const pattern = patterns[key];
695
+ if (pattern.test(dataKey)) {
696
+ const validate = validators[key];
697
+ result.addMatch(validate(data[dataKey], dataPath, dataRoot, dataKey));
698
+ }
699
+ }
700
+ return result;
701
+ };
702
+ }
703
+ function compileAdditionalProperties(schemaObj, jsonSchema) {
704
+ const additional = getBoolOrObjectClass(jsonSchema.additionalProperties);
705
+ if (additional == null) return void 0;
706
+ if (additional === false) {
707
+ const addError = schemaObj.createErrorHandler(false, "additionalProperties");
708
+ return function validateNoAdditionalProperties(data, dataPath, dataRoot, dataKey) {
709
+ return addError(dataKey, data);
710
+ };
711
+ }
712
+ const validator = schemaObj.createValidator(additional, "additionalProperties");
713
+ return function validateAdditionalPropertyItem(data, dataPath, dataRoot, dataKey) {
714
+ return validator(data[dataKey], dataPath, dataRoot, dataKey);
715
+ };
716
+ }
717
+ function compileUnevaluatedProperties(schemaObj, jsonSchema) {
718
+ const unevaluatedProperties = getBoolOrObjectClass(jsonSchema.unevaluatedProperties);
719
+ if (unevaluatedProperties == null) return void 0;
720
+ if (unevaluatedProperties === false) {
721
+ const addError = schemaObj.createErrorHandler(false, "unevaluatedProperties");
722
+ return (data, dataPath, dataRoot, dataKey) => addError(dataKey, data);
723
+ }
724
+ return schemaObj.createValidator(unevaluatedProperties, "unevaluatedProperties");
725
+ }
726
+ function compileDependentRequired(schemaObj, jsonSchema) {
727
+ const dependentRequired = getObjectType(jsonSchema.dependentRequired);
728
+ if (dependentRequired == null)
729
+ return void 0;
730
+ if (Object.keys(dependentRequired).length === 0)
731
+ return void 0;
732
+ const addError = schemaObj.createErrorHandler(false, "dependentRequired");
733
+ return function validateDependentRequiredItem(data, dataPath, dataRoot, dataKey) {
734
+ if (dataKey in dependentRequired) {
735
+ const required = dependentRequired[dataKey];
736
+ return includesAll(Object.keys(data), required) || addError(data, dataKey, dataPath);
737
+ }
738
+ return true;
739
+ };
740
+ }
741
+ function compileDependentSchemas(schemaObj, jsonSchema) {
742
+ const dependentSchemas = getObjectType(jsonSchema.dependentSchemas);
743
+ if (dependentSchemas == null) return void 0;
744
+ const validators = {};
745
+ for (const key in dependentSchemas) {
746
+ if (key in dependentSchemas) {
747
+ const schema = dependentSchemas[key];
748
+ if (!isBoolOrObjectClass(schema) && !schemaObj.options.skipErrors)
749
+ throw new Error(`Expected Schema at '${schemaObj.path}/${key}'`);
750
+ const validator = schemaObj.createValidator(schema, "dependentSchemas", key);
751
+ if (validator != null)
752
+ validators[key] = validator;
753
+ else
754
+ throw new Error(`Expected Validator at '${schemaObj.path}/${key}'`);
755
+ }
756
+ }
757
+ if (Object.keys(validators).length === 0)
758
+ return void 0;
759
+ return function validateDependentSchemasItem(data, dataPath, dataRoot, dataKey) {
760
+ if (dataKey in validators) {
761
+ const validator = validators[dataKey];
762
+ return validator(data, dataPath, dataRoot, dataKey);
763
+ }
764
+ return true;
765
+ };
766
+ }
767
+ function compileDependencies(schemaObj, jsonSchema) {
768
+ const dependencies = getObjectType(jsonSchema.dependencies);
769
+ if (dependencies == null)
770
+ return void 0;
771
+ const validators = {};
772
+ for (const key in dependencies) {
773
+ if (key in dependencies) {
774
+ const right = dependencies[key];
775
+ if (isBoolOrObjectClass(right)) {
776
+ const validator = schemaObj.createValidator(right, "dependencies", key);
777
+ if (validator != null)
778
+ validators[key] = validator;
779
+ else
780
+ throw new Error(`Expected Validator at '${schemaObj.path}/${key}'`);
781
+ } else if (isArrayClass(right)) {
782
+ const addError = schemaObj.createErrorHandler(right, ["dependencies", key]);
783
+ validators[key] = function validateRequiredDependency(data, dataPath, dataRoot, dataKey) {
784
+ return includesAll(Object.keys(data), right) || addError(data, dataKey, dataPath);
785
+ };
786
+ } else if (!schemaObj.options.skipErrors)
787
+ throw new Error(`Expected Schema or Array at '${schemaObj.path}/${key}'`);
788
+ }
789
+ }
790
+ if (Object.keys(validators).length === 0)
791
+ return void 0;
792
+ return function validateDependenciesItem(data, dataPath, dataRoot, dataKey) {
793
+ if (dataKey in validators) {
794
+ const validator = validators[dataKey];
795
+ return validator(data, dataPath, dataRoot, dataKey);
796
+ }
797
+ return true;
798
+ };
799
+ }
800
+ function compileObjectPrimitives(schemaObj, jsonSchema) {
801
+ const minProperties = compileMinProperties(schemaObj, jsonSchema);
802
+ const maxProperties = compileMaxProperties(schemaObj, jsonSchema);
803
+ const requiredProperties = compileRequiredProperties(schemaObj, jsonSchema);
804
+ if ((minProperties || maxProperties || requiredProperties) == null)
805
+ return void 0;
806
+ const isMinProperties = minProperties || trueThat;
807
+ const isMaxProperties = maxProperties || trueThat;
808
+ const hasRequiredProperties = requiredProperties || trueThat;
809
+ return function validateObjectPrimitives(data, dataPath, dataRoot, dataKeys) {
810
+ const keys = dataKeys || Object.keys(data);
811
+ const len = keys.length;
812
+ return isMinProperties(len, dataPath) && isMaxProperties(len, dataPath) && hasRequiredProperties(keys, dataPath);
813
+ };
814
+ }
815
+ function compileObjectProperty(schemaObj, jsonSchema) {
816
+ const namesValidator = compilePropertyNames(schemaObj, jsonSchema);
817
+ const propertyValidator = compileProperties(schemaObj, jsonSchema);
818
+ const patternValidator = compilePatternProperties(schemaObj, jsonSchema);
819
+ const additionalValidator = compileAdditionalProperties(schemaObj, jsonSchema);
820
+ const depSchemasValidator = compileDependentSchemas(schemaObj, jsonSchema);
821
+ const dependencyValidator = compileDependencies(schemaObj, jsonSchema);
822
+ const depRequiredValidator = compileDependentRequired(schemaObj, jsonSchema);
823
+ const unevaluatedValidator = compileUnevaluatedProperties(schemaObj, jsonSchema);
824
+ if ((patternValidator || namesValidator || propertyValidator || depRequiredValidator || depSchemasValidator || dependencyValidator || additionalValidator || unevaluatedValidator) == null)
825
+ return void 0;
826
+ const validateName = namesValidator || trueThat;
827
+ const validateProperty = propertyValidator || ValidationResult.undefThat;
828
+ const validatePattern = patternValidator || ValidationResult.undefThat;
829
+ const validateDepRequired = depRequiredValidator || trueThat;
830
+ const validateDepSchemas = depSchemasValidator || trueThat;
831
+ const validateDependency = dependencyValidator || trueThat;
832
+ return function validateObjectProperty(data, dataPath, dataRoot, dataKey) {
833
+ const result = new ValidationResult();
834
+ result.addValid(validateName(dataKey)).addResult(validateProperty(data, dataPath, dataRoot, dataKey)).addResult(validatePattern(data, dataPath, dataRoot, dataKey)).addValid(validateDepRequired(data, dataPath, dataRoot, dataKey)).addValid(validateDepSchemas(data, dataPath, dataRoot, dataKey)).addValid(validateDependency(data, dataPath, dataRoot, dataKey));
835
+ if (additionalValidator)
836
+ return !result.match ? result.addMatch(additionalValidator(data, dataPath, dataRoot, dataKey)) : result;
837
+ if (unevaluatedValidator)
838
+ result.addValid(unevaluatedValidator(data, dataPath, dataRoot, dataKey));
839
+ return result;
840
+ };
841
+ }
842
+ function compileObjectChildren(schemaObj, jsonSchema) {
843
+ const propertyValidator = compileObjectProperty(schemaObj, jsonSchema);
844
+ if (propertyValidator == null)
845
+ return void 0;
846
+ return function validateObjectChildren(data, dataPath, dataRoot, dataKeys) {
847
+ const result = new ValidationResult();
848
+ for (let i = 0; i < dataKeys.length; ++i) {
849
+ const dataKey = dataKeys[i];
850
+ result.addResult(propertyValidator(data, dataPath, dataRoot, dataKey));
851
+ }
852
+ return result.isValid();
853
+ };
854
+ }
855
+ function compileObjectSchema(schemaObj, jsonSchema) {
856
+ if (isOfSchemaType(jsonSchema, "map"))
857
+ return void 0;
858
+ const objectPrimitives = compileObjectPrimitives(schemaObj, jsonSchema);
859
+ const objectChildren = compileObjectChildren(schemaObj, jsonSchema);
860
+ if ((objectPrimitives || objectChildren) == null)
861
+ return void 0;
862
+ const validatePrimitives = objectPrimitives || trueThat;
863
+ const validateChildren = objectChildren || trueThat;
864
+ return function validateObjectSchema(data, dataPath, dataRoot) {
865
+ if (isObjectClass(data)) {
866
+ const dataKeys = Object.keys(data);
867
+ return validatePrimitives(data, dataPath, dataRoot, dataKeys) && validateChildren(data, dataPath, dataRoot, dataKeys);
868
+ }
869
+ return true;
870
+ };
871
+ }
872
+
873
+ // src/array.js
874
+ function compileMinItems(schemaObj, jsonSchema) {
875
+ const min = getIntishType(jsonSchema.minItems) || 0;
876
+ if (min < 1) return void 0;
877
+ const addError = schemaObj.createErrorHandler(min, "minItems");
878
+ return function validateMinItems(len = 0, dataPath = "") {
879
+ return len >= min || addError(len, dataPath);
880
+ };
881
+ }
882
+ function compileMaxItems(schemaObj, jsonSchema) {
883
+ const max = getIntishType(jsonSchema.maxItems) || -1;
884
+ if (max < 0) return void 0;
885
+ const min = getIntishType(jsonSchema.minItems) || 0;
886
+ if (max < min) throw new Error("maxItems must be greater then minItems");
887
+ const addError = schemaObj.createErrorHandler(max, "maxItems");
888
+ return function validateMaxItems(len = 0, dataPath = "") {
889
+ return len <= max || addError(len, dataPath);
890
+ };
891
+ }
892
+ function createBooleanValidator(schemaObj, jsonSchema, key, validationFn) {
893
+ const value = getBoolishType(jsonSchema[key]);
894
+ if (value !== true) return void 0;
895
+ const addError = schemaObj.createErrorHandler(value, key);
896
+ return function validateBooleanComparator(data, dataPath) {
897
+ return validationFn(data) || addError(data, dataPath);
898
+ };
899
+ }
900
+ var compileUniqueItems = (schemaObj, jsonSchema) => createBooleanValidator(schemaObj, jsonSchema, "uniqueItems", isUniqueArray);
901
+ function compileTupleInternal(schemaObj, jsonSchema, itemsKey, additionalKey) {
902
+ const tuple = getArrayClassMinItems(jsonSchema[itemsKey], 1);
903
+ if (tuple == null)
904
+ return void 0;
905
+ const validators = tuple.map((item, i) => {
906
+ if (item === true) return trueThat;
907
+ if (item === false) return falseThat;
908
+ return schemaObj.createValidator(item, itemsKey, i);
909
+ });
910
+ const vlength = validators.length;
911
+ const additional = getBoolOrObjectClass(jsonSchema[additionalKey], true);
912
+ if (typeof additional === "boolean") {
913
+ return function validateTupleBool(data, dataPath, dataRoot, i) {
914
+ return i >= vlength ? additional : validators[i](data, dataPath, dataRoot);
915
+ };
916
+ }
917
+ const validateAdditional = schemaObj.createValidator(additional, additionalKey);
918
+ return function validateTupleSchema(data, dataPath, dataRoot, i) {
919
+ return i < vlength ? validators[i](data, dataPath, dataRoot) : validateAdditional(data, dataPath, dataRoot);
920
+ };
921
+ }
922
+ function compilePrefixItems(schemaObj, jsonSchema) {
923
+ return compileTupleInternal(schemaObj, jsonSchema, "prefixItems", "items");
924
+ }
925
+ function compileTupleItems(schemaObj, jsonSchema) {
926
+ return compileTupleInternal(schemaObj, jsonSchema, "items", "additionalItems");
927
+ }
928
+ function compileArrayContains(schemaObj, jsonSchema) {
929
+ const contains = getObjectType(jsonSchema.contains);
930
+ if (contains == null) return void 0;
931
+ return schemaObj.createValidator(contains, "contains");
932
+ }
933
+ function compileContainsMinMax(schemaObj, jsonSchema) {
934
+ const contains = getObjectType(jsonSchema.contains);
935
+ if (contains == null) return void 0;
936
+ const minContains = getIntishType(jsonSchema.minContains);
937
+ const maxContains = getIntishType(jsonSchema.maxContains);
938
+ const addNonError = schemaObj.createErrorHandler(0, "contains");
939
+ const addMinError = schemaObj.createErrorHandler(minContains, "minContains");
940
+ const addMaxError = schemaObj.createErrorHandler(maxContains, "maxContains");
941
+ if (minContains == null && maxContains == null) {
942
+ return function validateContainsAtLeastOne(count, dataPath) {
943
+ return count > 0 || addNonError(count, dataPath);
944
+ };
945
+ }
946
+ if (maxContains == null) {
947
+ return function validateMinContains(count, dataPath) {
948
+ return count >= (minContains || 0) || addMinError(count, dataPath);
949
+ };
950
+ }
951
+ if (minContains == null) {
952
+ return function validateMaxContains(count, dataPath) {
953
+ return count === 0 ? addNonError(count, dataPath) : count <= maxContains || addMaxError(count, dataPath);
954
+ };
955
+ }
956
+ return function validateMinMaxContains(count, dataPath) {
957
+ return (count >= minContains || addMinError(count, dataPath)) && (count <= maxContains || addMaxError(count, dataPath));
958
+ };
959
+ }
960
+ function compileArrayContainsBoolean(schemaObj, jsonSchema) {
961
+ const contains = getBoolishType(jsonSchema.contains);
962
+ if (contains === true) {
963
+ const addError = schemaObj.createErrorHandler(true, "contains");
964
+ return function validateArrayContainsTrue(data, dataPath) {
965
+ return data.length > 0 || addError(data, dataPath);
966
+ };
967
+ }
968
+ if (contains === false) {
969
+ const addError = schemaObj.createErrorHandler(false, "contains");
970
+ return function validateArrayContainsFalse(data, dataPath) {
971
+ return addError(data, dataPath);
972
+ };
973
+ }
974
+ return void 0;
975
+ }
976
+ function compileArrayItemsBoolean(schemaObj, jsonSchema) {
977
+ const items = getBoolishType(jsonSchema.items);
978
+ if (items === true) return trueThat;
979
+ if (items !== false) return void 0;
980
+ const addError = schemaObj.createErrorHandler(false, "items");
981
+ return function validateArrayItemsFalse(data, dataPath) {
982
+ return data.length === 0 || addError(data, dataPath);
983
+ };
984
+ }
985
+ function compileArrayItems(schemaObj, jsonSchema) {
986
+ const items = getObjectType(jsonSchema.items);
987
+ if (items == null) return void 0;
988
+ return schemaObj.createValidator(items, "items");
989
+ }
990
+ function compileUnevaluatedItems(schemaObj, jsonSchema) {
991
+ const unevaluatedItems = getObjectType(jsonSchema.unevaluatedItems);
992
+ if (unevaluatedItems == null) return void 0;
993
+ return schemaObj.createValidator(unevaluatedItems, "unevaluatedItems");
994
+ }
995
+ function compileArrayPrimitives(schemaObj, jsonSchema) {
996
+ const minItems = compileMinItems(schemaObj, jsonSchema);
997
+ const maxItems = compileMaxItems(schemaObj, jsonSchema);
998
+ const uniqueItems = compileUniqueItems(schemaObj, jsonSchema);
999
+ if ((minItems || maxItems || uniqueItems) == null)
1000
+ return void 0;
1001
+ const isMinItems = minItems || trueThat;
1002
+ const isMaxItems = maxItems || trueThat;
1003
+ const isUniqueItems = uniqueItems || trueThat;
1004
+ return function validateArrayPrimitives(data, dataPath) {
1005
+ const len = data.length;
1006
+ return isMinItems(len, dataPath) && isMaxItems(len, dataPath) && isUniqueItems(data, dataPath);
1007
+ };
1008
+ }
1009
+ function compileArrayChildren(schemaObj, jsonSchema) {
1010
+ const validateItem = compilePrefixItems(schemaObj, jsonSchema) || compileTupleItems(schemaObj, jsonSchema) || compileArrayItems(schemaObj, jsonSchema);
1011
+ const validateContains = compileArrayContains(schemaObj, jsonSchema);
1012
+ const validateUnevaluated = compileUnevaluatedItems(schemaObj, jsonSchema);
1013
+ if ((validateItem || validateContains || validateUnevaluated) == null)
1014
+ return void 0;
1015
+ const validateMinMax = compileContainsMinMax(schemaObj, jsonSchema) || trueThat;
1016
+ const maxItems = getIntishType(jsonSchema.maxItems) || 0;
1017
+ const resolveLength = (len) => maxItems > 0 ? Math.min(maxItems, len) : len;
1018
+ if (validateContains == null) {
1019
+ const addError = schemaObj.createErrorHandler(0, "items");
1020
+ return function validateArrayItemsOnly(data, dataPath, dataRoot) {
1021
+ const len = resolveLength(data.length);
1022
+ let invalid = 0;
1023
+ for (let i = 0; i < len; ++i) {
1024
+ const obj = data[i];
1025
+ if (validateItem(obj, dataPath, dataRoot, i) === false) {
1026
+ invalid++;
1027
+ }
1028
+ }
1029
+ return invalid === 0 || addError(invalid, dataPath);
1030
+ };
1031
+ }
1032
+ if (validateItem == null) {
1033
+ return function validateArrayContainsOnly(data, dataPath) {
1034
+ const len = resolveLength(data.length);
1035
+ let contains = 0;
1036
+ for (let i = 0; i < len; ++i) {
1037
+ const obj = data[i];
1038
+ if (validateContains(obj, dataPath) === true) {
1039
+ contains++;
1040
+ }
1041
+ }
1042
+ return validateMinMax(contains, dataPath);
1043
+ };
1044
+ }
1045
+ return function validateArrayChildren(data, dataPath, dataRoot) {
1046
+ const len = resolveLength(data.length);
1047
+ let invalid = 0;
1048
+ let contains = 0;
1049
+ for (let i = 0; i < len; ++i) {
1050
+ const obj = data[i];
1051
+ if (validateItem(obj, dataPath, dataRoot, i) !== true) {
1052
+ invalid++;
1053
+ }
1054
+ if (validateContains(obj, dataPath, dataRoot) === true) {
1055
+ contains++;
1056
+ }
1057
+ }
1058
+ return invalid === 0 && validateMinMax(contains, dataPath);
1059
+ };
1060
+ }
1061
+ function compileArraySchema(schemaObj, jsonSchema) {
1062
+ if (isOfSchemaType(jsonSchema, "set"))
1063
+ return void 0;
1064
+ const compiledPrimitives = compileArrayPrimitives(schemaObj, jsonSchema);
1065
+ const compiledItemsBoolean = compileArrayItemsBoolean(schemaObj, jsonSchema);
1066
+ const compiledContainsBoolean = compileArrayContainsBoolean(schemaObj, jsonSchema);
1067
+ const compiledArrayChildren = compileArrayChildren(schemaObj, jsonSchema);
1068
+ if ((compiledPrimitives || compiledItemsBoolean || compiledContainsBoolean || compiledArrayChildren) === void 0)
1069
+ return void 0;
1070
+ const validatePrimitives = compiledPrimitives || trueThat;
1071
+ const hasBooleanItems = compiledItemsBoolean || trueThat;
1072
+ const hasBooleanContains = compiledContainsBoolean || trueThat;
1073
+ const validateItems = compiledArrayChildren || trueThat;
1074
+ return function validateArraySchema(data, dataPath, dataRoot) {
1075
+ if (isArrayClass(data)) {
1076
+ return validatePrimitives(data, dataPath) && hasBooleanItems(data, dataPath, dataRoot) && hasBooleanContains(data, dataPath, dataRoot) && validateItems(data, dataPath, dataRoot);
1077
+ }
1078
+ return true;
1079
+ };
1080
+ }
1081
+
1082
+ // src/combine.js
1083
+ function compileAllOf(schemaObj, jsonSchema) {
1084
+ const allOf = getArrayClassMinItems(jsonSchema.allOf, 1);
1085
+ if (allOf == null) return void 0;
1086
+ const validators = [];
1087
+ for (let i = 0; i < allOf.length; ++i) {
1088
+ const child = allOf[i];
1089
+ if (child === true)
1090
+ validators.push(trueThat);
1091
+ else if (child === false)
1092
+ validators.push(falseThat);
1093
+ else {
1094
+ const validator = schemaObj.createValidator(child, "allOf", i);
1095
+ validators.push(validator);
1096
+ }
1097
+ }
1098
+ if (validators.length === 0) return void 0;
1099
+ const addError = schemaObj.createErrorHandler(allOf, "allOf");
1100
+ return function validateAllOf(data, dataPath, dataRoot, dataKey) {
1101
+ if (data !== void 0) {
1102
+ for (let i = 0; i < validators.length; ++i) {
1103
+ const validator = validators[i];
1104
+ if (validator(data, dataPath, dataRoot, dataKey) === false) {
1105
+ return addError(data, dataPath);
1106
+ }
1107
+ }
1108
+ }
1109
+ return true;
1110
+ };
1111
+ }
1112
+ function compileAnyOf(schemaObj, jsonSchema) {
1113
+ const anyOf = getArrayClassMinItems(jsonSchema.anyOf, 1);
1114
+ if (anyOf == null) return void 0;
1115
+ const validators = [];
1116
+ for (let i = 0; i < anyOf.length; ++i) {
1117
+ const child = anyOf[i];
1118
+ if (child === true)
1119
+ validators.push(trueThat);
1120
+ else if (child === false)
1121
+ validators.push(falseThat);
1122
+ else {
1123
+ const validator = schemaObj.createValidator(child, "anyOf", i);
1124
+ validators.push(validator);
1125
+ }
1126
+ }
1127
+ if (validators.length === 0) return void 0;
1128
+ const addError = schemaObj.createErrorHandler(anyOf, "anyOf");
1129
+ return function validateAnyOf(data, dataPath, dataRoot, dataKey) {
1130
+ if (data !== void 0) {
1131
+ for (let i = 0; i < validators.length; ++i) {
1132
+ const validator = validators[i];
1133
+ if (validator(data, dataPath, dataRoot, dataKey) === true) return true;
1134
+ }
1135
+ return addError(data, dataPath);
1136
+ }
1137
+ return true;
1138
+ };
1139
+ }
1140
+ function compileOneOf(schemaObj, jsonSchema) {
1141
+ const oneOf = getArrayClassMinItems(jsonSchema.oneOf, 1);
1142
+ if (oneOf == null)
1143
+ return void 0;
1144
+ const validators = [];
1145
+ for (let i = 0; i < oneOf.length; ++i) {
1146
+ const child = oneOf[i];
1147
+ if (child === true)
1148
+ validators.push(trueThat);
1149
+ else if (child === false)
1150
+ validators.push(falseThat);
1151
+ else {
1152
+ const validator = schemaObj.createValidator(child, "oneOf", i);
1153
+ validators.push(validator);
1154
+ }
1155
+ }
1156
+ if (validators.length === 0)
1157
+ return void 0;
1158
+ const addError = schemaObj.createErrorHandler(oneOf, "oneOf");
1159
+ return function validateOneOf(data, dataPath, dataRoot, dataKey) {
1160
+ let found = false;
1161
+ for (let i = 0; i < validators.length; ++i) {
1162
+ const validator = validators[i];
1163
+ if (validator(data, dataPath, dataRoot, dataKey) === true) {
1164
+ if (found === true)
1165
+ return addError(data, dataPath);
1166
+ found = true;
1167
+ }
1168
+ }
1169
+ return found;
1170
+ };
1171
+ }
1172
+ function compileNotOf(schemaObj, jsonSchema) {
1173
+ const notOf = getBoolOrObjectClass(jsonSchema.not);
1174
+ if (notOf == null) return void 0;
1175
+ if (notOf === true) return falseThat;
1176
+ if (notOf === false) return trueThat;
1177
+ const validate = schemaObj.createValidator(notOf, "not");
1178
+ const addError = schemaObj.createErrorHandler(notOf, "not");
1179
+ return function validateNotOf(data, dataPath, dataRoot, dataKey) {
1180
+ if (data === void 0) return true;
1181
+ return validate(data, dataPath, dataRoot, dataKey) === false ? true : addError(data, dataPath);
1182
+ };
1183
+ }
1184
+ function compileCombineSchema(schemaObj, jsonSchema) {
1185
+ const validators = [];
1186
+ function addValidator(compiler) {
1187
+ if (isFn(compiler))
1188
+ validators.push(compiler);
1189
+ }
1190
+ addValidator(compileAllOf(schemaObj, jsonSchema));
1191
+ addValidator(compileAnyOf(schemaObj, jsonSchema));
1192
+ addValidator(compileOneOf(schemaObj, jsonSchema));
1193
+ addValidator(compileNotOf(schemaObj, jsonSchema));
1194
+ if (validators.length === 0)
1195
+ return void 0;
1196
+ if (validators.length === 1)
1197
+ return validators[0];
1198
+ if (validators.length === 2) {
1199
+ const first = validators[0];
1200
+ const second = validators[1];
1201
+ return function validateCombinaSchemaPair(data, dataPath, dataRoot, dataKey) {
1202
+ return first(data, dataPath, dataRoot, dataKey) && second(data, dataPath, dataRoot, dataKey);
1203
+ };
1204
+ } else {
1205
+ return function validateCombineSchema(data, dataPath, dataRoot, dataKey) {
1206
+ for (let i = 0; i < validators.length; ++i) {
1207
+ const validator = validators[i];
1208
+ if (validator(data, dataPath, dataRoot, dataKey) === false)
1209
+ return false;
1210
+ }
1211
+ return true;
1212
+ };
1213
+ }
1214
+ }
1215
+
1216
+ // src/condition.js
1217
+ function compileConditionSchema(schemaObj, jsonSchema) {
1218
+ const validateIf = schemaObj.createValidator(jsonSchema.if, "if");
1219
+ const tmpThen = schemaObj.createValidator(jsonSchema.then, "then");
1220
+ const tmpElse = schemaObj.createValidator(jsonSchema.else, "else");
1221
+ if (validateIf == null) return void 0;
1222
+ if (tmpThen == null && tmpElse == null) return void 0;
1223
+ const validateThen = fallbackFn(tmpThen);
1224
+ const validateElse = fallbackFn(tmpElse);
1225
+ return function validateCondition(data, dataRoot) {
1226
+ if (validateIf(data))
1227
+ return validateThen(data, dataRoot);
1228
+ else
1229
+ return validateElse(data, dataRoot);
1230
+ };
1231
+ }
1232
+
1233
+ // src/schema.js
1234
+ function compileRequired(schemaObj, jsonSchema) {
1235
+ const required = getBoolishType(jsonSchema.required);
1236
+ if (required !== true) return void 0;
1237
+ const addError = schemaObj.createErrorHandler(required, "required");
1238
+ return function validateRequiredType(data, dataPath) {
1239
+ return data === void 0 ? addError(data, dataPath) : true;
1240
+ };
1241
+ }
1242
+ function compileTypeSimple(schemaObj, jsonSchema) {
1243
+ const type = getStringType(jsonSchema.type);
1244
+ if (type == null) return void 0;
1245
+ const isDataType = createIsSchemaTypeHandler(type);
1246
+ if (!isDataType) throw new Error(`The explicit schema type '${type}' is unknown. (TODO: add trace)`);
1247
+ const addError = schemaObj.createErrorHandler(type, "type");
1248
+ return function validateTypeSimple(data, dataPath) {
1249
+ return isDataType(data) ? true : addError(data, dataPath);
1250
+ };
1251
+ }
1252
+ function compileTypeArray(schemaObj, jsonSchema) {
1253
+ const schemaTypes = getUniqueArray(jsonSchema.type);
1254
+ if (schemaTypes == null) return void 0;
1255
+ if (schemaTypes.length === 0)
1256
+ throw new Error("The schema type property can not be an empty array.");
1257
+ const types = [];
1258
+ const names = [];
1259
+ for (let i = 0; i < schemaTypes.length; ++i) {
1260
+ const type = schemaTypes[i];
1261
+ const callback = createIsSchemaTypeHandler(type);
1262
+ if (!callback)
1263
+ throw new Error(`The explicit schema type '${type}' of '${types} is unknown. (TODO: add trace)`);
1264
+ types.push(callback);
1265
+ names.push(type);
1266
+ }
1267
+ const addError = schemaObj.createErrorHandler(names, "type");
1268
+ if (types.length === 1) {
1269
+ const one = types[0];
1270
+ return function validateSingleType(data, dataPath) {
1271
+ return one(data) ? true : addError(data, dataPath);
1272
+ };
1273
+ } else if (types.length === 2) {
1274
+ const one = types[0];
1275
+ const two = types[1];
1276
+ return function validateDoubleTypes(data, dataPath) {
1277
+ return one(data) || two(data) ? true : addError(data, dataPath);
1278
+ };
1279
+ } else if (types.length === 3) {
1280
+ const one = types[0];
1281
+ const two = types[1];
1282
+ const three = types[2];
1283
+ return function validateTripleTypes(data, dataPath) {
1284
+ return one(data) || two(data) || three(data) ? true : addError(data, dataPath);
1285
+ };
1286
+ } else {
1287
+ return function validateAllTypes(data, dataPath) {
1288
+ for (let i = 0; i < types.length; ++i) {
1289
+ if (types[i](data) === true) return true;
1290
+ }
1291
+ return addError(data, dataPath);
1292
+ };
1293
+ }
1294
+ }
1295
+ function compileTypeBasic(schemaObj, jsonSchema) {
1296
+ const validator = compileTypeSimple(schemaObj, jsonSchema) || compileTypeArray(schemaObj, jsonSchema);
1297
+ const nullable = getBoolishType(jsonSchema.nullable);
1298
+ if (validator == null) {
1299
+ if (nullable !== false) return void 0;
1300
+ const addError = schemaObj.createErrorHandler(nullable, "nullable");
1301
+ return function validateNotIsNull(data, dataPath) {
1302
+ return data === void 0 || data !== null || addError(data, dataPath);
1303
+ };
1304
+ }
1305
+ if (nullable === true) {
1306
+ return function validateNullableType(data, dataPath) {
1307
+ return data === void 0 || data === null || validator(data, dataPath);
1308
+ };
1309
+ }
1310
+ return function validateType(data, dataPath) {
1311
+ return data === void 0 || validator(data, dataPath);
1312
+ };
1313
+ }
1314
+ function compileSchemaObject(schemaObj, jsonSchema) {
1315
+ if (jsonSchema === true) return trueThat;
1316
+ if (jsonSchema === false) return falseThat;
1317
+ if (!isObjectType(jsonSchema))
1318
+ throw new Error("JSON Schema MUST be a boolean or Object Type");
1319
+ if (Object.keys(jsonSchema).length === 0)
1320
+ return trueThat;
1321
+ const validators = [];
1322
+ addFunctionToArray(validators, compileRequired(schemaObj, jsonSchema));
1323
+ addFunctionToArray(validators, compileTypeBasic(schemaObj, jsonSchema));
1324
+ addFunctionToArray(validators, compileEnumBasic(schemaObj, jsonSchema));
1325
+ addFunctionToArray(validators, compileNumberBasic(schemaObj, jsonSchema));
1326
+ addFunctionToArray(validators, compileBigIntBasic(schemaObj, jsonSchema));
1327
+ addFunctionToArray(validators, compileStringBasic(schemaObj, jsonSchema));
1328
+ addFunctionToArray(validators, compileFormatBasic(schemaObj, jsonSchema));
1329
+ addFunctionToArray(validators, compileArraySchema(schemaObj, jsonSchema));
1330
+ addFunctionToArray(validators, compileObjectSchema(schemaObj, jsonSchema));
1331
+ addFunctionToArray(validators, compileCombineSchema(schemaObj, jsonSchema));
1332
+ addFunctionToArray(validators, compileConditionSchema(schemaObj, jsonSchema));
1333
+ if (validators.length === 0)
1334
+ return trueThat;
1335
+ if (validators.length === 1)
1336
+ return validators[0];
1337
+ if (validators.length === 2) {
1338
+ const first = validators[0];
1339
+ const second = validators[1];
1340
+ return function validateDoubleSchemaObject(data, dataPath, dataRoot) {
1341
+ return first(data, dataPath, dataRoot) && second(data, dataPath, dataRoot);
1342
+ };
1343
+ }
1344
+ if (validators.length === 3) {
1345
+ const first = validators[0];
1346
+ const second = validators[1];
1347
+ const thirth = validators[2];
1348
+ return function validateTripleSchemaObject(data, dataPath, dataRoot) {
1349
+ return first(data, dataPath, dataRoot) && second(data, dataPath, dataRoot) && thirth(data, dataPath, dataRoot);
1350
+ };
1351
+ }
1352
+ return function validateAllSchemaObject(data, dataPath, dataRoot) {
1353
+ for (let i = 0; i < validators.length; ++i) {
1354
+ const validator = validators[i];
1355
+ if (validator(data, dataPath, dataRoot) === false) {
1356
+ return false;
1357
+ }
1358
+ }
1359
+ return true;
1360
+ };
1361
+ }
1362
+
1363
+ // ../strings/src/punycode.js
1364
+ var base = 36;
1365
+ var tMin = 1;
1366
+ var baseMinusTMin = base - tMin;
1367
+ var stringFromCharCode = String.fromCharCode;
1368
+
1369
+ // ../strings/src/index.js
1370
+ var CONST_REGEXP_HTML_IDENTIFIER = /^[A-Za-z]+[\w\-\:\.]{0,30}$/;
1371
+ function isStringHtmlIdentifier(str) {
1372
+ return (
1373
+ /* str != null && */
1374
+ CONST_REGEXP_HTML_IDENTIFIER.test(str)
1375
+ );
1376
+ }
1377
+
1378
+ // src/traverse.js
1379
+ function encodeJsonPointerKey(key) {
1380
+ return encodeURIComponent(key.replace("~", "~0").replace("/", "~1"));
1381
+ }
1382
+ function encodeJsonPointerPath(path, key, index) {
1383
+ return index == null ? `${path}/${encodeJsonPointerKey(key)}` : `${path}/${encodeJsonPointerKey(key)}/${encodeJsonPointerKey(index)}`;
1384
+ }
1385
+ function decodeJsonPointerKey(key) {
1386
+ return decodeURIComponent(key.replace("~0", "~").replace("~1", "/"));
1387
+ }
1388
+ function decodeJsonPointerPath(path) {
1389
+ return path.split("/").map(decodeJsonPointerKey).splice(1);
1390
+ }
1391
+ var JsonPointerOptions = class {
1392
+ constructor(anchorsGlobal = true, anchorsAllowed = true, skipErrors = true) {
1393
+ this.anchorsGlobal = anchorsGlobal;
1394
+ this.anchorsAllowed = anchorsAllowed;
1395
+ this.skipErrors = skipErrors;
1396
+ }
1397
+ };
1398
+ var JsonPointer = class {
1399
+ constructor(id, search, leftUri, fragment) {
1400
+ this.id = id;
1401
+ this.search = search;
1402
+ this.leftUri = leftUri;
1403
+ this.fragment = fragment;
1404
+ }
1405
+ };
1406
+ function createJsonPointer(refUri, baseUri, opts = new JsonPointerOptions()) {
1407
+ const url = !isStringType(refUri) || isStringWhiteSpace(refUri) ? new URL(baseUri) : !isStringType(baseUri) || isStringWhiteSpace(baseUri) ? new URL(refUri) : new URL(refUri, baseUri);
1408
+ const [uri, fragment] = url.href.split("#");
1409
+ const [leftUri, search] = uri.split("?");
1410
+ if (!isStringWhiteSpace(fragment)) {
1411
+ if (opts.anchorsAllowed == true && isStringHtmlIdentifier(fragment)) {
1412
+ return opts.anchorsGlobal == true ? new JsonPointer(`#${fragment}`, search, `${leftUri}#`, fragment) : new JsonPointer(`${leftUri}#${fragment}`, search, `${leftUri}#`, fragment);
1413
+ } else if (fragment.startsWith("/")) {
1414
+ return new JsonPointer(`${leftUri}#${fragment}`, search, `${leftUri}#`, fragment);
1415
+ }
1416
+ }
1417
+ return new JsonPointer(`${leftUri}#`, search, `${leftUri}#`, null);
1418
+ }
1419
+ var TRAVERSE_SCHEMA_OBJECTS = [
1420
+ "items",
1421
+ "prefixItems",
1422
+ "additionalItems",
1423
+ "contains",
1424
+ "unevaluatedItems",
1425
+ "additionalProperties",
1426
+ "propertyNames",
1427
+ "unevaluatedProperties",
1428
+ "not",
1429
+ "oneOf",
1430
+ "anyOf",
1431
+ "allOf",
1432
+ "if",
1433
+ "then",
1434
+ "else"
1435
+ ];
1436
+ var TRAVERSE_SCHEMA_MAPS = [
1437
+ "properties",
1438
+ "patternProperties",
1439
+ "dependencies",
1440
+ "dependentSchemas",
1441
+ "dependentRequired",
1442
+ "definitions",
1443
+ "$defs",
1444
+ "components"
1445
+ ];
1446
+ function storeSchemaIdsInMap(schemas, baseUri, schema, opts = new JsonPointerOptions()) {
1447
+ if (!isObjectClass(schema)) {
1448
+ schemas.set(baseUri, schema);
1449
+ return null;
1450
+ }
1451
+ const { id: rootUri } = createJsonPointer(schema.$id, baseUri, opts);
1452
+ if (!isStringType(schema.$id) || isStringWhiteSpace(schema.$id)) {
1453
+ if (schemas.has(rootUri))
1454
+ throw new Error(`Schema '${rootUri}' already exists`);
1455
+ schemas.set(rootUri, schema);
1456
+ }
1457
+ baseUri = rootUri;
1458
+ const queue = [{ obj: schema, base: rootUri, path: "#" }];
1459
+ while (queue.length > 0) {
1460
+ const { obj, base: base2, path } = queue.shift();
1461
+ if (isStringType(obj.$id) && !isStringWhiteSpace(obj.$id)) {
1462
+ const { id } = createJsonPointer(obj.$id, base2, opts);
1463
+ if (!schemas.has(id))
1464
+ schemas.set(id, obj);
1465
+ else if (schemas.get(id) == null)
1466
+ schemas.set(id, obj);
1467
+ else
1468
+ throw new Error(`Schema '${id}' for path '${path}' in '${base2}' already exists`);
1469
+ baseUri = id;
1470
+ } else
1471
+ baseUri = base2;
1472
+ if (isStringType(obj.$anchor) && !isStringWhiteSpace(obj.$anchor)) {
1473
+ const { id } = createJsonPointer(`#${obj.$anchor}`, baseUri, opts);
1474
+ if (!schemas.has(id))
1475
+ schemas.set(id, obj);
1476
+ else if (schemas.get(id) == null)
1477
+ schemas.set(id, obj);
1478
+ else
1479
+ throw new Error(`Schema '${id}' for path '${path}' in '${base2}' already exists`);
1480
+ if (!id.startsWith("#"))
1481
+ baseUri = id;
1482
+ }
1483
+ if (isStringType(obj.$ref) && !isStringWhiteSpace(obj.$ref)) {
1484
+ const { id: ref } = createJsonPointer(obj.$ref, baseUri, opts);
1485
+ if (!schemas.has(ref))
1486
+ schemas.set(ref, null);
1487
+ continue;
1488
+ }
1489
+ for (const [key, value] of Object.entries(obj)) {
1490
+ if (!isObjectClass(value))
1491
+ continue;
1492
+ if (TRAVERSE_SCHEMA_OBJECTS.includes(key)) {
1493
+ if (Array.isArray(value)) {
1494
+ const len = value.length;
1495
+ for (let index = 0; index < len; index++) {
1496
+ const item = value[index];
1497
+ const nextpath = encodeJsonPointerPath(path, key, index);
1498
+ if (isBoolishType(item))
1499
+ continue;
1500
+ if (!isObjectClass(item)) {
1501
+ if (opts.skipErrors === true)
1502
+ continue;
1503
+ else
1504
+ throw new Error(`${nextpath} is not a schema`);
1505
+ }
1506
+ queue.push({ obj: item, base: baseUri, path: nextpath });
1507
+ }
1508
+ } else {
1509
+ const nextpath = encodeJsonPointerPath(path, key);
1510
+ queue.push({ obj: value, base: baseUri, path: nextpath });
1511
+ }
1512
+ } else if (TRAVERSE_SCHEMA_MAPS.includes(key)) {
1513
+ if (Array.isArray(value))
1514
+ continue;
1515
+ for (const [index, item] of Object.entries(value)) {
1516
+ const nextpath = encodeJsonPointerPath(path, key, index);
1517
+ if (isBoolishType(item))
1518
+ continue;
1519
+ if (!isObjectClass(item)) {
1520
+ if (opts.skipErrors === true)
1521
+ continue;
1522
+ else
1523
+ throw new Error(`${nextpath} is not a schema`);
1524
+ }
1525
+ queue.push({ obj: item, base: baseUri, path: nextpath });
1526
+ }
1527
+ }
1528
+ }
1529
+ }
1530
+ return rootUri;
1531
+ }
1532
+ function resolveRefSchemaShallow(schemas, refUri, baseUri, opts = new JsonPointerOptions()) {
1533
+ const { id: base2, leftUri, fragment } = createJsonPointer(refUri, baseUri, opts);
1534
+ if (!schemas.has(leftUri))
1535
+ throw new Error(`The root of reference: '$ref': '${base2}', is not found in init-cache`);
1536
+ let schema = schemas.get(leftUri);
1537
+ if (isStringWhiteSpace(fragment)) {
1538
+ return { id: base2, schema };
1539
+ }
1540
+ const fragments = decodeJsonPointerPath(fragment);
1541
+ let current = "";
1542
+ for (const part of fragments) {
1543
+ current = current + "/" + part;
1544
+ if (!isBoolOrObjectClass(schema[part]))
1545
+ throw new Error(`The '${current}' is not is not a valid schema in '${leftUri}'`);
1546
+ schema = schema[part];
1547
+ }
1548
+ return { id: base2, schema };
1549
+ }
1550
+ function restoreSchemaRefsInMap(schemas, opts = new JsonPointerOptions()) {
1551
+ for (const [id, item] of schemas.entries()) {
1552
+ if (item != null)
1553
+ continue;
1554
+ const { schema } = resolveRefSchemaShallow(schemas, id, null, opts);
1555
+ if (schema == null)
1556
+ throw new Error(`Can not resolve schema for '${id}'`);
1557
+ schemas.set(id, schema);
1558
+ }
1559
+ }
1560
+ var TraverseOptions = class extends JsonPointerOptions {
1561
+ constructor(origin = "https://github.com/jklarenbeek/jaren", mergeSchemas = true, anchorsGlobal, anchorsAllowed, skipErrors) {
1562
+ super(anchorsGlobal, anchorsAllowed, skipErrors);
1563
+ this.origin = origin, this.mergeSchemas = mergeSchemas;
1564
+ }
1565
+ };
1566
+ function resolveRefSchemaDeep(schemas, baseUri, refschema, opts = new TraverseOptions()) {
1567
+ if (!isObjectClass(refschema))
1568
+ return { id: baseUri, schema: refschema };
1569
+ if (!hasSchemaRef(refschema))
1570
+ return { id: baseUri, schema: refschema };
1571
+ const queue = [{ item: refschema, base: baseUri }];
1572
+ const seen = /* @__PURE__ */ new Set();
1573
+ let result = {};
1574
+ while (queue.length > 0) {
1575
+ const { item, base: base2 } = queue.shift();
1576
+ if (!isObjectClass(item))
1577
+ return { id: base2, schema: item };
1578
+ result = opts.mergeSchemas == true ? { ...item, ...result } : { ...item };
1579
+ if (!hasSchemaRef(item))
1580
+ return { id: base2, schema: result };
1581
+ delete result.$ref;
1582
+ const ref = item.$ref;
1583
+ const { id, schema } = resolveRefSchemaShallow(schemas, ref, base2, opts);
1584
+ if (seen.has(id))
1585
+ return { id: base2, schema: result };
1586
+ seen.add(id);
1587
+ queue.push({ item: schema, base: id });
1588
+ }
1589
+ throw new Error(`The json schema '${baseUri}' can not be resolved!`);
1590
+ }
1591
+
1592
+ // src/index.js
1593
+ var isBrowser = typeof window !== "undefined";
1594
+ var performance = (() => isBrowser ? window.performance : {
1595
+ now: function performanceNow(start) {
1596
+ const ps = process;
1597
+ if (!start) return ps.hrtime();
1598
+ const end = ps.hrtime(start);
1599
+ return Math.round(end[0] * 1e3 + end[1] / 1e6);
1600
+ }
1601
+ })();
1602
+ var ValidationError = class {
1603
+ constructor(obj, key, expected, dataKey, value, rest) {
1604
+ this.timeStamp = performance.now();
1605
+ this.object = obj;
1606
+ this.key = key;
1607
+ this.expected = expected;
1608
+ this.dataKey = dataKey;
1609
+ this.value = value;
1610
+ this.rest = rest;
1611
+ }
1612
+ };
1613
+ var ValidationOptions = class {
1614
+ constructor(skipErrors = true) {
1615
+ this.skipErrors = skipErrors;
1616
+ }
1617
+ };
1618
+ var ValidationRoot = class _ValidationRoot {
1619
+ static _createObject(self, path, schema) {
1620
+ const objects = self._objects;
1621
+ if (objects.has(path)) {
1622
+ const p = objects.get(path);
1623
+ if (p != null)
1624
+ throw new Error(`Object at '${path}' is already created`);
1625
+ }
1626
+ const obj = new ValidationObject(self, path, schema);
1627
+ objects.set(path, obj);
1628
+ return obj;
1629
+ }
1630
+ constructor(schemas, schema, origin, formats, opts = new ValidationOptions(), traverse = new TraverseOptions()) {
1631
+ this._options = opts;
1632
+ this._traverse = traverse;
1633
+ this._schemas = schemas;
1634
+ this._rootOrigin = origin;
1635
+ this._formats = formats;
1636
+ this._objects = /* @__PURE__ */ new Map();
1637
+ this._errors = [];
1638
+ this._firstSchema = _ValidationRoot._createObject(this, origin, schema);
1639
+ }
1640
+ get options() {
1641
+ return this._options;
1642
+ }
1643
+ get formats() {
1644
+ return this._formats;
1645
+ }
1646
+ createObject(path, schema) {
1647
+ return _ValidationRoot._createObject(this, path, schema);
1648
+ }
1649
+ unresolvedObject(path) {
1650
+ const objects = this._objects;
1651
+ if (objects.has(path))
1652
+ return objects.get(path);
1653
+ objects.set(path, null);
1654
+ return null;
1655
+ }
1656
+ resolveObject(ref, path, schema) {
1657
+ const objects = this._objects;
1658
+ if (objects.has(ref)) {
1659
+ const obj = objects.get(ref);
1660
+ if (obj != null)
1661
+ return objects.get(ref);
1662
+ }
1663
+ const schemas = this._schemas;
1664
+ const traverse = this._traverse;
1665
+ const { id, schema: root } = resolveRefSchemaDeep(schemas, path, schema, traverse);
1666
+ return _ValidationRoot._createObject(this, id, root);
1667
+ }
1668
+ addError(error) {
1669
+ this._errors.push(error);
1670
+ return false;
1671
+ }
1672
+ validate(data) {
1673
+ this._errors.length = 0;
1674
+ return this._firstSchema.validate(data, data);
1675
+ }
1676
+ };
1677
+ var ValidationObject = class _ValidationObject {
1678
+ /**
1679
+ * Compiles a schema validation error handler
1680
+ * @param {ValidationObject} self The validation object that is compiling this validator
1681
+ * @param {string} path The path to this schema object
1682
+ * @param {any} schema The schema object to compile
1683
+ * @returns {function(any, any):boolean} A function that validates data against the compiled schema and returns a boolean.
1684
+ */
1685
+ static _compileValidator(self, path, schema) {
1686
+ if (!hasSchemaRef(schema))
1687
+ return compileSchemaObject(self, schema);
1688
+ const root = self._root;
1689
+ const { id: ref } = createJsonPointer(schema.$ref, path, root.options);
1690
+ const resolved = root.unresolvedObject(ref);
1691
+ if (resolved != null) {
1692
+ const validator = resolved.validate;
1693
+ self._validator = function validateRefSchemaOnTime(data, dataRoot) {
1694
+ return validator(data, dataRoot);
1695
+ };
1696
+ return self._validator;
1697
+ }
1698
+ return function resolveSchemaCompiler(data, dataRoot) {
1699
+ const obj = root.resolveObject(ref, path, schema);
1700
+ const validator = obj.validate;
1701
+ self._validator = function validateRefSchemaLate(_data, _dataRoot) {
1702
+ return validator(_data, _dataRoot);
1703
+ };
1704
+ return self._validator(data, dataRoot);
1705
+ };
1706
+ }
1707
+ constructor(root, path, schema) {
1708
+ this._root = root;
1709
+ this._path = path;
1710
+ this._members = [];
1711
+ this._schema = schema;
1712
+ this._validator = null;
1713
+ this._validator = _ValidationObject._compileValidator(this, path, schema);
1714
+ }
1715
+ get path() {
1716
+ return this._path;
1717
+ }
1718
+ get errors() {
1719
+ return this._root.errors;
1720
+ }
1721
+ get validate() {
1722
+ return this._validator;
1723
+ }
1724
+ get options() {
1725
+ return this._root.options;
1726
+ }
1727
+ get formats() {
1728
+ return this._root.formats;
1729
+ }
1730
+ /**
1731
+ * Compiles a schema validation error handler
1732
+ * @param {any} expected - anything that is expected by this handler
1733
+ * @param {string | string[]} key - the key or keys that is expected
1734
+ * @returns {function(unknown): boolean} A function that validates data against the compiled schema and returns a boolean.
1735
+ */
1736
+ createErrorHandler(expected, key) {
1737
+ const self = this;
1738
+ if (!Array.isArray(key)) {
1739
+ return function addNormalError(data, ...meta) {
1740
+ const error = new ValidationError(self, key, expected, null, data, meta);
1741
+ return self._root.addError(error);
1742
+ };
1743
+ } else {
1744
+ return function addKeyedError(dataKey, data, ...meta) {
1745
+ const error = new ValidationError(self, key, expected, dataKey, data, meta);
1746
+ return self._root.addError(error);
1747
+ };
1748
+ }
1749
+ }
1750
+ createValidator(schema, key, index) {
1751
+ if (!isBoolOrObjectClass(schema))
1752
+ return void 0;
1753
+ const id = isObjectClass(schema) ? createJsonPointer(schema.$id, this._path).id : this._path;
1754
+ const root = this._root;
1755
+ const path = index == null ? encodeJsonPointerPath(id, key) : encodeJsonPointerPath(id, key, String(index));
1756
+ const child = root.createObject(path, schema);
1757
+ this._members.push(child);
1758
+ return child._validator;
1759
+ }
1760
+ };
1761
+ var ValidatorOptions = class {
1762
+ constructor(formats = {}, schemas = [], validation = new ValidationOptions(), traverse = new TraverseOptions()) {
1763
+ this.formats = formats;
1764
+ this.schemas = schemas;
1765
+ this.validation = validation;
1766
+ this.traverse = traverse;
1767
+ }
1768
+ };
1769
+ function compileSchemaValidator(schema, opts = new ValidatorOptions()) {
1770
+ const schemas = /* @__PURE__ */ new Map();
1771
+ const origin = storeSchemaIdsInMap(
1772
+ schemas,
1773
+ opts.traverse.origin,
1774
+ schema,
1775
+ opts.traverse
1776
+ );
1777
+ opts.schemas.forEach((ref) => storeSchemaIdsInMap(
1778
+ schemas,
1779
+ origin,
1780
+ ref,
1781
+ opts.traverse
1782
+ ));
1783
+ restoreSchemaRefsInMap(schemas, opts.traverse);
1784
+ const root = new ValidationRoot(
1785
+ schemas,
1786
+ schema,
1787
+ origin,
1788
+ opts.formats,
1789
+ opts.validation,
1790
+ opts.traverse
1791
+ );
1792
+ return {
1793
+ validate: (data) => root.validate(data)
1794
+ };
1795
+ }
1796
+ export {
1797
+ TraverseOptions,
1798
+ ValidatorOptions,
1799
+ compileSchemaValidator,
1800
+ registerFormatCompilers
1801
+ };
1802
+ //# sourceMappingURL=index.js.map