-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathdescribe.mjs
More file actions
1657 lines (1491 loc) · 55.9 KB
/
describe.mjs
File metadata and controls
1657 lines (1491 loc) · 55.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {
ASSERTION_FAIL, ASSERTION_DEFINES, ASSERTION_DEFINES_STRICT,
ASSERTION_DEFINES_ALL, ASSERTION_DEFINES_ALL_STRICT,
ASSERTION_DEFINES_EXACTLY, ASSERTION_DEFINES_EXACTLY_STRICT,
ASSERTION_DEFINES_EXACTLY_STRICT_HASH3, ASSERTION_PROPERTY_DEPENDENCIES,
ASSERTION_TYPE, ASSERTION_TYPE_ANY, ASSERTION_TYPE_STRICT,
ASSERTION_TYPE_STRICT_ANY, ASSERTION_NOT_TYPE_STRICT_ANY,
ASSERTION_TYPE_STRING_BOUNDED,
ASSERTION_TYPE_STRING_UPPER, ASSERTION_TYPE_ARRAY_BOUNDED,
ASSERTION_TYPE_ARRAY_UPPER, ASSERTION_TYPE_OBJECT_BOUNDED,
ASSERTION_TYPE_OBJECT_UPPER, ASSERTION_REGEX,
ASSERTION_STRING_SIZE_LESS, ASSERTION_STRING_SIZE_GREATER,
ASSERTION_ARRAY_SIZE_LESS, ASSERTION_ARRAY_SIZE_GREATER,
ASSERTION_OBJECT_SIZE_LESS, ASSERTION_OBJECT_SIZE_GREATER,
ASSERTION_EQUAL, ASSERTION_EQUALS_ANY, ASSERTION_EQUALS_ANY_STRING_HASH,
ASSERTION_GREATER_EQUAL, ASSERTION_LESS_EQUAL,
ASSERTION_GREATER, ASSERTION_LESS,
ASSERTION_UNIQUE, ASSERTION_DIVISIBLE,
ASSERTION_TYPE_INTEGER_BOUNDED, ASSERTION_TYPE_INTEGER_BOUNDED_STRICT,
ASSERTION_TYPE_INTEGER_LOWER_BOUND, ASSERTION_TYPE_INTEGER_LOWER_BOUND_STRICT,
ASSERTION_STRING_TYPE,
ASSERTION_PROPERTY_TYPE, ASSERTION_PROPERTY_TYPE_EVALUATE,
ASSERTION_PROPERTY_TYPE_STRICT, ASSERTION_PROPERTY_TYPE_STRICT_EVALUATE,
ASSERTION_PROPERTY_TYPE_STRICT_ANY, ASSERTION_PROPERTY_TYPE_STRICT_ANY_EVALUATE,
ASSERTION_ARRAY_PREFIX, ASSERTION_ARRAY_PREFIX_EVALUATE,
ASSERTION_OBJECT_PROPERTIES_SIMPLE,
ANNOTATION_EMIT, ANNOTATION_TO_PARENT, ANNOTATION_BASENAME_TO_PARENT,
EVALUATE,
LOGICAL_NOT, LOGICAL_NOT_EVALUATE,
LOGICAL_OR, LOGICAL_AND, LOGICAL_XOR, LOGICAL_CONDITION,
LOGICAL_WHEN_TYPE, LOGICAL_WHEN_DEFINES, LOGICAL_WHEN_ARRAY_SIZE_GREATER,
LOOP_PROPERTIES_UNEVALUATED, LOOP_PROPERTIES_UNEVALUATED_EXCEPT,
LOOP_PROPERTIES_MATCH, LOOP_PROPERTIES_MATCH_CLOSED,
LOOP_PROPERTIES, LOOP_PROPERTIES_EVALUATE,
LOOP_PROPERTIES_REGEX, LOOP_PROPERTIES_REGEX_CLOSED,
LOOP_PROPERTIES_STARTS_WITH, LOOP_PROPERTIES_EXCEPT,
LOOP_PROPERTIES_TYPE, LOOP_PROPERTIES_TYPE_EVALUATE,
LOOP_PROPERTIES_EXACTLY_TYPE_STRICT, LOOP_PROPERTIES_EXACTLY_TYPE_STRICT_HASH,
LOOP_PROPERTIES_TYPE_STRICT, LOOP_PROPERTIES_TYPE_STRICT_EVALUATE,
LOOP_PROPERTIES_TYPE_STRICT_ANY, LOOP_PROPERTIES_TYPE_STRICT_ANY_EVALUATE,
LOOP_KEYS, LOOP_ITEMS, LOOP_ITEMS_FROM, LOOP_ITEMS_UNEVALUATED,
LOOP_ITEMS_TYPE, LOOP_ITEMS_TYPE_STRICT, LOOP_ITEMS_TYPE_STRICT_ANY,
LOOP_ITEMS_PROPERTIES_EXACTLY_TYPE_STRICT_HASH,
LOOP_ITEMS_PROPERTIES_EXACTLY_TYPE_STRICT_HASH3,
LOOP_ITEMS_INTEGER_BOUNDED, LOOP_ITEMS_INTEGER_BOUNDED_SIZED,
LOOP_CONTAINS,
CONTROL_DYNAMIC_ANCHOR_JUMP, CONTROL_JUMP
} from './opcodes.mjs';
const TYPE_INTEGER = 2;
const TYPE_REAL = 3;
const TYPE_OBJECT = 6;
const TYPE_DECIMAL = 7;
const TYPE_NAMES = [ 'null', 'boolean', 'integer', 'number',
'string', 'array', 'object', 'number' ];
function typeName(typeIndex) {
return TYPE_NAMES[typeIndex];
}
function jsonTypeOf(value) {
if (value === null) return 0;
switch (typeof value) {
case 'boolean': return 1;
case 'number': return Number.isInteger(value) ? 2 : 3;
case 'bigint': return 2;
case 'string': return 4;
case 'object': return Array.isArray(value) ? 5 : 6;
default: return 0;
}
}
function valueTypeName(value) {
if (typeof value === 'bigint') return 'integer';
if (typeof value === 'number') {
return Number.isInteger(value) ? 'integer' : 'number';
}
return typeName(jsonTypeOf(value));
}
function escapeString(input) {
return '"' + String(input).replaceAll('"', '\\"') + '"';
}
function describeExtras(valid, target, allowed) {
if (valid || target === null || typeof target !== 'object' ||
Array.isArray(target)) {
return '';
}
const extras = [];
for (const key in target) {
if (!allowed.has(key)) {
extras.push(key);
}
}
if (extras.length === 0) return '';
extras.sort();
if (extras.length === 1) {
return ', but it also defines the property ' + escapeString(extras[0]);
}
let message = ', but it also defines properties ';
for (let index = 0; index < extras.length; index++) {
if (index === extras.length - 1) {
message += 'and ' + escapeString(extras[index]);
} else {
message += escapeString(extras[index]) + ', ';
}
}
return message;
}
function stringifyValue(value) {
if (typeof value === 'bigint') return String(value);
return JSON.stringify(value, (key, current) =>
typeof current === 'bigint' ? JSON.rawJSON(String(current)) : current);
}
function resolveTarget(instance, instanceLocation) {
if (instanceLocation === '') return instance;
const tokens = instanceLocation.slice(1).split('/');
let current = instance;
for (const raw of tokens) {
const token = raw.replaceAll('~1', '/').replaceAll('~0', '~');
if (Array.isArray(current)) {
current = current[Number(token)];
} else {
current = current[token];
}
}
return current;
}
function extractKeyword(evaluatePath) {
if (evaluatePath === '') return '';
const lastSlash = evaluatePath.lastIndexOf('/');
if (lastSlash === -1) return '';
const token = evaluatePath.slice(lastSlash + 1)
.replaceAll('~1', '/').replaceAll('~0', '~');
if (/^[0-9]+$/.test(token)) return '';
return token;
}
function isWithinKeyword(evaluatePath, keyword) {
const segments = evaluatePath.split('/');
for (let index = 1; index < segments.length; index++) {
if (segments[index].replaceAll('~1', '/').replaceAll('~0', '~') === keyword) {
return true;
}
}
return false;
}
function lastInstanceToken(instanceLocation) {
if (instanceLocation === '') return null;
const lastSlash = instanceLocation.lastIndexOf('/');
return instanceLocation.slice(lastSlash + 1)
.replaceAll('~1', '/').replaceAll('~0', '~');
}
function objectSize(value) {
let count = 0;
for (const _key in value) count++;
return count;
}
function objectKeys(value) {
const keys = [];
for (const key in value) keys.push(key);
return keys;
}
function unicodeLength(string) {
let count = 0;
for (let index = 0; index < string.length; index++) {
count++;
const code = string.charCodeAt(index);
if (code >= 0xD800 && code <= 0xDBFF) index++;
}
return count;
}
function isObject(value) {
return value !== null && typeof value === 'object' && !Array.isArray(value);
}
function jsonEqual(left, right) {
if (left === right) return true;
if (left === null || right === null) return left === right;
if (typeof left !== typeof right) return false;
if (typeof left !== 'object') return left === right;
if (Array.isArray(left) !== Array.isArray(right)) return false;
if (Array.isArray(left)) {
if (left.length !== right.length) return false;
for (let index = 0; index < left.length; index++) {
if (!jsonEqual(left[index], right[index])) return false;
}
return true;
}
const leftKeys = Object.keys(left).sort();
const rightKeys = Object.keys(right).sort();
if (leftKeys.length !== rightKeys.length) return false;
for (let index = 0; index < leftKeys.length; index++) {
if (leftKeys[index] !== rightKeys[index]) return false;
if (!jsonEqual(left[leftKeys[index]], right[rightKeys[index]])) return false;
}
return true;
}
function jsonCompare(left, right) {
const leftType = jsonTypeOf(left);
const rightType = jsonTypeOf(right);
if (leftType !== rightType) return leftType - rightType;
if (left === null) return 0;
if (typeof left === 'boolean') return (left ? 1 : 0) - (right ? 1 : 0);
if (typeof left === 'number' || typeof left === 'bigint') {
if (left < right) return -1;
if (left > right) return 1;
return 0;
}
if (typeof left === 'string') return left < right ? -1 : left > right ? 1 : 0;
const leftStr = JSON.stringify(left);
const rightStr = JSON.stringify(right);
return leftStr < rightStr ? -1 : leftStr > rightStr ? 1 : 0;
}
function normalizeTypes(bitmask) {
let types = bitmask;
const hasReal = (types & (1 << TYPE_REAL)) !== 0;
const hasInteger = (types & (1 << TYPE_INTEGER)) !== 0;
const hasDecimal = (types & (1 << 7)) !== 0;
if (hasReal && hasInteger) types &= ~(1 << TYPE_INTEGER);
if (hasReal && hasDecimal) types &= ~(1 << 7);
if (hasInteger && hasDecimal) types &= ~(1 << 7);
return types;
}
function describeTypeCheck(valid, currentType, expectedType) {
let message = 'The value was expected to be of type ' + typeName(expectedType);
if (!valid) {
message += ' but it was of type ' + typeName(currentType);
}
return message;
}
function describeNotTypeCheck(valid, currentType, expectedType) {
let message = 'The value was expected to NOT be of type ' + typeName(expectedType);
if (!valid) {
message += ' but it was of type ';
if (currentType === TYPE_DECIMAL && expectedType === TYPE_INTEGER) {
message += 'integer';
} else if ((currentType === TYPE_INTEGER && expectedType === TYPE_REAL) ||
currentType === TYPE_DECIMAL) {
message += 'number';
} else {
message += typeName(currentType);
}
}
return message;
}
function describeTypesCheck(valid, currentType, bitmask) {
let types = normalizeTypes(bitmask);
const hasReal = (bitmask & (1 << TYPE_REAL)) !== 0;
const hasInteger = (bitmask & (1 << TYPE_INTEGER)) !== 0;
let popcount = 0;
for (let bit = 0; bit < 8; bit++) {
if ((types & (1 << bit)) !== 0) popcount++;
}
if (popcount === 1) {
let typeIndex = 0;
for (let bit = 0; bit < 8; bit++) {
if ((types & (1 << bit)) !== 0) { typeIndex = bit; break; }
}
return describeTypeCheck(valid, currentType, typeIndex);
}
let message = 'The value was expected to be of type ';
let first = true;
let lastBit = 0;
for (let bit = 0; bit < 8; bit++) {
if ((types & (1 << bit)) !== 0) lastBit = bit;
}
for (let bit = 0; bit < 8; bit++) {
if ((types & (1 << bit)) !== 0) {
if (!first) message += ', ';
if (bit === lastBit) message += 'or ';
message += typeName(bit);
first = false;
}
}
if (valid) {
message += ' and it was of type ';
} else {
message += ' but it was of type ';
}
if (valid && currentType === TYPE_INTEGER && hasReal) {
message += 'number';
} else if ((valid && currentType === TYPE_INTEGER && hasReal) ||
currentType === TYPE_REAL) {
message += 'number';
} else {
message += typeName(currentType);
}
return message;
}
function describeNotTypesCheck(valid, currentType, bitmask) {
let types = normalizeTypes(bitmask);
const hasReal = (bitmask & (1 << TYPE_REAL)) !== 0;
const hasInteger = (bitmask & (1 << TYPE_INTEGER)) !== 0;
let popcount = 0;
for (let bit = 0; bit < 8; bit++) {
if ((types & (1 << bit)) !== 0) popcount++;
}
if (popcount === 1) {
let typeIndex = 0;
for (let bit = 0; bit < 8; bit++) {
if ((types & (1 << bit)) !== 0) { typeIndex = bit; break; }
}
return describeNotTypeCheck(valid, currentType, typeIndex);
}
let message = 'The value was expected to NOT be of type ';
let first = true;
let lastBit = 0;
for (let bit = 0; bit < 8; bit++) {
if ((types & (1 << bit)) !== 0) lastBit = bit;
}
for (let bit = 0; bit < 8; bit++) {
if ((types & (1 << bit)) !== 0) {
if (!first) message += ', ';
if (bit === lastBit) message += 'or ';
message += typeName(bit);
first = false;
}
}
if (valid) {
message += ' and it was of type ';
} else {
message += ' but it was of type ';
}
if (!valid && currentType === TYPE_INTEGER && hasReal) {
message += 'number';
} else if ((!valid && currentType === TYPE_INTEGER && hasReal) ||
currentType === TYPE_REAL) {
message += 'number';
} else {
message += typeName(currentType);
}
return message;
}
function describeReference(target) {
return 'The ' + typeName(jsonTypeOf(target)) +
' value was expected to validate against the referenced schema';
}
function describeTypeList(bitmask) {
let types = normalizeTypes(bitmask);
let popcount = 0;
for (let bit = 0; bit < 8; bit++) {
if ((types & (1 << bit)) !== 0) popcount++;
}
if (popcount === 1) {
for (let bit = 0; bit < 8; bit++) {
if ((types & (1 << bit)) !== 0) return typeName(bit);
}
}
let result = '';
let first = true;
let lastBit = 0;
for (let bit = 0; bit < 8; bit++) {
if ((types & (1 << bit)) !== 0) lastBit = bit;
}
for (let bit = 0; bit < 8; bit++) {
if ((types & (1 << bit)) !== 0) {
if (!first) result += ', ';
if (bit === lastBit) result += 'or ';
result += typeName(bit);
first = false;
}
}
return result;
}
function formatList(items, conjunction) {
let result = '';
for (let index = 0; index < items.length; index++) {
if (index === items.length - 1) {
result += conjunction + ' ' + items[index];
} else {
result += items[index] + ', ';
}
}
return result;
}
export function describe(valid, instruction, evaluatePath,
instanceLocation, instance, annotation) {
const opcode = instruction[0];
const value = instruction[5];
const children = instruction[6];
const keyword = extractKeyword(evaluatePath);
const target = resolveTarget(instance, instanceLocation);
const targetType = jsonTypeOf(target);
if (opcode === ASSERTION_FAIL) {
if (keyword === 'enum') {
return 'The ' + typeName(targetType) +
' value was not expected to validate against the empty enumeration';
}
if (keyword === 'contains') {
return 'The constraints declared for this keyword were not satisfiable';
}
if (keyword === 'additionalProperties' ||
keyword === 'unevaluatedProperties') {
const property = lastInstanceToken(instanceLocation);
return 'The object value was not expected to define the property ' +
escapeString(property);
}
if (keyword === 'unevaluatedItems') {
const tokenValue = lastInstanceToken(instanceLocation);
return 'The array value was not expected to define the item at index ' +
tokenValue;
}
return 'No instance is expected to succeed against the false schema';
}
if (opcode === LOGICAL_OR) {
const childCount = children ? children.length : 0;
let message = 'The ' + typeName(targetType) +
' value was expected to validate against ';
if (childCount > 1) {
message += 'at least one of the ' + childCount + ' given subschemas';
} else {
message += 'the given subschema';
}
return message;
}
if (opcode === LOGICAL_AND) {
if (keyword === 'allOf' || keyword === 'extends') {
const childCount = children ? children.length : 0;
let message = 'The ' + typeName(targetType) +
' value was expected to validate against the ';
if (childCount > 1) {
message += childCount + ' given subschemas';
} else {
message += 'given subschema';
}
return message;
}
if (keyword === '$ref') {
return describeReference(target);
}
return '<unknown>';
}
if (opcode === LOGICAL_XOR) {
const childCount = children ? children.length : 0;
let message = '';
if (isWithinKeyword(evaluatePath, 'propertyNames') &&
instanceLocation !== '' && lastInstanceToken(instanceLocation) !== null) {
const propertyName = lastInstanceToken(instanceLocation);
message += 'The property name ' + escapeString(propertyName);
} else {
message += 'The ' + typeName(targetType) + ' value';
}
message += ' was expected to validate against ';
if (childCount > 1) {
message += 'one and only one of the ' + childCount + ' given subschemas';
} else {
message += 'the given subschema';
}
return message;
}
if (opcode === LOGICAL_CONDITION) {
return 'The ' + typeName(targetType) +
' value was expected to validate against the given conditional';
}
if (opcode === LOGICAL_NOT) {
let message = 'The ' + typeName(targetType) +
' value was expected to not validate against the given subschema';
if (!valid) message += ', but it did';
return message;
}
if (opcode === LOGICAL_NOT_EVALUATE) {
let message = 'The ' + typeName(targetType) +
' value was expected to not validate against the given subschema';
if (!valid) message += ', but it did';
return message;
}
if (opcode === EVALUATE) {
return 'The instance location was marked as evaluated';
}
if (opcode === CONTROL_DYNAMIC_ANCHOR_JUMP) {
if (keyword === '$dynamicRef') {
return 'The ' + typeName(targetType) +
' value was expected to validate against the first subschema ' +
'in scope that declared the dynamic anchor ' + escapeString(value);
}
return 'The ' + typeName(targetType) +
' value was expected to validate against the first subschema ' +
'in scope that declared a recursive anchor';
}
if (opcode === ANNOTATION_EMIT) {
if (keyword === 'properties') {
return 'The object property ' + escapeString(annotation) +
' successfully validated against its property subschema';
}
if ((keyword === 'items' || keyword === 'additionalItems') &&
annotation === true) {
return 'Every item in the array value was successfully validated';
}
if ((keyword === 'prefixItems' || keyword === 'items') &&
typeof annotation === 'number') {
if (annotation === 0) {
return 'The first item of the array value successfully validated ' +
'against the first positional subschema';
}
return 'The first ' + (annotation + 1) +
' items of the array value successfully validated against the given ' +
'positional subschemas';
}
if (keyword === 'prefixItems' && annotation === true) {
return 'Every item of the array value validated against the given ' +
'positional subschemas';
}
if (keyword === 'title' || keyword === 'description') {
let message = 'The ' + keyword + ' of the';
if (instanceLocation === '') {
message += ' instance';
} else {
message += ' instance location "' + instanceLocation + '"';
}
message += ' was ' + escapeString(annotation);
return message;
}
if (keyword === 'default') {
let message = 'The default value of the';
if (instanceLocation === '') {
message += ' instance';
} else {
message += ' instance location "' + instanceLocation + '"';
}
message += ' was ' + stringifyValue(annotation);
return message;
}
if (keyword === 'deprecated' && typeof annotation === 'boolean') {
let message = '';
if (instanceLocation === '') {
message += 'The instance';
} else {
message += 'The instance location "' + instanceLocation + '"';
}
message += annotation
? ' was considered deprecated'
: ' was not considered deprecated';
return message;
}
if (keyword === 'readOnly' && typeof annotation === 'boolean') {
let message = '';
if (instanceLocation === '') {
message += 'The instance';
} else {
message += 'The instance location "' + instanceLocation + '"';
}
message += annotation
? ' was considered read-only'
: ' was not considered read-only';
return message;
}
if (keyword === 'writeOnly' && typeof annotation === 'boolean') {
let message = '';
if (instanceLocation === '') {
message += 'The instance';
} else {
message += 'The instance location "' + instanceLocation + '"';
}
message += annotation
? ' was considered write-only'
: ' was not considered write-only';
return message;
}
if (keyword === 'examples') {
let message = '';
if (instanceLocation === '') {
message += 'Examples of the instance';
} else {
message += 'Examples of the instance location "' +
instanceLocation + '"';
}
message += ' were ';
for (let index = 0; index < annotation.length; index++) {
if (index === annotation.length - 1) {
message += 'and ' + stringifyValue(annotation[index]);
} else {
message += stringifyValue(annotation[index]) + ', ';
}
}
return message;
}
if (keyword === 'contentEncoding') {
let message = 'The content encoding of the';
if (instanceLocation === '') {
message += ' instance';
} else {
message += ' instance location "' + instanceLocation + '"';
}
message += ' was ' + escapeString(annotation);
return message;
}
if (keyword === 'contentMediaType') {
let message = 'The content media type of the';
if (instanceLocation === '') {
message += ' instance';
} else {
message += ' instance location "' + instanceLocation + '"';
}
message += ' was ' + escapeString(annotation);
return message;
}
if (keyword === 'contentSchema') {
let message = 'When decoded, the';
if (instanceLocation === '') {
message += ' instance';
} else {
message += ' instance location "' + instanceLocation + '"';
}
message += ' was expected to validate against the schema ' +
stringifyValue(annotation);
return message;
}
if (keyword === 'format') {
let message = 'The logical type of the';
if (instanceLocation === '') {
message += ' instance';
} else {
message += ' instance location "' + instanceLocation + '"';
}
message += ' was expected to be ' + stringifyValue(annotation);
return message;
}
return 'The unrecognized keyword ' + escapeString(keyword) +
' was collected as the annotation ' + stringifyValue(annotation);
}
if (opcode === ANNOTATION_TO_PARENT) {
if (keyword === 'unevaluatedItems' && annotation === true) {
return 'At least one item of the array value successfully validated ' +
'against the subschema for unevaluated items';
}
return '<unknown>';
}
if (opcode === ANNOTATION_BASENAME_TO_PARENT) {
if (keyword === 'patternProperties') {
return 'The object property ' + escapeString(String(annotation)) +
' successfully validated against its pattern property subschema';
}
if (keyword === 'additionalProperties') {
return 'The object property ' + escapeString(String(annotation)) +
' successfully validated against the additional properties subschema';
}
if (keyword === 'unevaluatedProperties') {
return 'The object property ' + escapeString(String(annotation)) +
' successfully validated against the subschema for ' +
'unevaluated properties';
}
if (keyword === 'contains' && typeof annotation === 'number') {
return 'The item at index ' + annotation +
' of the array value successfully validated against the ' +
'containment check subschema';
}
return '<unknown>';
}
if (opcode === LOOP_PROPERTIES) {
const childCount = children ? children.length : 0;
if (childCount > 0 && children[0][0] === ASSERTION_FAIL) {
if (keyword === 'unevaluatedProperties') {
return 'The object value was not expected to define unevaluated properties';
}
return 'The object value was not expected to define additional properties';
}
if (keyword === 'unevaluatedProperties') {
return 'The object properties not covered by other object ' +
'keywords were expected to validate against this subschema';
}
return 'The object properties not covered by other adjacent object ' +
'keywords were expected to validate against this subschema';
}
if (opcode === LOOP_PROPERTIES_EVALUATE) {
const childCount = children ? children.length : 0;
if (childCount === 1 && children[0][0] === ASSERTION_FAIL) {
return 'The object value was not expected to define additional properties';
}
return 'The object properties not covered by other adjacent object ' +
'keywords were expected to validate against this subschema';
}
if (opcode === LOOP_PROPERTIES_UNEVALUATED) {
if (keyword === 'unevaluatedProperties') {
const childCount = children ? children.length : 0;
if (childCount > 0 && children[0][0] === ASSERTION_FAIL) {
return 'The object value was not expected to define unevaluated properties';
}
return 'The object properties not covered by other object ' +
'keywords were expected to validate against this subschema';
}
return '<unknown>';
}
if (opcode === LOOP_PROPERTIES_UNEVALUATED_EXCEPT) {
if (keyword === 'unevaluatedProperties') {
const childCount = children ? children.length : 0;
if (childCount > 0 && children[0][0] === ASSERTION_FAIL) {
return 'The object value was not expected to define unevaluated properties';
}
return 'The object properties not covered by other object ' +
'keywords were expected to validate against this subschema';
}
return '<unknown>';
}
if (opcode === LOOP_PROPERTIES_EXCEPT) {
const childCount = children ? children.length : 0;
if (childCount > 0 && children[0][0] === ASSERTION_FAIL) {
if (keyword === 'unevaluatedProperties') {
return 'The object value was not expected to define unevaluated properties';
}
return 'The object value was not expected to define additional properties';
}
if (keyword === 'unevaluatedProperties') {
return 'The object properties not covered by other object ' +
'keywords were expected to validate against this subschema';
}
return 'The object properties not covered by other adjacent object ' +
'keywords were expected to validate against this subschema';
}
if (opcode === LOOP_PROPERTIES_EXACTLY_TYPE_STRICT) {
return 'The required object properties were expected to be of type ' +
typeName(value[0]);
}
if (opcode === LOOP_PROPERTIES_EXACTLY_TYPE_STRICT_HASH) {
return 'The required object properties were expected to be of type ' +
typeName(value[0]);
}
if (opcode === LOOP_ITEMS_PROPERTIES_EXACTLY_TYPE_STRICT_HASH ||
opcode === LOOP_ITEMS_PROPERTIES_EXACTLY_TYPE_STRICT_HASH3) {
return 'Every item in the array was expected to be an object whose ' +
'required properties were of type ' + typeName(value[0]);
}
if (opcode === LOOP_ITEMS_INTEGER_BOUNDED ||
opcode === LOOP_ITEMS_INTEGER_BOUNDED_SIZED) {
return 'Every item in the array was expected to be a number within the given range';
}
if (opcode === ASSERTION_TYPE_INTEGER_BOUNDED ||
opcode === ASSERTION_TYPE_INTEGER_BOUNDED_STRICT) {
return 'The value was expected to be an integer within the given range';
}
if (opcode === ASSERTION_TYPE_INTEGER_LOWER_BOUND ||
opcode === ASSERTION_TYPE_INTEGER_LOWER_BOUND_STRICT) {
return 'The value was expected to be an integer above the given minimum';
}
if (opcode === ASSERTION_OBJECT_PROPERTIES_SIMPLE) {
return 'The object value was expected to validate against the defined property subschemas';
}
if (opcode === LOOP_PROPERTIES_TYPE ||
opcode === LOOP_PROPERTIES_TYPE_EVALUATE ||
opcode === LOOP_PROPERTIES_TYPE_STRICT ||
opcode === LOOP_PROPERTIES_TYPE_STRICT_EVALUATE) {
return 'The object properties were expected to be of type ' + typeName(value);
}
if (opcode === LOOP_PROPERTIES_TYPE_STRICT_ANY) {
return 'The object properties were expected to be of type ' +
describeTypeList(value);
}
if (opcode === LOOP_PROPERTIES_TYPE_STRICT_ANY_EVALUATE) {
return 'The object properties were expected to be of type ' +
describeTypeList(value);
}
if (opcode === LOOP_KEYS) {
const size = objectSize(target);
const keys = objectKeys(target);
if (size === 0) {
return 'The object is empty and no properties were expected to ' +
'validate against the given subschema';
}
if (size === 1) {
return 'The object property ' + escapeString(keys[0]) +
' was expected to validate against the given subschema';
}
let message = 'The object properties ';
for (let index = 0; index < keys.length; index++) {
if (index === keys.length - 1) {
message += 'and ' + escapeString(keys[index]);
} else {
message += escapeString(keys[index]) + ', ';
}
}
message += ' were expected to validate against the given subschema';
return message;
}
if (opcode === LOOP_ITEMS) {
return 'Every item in the array value was expected to validate against the given subschema';
}
if (opcode === LOOP_ITEMS_FROM) {
let message = 'Every item in the array value';
if (value === 1) {
message += ' except for the first one';
} else if (value > 0) {
message += ' except for the first ' + value;
}
message += ' was expected to validate against the given subschema';
return message;
}
if (opcode === LOOP_ITEMS_UNEVALUATED) {
return 'The array items not covered by other array keywords, if any, ' +
'were expected to validate against this subschema';
}
if (opcode === LOOP_ITEMS_TYPE || opcode === LOOP_ITEMS_TYPE_STRICT) {
return 'The array items were expected to be of type ' + typeName(value);
}
if (opcode === LOOP_ITEMS_TYPE_STRICT_ANY) {
return 'The array items were expected to be of type ' + describeTypeList(value);
}
if (opcode === LOOP_CONTAINS) {
const minimum = value[0];
const maximum = value[1];
let plural = true;
let message = 'The array value was expected to contain ';
if (maximum !== null) {
if (minimum === maximum && minimum === 0) {
message += 'any number of';
} else if (minimum === maximum) {
message += 'exactly ' + minimum;
if (minimum === 1) plural = false;
} else if (minimum === 0) {
message += 'up to ' + maximum;
if (maximum === 1) plural = false;
} else {
message += minimum + ' to ' + maximum;
if (maximum === 1) plural = false;
}
} else {
message += 'at least ' + minimum;
if (minimum === 1) plural = false;
}
message += plural
? ' items that validate against the given subschema'
: ' item that validates against the given subschema';
return message;
}
if (opcode === ASSERTION_DEFINES) {
return 'The object value was expected to define the property ' +
escapeString(value);
}
if (opcode === ASSERTION_DEFINES_STRICT) {
return 'The value was expected to be an object that defines the property ' +
escapeString(value);
}
if (opcode === ASSERTION_DEFINES_ALL) {
let message = 'The object value was expected to define properties ';
for (let index = 0; index < value.length; index++) {
if (index === value.length - 1) {
message += 'and ' + escapeString(value[index]);
} else {
message += escapeString(value[index]) + ', ';
}
}
if (valid) return message;
const missing = [];
for (let index = 0; index < value.length; index++) {
if (!Object.hasOwn(target, value[index])) {
missing.push(value[index]);
}
}
missing.sort();
if (missing.length === 1) {
message += ' but did not define the property ' + escapeString(missing[0]);
} else {
message += ' but did not define properties ';
for (let index = 0; index < missing.length; index++) {
if (index === missing.length - 1) {
message += 'and ' + escapeString(missing[index]);
} else {
message += escapeString(missing[index]) + ', ';
}
}
}
return message;
}
if (opcode === ASSERTION_DEFINES_ALL_STRICT) {
let message = 'The value was expected to be an object that defines properties ';
for (let index = 0; index < value.length; index++) {
if (index === value.length - 1) {
message += 'and ' + escapeString(value[index]);
} else {
message += escapeString(value[index]) + ', ';
}
}
return message;
}
if (opcode === ASSERTION_DEFINES_EXACTLY) {
const sorted = [...value].sort();
let message = 'The object value was expected to only define properties ';
for (let index = 0; index < sorted.length; index++) {
if (index === sorted.length - 1) {
message += 'and ' + escapeString(sorted[index]);
} else {
message += escapeString(sorted[index]) + ', ';
}
}
return message + describeExtras(valid, target, new Set(value));
}
if (opcode === ASSERTION_DEFINES_EXACTLY_STRICT) {
const sorted = [...value].sort();
let message =
'The value was expected to be an object that only defines properties ';
for (let index = 0; index < sorted.length; index++) {
if (index === sorted.length - 1) {
message += 'and ' + escapeString(sorted[index]);
} else {
message += escapeString(sorted[index]) + ', ';
}
}
return message + describeExtras(valid, target, new Set(value));
}
if (opcode === ASSERTION_DEFINES_EXACTLY_STRICT_HASH3) {
const entries = value[0];
const message = 'The value was expected to be an object that only defines the ' +
entries.length + ' given properties';
return message + describeExtras(valid, target,
new Set(entries.map(entry => entry[1])));