@cfworker/json-schema 4.0.1 → 4.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/dist/commonjs/package.json +3 -0
  2. package/dist/esm/deep-compare-strict.d.ts +1 -0
  3. package/dist/esm/dereference.d.ts +7 -0
  4. package/dist/esm/format.d.ts +2 -0
  5. package/dist/esm/index.d.ts +8 -0
  6. package/dist/esm/package.json +3 -0
  7. package/dist/esm/pointer.d.ts +2 -0
  8. package/dist/esm/types.d.ts +72 -0
  9. package/dist/esm/ucs2-length.d.ts +1 -0
  10. package/dist/esm/validate.d.ts +3 -0
  11. package/dist/esm/validator.d.ts +10 -0
  12. package/package.json +33 -27
  13. package/src/deep-compare-strict.d.ts +1 -0
  14. package/src/deep-compare-strict.js +39 -0
  15. package/src/dereference.d.ts +12 -0
  16. package/src/dereference.js +148 -0
  17. package/src/format.d.ts +2 -0
  18. package/src/format.js +137 -0
  19. package/src/index.d.ts +8 -0
  20. package/src/index.js +8 -0
  21. package/src/pointer.d.ts +2 -0
  22. package/src/pointer.js +6 -0
  23. package/src/types.d.ts +79 -0
  24. package/src/types.js +6 -0
  25. package/src/ucs2-length.d.ts +1 -0
  26. package/src/ucs2-length.js +17 -0
  27. package/src/validate.d.ts +13 -0
  28. package/src/validate.js +1099 -0
  29. package/src/validator.d.ts +14 -0
  30. package/src/validator.js +29 -0
  31. package/dist/tsconfig.cjs.tsbuildinfo +0 -1
  32. package/dist/tsconfig.tsbuildinfo +0 -1
  33. /package/dist/{types → commonjs}/deep-compare-strict.d.ts +0 -0
  34. /package/dist/{cjs → commonjs}/deep-compare-strict.js +0 -0
  35. /package/dist/{types → commonjs}/dereference.d.ts +0 -0
  36. /package/dist/{cjs → commonjs}/dereference.js +0 -0
  37. /package/dist/{types → commonjs}/format.d.ts +0 -0
  38. /package/dist/{cjs → commonjs}/format.js +0 -0
  39. /package/dist/{types → commonjs}/index.d.ts +0 -0
  40. /package/dist/{cjs → commonjs}/index.js +0 -0
  41. /package/dist/{types → commonjs}/pointer.d.ts +0 -0
  42. /package/dist/{cjs → commonjs}/pointer.js +0 -0
  43. /package/dist/{types → commonjs}/types.d.ts +0 -0
  44. /package/dist/{cjs → commonjs}/types.js +0 -0
  45. /package/dist/{types → commonjs}/ucs2-length.d.ts +0 -0
  46. /package/dist/{cjs → commonjs}/ucs2-length.js +0 -0
  47. /package/dist/{types → commonjs}/validate.d.ts +0 -0
  48. /package/dist/{cjs → commonjs}/validate.js +0 -0
  49. /package/dist/{types → commonjs}/validator.d.ts +0 -0
  50. /package/dist/{cjs → commonjs}/validator.js +0 -0
