Debian Codegraph @

t/result-object.t

raw
1 # vim: set ft=perl ts=8 sts=2 sw=2 tw=100 et :
2 use strictures 2;
3 use 5.020;
4 use stable 0.031 'postderef';
5 use experimental 'signatures';
6 no autovivification warn => qw(fetch store exists delete);
7 use if "$]" >= 5.022, experimental => 're_strict';
8 no if "$]" >= 5.031009, feature => 'indirect';
9 no if "$]" >= 5.033001, feature => 'multidimensional';
10 no if "$]" >= 5.033006, feature => 'bareword_filehandles';
11 no if "$]" >= 5.041009, feature => 'smartmatch';
12 no feature 'switch';
13 use open ':std', ':encoding(UTF-8)'; # force stdin, stdout, stderr into utf8
14
15 use builtin::compat 'refaddr';
16 use lib 't/lib';
17 use Helper;
18
19 my $js = JSON::Schema::Modern->new(short_circuit => 0, collect_annotations => 1);
20 is($js->output_format, 'basic', 'output_format defaults to basic');
21
22 my $result = $js->evaluate(
23 { alpha => 1, beta => 1, foo => 1, gamma => [ 0, 1 ], theta => [ 1 ], zulu => 2 },
24 {
25 required => [ 'bar' ],
26 allOf => [
27 { type => 'number' },
28 { oneOf => [ { type => 'number' } ] },
29 { oneOf => [ true, true ] },
30 ],
31 anyOf => [ { type => 'number' }, { if => true, then => { type => 'array' }, else => false } ],
32 if => false, then => false, else => { type => 'number' },
33 not => true,
34 properties => {
35 alpha => false,
36 beta => { multipleOf => 2 },
37 gamma => {
38 prefixItems => [ false ],
39 items => false,
40 unevaluatedItems => false,
41 },
42 theta => { items => false },
43 },
44 patternProperties => { 'o' => false },
45 additionalProperties => false,
46 unevaluatedProperties => false,
47 propertyNames => { pattern => '[ao]' },
48 },
49 );
50
51 is($result->output_format, 'basic', 'Result object gets the output_format from the evaluator');
52
53 is_equal(
54 $result->TO_JSON,
55 {
56 valid => false,
57 errors => [
58 {
59 instanceLocation => '',
60 keywordLocation => '/required',
61 error => 'object is missing property: bar',
62 },
63 {
64 instanceLocation => '',
65 keywordLocation => '/allOf/0/type',
66 error => 'got object, not number',
67 },
68 {
69 instanceLocation => '',
70 keywordLocation => '/allOf/1/oneOf/0/type',
71 error => 'got object, not number',
72 },
73 {
74 instanceLocation => '',
75 keywordLocation => '/allOf/1/oneOf',
76 error => 'no subschemas are valid',
77 },
78 {
79 instanceLocation => '',
80 keywordLocation => '/allOf/2/oneOf',
81 error => 'multiple subschemas are valid: 0, 1',
82 },
83 {
84 instanceLocation => '',
85 keywordLocation => '/allOf',
86 error => 'subschemas 0, 1, 2 are not valid',
87 },
88 {
89 instanceLocation => '',
90 keywordLocation => '/anyOf/0/type',
91 error => 'got object, not number',
92 },
93 {
94 instanceLocation => '',
95 keywordLocation => '/anyOf/1/then/type',
96 error => 'got object, not array',
97 },
98 {
99 instanceLocation => '',
100 keywordLocation => '/anyOf/1/then',
101 error => 'subschema is not valid',
102 },
103 {
104 instanceLocation => '',
105 keywordLocation => '/anyOf',
106 error => 'no subschemas are valid',
107 },
108 {
109 instanceLocation => '',
110 keywordLocation => '/not',
111 error => 'subschema is true',
112 },
113 {
114 instanceLocation => '',
115 keywordLocation => '/else/type',
116 error => 'got object, not number',
117 },
118 {
119 instanceLocation => '',
120 keywordLocation => '/else',
121 error => 'subschema is not valid',
122 },
123 {
124 instanceLocation => '/alpha',
125 keywordLocation => '/properties/alpha',
126 error => 'property not permitted',
127 },
128 {
129 instanceLocation => '/beta',
130 keywordLocation => '/properties/beta/multipleOf',
131 error => 'value is not a multiple of 2',
132 },
133 {
134 instanceLocation => '/gamma/0',
135 keywordLocation => '/properties/gamma/prefixItems/0',
136 error => 'item not permitted',
137 },
138 {
139 instanceLocation => '/gamma',
140 keywordLocation => '/properties/gamma/prefixItems',
141 error => 'not all items are valid',
142 },
143 {
144 instanceLocation => '/gamma/1',
145 keywordLocation => '/properties/gamma/items',
146 error => 'additional item not permitted',
147 },
148 {
149 instanceLocation => '/gamma',
150 keywordLocation => '/properties/gamma/items',
151 error => 'subschema is not valid against all items',
152 },
153 {
154 instanceLocation => '/theta/0',
155 keywordLocation => '/properties/theta/items',
156 error => 'item not permitted',
157 },
158 {
159 instanceLocation => '/theta',
160 keywordLocation => '/properties/theta/items',
161 error => 'subschema is not valid against all items',
162 },
163 {
164 instanceLocation => '',
165 keywordLocation => '/properties',
166 error => 'not all properties are valid',
167 },
168 {
169 instanceLocation => '/foo',
170 keywordLocation => '/patternProperties/o',
171 error => 'property not permitted',
172 },
173 {
174 instanceLocation => '',
175 keywordLocation => '/patternProperties',
176 error => 'not all properties are valid',
177 },
178 {
179 instanceLocation => '/zulu',
180 keywordLocation => '/additionalProperties',
181 error => 'additional property not permitted',
182 },
183 {
184 instanceLocation => '',
185 keywordLocation => '/additionalProperties',
186 error => 'not all additional properties are valid',
187 },
188 {
189 instanceLocation => '/zulu',
190 keywordLocation => '/propertyNames/pattern',
191 error => 'pattern does not match',
192 },
193 {
194 instanceLocation => '',
195 keywordLocation => '/propertyNames',
196 error => 'not all property names are valid',
197 },
198 ],
199 },
200 'basic format includes all errors linearly',
201 );
202
203 $result->output_format('flag');
204 is_equal(
205 $result->TO_JSON,
206 {
207 valid => false,
208 },
209 'flag format only includes the valid property',
210 );
211
212 $result->output_format('terse');
213 is_equal(
214 $result->TO_JSON,
215 {
216 valid => false,
217 errors => [
218 {
219 instanceLocation => '',
220 keywordLocation => '/required',
221 error => 'object is missing property: bar',
222 },
223 {
224 instanceLocation => '',
225 keywordLocation => '/allOf/0/type',
226 error => 'got object, not number',
227 },
228 {
229 instanceLocation => '',
230 keywordLocation => '/allOf/1/oneOf/0/type',
231 error => 'got object, not number',
232 },
233 # - "summary" error from /allOf/1/oneOf is omitted
234 {
235 instanceLocation => '',
236 keywordLocation => '/allOf/2/oneOf',
237 error => 'multiple subschemas are valid: 0, 1',
238 },
239 # - "summary" error from /allOf is omitted
240 {
241 instanceLocation => '',
242 keywordLocation => '/anyOf/0/type',
243 error => 'got object, not number',
244 },
245 {
246 instanceLocation => '',
247 keywordLocation => '/anyOf/1/then/type',
248 error => 'got object, not array',
249 },
250 # - "summary" error from /anyOf/1/then is omitted
251 # - "summary" error from /anyOf is omitted
252 {
253 instanceLocation => '',
254 keywordLocation => '/not',
255 error => 'subschema is true',
256 },
257 {
258 instanceLocation => '',
259 keywordLocation => '/else/type',
260 error => 'got object, not number',
261 },
262 # - "summary" error from /else is omitted
263 {
264 instanceLocation => '/alpha',
265 keywordLocation => '/properties/alpha',
266 error => 'property not permitted',
267 },
268 {
269 instanceLocation => '/beta',
270 keywordLocation => '/properties/beta/multipleOf',
271 error => 'value is not a multiple of 2',
272 },
273 {
274 instanceLocation => '/gamma/0',
275 keywordLocation => '/properties/gamma/prefixItems/0',
276 error => 'item not permitted',
277 },
278 # - "summary" error from /properties/gamma/prefixItems is omitted
279 {
280 instanceLocation => '/gamma/1',
281 keywordLocation => '/properties/gamma/items',
282 error => 'additional item not permitted',
283 },
284 # - "summary" error from /properties/gamma/items is omitted
285 # - "summary" error from /properties/gamma/unevaluatedItems is omitted
286 {
287 instanceLocation => '/theta/0',
288 keywordLocation => '/properties/theta/items',
289 error => 'item not permitted',
290 },
291 # - "summary" error from /properties/theta/items is omitted
292 # - "summary" error from /properties is omitted
293 {
294 instanceLocation => '/foo',
295 keywordLocation => '/patternProperties/o',
296 error => 'property not permitted',
297 },
298 # - "summary" error from /patternProperties is omitted
299 {
300 instanceLocation => '/zulu',
301 keywordLocation => '/additionalProperties',
302 error => 'additional property not permitted',
303 },
304 # - "summary" error from /additionalProperties is omitted
305 # - "summary" error from /unevaluatedProperties is omitted
306 {
307 instanceLocation => '/zulu',
308 keywordLocation => '/propertyNames/pattern',
309 error => 'pattern does not match',
310 },
311 # - "summary" error from /propertyNames is omitted
312 ],
313 },
314 'terse format omits errors from redundant applicator keywords',
315 );
316
317
318 $js = JSON::Schema::Modern->new(validate_formats => 1);
319 {
320 $result = $js->evaluate(
321 'foo',
322 { format => 'uuid'},
323 );
324
325 is_equal(
326 $result->TO_JSON,
327 {
328 valid => false,
329 errors => my $errors = [
330 {
331 instanceLocation => '',
332 keywordLocation => '/format',
333 error => 'not a valid uuid string',
334 },
335 ],
336 },
337 'basic format includes all errors linearly',
338 );
339
340 $result->output_format('terse');
341 is_equal(
342 $result->TO_JSON,
343 {
344 valid => false,
345 errors => $errors,
346 },
347 'terse format does not omit these crucial errors',
348 );
349 }
350
351 subtest 'strict_basic' => sub {
352 # see "JSON pointer escaping" in t/errors.t
353
354 is_equal(
355 JSON::Schema::Modern->new(specification_version => 'draft2019-09', output_format => 'strict_basic')->evaluate(
356 { '{}' => { 'my~tilde/slash-property' => 1 } },
357 {
358 '$id' => 'foo.json',
359 properties => {
360 '{}' => {
361 properties => {
362 'my~tilde/slash-property' => false,
363 },
364 patternProperties => {
365 '/' => { minimum => 6 },
366 '[~/]' => { minimum => 7 },
367 '~' => { minimum => 5 },
368 '~.*/' => false,
369 },
370 },
371 },
372 },
373 )->TO_JSON,
374 {
375 valid => false,
376 errors => [
377 {
378 instanceLocation => '#/%7B%7D/my~0tilde~1slash-property',
379 keywordLocation => '#/properties/%7B%7D/properties/my~0tilde~1slash-property',
380 absoluteKeywordLocation => 'foo.json#/properties/%7B%7D/properties/my~0tilde~1slash-property',
381 error => 'property not permitted',
382 },
383 {
384 instanceLocation => '#/%7B%7D',
385 keywordLocation => '#/properties/%7B%7D/properties',
386 absoluteKeywordLocation => 'foo.json#/properties/%7B%7D/properties',
387 error => 'not all properties are valid',
388 },
389 {
390 instanceLocation => '#/%7B%7D/my~0tilde~1slash-property',
391 keywordLocation => '#/properties/%7B%7D/patternProperties/~1/minimum', # /
392 absoluteKeywordLocation => 'foo.json#/properties/%7B%7D/patternProperties/~1/minimum', # /
393 error => 'value is less than 6',
394 },
395 {
396 instanceLocation => '#/%7B%7D/my~0tilde~1slash-property',
397 keywordLocation => '#/properties/%7B%7D/patternProperties/%5B~0~1%5D/minimum', # [~/]
398 absoluteKeywordLocation => 'foo.json#/properties/%7B%7D/patternProperties/%5B~0~1%5D/minimum', # [~/]
399 error => 'value is less than 7',
400 },
401 {
402 instanceLocation => '#/%7B%7D/my~0tilde~1slash-property',
403 keywordLocation => '#/properties/%7B%7D/patternProperties/~0/minimum', # ~
404 absoluteKeywordLocation => 'foo.json#/properties/%7B%7D/patternProperties/~0/minimum', # ~
405 error => 'value is less than 5',
406 },
407 {
408 instanceLocation => '#/%7B%7D/my~0tilde~1slash-property',
409 keywordLocation => '#/properties/%7B%7D/patternProperties/~0.*~1', # ~.*/
410 absoluteKeywordLocation => 'foo.json#/properties/%7B%7D/patternProperties/~0.*~1', # ~.*/
411 error => 'property not permitted',
412 },
413 {
414 instanceLocation => '#/%7B%7D',
415 keywordLocation => '#/properties/%7B%7D/patternProperties',
416 absoluteKeywordLocation => 'foo.json#/properties/%7B%7D/patternProperties',
417 error => 'not all properties are valid',
418 },
419 {
420 instanceLocation => '#',
421 keywordLocation => '#/properties',
422 absoluteKeywordLocation => 'foo.json#/properties',
423 error => 'not all properties are valid',
424 },
425 ],
426 },
427 'strict_basic turns json pointers into URIs, including uri escapes',
428 );
429 };
430
431 subtest 'AND two result objects together' => sub {
432 my @results = map do {
433 my $count = $_;
434 my $valid = $count % 2;
435 JSON::Schema::Modern::Result->new(
436 valid => $valid,
437 ($valid ? 'annotations' : 'errors') => [
438 map ${\ ('JSON::Schema::Modern::'.($valid ? 'Annotation' : 'Error'))}->new(
439 depth => 0,
440 mode => 'evaluate',
441 keyword => 'keyword '.$count.'-'.$_,
442 instance_location => '/instance_location/'.$count.'-'.$_,
443 keyword_location => '/keyword_location/'.$count.'-'.$_,
444 $valid ? (annotation => 'annotation '.$count.'-'.$_) : (error => 'error '.$count.'-'.$_),
445 ), 0..1
446 ],
447 defaults => { '/a/b/'.$count => 1 },
448 )
449 }, 0..3;
450
451 cmp_result(
452 (my $one_true = $results[0] & $results[1]),
453 all(
454 methods(
455 valid => bool(0),
456 defaults => {
457 '/a/b/0' => 1,
458 '/a/b/1' => 1,
459 },
460 ),
461 listmethods(
462 errors => [
463 map methods(TO_JSON => {
464 instanceLocation => '/instance_location/0-'.$_,
465 keywordLocation => '/keyword_location/0-'.$_,
466 error => 'error 0-'.$_,
467 }), 0..1
468 ],
469 annotations => [
470 map methods(TO_JSON => {
471 instanceLocation => '/instance_location/1-'.$_,
472 keywordLocation => '/keyword_location/1-'.$_,
473 annotation => 'annotation 1-'.$_,
474 }), 0..1
475 ],
476 ),
477 ),
478 'ANDing true and false results = invalid, but errors and annotations both preserved',
479 );
480
481 cmp_result(
482 (my $both_true = $results[1] & $results[3]),
483 all(
484 methods(valid => bool(1)),
485 listmethods(
486 annotations => [
487 map do {
488 my $count = $_;
489 map methods(TO_JSON => {
490 instanceLocation => '/instance_location/'.$count.'-'.$_,
491 keywordLocation => '/keyword_location/'.$count.'-'.$_,
492 annotation => 'annotation '.$count.'-'.$_,
493 }), 0..1
494 }, 1,3
495 ],
496 ),
497 ),
498 'ANDing two true results = valid',
499 );
500
501 cmp_result(
502 (my $both_false = $results[0] & $results[2]),
503 all(
504 methods(
505 valid => bool(0),
506 defaults => {
507 '/a/b/0' => 1,
508 '/a/b/2' => 1,
509 },
510 ),
511 listmethods(
512 errors => [
513 map do {
514 my $count = $_;
515 map methods(TO_JSON => {
516 instanceLocation => '/instance_location/'.$count.'-'.$_,
517 keywordLocation => '/keyword_location/'.$count.'-'.$_,
518 error => 'error '.$count.'-'.$_,
519 }), 0..1
520 }, 0,2
521 ],
522 ),
523 ),
524 'ANDing two false results = invalid',
525 );
526
527 like(
528 dies { $results[0] & 0 },
529 qr/^wrong type for \& operation/,
530 'only Result objects can be processed',
531 );
532
533 is(
534 refaddr(my $itself = $results[0] & $results[0]),
535 refaddr($results[0]),
536 'ANDing a result with itself is a no-op',
537 );
538 };
539
540 subtest annotations => sub {
541 my %args = (
542 valid => 1,
543 annotations => [
544 JSON::Schema::Modern::Annotation->new(
545 depth => 0,
546 keyword => 'foo',
547 instance_location => '/instance_location',
548 keyword_location => '/keyword_location ',
549 annotation => 'annotation',
550 )
551 ],
552 );
553
554 is_equal(
555 JSON::Schema::Modern::Result->new(%args)->TO_JSON,
556 {
557 valid => true,
558 annotations => [
559 {
560 instanceLocation => '/instance_location',
561 keywordLocation => '/keyword_location ',
562 annotation => 'annotation',
563 },
564 ],
565 },
566 'by default, annotations are included in the formatted output',
567 );
568
569 is_equal(
570 JSON::Schema::Modern::Result->new(%args, formatted_annotations => 0)->TO_JSON,
571 { valid => true },
572 'but inclusion can be disabled',
573 );
574 };
575
576 subtest 'data_only' => sub {
577 my $result = JSON::Schema::Modern::Result->new(
578 valid => 0,
579 errors => [
580 JSON::Schema::Modern::Error->new(
581 depth => 1,
582 mode => 'evaluate',
583 keyword => 'hello',
584 instance_location => '/foo/bar',
585 keyword_location => '/allOf/0/hello',
586 error => 'schema is invalid',
587 ),
588 JSON::Schema::Modern::Error->new(
589 depth => 1,
590 mode => 'evaluate',
591 keyword => 'goodbye',
592 instance_location => '/foo/bar',
593 keyword_location => '/allOf/1/goodbye',
594 error => 'schema is invalid',
595 ),
596 JSON::Schema::Modern::Error->new(
597 depth => 0,
598 mode => 'evaluate',
599 keyword => 'allOf',
600 instance_location => '/foo/bar',
601 keyword_location => '/allOf',
602 error => 'subschemas 0, 1 are not valid',
603 ),
604 ],
605 );
606
607 is(
608 $result->format('data_only'),
609 "'/foo/bar': schema is invalid\n'/foo/bar': subschemas 0, 1 are not valid",
610 'data_only format outputs a string of data locations only, with duplicates removed',
611 );
612
613 is(
614 JSON::Schema::Modern::Result->new(
615 valid => 0,
616 errors => [
617 map JSON::Schema::Modern::Error->new(
618 do { my $e = $_; map +($_ => $e->$_), qw(depth keyword instance_location keyword_location error) },
619 mode => 'traverse',
620 ), $result->errors
621 ],
622 )->format('data_only'),
623 "'/allOf/0/hello': schema is invalid\n'/allOf/1/goodbye': schema is invalid\n'/allOf': subschemas 0, 1 are not valid",
624 'data_only format uses keyword locations when result came from traverse',
625 );
626 };
627
628 subtest 'construction errors' => sub {
629 my $error = JSON::Schema::Modern::Error->new(
630 error => 'oh no!',
631 mode => 'evaluate',
632 depth => 1,
633 keyword => 'me',
634 instance_location => '',
635 keyword_location => '',
636 );
637
638 like(
639 dies { JSON::Schema::Modern::Result->new(valid => true, errors => [$error]) },
640 qr/^inconsistent inputs: errors is not empty but valid is true/,
641 'valid results must not have errors',
642 );
643
644 like(
645 dies { JSON::Schema::Modern::Result->new(valid => false, errors => []) },
646 qr/^inconsistent inputs: errors is empty but valid is false/,
647 'invalid results must have errors',
648 );
649
650 ok(
651 lives { JSON::Schema::Modern::Result->new(valid => true, errors => []) },
652 'no errors when valid is true and errors is empty',
653 );
654
655 ok(
656 lives { JSON::Schema::Modern::Result->new(valid => false, errors => [$error]) },
657 'no errors when valid is false and errors is not empty',
658 );
659 };
660
661 subtest 'instance data in result' => sub {
662 my $result = JSON::Schema::Modern->new->evaluate(
663 my $data = {
664 foo => 1,
665 bar => 2,
666 baz => [ 1, 2, 3 ],
667 },
668 {},
669 );
670
671 is_equal($result->TO_JSON, { valid => true }, 'evaluation is successful');
672 is_equal($result->data, $data, 'result object contains the instance data');
673 isnt(refaddr($result->data), refaddr($data), 'data is cloned, rather than using the same reference');
674 };
675
676 done_testing;