Debian Codegraph @

t/add-schema.t

raw
1 # vim: set ft=perl ts=8 sts=2 sw=2 tw=100 et :
2 use strictures 2;
3 use stable 0.031 'postderef';
4 use experimental 'signatures';
5 no autovivification warn => qw(fetch store exists delete);
6 use if "$]" >= 5.022, experimental => 're_strict';
7 no if "$]" >= 5.031009, feature => 'indirect';
8 no if "$]" >= 5.033001, feature => 'multidimensional';
9 no if "$]" >= 5.033006, feature => 'bareword_filehandles';
10 no if "$]" >= 5.041009, feature => 'smartmatch';
11 no feature 'switch';
12 use open ':std', ':encoding(UTF-8)'; # force stdin, stdout, stderr into utf8
13
14 use List::Util 'unpairs';
15 use lib 't/lib';
16 use Helper;
17 use Test2::Warnings 0.038 qw(warnings :no_end_test had_no_warnings);
18
19 use constant METASCHEMA => 'https://json-schema.org/draft/2019-09/schema';
20
21 # spec version -> vocab classes
22 my %vocabularies = unpairs(JSON::Schema::Modern->new->__all_metaschema_vocabulary_classes);
23
24 subtest 'evaluate a document' => sub {
25 my $document = JSON::Schema::Modern::Document->new(
26 schema => {
27 '$id' => 'https://foo.com',
28 allOf => [ false, true ],
29 });
30
31 my $js = JSON::Schema::Modern->new;
32 $js->add_document($document);
33 is_equal(
34 $js->evaluate(1, $document->canonical_uri)->TO_JSON,
35 {
36 valid => false,
37 errors => my $errors = [
38 {
39 instanceLocation => '',
40 keywordLocation => '/allOf/0',
41 absoluteKeywordLocation => 'https://foo.com#/allOf/0',
42 error => 'subschema is false',
43 },
44 {
45 instanceLocation => '',
46 keywordLocation => '/allOf',
47 absoluteKeywordLocation => 'https://foo.com#/allOf',
48 error => 'subschema 0 is not valid',
49 },
50 ],
51 },
52 'evaluate a Document object',
53 );
54
55 cmp_result(
56 { $js->_resource_index },
57 {
58 'https://foo.com' => {
59 path => '',
60 canonical_uri => str('https://foo.com'),
61 document => shallow($document),
62 specification_version => 'draft2020-12',
63 vocabularies => $vocabularies{'draft2020-12'},
64 },
65 },
66 'resource index from the document is copied to the main object',
67 );
68
69 is_equal(
70 $js->evaluate(1, $document->canonical_uri)->TO_JSON,
71 {
72 valid => false,
73 errors => $errors,
74 },
75 'evaluate a Document object again without error',
76 );
77 };
78
79 subtest 'evaluate a uri' => sub {
80 my $js = JSON::Schema::Modern->new;
81
82 is_equal(
83 $js->evaluate({ '$schema' => 1 }, METASCHEMA)->TO_JSON,
84 {
85 valid => false,
86 errors => my $errors = [
87 {
88 instanceLocation => '/$schema',
89 keywordLocation => '/allOf/0/$ref/properties/$schema/type',
90 absoluteKeywordLocation => 'https://json-schema.org/draft/2019-09/meta/core#/properties/$schema/type',
91 error => 'got integer, not string',
92 },
93 {
94 instanceLocation => '',
95 keywordLocation => '/allOf/0/$ref/properties',
96 absoluteKeywordLocation => 'https://json-schema.org/draft/2019-09/meta/core#/properties',
97 error => 'not all properties are valid',
98 },
99 {
100 instanceLocation => '',
101 keywordLocation => '/allOf',
102 absoluteKeywordLocation => METASCHEMA.'#/allOf',
103 error => 'subschema 0 is not valid',
104 },
105 ],
106 },
107 'evaluate with a uri that is not yet loaded',
108 );
109
110 cmp_result(
111 { $js->_resource_index },
112 {
113 map +(
114 $_ => {
115 path => '',
116 canonical_uri => str($_),
117 document => isa('JSON::Schema::Modern::Document'),
118 specification_version => 'draft2019-09',
119 vocabularies => $vocabularies{'draft2019-09'},
120 }
121 ),
122 METASCHEMA,
123 map 'https://json-schema.org/draft/2019-09/meta/'.$_,
124 qw(core applicator validation meta-data format content)
125 },
126 'the metaschema is now loaded and its resources are indexed',
127 );
128
129 # and again, we can use the same resource without reloading it
130 is_equal(
131 $js->evaluate({ '$schema' => 1 }, METASCHEMA)->TO_JSON,
132 {
133 valid => false,
134 errors => $errors,
135 },
136 'evaluate against the metaschema again',
137 );
138
139 # now use a subschema at that url to evaluate with.
140 # multiple things are being tested here:
141 # - we can load a schema resource, or find an existing one, with a fragment
142 # - the json path we used is saved in the state, for correct errors
143 is_equal(
144 $js->evaluate(
145 1,
146 'https://json-schema.org/draft/2019-09/meta/core#/properties/$schema',
147 )->TO_JSON,
148 {
149 valid => false,
150 errors => [
151 {
152 instanceLocation => '',
153 keywordLocation => '/type',
154 absoluteKeywordLocation => 'https://json-schema.org/draft/2019-09/meta/core#/properties/$schema/type',
155 error => 'got integer, not string',
156 },
157 ],
158 },
159 'evaluate against the a subschema of the metaschema',
160 );
161
162 is_equal(
163 $js->evaluate(
164 1,
165 METASCHEMA.'#/does/not/exist',
166 )->TO_JSON,
167 {
168 valid => false,
169 errors => [
170 {
171 instanceLocation => '',
172 keywordLocation => '',
173 error => 'EXCEPTION: unable to find resource "'.METASCHEMA.'#/does/not/exist"',
174 },
175 ],
176 },
177 'evaluate against the a fragment of the metaschema that does not exist',
178 );
179
180 is_equal(
181 $js->evaluate(
182 1,
183 METASCHEMA.'#does_not_exist',
184 )->TO_JSON,
185 {
186 valid => false,
187 errors => [
188 {
189 instanceLocation => '',
190 keywordLocation => '',
191 error => 'EXCEPTION: unable to find resource "'.METASCHEMA.'#does_not_exist"',
192 },
193 ],
194 },
195 'evaluate against the a plain-name fragment of the metaschema that does not exist',
196 );
197 };
198
199 subtest 'add a uri resource' => sub {
200 my $js = JSON::Schema::Modern->new;
201
202 is_equal(
203 my $get_metaschema = scalar $js->get(METASCHEMA),
204 my $orig_metaschema = $js->_get_resource(METASCHEMA)->{document}->schema,
205 '->get in scalar context on a URI to the head of a document',
206 );
207
208 ok($get_metaschema != $orig_metaschema, 'get() did not return a reference to the original data');
209
210 cmp_result(
211 [ $js->get(METASCHEMA) ],
212 [ $js->_get_resource(METASCHEMA)->{document}->schema,
213 all(isa('Mojo::URL'), str(METASCHEMA)) ],
214 '->get in list context on a URI to the head of a document',
215 );
216
217 is_equal(
218 scalar $js->get(METASCHEMA.'#/properties/definitions/type'),
219 'object', # $document->schema->{properties}{definitions}{type}
220 '->get in scalar context on a URI to inside of a document',
221 );
222 cmp_result(
223 [ $js->get(METASCHEMA.'#/properties/definitions/type') ],
224 [ 'object', all(isa('Mojo::URL'), str(METASCHEMA.'#/properties/definitions/type')) ],
225 '->get in list context on a URI to inside of a document',
226 );
227 };
228
229 subtest 'add a schema associated with a uri' => sub {
230 my $js = JSON::Schema::Modern->new;
231
232 like(
233 dies { $js->add_schema('https://foo.com#/x/y/z', {}) },
234 qr/^cannot add a schema with a uri with a fragment/,
235 'cannot use a uri with a fragment',
236 );
237
238 cmp_result(
239 my $document = $js->add_schema(
240 'https://foo.com',
241 { '$id' => 'https://bar.com', allOf => [ false, true ] },
242 ),
243 all(
244 isa('JSON::Schema::Modern::Document'),
245 listmethods(
246 resource_index => [
247 'https://bar.com' => {
248 path => '',
249 canonical_uri => str('https://bar.com'),
250 specification_version => 'draft2020-12',
251 vocabularies => $vocabularies{'draft2020-12'},
252 },
253 ],
254 canonical_uri => [ str('https://bar.com') ],
255 ),
256 ),
257 'added the schema data with an associated uri; the document does not see the overridden uri',
258 );
259
260 is_equal(
261 $js->evaluate(1, 'https://bar.com#/allOf/0')->TO_JSON,
262 {
263 valid => false,
264 errors => [
265 {
266 instanceLocation => '',
267 keywordLocation => '',
268 absoluteKeywordLocation => 'https://bar.com#/allOf/0',
269 error => 'subschema is false',
270 },
271 ],
272 },
273 'can now evaluate using a uri to a subschema of a resource we loaded earlier',
274 );
275
276 is_equal(
277 $js->evaluate(1, 'https://foo.com')->TO_JSON,
278 {
279 valid => false,
280 errors => my $errors = [
281 {
282 instanceLocation => '',
283 keywordLocation => '/allOf/0',
284 absoluteKeywordLocation => 'https://bar.com#/allOf/0',
285 error => 'subschema is false',
286 },
287 {
288 instanceLocation => '',
289 keywordLocation => '/allOf',
290 absoluteKeywordLocation => 'https://bar.com#/allOf',
291 error => 'subschema 0 is not valid',
292 },
293 ],
294 },
295 'can also evaluate using a non-canonical uri',
296 );
297
298 cmp_result(
299 $js->add_document('https://bloop.com', $document),
300 shallow($document),
301 'can add the same document and associate it with another schema',
302 );
303
304 my @warnings = warnings {
305 cmp_result(
306 $js->add_schema('https://bloop.com', $document),
307 shallow($document),
308 'can add the same document twice, using deprecated interface',
309 );
310 };
311
312 cmp_result(
313 \@warnings,
314 [ re(qr/^use of deprecated form of add_schema with document/) ],
315 'warned when using deprecated form of add_schema with URI',
316 );
317
318 @warnings = warnings {
319 cmp_result(
320 $js->add_schema($document),
321 shallow($document),
322 'can add the same document again, using deprecated interface',
323 );
324 };
325
326 cmp_result(
327 \@warnings,
328 [ re(qr/^use of deprecated form of add_schema with document/) ],
329 'warned when using deprecated form of add_schema without URI',
330 );
331
332 # this actually does nothing, via the duplicate check in _add_resource
333 cmp_result(
334 $js->add_document($document),
335 shallow($document),
336 'can add the same document again with the proper interface',
337 );
338
339 cmp_result(
340 { $js->_resource_index },
341 {
342 map +($_ => {
343 path => '',
344 canonical_uri => str('https://bar.com'),
345 document => shallow($document),
346 specification_version => 'draft2020-12',
347 vocabularies => $vocabularies{'draft2020-12'},
348 }), qw(https://foo.com https://bar.com https://bloop.com)
349 },
350 'now the document is available as all three uris, with the same canonical_uri',
351 );
352 };
353
354 subtest 'multiple anonymous schemas' => sub {
355 my $js = JSON::Schema::Modern->new;
356
357 is_equal(
358 $js->evaluate(1, { minimum => 1 })->TO_JSON,
359 { valid => true },
360 'evaluate an anonymous schema',
361 );
362
363 is_equal([ keys $js->{_resource_index}->%* ], [ '' ], 'one resource is indexed');
364
365 is_equal(
366 $js->evaluate(2, { minimum => 2 })->TO_JSON,
367 { valid => true },
368 'evaluate another anonymous schema',
369 );
370
371 is_equal([ keys $js->{_resource_index}->%* ], [ '' ], 'still only one resource is indexed');
372 };
373
374 subtest 'add a document without associating it with a uri' => sub {
375 my $js = JSON::Schema::Modern->new;
376
377 cmp_result(
378 $js->add_document(
379 my $document = JSON::Schema::Modern::Document->new(
380 schema => { '$id' => 'https://bar.com', allOf => [ false, true ] },
381 )),
382 all(
383 isa('JSON::Schema::Modern::Document'),
384 listmethods(
385 resource_index => [
386 'https://bar.com' => {
387 path => '',
388 canonical_uri => str('https://bar.com'),
389 specification_version => 'draft2020-12',
390 vocabularies => $vocabularies{'draft2020-12'},
391 },
392 ],
393 canonical_uri => [ str('https://bar.com') ],
394 ),
395 ),
396 'added the document without an associated uri',
397 );
398
399 cmp_result(
400 { $js->_resource_index },
401 {
402 'https://bar.com' => {
403 path => '',
404 canonical_uri => str('https://bar.com'),
405 document => shallow($document),
406 specification_version => 'draft2020-12',
407 vocabularies => $vocabularies{'draft2020-12'},
408 },
409 },
410 'document only added under its canonical uri',
411 );
412 };
413
414 subtest 'add a schema without a uri' => sub {
415 my $js = JSON::Schema::Modern->new;
416
417 cmp_result(
418 my $document = $js->add_schema(
419 { '$id' => 'https://bar.com', allOf => [ false, true ] },
420 ),
421 all(
422 isa('JSON::Schema::Modern::Document'),
423 listmethods(
424 resource_index => [
425 'https://bar.com' => {
426 path => '',
427 canonical_uri => str('https://bar.com'),
428 specification_version => 'draft2020-12',
429 vocabularies => $vocabularies{'draft2020-12'},
430 },
431 ],
432 canonical_uri => [ str('https://bar.com') ],
433 ),
434 ),
435 'added the schema data without an associated uri',
436 );
437
438 cmp_result(
439 { $js->_resource_index },
440 {
441 'https://bar.com' => {
442 path => '',
443 canonical_uri => str('https://bar.com'),
444 document => shallow($document),
445 specification_version => 'draft2020-12',
446 vocabularies => $vocabularies{'draft2020-12'},
447 },
448 },
449 'document only added under its canonical uri',
450 );
451 };
452
453 subtest '$ref to non-canonical uri' => sub {
454 my $schema = {
455 '$id' => 'http://localhost:4242/my_document', # the canonical_uri
456 properties => {
457 alpha => false,
458 beta => {
459 '$id' => 'beta',
460 properties => {
461 gamma => {
462 minimum => 2,
463 },
464 },
465 },
466 delta => {
467 '$ref' => 'http://otherhost:4242/another_uri#/properties/alpha',
468 },
469 },
470 };
471
472 my $js = JSON::Schema::Modern->new;
473 $js->add_schema('http://otherhost:4242/another_uri', $schema);
474
475 is_equal(
476 $js->evaluate({ alpha => 1 }, 'http://otherhost:4242/another_uri')->TO_JSON,
477 {
478 valid => false,
479 errors => [
480 {
481 instanceLocation => '/alpha',
482 keywordLocation => '/properties/alpha',
483 absoluteKeywordLocation => 'http://localhost:4242/my_document#/properties/alpha',
484 error => 'property not permitted',
485 },
486 {
487 instanceLocation => '',
488 keywordLocation => '/properties',
489 absoluteKeywordLocation => 'http://localhost:4242/my_document#/properties',
490 error => 'not all properties are valid',
491 },
492 ],
493 },
494 'errors use the canonical uri, not the uri used to evaluate against',
495 );
496
497 is_equal(
498 $js->evaluate({ gamma => 1 }, 'http://otherhost:4242/beta')->TO_JSON,
499 {
500 valid => false,
501 errors => [
502 {
503 instanceLocation => '',
504 keywordLocation => '',
505 error => 'EXCEPTION: unable to find resource "http://otherhost:4242/beta"',
506 },
507 ],
508 },
509 'non-canonical uri is not used to resolve inner $id keywords',
510 );
511
512 is_equal(
513 $js->evaluate({ gamma => 1 }, 'http://localhost:4242/beta')->TO_JSON,
514 {
515 valid => false,
516 errors => [
517 {
518 instanceLocation => '/gamma',
519 keywordLocation => '/properties/gamma/minimum',
520 absoluteKeywordLocation => 'http://localhost:4242/beta#/properties/gamma/minimum',
521 error => 'value is less than 2',
522 },
523 {
524 instanceLocation => '',
525 keywordLocation => '/properties',
526 absoluteKeywordLocation => 'http://localhost:4242/beta#/properties',
527 error => 'not all properties are valid',
528 },
529 ],
530 },
531 'the canonical uri is updated when use the canonical uri, not the uri used to evaluate against',
532 );
533
534 is_equal(
535 $js->evaluate({ delta => 1 }, 'http://otherhost:4242/another_uri')->TO_JSON,
536 {
537 valid => false,
538 errors => [
539 {
540 instanceLocation => '/delta',
541 keywordLocation => '/properties/delta/$ref',
542 absoluteKeywordLocation => 'http://localhost:4242/my_document#/properties/alpha',
543 error => 'subschema is false',
544 },
545 {
546 instanceLocation => '',
547 keywordLocation => '/properties',
548 absoluteKeywordLocation => 'http://localhost:4242/my_document#/properties',
549 error => 'not all properties are valid',
550 },
551 ],
552 },
553 'canonical_uri is not always what was in the $ref, even when no local $id is present',
554 );
555
556 is_equal(
557 $js->evaluate(1, 'http://otherhost:4242/another_uri#/properties/alpha')->TO_JSON,
558 {
559 valid => false,
560 errors => [
561 {
562 instanceLocation => '',
563 keywordLocation => '',
564 absoluteKeywordLocation => 'http://localhost:4242/my_document#/properties/alpha',
565 error => 'subschema is false',
566 },
567 ],
568 },
569 'canonical_uri fragment also needs to be adjusted',
570 );
571
572 delete $schema->{properties}{beta}{'$id'};
573
574 is_equal(
575 $js->evaluate({ gamma => 1 }, 'http://otherhost:4242/another_uri#/properties/beta')->TO_JSON,
576 {
577 valid => false,
578 errors => [
579 {
580 instanceLocation => '/gamma',
581 keywordLocation => '/properties/gamma/minimum',
582 absoluteKeywordLocation => 'http://localhost:4242/beta#/properties/gamma/minimum',
583 error => 'value is less than 2',
584 },
585 {
586 instanceLocation => '',
587 keywordLocation => '/properties',
588 absoluteKeywordLocation => 'http://localhost:4242/beta#/properties',
589 error => 'not all properties are valid',
590 },
591 ],
592 },
593 'canonical_uri starts out containing a fragment and can be appended to during traversal',
594 );
595 };
596
597 subtest 'register a document against multiple uris, with absolute root uri' => sub {
598 my $js = JSON::Schema::Modern->new;
599 my $document = JSON::Schema::Modern::Document->new(
600 schema => {
601 '$id' => 'https://foo.com',
602 '$anchor' => 'my_anchor',
603 maximum => 1,
604 '$defs' => {
605 foo => {
606 '$id' => 'my_dir',
607 allOf => [ true ],
608 },
609 },
610 });
611
612 my %more_configs;
613
614 cmp_result(
615 { $document->resource_index },
616 my $doc_resource_index = {
617 'https://foo.com' => {
618 path => '',
619 canonical_uri => str('https://foo.com'),
620 do { %more_configs = (
621 specification_version => 'draft2020-12',
622 vocabularies => $vocabularies{'draft2020-12'},
623 ) },
624 anchors => {
625 my_anchor => {
626 path => '',
627 canonical_uri => str('https://foo.com'),
628 },
629 },
630 },
631 'https://foo.com/my_dir' => {
632 path => '/$defs/foo',
633 canonical_uri => str('https://foo.com/my_dir'),
634 %more_configs,
635 },
636 },
637 'identifiers stored for the document',
638 );
639
640 $js->add_document($document);
641
642 cmp_result(
643 { $js->_resource_index },
644 my $main_resource_index = {
645 'https://foo.com' => {
646 path => '',
647 canonical_uri => str('https://foo.com'),
648 document => shallow($document),
649 %more_configs,
650 anchors => {
651 my_anchor => {
652 path => '',
653 canonical_uri => str('https://foo.com'),
654 },
655 },
656 },
657 'https://foo.com/my_dir' => {
658 path => '/$defs/foo',
659 canonical_uri => str('https://foo.com/my_dir'),
660 document => shallow($document),
661 %more_configs,
662 },
663 },
664 'resource index from the document is copied to the main object',
665 );
666
667 $js->add_document('https://uri2.com', $document);
668
669 cmp_result(
670 { $js->_resource_index },
671 $main_resource_index = {
672 %$main_resource_index,
673 'https://uri2.com' => {
674 path => '',
675 canonical_uri => str('https://foo.com'),
676 document => shallow($document),
677 %more_configs,
678 anchors => {
679 my_anchor => {
680 path => '',
681 canonical_uri => str('https://foo.com'),
682 },
683 },
684 },
685 },
686 'add a secondary uri for the same document',
687 );
688
689 cmp_result(
690 { $document->resource_index },
691 $doc_resource_index,
692 'secondary uri not also added to the document',
693 );
694
695 like(
696 dies { $js->add_schema('https://uri2.com', { x => 1 }) },
697 qr!^\Quri "https://uri2.com" conflicts with an existing schema resource\E!,
698 'cannot call add_schema with the same URI as for another schema',
699 );
700
701 like(
702 dies { $js->add_schema('https://uri3.com', { '$id' => 'https://foo.com', x => 1 }) },
703 qr!^\Quri "https://foo.com" conflicts with an existing schema resource\E!,
704 'cannot reuse the same $id in another document',
705 );
706
707 cmp_result(
708 { $js->_resource_index },
709 $main_resource_index,
710 'resource index remains unchanged after erroneous add_schema calls',
711 );
712
713 $js->add_schema('https://uri4.com', +{ $document->schema->%* });
714
715 cmp_result(
716 { $js->_resource_index },
717 {
718 %$main_resource_index,
719 'https://uri4.com' => {
720 path => '',
721 canonical_uri => str('https://foo.com'),
722 document => shallow($document),
723 %more_configs,
724 anchors => {
725 my_anchor => {
726 path => '',
727 canonical_uri => str('https://foo.com'),
728 },
729 },
730 },
731 },
732 'adding the same schema content again is permitted',
733 );
734
735 is(
736 scalar $js->get('https://foo.com#i_do_not_exist'),
737 undef,
738 '->get in scalar context for a nonexistent resource returns undef',
739 );
740 is_equal(
741 [ $js->get('https://foo.com#i_do_not_exist') ],
742 [],
743 '->get in list context for a nonexistent resource returns empty list',
744 );
745 };
746
747 subtest 'register a document against multiple uris, with relative root uri' => sub {
748 my $js = JSON::Schema::Modern->new;
749 my $document = JSON::Schema::Modern::Document->new(
750 schema => {
751 '$id' => 'my_dir/',
752 '$anchor' => 'my_anchor',
753 maximum => 1,
754 '$defs' => {
755 foo => {
756 '$id' => 'my_dir2',
757 allOf => [ true ],
758 },
759 },
760 });
761
762 my %more_configs;
763
764 cmp_result(
765 { $document->resource_index },
766 my $doc_resource_index = {
767 'my_dir/' => {
768 path => '',
769 canonical_uri => str('my_dir/'),
770 do { %more_configs = (
771 specification_version => 'draft2020-12',
772 vocabularies => $vocabularies{'draft2020-12'},
773 ) },
774 anchors => {
775 my_anchor => {
776 path => '',
777 canonical_uri => str('my_dir/'),
778 },
779 },
780 },
781 'my_dir/my_dir2' => {
782 path => '/$defs/foo',
783 canonical_uri => str('my_dir/my_dir2'),
784 %more_configs,
785 },
786 },
787 'identifiers stored for the document',
788 );
789
790 $js->add_document($document);
791
792 cmp_result(
793 { $js->_resource_index },
794 my $main_resource_index = {
795 'my_dir/' => {
796 path => '',
797 canonical_uri => str('my_dir/'),
798 document => shallow($document),
799 %more_configs,
800 anchors => {
801 my_anchor => {
802 path => '',
803 canonical_uri => str('my_dir/'),
804 },
805 },
806 },
807 'my_dir/my_dir2' => {
808 path => '/$defs/foo',
809 canonical_uri => str('my_dir/my_dir2'),
810 document => shallow($document),
811 %more_configs,
812 },
813 },
814 'resource index from the document is copied to the main object',
815 );
816
817 $js->add_document('https://uri2.com', $document);
818
819 cmp_result(
820 { $js->_resource_index },
821 $main_resource_index = {
822 %$main_resource_index,
823 'https://uri2.com' => {
824 path => '',
825 canonical_uri => str('https://uri2.com/my_dir/'),
826 document => shallow($document),
827 %more_configs,
828 anchors => {
829 my_anchor => {
830 path => '',
831 canonical_uri => str('https://uri2.com/my_dir/'),
832 },
833 },
834 },
835 'https://uri2.com/my_dir/' => {
836 path => '',
837 canonical_uri => str('https://uri2.com/my_dir/'),
838 document => shallow($document),
839 %more_configs,
840 anchors => {
841 my_anchor => {
842 path => '',
843 canonical_uri => str('https://uri2.com/my_dir/'),
844 },
845 },
846 },
847 'https://uri2.com/my_dir/my_dir2' => {
848 path => '/$defs/foo',
849 canonical_uri => str('https://uri2.com/my_dir/my_dir2'),
850 document => shallow($document),
851 %more_configs,
852 },
853 },
854 'add a secondary (absolute) uri for the same document',
855 );
856
857 cmp_result(
858 { $document->resource_index },
859 $doc_resource_index,
860 'secondary uri not also added to the document',
861 );
862
863 like(
864 dies { $js->add_schema('https://uri2.com', { x => 1 }) },
865 qr!^\Quri "https://uri2.com" conflicts with an existing schema resource\E!,
866 'cannot call add_schema with the same URI as for another schema',
867 );
868
869 like(
870 dies { $js->add_schema('https://uri3.com', { '$id' => 'https://uri2.com', x => 1 }) },
871 qr!^\Quri "https://uri2.com" conflicts with an existing schema resource\E!,
872 'cannot reuse the same $id in another document',
873 );
874
875 cmp_result(
876 { $js->_resource_index },
877 $main_resource_index,
878 'resource index remains unchanged after erroneous add_schema calls',
879 );
880
881 $js->add_schema('https://uri4.com', +{ $document->schema->%* });
882
883 cmp_result(
884 { $js->_resource_index },
885 {
886 %$main_resource_index,
887 'https://uri4.com' => {
888 path => '',
889 canonical_uri => str('https://uri4.com/my_dir/'),
890 document => shallow($document),
891 %more_configs,
892 anchors => {
893 my_anchor => {
894 path => '',
895 canonical_uri => str('https://uri4.com/my_dir/'),
896 },
897 },
898 },
899 'https://uri4.com/my_dir/' => {
900 path => '',
901 canonical_uri => str('https://uri4.com/my_dir/'),
902 document => shallow($document),
903 %more_configs,
904 anchors => {
905 my_anchor => {
906 path => '',
907 canonical_uri => str('https://uri4.com/my_dir/'),
908 },
909 },
910 },
911 'https://uri4.com/my_dir/my_dir2' => {
912 path => '/$defs/foo',
913 canonical_uri => str('https://uri4.com/my_dir/my_dir2'),
914 document => shallow($document),
915 %more_configs,
916 },
917 },
918 'adding the same schema content again is permitted',
919 );
920 };
921
922 subtest 'register a document against multiple uris, with no root uri' => sub {
923 my $js = JSON::Schema::Modern->new;
924 my $document = JSON::Schema::Modern::Document->new(
925 schema => {
926 '$anchor' => 'my_anchor',
927 maximum => 1,
928 '$defs' => {
929 foo => {
930 '$id' => 'my_dir',
931 allOf => [ true ],
932 },
933 },
934 });
935
936 my %more_configs;
937
938 cmp_result(
939 { $document->resource_index },
940 my $doc_resource_index = {
941 '' => {
942 path => '',
943 canonical_uri => str(''),
944 do { %more_configs = (
945 specification_version => 'draft2020-12',
946 vocabularies => $vocabularies{'draft2020-12'},
947 ) },
948 anchors => {
949 my_anchor => {
950 path => '',
951 canonical_uri => str(''),
952 },
953 },
954 },
955 'my_dir' => {
956 path => '/$defs/foo',
957 canonical_uri => str('my_dir'),
958 %more_configs,
959 },
960 },
961 'identifiers stored for the document',
962 );
963
964 $js->add_document($document);
965
966 cmp_result(
967 { $js->_resource_index },
968 my $main_resource_index = {
969 '' => {
970 path => '',
971 canonical_uri => str(''),
972 document => shallow($document),
973 %more_configs,
974 anchors => {
975 my_anchor => {
976 path => '',
977 canonical_uri => str(''),
978 },
979 },
980 },
981 'my_dir' => {
982 path => '/$defs/foo',
983 canonical_uri => str('my_dir'),
984 document => shallow($document),
985 %more_configs,
986 },
987 },
988 'resource index from the document is copied to the main object',
989 );
990
991 $js->add_document('https://uri2.com', $document);
992
993 cmp_result(
994 { $js->_resource_index },
995 $main_resource_index = {
996 %$main_resource_index,
997 'https://uri2.com' => {
998 path => '',
999 canonical_uri => str('https://uri2.com'),
1000 document => shallow($document),
1001 %more_configs,
1002 anchors => {
1003 my_anchor => {
1004 path => '',
1005 canonical_uri => str('https://uri2.com'),
1006 },
1007 },
1008 },
1009 'https://uri2.com/my_dir' => {
1010 path => '/$defs/foo',
1011 canonical_uri => str('https://uri2.com/my_dir'),
1012 document => shallow($document),
1013 %more_configs,
1014 },
1015 },
1016 'add a secondary (absolute) uri for the same document',
1017 );
1018
1019 cmp_result(
1020 { $document->resource_index },
1021 $doc_resource_index,
1022 'secondary uri not also added to the document',
1023 );
1024
1025 like(
1026 dies { $js->add_schema('https://uri2.com', { x => 1 }) },
1027 qr!^\Quri "https://uri2.com" conflicts with an existing schema resource\E!,
1028 'cannot call add_schema with the same URI as for another schema',
1029 );
1030
1031 like(
1032 dies { $js->add_schema('https://uri3.com', { '$id' => 'https://uri2.com', x => 1 }) },
1033 qr!^\Quri "https://uri2.com" conflicts with an existing schema resource\E!,
1034 'cannot reuse the same $id in another document',
1035 );
1036
1037 cmp_result(
1038 { $js->_resource_index },
1039 $main_resource_index,
1040 'resource index remains unchanged after erroneous add_schema calls',
1041 );
1042
1043 $js->add_schema('https://uri4.com', +{ $document->schema->%* });
1044
1045 cmp_result(
1046 { $js->_resource_index },
1047 {
1048 %$main_resource_index,
1049 'https://uri4.com' => {
1050 path => '',
1051 canonical_uri => str('https://uri4.com'),
1052 document => shallow($document),
1053 %more_configs,
1054 anchors => {
1055 my_anchor => {
1056 path => '',
1057 canonical_uri => str('https://uri4.com'),
1058 },
1059 },
1060 },
1061 'https://uri4.com/my_dir' => {
1062 path => '/$defs/foo',
1063 canonical_uri => str('https://uri4.com/my_dir'),
1064 document => shallow($document),
1065 %more_configs,
1066 },
1067 },
1068 'adding the same schema content again is permitted',
1069 );
1070 };
1071
1072 subtest 'external resource with externally-supplied uri; main resource with multiple uris' => sub {
1073 my $js = JSON::Schema::Modern->new;
1074
1075 $js->add_schema('http://localhost:1234/integer.json', { type => 'integer' });
1076
1077 $js->add_schema(
1078 'https://secondary.com',
1079 my $schema = {
1080 '$id' => 'https://main.com',
1081 '$ref' => 'http://localhost:1234/integer.json',
1082 type => 'object',
1083 },
1084 );
1085
1086 is_equal(
1087 my $result = $js->evaluate('string', 'https://secondary.com')->TO_JSON,
1088 {
1089 valid => false,
1090 errors => [
1091 {
1092 instanceLocation => '',
1093 keywordLocation => '/$ref/type',
1094 absoluteKeywordLocation => 'http://localhost:1234/integer.json#/type',
1095 error => 'got string, not integer',
1096 },
1097 {
1098 instanceLocation => '',
1099 keywordLocation => '/type',
1100 absoluteKeywordLocation => 'https://main.com#/type',
1101 error => 'got string, not object',
1102 },
1103 ],
1104 },
1105 'all uris in result are correct, using secondary uri as the target',
1106 );
1107
1108 is_equal(
1109 $js->evaluate('string', 'https://main.com')->TO_JSON,
1110 $result,
1111 'all uris in result are correct, using main uri as the target',
1112 );
1113 };
1114
1115 subtest 'document with no canonical URI, but assigned a URI through add_schema' => sub {
1116 my $js = JSON::Schema::Modern->new;
1117
1118 # the document itself doesn't know about this URI, but the evaluator does
1119 $js->add_schema(
1120 'https://localhost:1234/mydef.json',
1121 my $def_schema = { '$defs' => { integer => { type => 'integer' } } },
1122 );
1123
1124 is_equal(
1125 $js->evaluate(
1126 { foo => 'string' },
1127 my $schema = {
1128 # no $id here!
1129 type => 'object',
1130 additionalProperties => {
1131 '$ref' => 'https://localhost:1234/mydef.json#/$defs/integer',
1132 },
1133 },
1134 )->TO_JSON,
1135 {
1136 valid => false,
1137 errors => [
1138 {
1139 instanceLocation => '/foo',
1140 keywordLocation => '/additionalProperties/$ref/type',
1141 # the canonical URI is what the evaluator knows it as, even if the document doesn't know
1142 absoluteKeywordLocation => 'https://localhost:1234/mydef.json#/$defs/integer/type',
1143 error => 'got string, not integer',
1144 },
1145 {
1146 instanceLocation => '',
1147 keywordLocation => '/additionalProperties',
1148 error => 'not all additional properties are valid',
1149 },
1150 ],
1151 },
1152 'evaluate a schema referencing a document given an ad-hoc uri',
1153 );
1154
1155 # start over with a new evaluator...
1156 $js = JSON::Schema::Modern->new;
1157
1158 $js->add_document(
1159 'https://localhost:1234/mydef.json',
1160 JSON::Schema::Modern::Document->new(schema => {
1161 '$id' => 'https://otherhost.com/mydef.json',
1162 %$def_schema,
1163 }),
1164 );
1165
1166 is_equal(
1167 $js->evaluate(
1168 { foo => 'string' },
1169 $schema,
1170 )->TO_JSON,
1171 {
1172 valid => false,
1173 errors => [
1174 {
1175 instanceLocation => '/foo',
1176 keywordLocation => '/additionalProperties/$ref/type',
1177 absoluteKeywordLocation => 'https://otherhost.com/mydef.json#/$defs/integer/type',
1178 error => 'got string, not integer',
1179 },
1180 {
1181 instanceLocation => '',
1182 keywordLocation => '/additionalProperties',
1183 error => 'not all additional properties are valid',
1184 },
1185 ],
1186 },
1187 'adding a uri to an existing document does not change its canonical uri',
1188 );
1189 };
1190
1191 had_no_warnings if $ENV{AUTHOR_TESTING};
1192 done_testing;