@@ -0,0 +1,1099 @@
1
+ import { deepCompareStrict } from './deep-compare-strict.js';
2
+ import { dereference } from './dereference.js';
3
+ import { fastFormat } from './format.js';
4
+ import { encodePointer } from './pointer.js';
5
+ import { ucs2length } from './ucs2-length.js';
6
+ export function validate(
7
+ instance,
8
+ schema,
9
+ draft = '2019-09',
10
+ lookup = dereference(schema),
11
+ shortCircuit = true,
12
+ recursiveAnchor = null,
13
+ instanceLocation = '#',
14
+ schemaLocation = '#',
15
+ evaluated = Object.create(null)
16
+ ) {
17
+ if (schema === true) {
18
+ return { valid: true, errors: [] };
19
+ }
20
+ if (schema === false) {
21
+ return {
22
+ valid: false,
23
+ errors: [
24
+ {
25
+ instanceLocation,
26
+ keyword: 'false',
27
+ keywordLocation: instanceLocation,
28
+ error: 'False boolean schema.'
29
+ }
30
+ ]
31
+ };
32
+ }
33
+ const rawInstanceType = typeof instance;
34
+ let instanceType;
35
+ switch (rawInstanceType) {
36
+ case 'boolean':
37
+ case 'number':
38
+ case 'string':
39
+ instanceType = rawInstanceType;
40
+ break;
41
+ case 'object':
42
+ if (instance === null) {
43
+ instanceType = 'null';
44
+ } else if (Array.isArray(instance)) {
45
+ instanceType = 'array';
46
+ } else {
47
+ instanceType = 'object';
48
+ }
49
+ break;
50
+ default:
51
+ throw new Error(
52
+ `Instances of "${rawInstanceType}" type are not supported.`
53
+ );
54
+ }
55
+ const {
56
+ $ref,
57
+ $recursiveRef,
58
+ $recursiveAnchor,
59
+ type: $type,
60
+ const: $const,
61
+ enum: $enum,
62
+ required: $required,
63
+ not: $not,
64
+ anyOf: $anyOf,
65
+ allOf: $allOf,
66
+ oneOf: $oneOf,
67
+ if: $if,
68
+ then: $then,
69
+ else: $else,
70
+ format: $format,
71
+ properties: $properties,
72
+ patternProperties: $patternProperties,
73
+ additionalProperties: $additionalProperties,
74
+ unevaluatedProperties: $unevaluatedProperties,
75
+ minProperties: $minProperties,
76
+ maxProperties: $maxProperties,
77
+ propertyNames: $propertyNames,
78
+ dependentRequired: $dependentRequired,
79
+ dependentSchemas: $dependentSchemas,
80
+ dependencies: $dependencies,
81
+ prefixItems: $prefixItems,
82
+ items: $items,
83
+ additionalItems: $additionalItems,
84
+ unevaluatedItems: $unevaluatedItems,
85
+ contains: $contains,
86
+ minContains: $minContains,
87
+ maxContains: $maxContains,
88
+ minItems: $minItems,
89
+ maxItems: $maxItems,
90
+ uniqueItems: $uniqueItems,
91
+ minimum: $minimum,
92
+ maximum: $maximum,
93
+ exclusiveMinimum: $exclusiveMinimum,
94
+ exclusiveMaximum: $exclusiveMaximum,
95
+ multipleOf: $multipleOf,
96
+ minLength: $minLength,
97
+ maxLength: $maxLength,
98
+ pattern: $pattern,
99
+ __absolute_ref__,
100
+ __absolute_recursive_ref__
101
+ } = schema;
102
+ const errors = [];
103
+ if ($recursiveAnchor === true && recursiveAnchor === null) {
104
+ recursiveAnchor = schema;
105
+ }
106
+ if ($recursiveRef === '#') {
107
+ const refSchema =
108
+ recursiveAnchor === null
109
+ ? lookup[__absolute_recursive_ref__]
110
+ : recursiveAnchor;
111
+ const keywordLocation = `${schemaLocation}/$recursiveRef`;
112
+ const result = validate(
113
+ instance,
114
+ recursiveAnchor === null ? schema : recursiveAnchor,
115
+ draft,
116
+ lookup,
117
+ shortCircuit,
118
+ refSchema,
119
+ instanceLocation,
120
+ keywordLocation,
121
+ evaluated
122
+ );
123
+ if (!result.valid) {
124
+ errors.push(
125
+ {
126
+ instanceLocation,
127
+ keyword: '$recursiveRef',
128
+ keywordLocation,
129
+ error: 'A subschema had errors.'
130
+ },
131
+ ...result.errors
132
+ );
133
+ }
134
+ }
135
+ if ($ref !== undefined) {
136
+ const uri = __absolute_ref__ || $ref;
137
+ const refSchema = lookup[uri];
138
+ if (refSchema === undefined) {
139
+ let message = `Unresolved $ref "${$ref}".`;
140
+ if (__absolute_ref__ && __absolute_ref__ !== $ref) {
141
+ message += ` Absolute URI "${__absolute_ref__}".`;
142
+ }
143
+ message += `\nKnown schemas:\n- ${Object.keys(lookup).join('\n- ')}`;
144
+ throw new Error(message);
145
+ }
146
+ const keywordLocation = `${schemaLocation}/$ref`;
147
+ const result = validate(
148
+ instance,
149
+ refSchema,
150
+ draft,
151
+ lookup,
152
+ shortCircuit,
153
+ recursiveAnchor,
154
+ instanceLocation,
155
+ keywordLocation,
156
+ evaluated
157
+ );
158
+ if (!result.valid) {
159
+ errors.push(
160
+ {
161
+ instanceLocation,
162
+ keyword: '$ref',
163
+ keywordLocation,
164
+ error: 'A subschema had errors.'
165
+ },
166
+ ...result.errors
167
+ );
168
+ }
169
+ if (draft === '4' || draft === '7') {
170
+ return { valid: errors.length === 0, errors };
171
+ }
172
+ }
173
+ if (Array.isArray($type)) {
174
+ let length = $type.length;
175
+ let valid = false;
176
+ for (let i = 0; i < length; i++) {
177
+ if (
178
+ instanceType === $type[i] ||
179
+ ($type[i] === 'integer' &&
180
+ instanceType === 'number' &&
181
+ instance % 1 === 0 &&
182
+ instance === instance)
183
+ ) {
184
+ valid = true;
185
+ break;
186
+ }
187
+ }
188
+ if (!valid) {
189
+ errors.push({
190
+ instanceLocation,
191
+ keyword: 'type',
192
+ keywordLocation: `${schemaLocation}/type`,
193
+ error: `Instance type "${instanceType}" is invalid. Expected "${$type.join('", "')}".`
194
+ });
195
+ }
196
+ } else if ($type === 'integer') {
197
+ if (instanceType !== 'number' || instance % 1 || instance !== instance) {
198
+ errors.push({
199
+ instanceLocation,
200
+ keyword: 'type',
201
+ keywordLocation: `${schemaLocation}/type`,
202
+ error: `Instance type "${instanceType}" is invalid. Expected "${$type}".`
203
+ });
204
+ }
205
+ } else if ($type !== undefined && instanceType !== $type) {
206
+ errors.push({
207
+ instanceLocation,
208
+ keyword: 'type',
209
+ keywordLocation: `${schemaLocation}/type`,
210
+ error: `Instance type "${instanceType}" is invalid. Expected "${$type}".`
211
+ });
212
+ }
213
+ if ($const !== undefined) {
214
+ if (instanceType === 'object' || instanceType === 'array') {
215
+ if (!deepCompareStrict(instance, $const)) {
216
+ errors.push({
217
+ instanceLocation,
218
+ keyword: 'const',
219
+ keywordLocation: `${schemaLocation}/const`,
220
+ error: `Instance does not match ${JSON.stringify($const)}.`
221
+ });
222
+ }
223
+ } else if (instance !== $const) {
224
+ errors.push({
225
+ instanceLocation,
226
+ keyword: 'const',
227
+ keywordLocation: `${schemaLocation}/const`,
228
+ error: `Instance does not match ${JSON.stringify($const)}.`
229
+ });
230
+ }
231
+ }
232
+ if ($enum !== undefined) {
233
+ if (instanceType === 'object' || instanceType === 'array') {
234
+ if (!$enum.some(value => deepCompareStrict(instance, value))) {
235
+ errors.push({
236
+ instanceLocation,
237
+ keyword: 'enum',
238
+ keywordLocation: `${schemaLocation}/enum`,
239
+ error: `Instance does not match any of ${JSON.stringify($enum)}.`
240
+ });
241
+ }
242
+ } else if (!$enum.some(value => instance === value)) {
243
+ errors.push({
244
+ instanceLocation,
245
+ keyword: 'enum',
246
+ keywordLocation: `${schemaLocation}/enum`,
247
+ error: `Instance does not match any of ${JSON.stringify($enum)}.`
248
+ });
249
+ }
250
+ }
251
+ if ($not !== undefined) {
252
+ const keywordLocation = `${schemaLocation}/not`;
253
+ const result = validate(
254
+ instance,
255
+ $not,
256
+ draft,
257
+ lookup,
258
+ shortCircuit,
259
+ recursiveAnchor,
260
+ instanceLocation,
261
+ keywordLocation
262
+ );
263
+ if (result.valid) {
264
+ errors.push({
265
+ instanceLocation,
266
+ keyword: 'not',
267
+ keywordLocation,
268
+ error: 'Instance matched "not" schema.'
269
+ });
270
+ }
271
+ }
272
+ let subEvaluateds = [];
273
+ if ($anyOf !== undefined) {
274
+ const keywordLocation = `${schemaLocation}/anyOf`;
275
+ const errorsLength = errors.length;
276
+ let anyValid = false;
277
+ for (let i = 0; i < $anyOf.length; i++) {
278
+ const subSchema = $anyOf[i];
279
+ const subEvaluated = Object.create(evaluated);
280
+ const result = validate(
281
+ instance,
282
+ subSchema,
283
+ draft,
284
+ lookup,
285
+ shortCircuit,
286
+ $recursiveAnchor === true ? recursiveAnchor : null,
287
+ instanceLocation,
288
+ `${keywordLocation}/${i}`,
289
+ subEvaluated
290
+ );
291
+ errors.push(...result.errors);
292
+ anyValid = anyValid || result.valid;
293
+ if (result.valid) {
294
+ subEvaluateds.push(subEvaluated);
295
+ }
296
+ }
297
+ if (anyValid) {
298
+ errors.length = errorsLength;
299
+ } else {
300
+ errors.splice(errorsLength, 0, {
301
+ instanceLocation,
302
+ keyword: 'anyOf',
303
+ keywordLocation,
304
+ error: 'Instance does not match any subschemas.'
305
+ });
306
+ }
307
+ }
308
+ if ($allOf !== undefined) {
309
+ const keywordLocation = `${schemaLocation}/allOf`;
310
+ const errorsLength = errors.length;
311
+ let allValid = true;
312
+ for (let i = 0; i < $allOf.length; i++) {
313
+ const subSchema = $allOf[i];
314
+ const subEvaluated = Object.create(evaluated);
315
+ const result = validate(
316
+ instance,
317
+ subSchema,
318
+ draft,
319
+ lookup,
320
+ shortCircuit,
321
+ $recursiveAnchor === true ? recursiveAnchor : null,
322
+ instanceLocation,
323
+ `${keywordLocation}/${i}`,
324
+ subEvaluated
325
+ );
326
+ errors.push(...result.errors);
327
+ allValid = allValid && result.valid;
328
+ if (result.valid) {
329
+ subEvaluateds.push(subEvaluated);
330
+ }
331
+ }
332
+ if (allValid) {
333
+ errors.length = errorsLength;
334
+ } else {
335
+ errors.splice(errorsLength, 0, {
336
+ instanceLocation,
337
+ keyword: 'allOf',
338
+ keywordLocation,
339
+ error: `Instance does not match every subschema.`
340
+ });
341
+ }
342
+ }
343
+ if ($oneOf !== undefined) {
344
+ const keywordLocation = `${schemaLocation}/oneOf`;
345
+ const errorsLength = errors.length;
346
+ const matches = $oneOf.filter((subSchema, i) => {
347
+ const subEvaluated = Object.create(evaluated);
348
+ const result = validate(
349
+ instance,
350
+ subSchema,
351
+ draft,
352
+ lookup,
353
+ shortCircuit,
354
+ $recursiveAnchor === true ? recursiveAnchor : null,
355
+ instanceLocation,
356
+ `${keywordLocation}/${i}`,
357
+ subEvaluated
358
+ );
359
+ errors.push(...result.errors);
360
+ if (result.valid) {
361
+ subEvaluateds.push(subEvaluated);
362
+ }
363
+ return result.valid;
364
+ }).length;
365
+ if (matches === 1) {
366
+ errors.length = errorsLength;
367
+ } else {
368
+ errors.splice(errorsLength, 0, {
369
+ instanceLocation,
370
+ keyword: 'oneOf',
371
+ keywordLocation,
372
+ error: `Instance does not match exactly one subschema (${matches} matches).`
373
+ });
374
+ }
375
+ }
376
+ if (instanceType === 'object' || instanceType === 'array') {
377
+ Object.assign(evaluated, ...subEvaluateds);
378
+ }
379
+ if ($if !== undefined) {
380
+ const keywordLocation = `${schemaLocation}/if`;
381
+ const conditionResult = validate(
382
+ instance,
383
+ $if,
384
+ draft,
385
+ lookup,
386
+ shortCircuit,
387
+ recursiveAnchor,
388
+ instanceLocation,
389
+ keywordLocation,
390
+ evaluated
391
+ ).valid;
392
+ if (conditionResult) {
393
+ if ($then !== undefined) {
394
+ const thenResult = validate(
395
+ instance,
396
+ $then,
397
+ draft,
398
+ lookup,
399
+ shortCircuit,
400
+ recursiveAnchor,
401
+ instanceLocation,
402
+ `${schemaLocation}/then`,
403
+ evaluated
404
+ );
405
+ if (!thenResult.valid) {
406
+ errors.push(
407
+ {
408
+ instanceLocation,
409
+ keyword: 'if',
410
+ keywordLocation,
411
+ error: `Instance does not match "then" schema.`
412
+ },
413
+ ...thenResult.errors
414
+ );
415
+ }
416
+ }
417
+ } else if ($else !== undefined) {
418
+ const elseResult = validate(
419
+ instance,
420
+ $else,
421
+ draft,
422
+ lookup,
423
+ shortCircuit,
424
+ recursiveAnchor,
425
+ instanceLocation,
426
+ `${schemaLocation}/else`,
427
+ evaluated
428
+ );
429
+ if (!elseResult.valid) {
430
+ errors.push(
431
+ {
432
+ instanceLocation,
433
+ keyword: 'if',
434
+ keywordLocation,
435
+ error: `Instance does not match "else" schema.`
436
+ },
437
+ ...elseResult.errors
438
+ );
439
+ }
440
+ }
441
+ }
442
+ if (instanceType === 'object') {
443
+ if ($required !== undefined) {
444
+ for (const key of $required) {
445
+ if (!(key in instance)) {
446
+ errors.push({
447
+ instanceLocation,
448
+ keyword: 'required',
449
+ keywordLocation: `${schemaLocation}/required`,
450
+ error: `Instance does not have required property "${key}".`
451
+ });
452
+ }
453
+ }
454
+ }
455
+ const keys = Object.keys(instance);
456
+ if ($minProperties !== undefined && keys.length < $minProperties) {
457
+ errors.push({
458
+ instanceLocation,
459
+ keyword: 'minProperties',
460
+ keywordLocation: `${schemaLocation}/minProperties`,
461
+ error: `Instance does not have at least ${$minProperties} properties.`
462
+ });
463
+ }
464
+ if ($maxProperties !== undefined && keys.length > $maxProperties) {
465
+ errors.push({
466
+ instanceLocation,
467
+ keyword: 'maxProperties',
468
+ keywordLocation: `${schemaLocation}/maxProperties`,
469
+ error: `Instance does not have at least ${$maxProperties} properties.`
470
+ });
471
+ }
472
+ if ($propertyNames !== undefined) {
473
+ const keywordLocation = `${schemaLocation}/propertyNames`;
474
+ for (const key in instance) {
475
+ const subInstancePointer = `${instanceLocation}/${encodePointer(key)}`;
476
+ const result = validate(
477
+ key,
478
+ $propertyNames,
479
+ draft,
480
+ lookup,
481
+ shortCircuit,
482
+ recursiveAnchor,
483
+ subInstancePointer,
484
+ keywordLocation
485
+ );
486
+ if (!result.valid) {
487
+ errors.push(
488
+ {
489
+ instanceLocation,
490
+ keyword: 'propertyNames',
491
+ keywordLocation,
492
+ error: `Property name "${key}" does not match schema.`
493
+ },
494
+ ...result.errors
495
+ );
496
+ }
497
+ }
498
+ }
499
+ if ($dependentRequired !== undefined) {
500
+ const keywordLocation = `${schemaLocation}/dependantRequired`;
501
+ for (const key in $dependentRequired) {
502
+ if (key in instance) {
503
+ const required = $dependentRequired[key];
504
+ for (const dependantKey of required) {
505
+ if (!(dependantKey in instance)) {
506
+ errors.push({
507
+ instanceLocation,
508
+ keyword: 'dependentRequired',
509
+ keywordLocation,
510
+ error: `Instance has "${key}" but does not have "${dependantKey}".`
511
+ });
512
+ }
513
+ }
514
+ }
515
+ }
516
+ }
517
+ if ($dependentSchemas !== undefined) {
518
+ for (const key in $dependentSchemas) {
519
+ const keywordLocation = `${schemaLocation}/dependentSchemas`;
520
+ if (key in instance) {
521
+ const result = validate(
522
+ instance,
523
+ $dependentSchemas[key],
524
+ draft,
525
+ lookup,
526
+ shortCircuit,
527
+ recursiveAnchor,
528
+ instanceLocation,
529
+ `${keywordLocation}/${encodePointer(key)}`,
530
+ evaluated
531
+ );
532
+ if (!result.valid) {
533
+ errors.push(
534
+ {
535
+ instanceLocation,
536
+ keyword: 'dependentSchemas',
537
+ keywordLocation,
538
+ error: `Instance has "${key}" but does not match dependant schema.`
539
+ },
540
+ ...result.errors
541
+ );
542
+ }
543
+ }
544
+ }
545
+ }
546
+ if ($dependencies !== undefined) {
547
+ const keywordLocation = `${schemaLocation}/dependencies`;
548
+ for (const key in $dependencies) {
549
+ if (key in instance) {
550
+ const propsOrSchema = $dependencies[key];
551
+ if (Array.isArray(propsOrSchema)) {
552
+ for (const dependantKey of propsOrSchema) {
553
+ if (!(dependantKey in instance)) {
554
+ errors.push({
555
+ instanceLocation,
556
+ keyword: 'dependencies',
557
+ keywordLocation,
558
+ error: `Instance has "${key}" but does not have "${dependantKey}".`
559
+ });
560
+ }
561
+ }
562
+ } else {
563
+ const result = validate(
564
+ instance,
565
+ propsOrSchema,
566
+ draft,
567
+ lookup,
568
+ shortCircuit,
569
+ recursiveAnchor,
570
+ instanceLocation,
571
+ `${keywordLocation}/${encodePointer(key)}`
572
+ );
573
+ if (!result.valid) {
574
+ errors.push(
575
+ {
576
+ instanceLocation,
577
+ keyword: 'dependencies',
578
+ keywordLocation,
579
+ error: `Instance has "${key}" but does not match dependant schema.`
580
+ },
581
+ ...result.errors
582
+ );
583
+ }
584
+ }
585
+ }
586
+ }
587
+ }
588
+ const thisEvaluated = Object.create(null);
589
+ let stop = false;
590
+ if ($properties !== undefined) {
591
+ const keywordLocation = `${schemaLocation}/properties`;
592
+ for (const key in $properties) {
593
+ if (!(key in instance)) {
594
+ continue;
595
+ }
596
+ const subInstancePointer = `${instanceLocation}/${encodePointer(key)}`;
597
+ const result = validate(
598
+ instance[key],
599
+ $properties[key],
600
+ draft,
601
+ lookup,
602
+ shortCircuit,
603
+ recursiveAnchor,
604
+ subInstancePointer,
605
+ `${keywordLocation}/${encodePointer(key)}`
606
+ );
607
+ if (result.valid) {
608
+ evaluated[key] = thisEvaluated[key] = true;
609
+ } else {
610
+ stop = shortCircuit;
611
+ errors.push(
612
+ {
613
+ instanceLocation,
614
+ keyword: 'properties',
615
+ keywordLocation,
616
+ error: `Property "${key}" does not match schema.`
617
+ },
618
+ ...result.errors
619
+ );
620
+ if (stop) break;
621
+ }
622
+ }
623
+ }
624
+ if (!stop && $patternProperties !== undefined) {
625
+ const keywordLocation = `${schemaLocation}/patternProperties`;
626
+ for (const pattern in $patternProperties) {
627
+ const regex = new RegExp(pattern, 'u');
628
+ const subSchema = $patternProperties[pattern];
629
+ for (const key in instance) {
630
+ if (!regex.test(key)) {
631
+ continue;
632
+ }
633
+ const subInstancePointer = `${instanceLocation}/${encodePointer(key)}`;
634
+ const result = validate(
635
+ instance[key],
636
+ subSchema,
637
+ draft,
638
+ lookup,
639
+ shortCircuit,
640
+ recursiveAnchor,
641
+ subInstancePointer,
642
+ `${keywordLocation}/${encodePointer(pattern)}`
643
+ );
644
+ if (result.valid) {
645
+ evaluated[key] = thisEvaluated[key] = true;
646
+ } else {
647
+ stop = shortCircuit;
648
+ errors.push(
649
+ {
650
+ instanceLocation,
651
+ keyword: 'patternProperties',
652
+ keywordLocation,
653
+ error: `Property "${key}" matches pattern "${pattern}" but does not match associated schema.`
654
+ },
655
+ ...result.errors
656
+ );
657
+ }
658
+ }
659
+ }
660
+ }
661
+ if (!stop && $additionalProperties !== undefined) {
662
+ const keywordLocation = `${schemaLocation}/additionalProperties`;
663
+ for (const key in instance) {
664
+ if (thisEvaluated[key]) {
665
+ continue;
666
+ }
667
+ const subInstancePointer = `${instanceLocation}/${encodePointer(key)}`;
668
+ const result = validate(
669
+ instance[key],
670
+ $additionalProperties,
671
+ draft,
672
+ lookup,
673
+ shortCircuit,
674
+ recursiveAnchor,
675
+ subInstancePointer,
676
+ keywordLocation
677
+ );
678
+ if (result.valid) {
679
+ evaluated[key] = true;
680
+ } else {
681
+ stop = shortCircuit;
682
+ errors.push(
683
+ {
684
+ instanceLocation,
685
+ keyword: 'additionalProperties',
686
+ keywordLocation,
687
+ error: `Property "${key}" does not match additional properties schema.`
688
+ },
689
+ ...result.errors
690
+ );
691
+ }
692
+ }
693
+ } else if (!stop && $unevaluatedProperties !== undefined) {
694
+ const keywordLocation = `${schemaLocation}/unevaluatedProperties`;
695
+ for (const key in instance) {
696
+ if (!evaluated[key]) {
697
+ const subInstancePointer = `${instanceLocation}/${encodePointer(key)}`;
698
+ const result = validate(
699
+ instance[key],
700
+ $unevaluatedProperties,
701
+ draft,
702
+ lookup,
703
+ shortCircuit,
704
+ recursiveAnchor,
705
+ subInstancePointer,
706
+ keywordLocation
707
+ );
708
+ if (result.valid) {
709
+ evaluated[key] = true;
710
+ } else {
711
+ errors.push(
712
+ {
713
+ instanceLocation,
714
+ keyword: 'unevaluatedProperties',
715
+ keywordLocation,
716
+ error: `Property "${key}" does not match unevaluated properties schema.`
717
+ },
718
+ ...result.errors
719
+ );
720
+ }
721
+ }
722
+ }
723
+ }
724
+ } else if (instanceType === 'array') {
725
+ if ($maxItems !== undefined && instance.length > $maxItems) {
726
+ errors.push({
727
+ instanceLocation,
728
+ keyword: 'maxItems',
729
+ keywordLocation: `${schemaLocation}/maxItems`,
730
+ error: `Array has too many items (${instance.length} > ${$maxItems}).`
731
+ });
732
+ }
733
+ if ($minItems !== undefined && instance.length < $minItems) {
734
+ errors.push({
735
+ instanceLocation,
736
+ keyword: 'minItems',
737
+ keywordLocation: `${schemaLocation}/minItems`,
738
+ error: `Array has too few items (${instance.length} < ${$minItems}).`
739
+ });
740
+ }
741
+ const length = instance.length;
742
+ let i = 0;
743
+ let stop = false;
744
+ if ($prefixItems !== undefined) {
745
+ const keywordLocation = `${schemaLocation}/prefixItems`;
746
+ const length2 = Math.min($prefixItems.length, length);
747
+ for (; i < length2; i++) {
748
+ const result = validate(
749
+ instance[i],
750
+ $prefixItems[i],
751
+ draft,
752
+ lookup,
753
+ shortCircuit,
754
+ recursiveAnchor,
755
+ `${instanceLocation}/${i}`,
756
+ `${keywordLocation}/${i}`
757
+ );
758
+ evaluated[i] = true;
759
+ if (!result.valid) {
760
+ stop = shortCircuit;
761
+ errors.push(
762
+ {
763
+ instanceLocation,
764
+ keyword: 'prefixItems',
765
+ keywordLocation,
766
+ error: `Items did not match schema.`
767
+ },
768
+ ...result.errors
769
+ );
770
+ if (stop) break;
771
+ }
772
+ }
773
+ }
774
+ if ($items !== undefined) {
775
+ const keywordLocation = `${schemaLocation}/items`;
776
+ if (Array.isArray($items)) {
777
+ const length2 = Math.min($items.length, length);
778
+ for (; i < length2; i++) {
779
+ const result = validate(
780
+ instance[i],
781
+ $items[i],
782
+ draft,
783
+ lookup,
784
+ shortCircuit,
785
+ recursiveAnchor,
786
+ `${instanceLocation}/${i}`,
787
+ `${keywordLocation}/${i}`
788
+ );
789
+ evaluated[i] = true;
790
+ if (!result.valid) {
791
+ stop = shortCircuit;
792
+ errors.push(
793
+ {
794
+ instanceLocation,
795
+ keyword: 'items',
796
+ keywordLocation,
797
+ error: `Items did not match schema.`
798
+ },
799
+ ...result.errors
800
+ );
801
+ if (stop) break;
802
+ }
803
+ }
804
+ } else {
805
+ for (; i < length; i++) {
806
+ const result = validate(
807
+ instance[i],
808
+ $items,
809
+ draft,
810
+ lookup,
811
+ shortCircuit,
812
+ recursiveAnchor,
813
+ `${instanceLocation}/${i}`,
814
+ keywordLocation
815
+ );
816
+ evaluated[i] = true;
817
+ if (!result.valid) {
818
+ stop = shortCircuit;
819
+ errors.push(
820
+ {
821
+ instanceLocation,
822
+ keyword: 'items',
823
+ keywordLocation,
824
+ error: `Items did not match schema.`
825
+ },
826
+ ...result.errors
827
+ );
828
+ if (stop) break;
829
+ }
830
+ }
831
+ }
832
+ if (!stop && $additionalItems !== undefined) {
833
+ const keywordLocation = `${schemaLocation}/additionalItems`;
834
+ for (; i < length; i++) {
835
+ const result = validate(
836
+ instance[i],
837
+ $additionalItems,
838
+ draft,
839
+ lookup,
840
+ shortCircuit,
841
+ recursiveAnchor,
842
+ `${instanceLocation}/${i}`,
843
+ keywordLocation
844
+ );
845
+ evaluated[i] = true;
846
+ if (!result.valid) {
847
+ stop = shortCircuit;
848
+ errors.push(
849
+ {
850
+ instanceLocation,
851
+ keyword: 'additionalItems',
852
+ keywordLocation,
853
+ error: `Items did not match additional items schema.`
854
+ },
855
+ ...result.errors
856
+ );
857
+ }
858
+ }
859
+ }
860
+ }
861
+ if ($contains !== undefined) {
862
+ if (length === 0 && $minContains === undefined) {
863
+ errors.push({
864
+ instanceLocation,
865
+ keyword: 'contains',
866
+ keywordLocation: `${schemaLocation}/contains`,
867
+ error: `Array is empty. It must contain at least one item matching the schema.`
868
+ });
869
+ } else if ($minContains !== undefined && length < $minContains) {
870
+ errors.push({
871
+ instanceLocation,
872
+ keyword: 'minContains',
873
+ keywordLocation: `${schemaLocation}/minContains`,
874
+ error: `Array has less items (${length}) than minContains (${$minContains}).`
875
+ });
876
+ } else {
877
+ const keywordLocation = `${schemaLocation}/contains`;
878
+ const errorsLength = errors.length;
879
+ let contained = 0;
880
+ for (let j = 0; j < length; j++) {
881
+ const result = validate(
882
+ instance[j],
883
+ $contains,
884
+ draft,
885
+ lookup,
886
+ shortCircuit,
887
+ recursiveAnchor,
888
+ `${instanceLocation}/${j}`,
889
+ keywordLocation
890
+ );
891
+ if (result.valid) {
892
+ evaluated[j] = true;
893
+ contained++;
894
+ } else {
895
+ errors.push(...result.errors);
896
+ }
897
+ }
898
+ if (contained >= ($minContains || 0)) {
899
+ errors.length = errorsLength;
900
+ }
901
+ if (
902
+ $minContains === undefined &&
903
+ $maxContains === undefined &&
904
+ contained === 0
905
+ ) {
906
+ errors.splice(errorsLength, 0, {
907
+ instanceLocation,
908
+ keyword: 'contains',
909
+ keywordLocation,
910
+ error: `Array does not contain item matching schema.`
911
+ });
912
+ } else if ($minContains !== undefined && contained < $minContains) {
913
+ errors.push({
914
+ instanceLocation,
915
+ keyword: 'minContains',
916
+ keywordLocation: `${schemaLocation}/minContains`,
917
+ error: `Array must contain at least ${$minContains} items matching schema. Only ${contained} items were found.`
918
+ });
919
+ } else if ($maxContains !== undefined && contained > $maxContains) {
920
+ errors.push({
921
+ instanceLocation,
922
+ keyword: 'maxContains',
923
+ keywordLocation: `${schemaLocation}/maxContains`,
924
+ error: `Array may contain at most ${$maxContains} items matching schema. ${contained} items were found.`
925
+ });
926
+ }
927
+ }
928
+ }
929
+ if (!stop && $unevaluatedItems !== undefined) {
930
+ const keywordLocation = `${schemaLocation}/unevaluatedItems`;
931
+ for (i; i < length; i++) {
932
+ if (evaluated[i]) {
933
+ continue;
934
+ }
935
+ const result = validate(
936
+ instance[i],
937
+ $unevaluatedItems,
938
+ draft,
939
+ lookup,
940
+ shortCircuit,
941
+ recursiveAnchor,
942
+ `${instanceLocation}/${i}`,
943
+ keywordLocation
944
+ );
945
+ evaluated[i] = true;
946
+ if (!result.valid) {
947
+ errors.push(
948
+ {
949
+ instanceLocation,
950
+ keyword: 'unevaluatedItems',
951
+ keywordLocation,
952
+ error: `Items did not match unevaluated items schema.`
953
+ },
954
+ ...result.errors
955
+ );
956
+ }
957
+ }
958
+ }
959
+ if ($uniqueItems) {
960
+ for (let j = 0; j < length; j++) {
961
+ const a = instance[j];
962
+ const ao = typeof a === 'object' && a !== null;
963
+ for (let k = 0; k < length; k++) {
964
+ if (j === k) {
965
+ continue;
966
+ }
967
+ const b = instance[k];
968
+ const bo = typeof b === 'object' && b !== null;
969
+ if (a === b || (ao && bo && deepCompareStrict(a, b))) {
970
+ errors.push({
971
+ instanceLocation,
972
+ keyword: 'uniqueItems',
973
+ keywordLocation: `${schemaLocation}/uniqueItems`,
974
+ error: `Duplicate items at indexes ${j} and ${k}.`
975
+ });
976
+ j = Number.MAX_SAFE_INTEGER;
977
+ k = Number.MAX_SAFE_INTEGER;
978
+ }
979
+ }
980
+ }
981
+ }
982
+ } else if (instanceType === 'number') {
983
+ if (draft === '4') {
984
+ if (
985
+ $minimum !== undefined &&
986
+ (($exclusiveMinimum === true && instance <= $minimum) ||
987
+ instance < $minimum)
988
+ ) {
989
+ errors.push({
990
+ instanceLocation,
991
+ keyword: 'minimum',
992
+ keywordLocation: `${schemaLocation}/minimum`,
993
+ error: `${instance} is less than ${$exclusiveMinimum ? 'or equal to ' : ''} ${$minimum}.`
994
+ });
995
+ }
996
+ if (
997
+ $maximum !== undefined &&
998
+ (($exclusiveMaximum === true && instance >= $maximum) ||
999
+ instance > $maximum)
1000
+ ) {
1001
+ errors.push({
1002
+ instanceLocation,
1003
+ keyword: 'maximum',
1004
+ keywordLocation: `${schemaLocation}/maximum`,
1005
+ error: `${instance} is greater than ${$exclusiveMaximum ? 'or equal to ' : ''} ${$maximum}.`
1006
+ });
1007
+ }
1008
+ } else {
1009
+ if ($minimum !== undefined && instance < $minimum) {
1010
+ errors.push({
1011
+ instanceLocation,
1012
+ keyword: 'minimum',
1013
+ keywordLocation: `${schemaLocation}/minimum`,
1014
+ error: `${instance} is less than ${$minimum}.`
1015
+ });
1016
+ }
1017
+ if ($maximum !== undefined && instance > $maximum) {
1018
+ errors.push({
1019
+ instanceLocation,
1020
+ keyword: 'maximum',
1021
+ keywordLocation: `${schemaLocation}/maximum`,
1022
+ error: `${instance} is greater than ${$maximum}.`
1023
+ });
1024
+ }
1025
+ if ($exclusiveMinimum !== undefined && instance <= $exclusiveMinimum) {
1026
+ errors.push({
1027
+ instanceLocation,
1028
+ keyword: 'exclusiveMinimum',
1029
+ keywordLocation: `${schemaLocation}/exclusiveMinimum`,
1030
+ error: `${instance} is less than ${$exclusiveMinimum}.`
1031
+ });
1032
+ }
1033
+ if ($exclusiveMaximum !== undefined && instance >= $exclusiveMaximum) {
1034
+ errors.push({
1035
+ instanceLocation,
1036
+ keyword: 'exclusiveMaximum',
1037
+ keywordLocation: `${schemaLocation}/exclusiveMaximum`,
1038
+ error: `${instance} is greater than or equal to ${$exclusiveMaximum}.`
1039
+ });
1040
+ }
1041
+ }
1042
+ if ($multipleOf !== undefined) {
1043
+ const remainder = instance % $multipleOf;
1044
+ if (
1045
+ Math.abs(0 - remainder) >= 1.1920929e-7 &&
1046
+ Math.abs($multipleOf - remainder) >= 1.1920929e-7
1047
+ ) {
1048
+ errors.push({
1049
+ instanceLocation,
1050
+ keyword: 'multipleOf',
1051
+ keywordLocation: `${schemaLocation}/multipleOf`,
1052
+ error: `${instance} is not a multiple of ${$multipleOf}.`
1053
+ });
1054
+ }
1055
+ }
1056
+ } else if (instanceType === 'string') {
1057
+ const length =
1058
+ $minLength === undefined && $maxLength === undefined
1059
+ ? 0
1060
+ : ucs2length(instance);
1061
+ if ($minLength !== undefined && length < $minLength) {
1062
+ errors.push({
1063
+ instanceLocation,
1064
+ keyword: 'minLength',
1065
+ keywordLocation: `${schemaLocation}/minLength`,
1066
+ error: `String is too short (${length} < ${$minLength}).`
1067
+ });
1068
+ }
1069
+ if ($maxLength !== undefined && length > $maxLength) {
1070
+ errors.push({
1071
+ instanceLocation,
1072
+ keyword: 'maxLength',
1073
+ keywordLocation: `${schemaLocation}/maxLength`,
1074
+ error: `String is too long (${length} > ${$maxLength}).`
1075
+ });
1076
+ }
1077
+ if ($pattern !== undefined && !new RegExp($pattern, 'u').test(instance)) {
1078
+ errors.push({
1079
+ instanceLocation,
1080
+ keyword: 'pattern',
1081
+ keywordLocation: `${schemaLocation}/pattern`,
1082
+ error: `String does not match pattern.`
1083
+ });
1084
+ }
1085
+ if (
1086
+ $format !== undefined &&
1087
+ fastFormat[$format] &&
1088
+ !fastFormat[$format](instance)
1089
+ ) {
1090
+ errors.push({
1091
+ instanceLocation,
1092
+ keyword: 'format',
1093
+ keywordLocation: `${schemaLocation}/format`,
1094
+ error: `String does not match format "${$format}".`
1095
+ });
1096
+ }
1097
+ }
1098
+ return { valid: errors.length === 0, errors };
1099
+ }