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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
|
# vim: set ft=perl ts=8 sts=2 sw=2 tw=100 et :
use strictures 2;
use stable 0.031 'postderef';
use experimental 'signatures';
no autovivification warn => qw(fetch store exists delete);
use if "$]" >= 5.022, experimental => 're_strict';
no if "$]" >= 5.031009, feature => 'indirect';
no if "$]" >= 5.033001, feature => 'multidimensional';
no if "$]" >= 5.033006, feature => 'bareword_filehandles';
no if "$]" >= 5.041009, feature => 'smartmatch';
no feature 'switch';
use open ':std', ':encoding(UTF-8)'; # force stdin, stdout, stderr into utf8
use Test::Fatal;
use Test::Warnings qw(warnings :no_end_test had_no_warnings);
use List::Util 'unpairs';
use lib 't/lib';
use Helper;
use constant METASCHEMA => 'https://json-schema.org/draft/2019-09/schema';
# spec version -> vocab classes
my %vocabularies = unpairs(JSON::Schema::Modern->new->__all_metaschema_vocabulary_classes);
subtest 'evaluate a document' => sub {
my $document = JSON::Schema::Modern::Document->new(
schema => {
'$id' => 'https://foo.com',
allOf => [ false, true ],
});
my $js = JSON::Schema::Modern->new;
$js->add_document($document);
cmp_result(
$js->evaluate(1, $document->canonical_uri)->TO_JSON,
{
valid => false,
errors => my $errors = [
{
instanceLocation => '',
keywordLocation => '/allOf/0',
absoluteKeywordLocation => 'https://foo.com#/allOf/0',
error => 'subschema is false',
},
{
instanceLocation => '',
keywordLocation => '/allOf',
absoluteKeywordLocation => 'https://foo.com#/allOf',
error => 'subschema 0 is not valid',
},
],
},
'evaluate a Document object',
);
cmp_result(
{ $js->_resource_index },
{
'https://foo.com' => {
path => '',
canonical_uri => str('https://foo.com'),
document => shallow($document),
specification_version => 'draft2020-12',
vocabularies => $vocabularies{'draft2020-12'},
},
},
'resource index from the document is copied to the main object',
);
cmp_result(
$js->evaluate(1, $document->canonical_uri)->TO_JSON,
{
valid => false,
errors => $errors,
},
'evaluate a Document object again without error',
);
};
subtest 'evaluate a uri' => sub {
my $js = JSON::Schema::Modern->new;
cmp_result(
$js->evaluate({ '$schema' => 1 }, METASCHEMA)->TO_JSON,
{
valid => false,
errors => my $errors = [
{
instanceLocation => '/$schema',
keywordLocation => '/allOf/0/$ref/properties/$schema/type',
absoluteKeywordLocation => 'https://json-schema.org/draft/2019-09/meta/core#/properties/$schema/type',
error => 'got integer, not string',
},
{
instanceLocation => '',
keywordLocation => '/allOf/0/$ref/properties',
absoluteKeywordLocation => 'https://json-schema.org/draft/2019-09/meta/core#/properties',
error => 'not all properties are valid',
},
{
instanceLocation => '',
keywordLocation => '/allOf',
absoluteKeywordLocation => METASCHEMA.'#/allOf',
error => 'subschema 0 is not valid',
},
],
},
'evaluate with a uri that is not yet loaded',
);
cmp_result(
{ $js->_resource_index },
{
map +(
$_ => {
path => '',
canonical_uri => str($_),
document => isa('JSON::Schema::Modern::Document'),
specification_version => 'draft2019-09',
vocabularies => $vocabularies{'draft2019-09'},
}
),
METASCHEMA,
map 'https://json-schema.org/draft/2019-09/meta/'.$_,
qw(core applicator validation meta-data format content)
},
'the metaschema is now loaded and its resources are indexed',
);
# and again, we can use the same resource without reloading it
cmp_result(
$js->evaluate({ '$schema' => 1 }, METASCHEMA)->TO_JSON,
{
valid => false,
errors => $errors,
},
'evaluate against the metaschema again',
);
# now use a subschema at that url to evaluate with.
# multiple things are being tested here:
# - we can load a schema resource, or find an existing one, with a fragment
# - the json path we used is saved in the state, for correct errors
cmp_result(
$js->evaluate(
1,
'https://json-schema.org/draft/2019-09/meta/core#/properties/$schema',
)->TO_JSON,
{
valid => false,
errors => [
{
instanceLocation => '',
keywordLocation => '/type',
absoluteKeywordLocation => 'https://json-schema.org/draft/2019-09/meta/core#/properties/$schema/type',
error => 'got integer, not string',
},
],
},
'evaluate against the a subschema of the metaschema',
);
cmp_result(
$js->evaluate(
1,
METASCHEMA.'#/does/not/exist',
)->TO_JSON,
{
valid => false,
errors => [
{
instanceLocation => '',
keywordLocation => '',
error => 'EXCEPTION: unable to find resource "'.METASCHEMA.'#/does/not/exist"',
},
],
},
'evaluate against the a fragment of the metaschema that does not exist',
);
cmp_result(
$js->evaluate(
1,
METASCHEMA.'#does_not_exist',
)->TO_JSON,
{
valid => false,
errors => [
{
instanceLocation => '',
keywordLocation => '',
error => 'EXCEPTION: unable to find resource "'.METASCHEMA.'#does_not_exist"',
},
],
},
'evaluate against the a plain-name fragment of the metaschema that does not exist',
);
};
subtest 'add a uri resource' => sub {
my $js = JSON::Schema::Modern->new;
cmp_result(
my $get_metaschema = scalar $js->get(METASCHEMA),
my $orig_metaschema = $js->_get_resource(METASCHEMA)->{document}->schema,
'->get in scalar context on a URI to the head of a document',
);
ok($get_metaschema != $orig_metaschema, 'get() did not return a reference to the original data');
cmp_result(
[ $js->get(METASCHEMA) ],
[ $js->_get_resource(METASCHEMA)->{document}->schema,
all(isa('Mojo::URL'), str(METASCHEMA)) ],
'->get in list context on a URI to the head of a document',
);
cmp_result(
scalar $js->get(METASCHEMA.'#/properties/definitions/type'),
'object', # $document->schema->{properties}{definitions}{type}
'->get in scalar context on a URI to inside of a document',
);
cmp_result(
[ $js->get(METASCHEMA.'#/properties/definitions/type') ],
[ 'object', all(isa('Mojo::URL'), str(METASCHEMA.'#/properties/definitions/type')) ],
'->get in list context on a URI to inside of a document',
);
};
subtest 'add a schema associated with a uri' => sub {
my $js = JSON::Schema::Modern->new;
like(
exception { $js->add_schema('https://foo.com#/x/y/z', {}) },
qr/^cannot add a schema with a uri with a fragment/,
'cannot use a uri with a fragment',
);
cmp_result(
my $document = $js->add_schema(
'https://foo.com',
{ '$id' => 'https://bar.com', allOf => [ false, true ] },
),
all(
isa('JSON::Schema::Modern::Document'),
listmethods(
resource_index => [
'https://bar.com' => {
path => '',
canonical_uri => str('https://bar.com'),
specification_version => 'draft2020-12',
vocabularies => $vocabularies{'draft2020-12'},
},
],
canonical_uri => [ str('https://bar.com') ],
),
),
'added the schema data with an associated uri; the document does not see the overridden uri',
);
cmp_result(
$js->evaluate(1, 'https://bar.com#/allOf/0')->TO_JSON,
{
valid => false,
errors => [
{
instanceLocation => '',
keywordLocation => '',
absoluteKeywordLocation => 'https://bar.com#/allOf/0',
error => 'subschema is false',
},
],
},
'can now evaluate using a uri to a subschema of a resource we loaded earlier',
);
cmp_result(
$js->evaluate(1, 'https://foo.com')->TO_JSON,
{
valid => false,
errors => my $errors = [
{
instanceLocation => '',
keywordLocation => '/allOf/0',
absoluteKeywordLocation => 'https://bar.com#/allOf/0',
error => 'subschema is false',
},
{
instanceLocation => '',
keywordLocation => '/allOf',
absoluteKeywordLocation => 'https://bar.com#/allOf',
error => 'subschema 0 is not valid',
},
],
},
'can also evaluate using a non-canonical uri',
);
cmp_result(
$js->add_document('https://bloop.com', $document),
shallow($document),
'can add the same document and associate it with another schema',
);
my @warnings = warnings {
cmp_result(
$js->add_schema('https://bloop.com', $document),
shallow($document),
'can add the same document twice, using deprecated interface',
);
};
cmp_result(
\@warnings,
[ re(qr/use of deprecated form of add_schema with document/) ],
'warned when using deprecated form of add_schema with URI',
);
@warnings = warnings {
cmp_result(
$js->add_schema($document),
shallow($document),
'can add the same document again, using deprecated interface',
);
};
cmp_result(
\@warnings,
[ re(qr/use of deprecated form of add_schema with document/) ],
'warned when using deprecated form of add_schema without URI',
);
# this actually does nothing, via the duplicate check in _add_resource
cmp_result(
$js->add_document($document),
shallow($document),
'can add the same document again with the proper interface',
);
cmp_result(
{ $js->_resource_index },
{
map +( $_ => {
path => '',
canonical_uri => str('https://bar.com'),
document => shallow($document),
specification_version => 'draft2020-12',
vocabularies => $vocabularies{'draft2020-12'},
} ), qw(https://foo.com https://bar.com https://bloop.com)
},
'now the document is available as all three uris, with the same canonical_uri',
);
};
subtest 'multiple anonymous schemas' => sub {
my $js = JSON::Schema::Modern->new;
cmp_result(
$js->evaluate(1, { minimum => 1 })->TO_JSON,
{ valid => true },
'evaluate an anonymous schema',
);
cmp_result([ keys $js->{_resource_index}->%* ], [ '' ], 'one resource is indexed');
cmp_result(
$js->evaluate(2, { minimum => 2 })->TO_JSON,
{ valid => true },
'evaluate another anonymous schema',
);
cmp_result([ keys $js->{_resource_index}->%* ], [ '' ], 'still only one resource is indexed');
};
subtest 'add a document without associating it with a uri' => sub {
my $js = JSON::Schema::Modern->new;
cmp_result(
$js->add_document(
my $document = JSON::Schema::Modern::Document->new(
schema => { '$id' => 'https://bar.com', allOf => [ false, true ] },
)),
all(
isa('JSON::Schema::Modern::Document'),
listmethods(
resource_index => [
'https://bar.com' => {
path => '',
canonical_uri => str('https://bar.com'),
specification_version => 'draft2020-12',
vocabularies => $vocabularies{'draft2020-12'},
},
],
canonical_uri => [ str('https://bar.com') ],
),
),
'added the document without an associated uri',
);
cmp_result(
{ $js->_resource_index },
{
'https://bar.com' => {
path => '',
canonical_uri => str('https://bar.com'),
document => shallow($document),
specification_version => 'draft2020-12',
vocabularies => $vocabularies{'draft2020-12'},
},
},
'document only added under its canonical uri',
);
};
subtest 'add a schema without a uri' => sub {
my $js = JSON::Schema::Modern->new;
cmp_result(
my $document = $js->add_schema(
{ '$id' => 'https://bar.com', allOf => [ false, true ] },
),
all(
isa('JSON::Schema::Modern::Document'),
listmethods(
resource_index => [
'https://bar.com' => {
path => '',
canonical_uri => str('https://bar.com'),
specification_version => 'draft2020-12',
vocabularies => $vocabularies{'draft2020-12'},
},
],
canonical_uri => [ str('https://bar.com') ],
),
),
'added the schema data without an associated uri',
);
cmp_result(
{ $js->_resource_index },
{
'https://bar.com' => {
path => '',
canonical_uri => str('https://bar.com'),
document => shallow($document),
specification_version => 'draft2020-12',
vocabularies => $vocabularies{'draft2020-12'},
},
},
'document only added under its canonical uri',
);
};
subtest '$ref to non-canonical uri' => sub {
my $schema = {
'$id' => 'http://localhost:4242/my_document', # the canonical_uri
properties => {
alpha => false,
beta => {
'$id' => 'beta',
properties => {
gamma => {
minimum => 2,
},
},
},
delta => {
'$ref' => 'http://otherhost:4242/another_uri#/properties/alpha',
},
},
};
my $js = JSON::Schema::Modern->new;
$js->add_schema('http://otherhost:4242/another_uri', $schema);
cmp_result(
$js->evaluate({ alpha => 1 }, 'http://otherhost:4242/another_uri')->TO_JSON,
{
valid => false,
errors => [
{
instanceLocation => '/alpha',
keywordLocation => '/properties/alpha',
absoluteKeywordLocation => 'http://localhost:4242/my_document#/properties/alpha',
error => 'property not permitted',
},
{
instanceLocation => '',
keywordLocation => '/properties',
absoluteKeywordLocation => 'http://localhost:4242/my_document#/properties',
error => 'not all properties are valid',
},
],
},
'errors use the canonical uri, not the uri used to evaluate against',
);
cmp_result(
$js->evaluate({ gamma => 1 }, 'http://otherhost:4242/beta')->TO_JSON,
{
valid => false,
errors => [
{
instanceLocation => '',
keywordLocation => '',
error => 'EXCEPTION: unable to find resource "http://otherhost:4242/beta"',
},
],
},
'non-canonical uri is not used to resolve inner $id keywords',
);
cmp_result(
$js->evaluate({ gamma => 1 }, 'http://localhost:4242/beta')->TO_JSON,
{
valid => false,
errors => [
{
instanceLocation => '/gamma',
keywordLocation => '/properties/gamma/minimum',
absoluteKeywordLocation => 'http://localhost:4242/beta#/properties/gamma/minimum',
error => 'value is less than 2',
},
{
instanceLocation => '',
keywordLocation => '/properties',
absoluteKeywordLocation => 'http://localhost:4242/beta#/properties',
error => 'not all properties are valid',
},
],
},
'the canonical uri is updated when use the canonical uri, not the uri used to evaluate against',
);
cmp_result(
$js->evaluate({ delta => 1 }, 'http://otherhost:4242/another_uri')->TO_JSON,
{
valid => false,
errors => [
{
instanceLocation => '/delta',
keywordLocation => '/properties/delta/$ref',
absoluteKeywordLocation => 'http://localhost:4242/my_document#/properties/alpha',
error => 'subschema is false',
},
{
instanceLocation => '',
keywordLocation => '/properties',
absoluteKeywordLocation => 'http://localhost:4242/my_document#/properties',
error => 'not all properties are valid',
},
],
},
'canonical_uri is not always what was in the $ref, even when no local $id is present',
);
cmp_result(
$js->evaluate(1, 'http://otherhost:4242/another_uri#/properties/alpha')->TO_JSON,
{
valid => false,
errors => [
{
instanceLocation => '',
keywordLocation => '',
absoluteKeywordLocation => 'http://localhost:4242/my_document#/properties/alpha',
error => 'subschema is false',
},
],
},
'canonical_uri fragment also needs to be adjusted',
);
delete $schema->{properties}{beta}{'$id'};
cmp_result(
$js->evaluate({ gamma => 1 }, 'http://otherhost:4242/another_uri#/properties/beta')->TO_JSON,
{
valid => false,
errors => [
{
instanceLocation => '/gamma',
keywordLocation => '/properties/gamma/minimum',
absoluteKeywordLocation => 'http://localhost:4242/beta#/properties/gamma/minimum',
error => 'value is less than 2',
},
{
instanceLocation => '',
keywordLocation => '/properties',
absoluteKeywordLocation => 'http://localhost:4242/beta#/properties',
error => 'not all properties are valid',
},
],
},
'canonical_uri starts out containing a fragment and can be appended to during traversal',
);
};
subtest 'register a document against multiple uris, with absolute root uri' => sub {
my $js = JSON::Schema::Modern->new;
my $document = JSON::Schema::Modern::Document->new(
schema => {
'$id' => 'https://foo.com',
'$anchor' => 'my_anchor',
maximum => 1,
'$defs' => {
foo => {
'$id' => 'my_dir',
allOf => [ true ],
},
},
});
my %more_configs;
cmp_result(
{ $document->resource_index },
my $doc_resource_index = {
'https://foo.com' => {
path => '',
canonical_uri => str('https://foo.com'),
do { %more_configs = (
specification_version => 'draft2020-12',
vocabularies => $vocabularies{'draft2020-12'},
) },
anchors => {
my_anchor => {
path => '',
canonical_uri => str('https://foo.com'),
},
},
},
'https://foo.com/my_dir' => {
path => '/$defs/foo',
canonical_uri => str('https://foo.com/my_dir'),
%more_configs,
},
},
'identifiers stored for the document',
);
$js->add_document($document);
cmp_result(
{ $js->_resource_index },
my $main_resource_index = {
'https://foo.com' => {
path => '',
canonical_uri => str('https://foo.com'),
document => shallow($document),
%more_configs,
anchors => {
my_anchor => {
path => '',
canonical_uri => str('https://foo.com'),
},
},
},
'https://foo.com/my_dir' => {
path => '/$defs/foo',
canonical_uri => str('https://foo.com/my_dir'),
document => shallow($document),
%more_configs,
},
},
'resource index from the document is copied to the main object',
);
$js->add_document('https://uri2.com', $document);
cmp_result(
{ $js->_resource_index },
$main_resource_index = {
%$main_resource_index,
'https://uri2.com' => {
path => '',
canonical_uri => str('https://foo.com'),
document => shallow($document),
%more_configs,
anchors => {
my_anchor => {
path => '',
canonical_uri => str('https://foo.com'),
},
},
},
},
'add a secondary uri for the same document',
);
cmp_result(
{ $document->resource_index },
$doc_resource_index,
'secondary uri not also added to the document',
);
like(
exception { $js->add_schema('https://uri2.com', { x => 1 }) },
qr!^\Quri "https://uri2.com" conflicts with an existing schema resource\E!,
'cannot call add_schema with the same URI as for another schema',
);
like(
exception { $js->add_schema('https://uri3.com', { '$id' => 'https://foo.com', x => 1 }) },
qr!^\Quri "https://foo.com" conflicts with an existing schema resource\E!,
'cannot reuse the same $id in another document',
);
cmp_result(
{ $js->_resource_index },
$main_resource_index,
'resource index remains unchanged after erroneous add_schema calls',
);
$js->add_schema('https://uri4.com', +{ $document->schema->%* });
cmp_result(
{ $js->_resource_index },
{
%$main_resource_index,
'https://uri4.com' => {
path => '',
canonical_uri => str('https://foo.com'),
document => shallow($document),
%more_configs,
anchors => {
my_anchor => {
path => '',
canonical_uri => str('https://foo.com'),
},
},
},
},
'adding the same schema content again is permitted',
);
is(
scalar $js->get('https://foo.com#i_do_not_exist'),
undef,
'->get in scalar context for a nonexistent resource returns undef',
);
cmp_result(
[ $js->get('https://foo.com#i_do_not_exist') ],
[],
'->get in list context for a nonexistent resource returns empty list',
);
};
subtest 'register a document against multiple uris, with relative root uri' => sub {
my $js = JSON::Schema::Modern->new;
my $document = JSON::Schema::Modern::Document->new(
schema => {
'$id' => 'my_dir/',
'$anchor' => 'my_anchor',
maximum => 1,
'$defs' => {
foo => {
'$id' => 'my_dir2',
allOf => [ true ],
},
},
});
my %more_configs;
cmp_result(
{ $document->resource_index },
my $doc_resource_index = {
'my_dir/' => {
path => '',
canonical_uri => str('my_dir/'),
do { %more_configs = (
specification_version => 'draft2020-12',
vocabularies => $vocabularies{'draft2020-12'},
) },
anchors => {
my_anchor => {
path => '',
canonical_uri => str('my_dir/'),
},
},
},
'my_dir/my_dir2' => {
path => '/$defs/foo',
canonical_uri => str('my_dir/my_dir2'),
%more_configs,
},
},
'identifiers stored for the document',
);
$js->add_document($document);
cmp_result(
{ $js->_resource_index },
my $main_resource_index = {
'my_dir/' => {
path => '',
canonical_uri => str('my_dir/'),
document => shallow($document),
%more_configs,
anchors => {
my_anchor => {
path => '',
canonical_uri => str('my_dir/'),
},
},
},
'my_dir/my_dir2' => {
path => '/$defs/foo',
canonical_uri => str('my_dir/my_dir2'),
document => shallow($document),
%more_configs,
},
},
'resource index from the document is copied to the main object',
);
$js->add_document('https://uri2.com', $document);
cmp_result(
{ $js->_resource_index },
$main_resource_index = {
%$main_resource_index,
'https://uri2.com' => {
path => '',
canonical_uri => str('https://uri2.com/my_dir/'),
document => shallow($document),
%more_configs,
anchors => {
my_anchor => {
path => '',
canonical_uri => str('https://uri2.com/my_dir/'),
},
},
},
'https://uri2.com/my_dir/' => {
path => '',
canonical_uri => str('https://uri2.com/my_dir/'),
document => shallow($document),
%more_configs,
anchors => {
my_anchor => {
path => '',
canonical_uri => str('https://uri2.com/my_dir/'),
},
},
},
'https://uri2.com/my_dir/my_dir2' => {
path => '/$defs/foo',
canonical_uri => str('https://uri2.com/my_dir/my_dir2'),
document => shallow($document),
%more_configs,
},
},
'add a secondary (absolute) uri for the same document',
);
cmp_result(
{ $document->resource_index },
$doc_resource_index,
'secondary uri not also added to the document',
);
like(
exception { $js->add_schema('https://uri2.com', { x => 1 }) },
qr!^\Quri "https://uri2.com" conflicts with an existing schema resource\E!,
'cannot call add_schema with the same URI as for another schema',
);
like(
exception { $js->add_schema('https://uri3.com', { '$id' => 'https://uri2.com', x => 1 }) },
qr!^\Quri "https://uri2.com" conflicts with an existing schema resource\E!,
'cannot reuse the same $id in another document',
);
cmp_result(
{ $js->_resource_index },
$main_resource_index,
'resource index remains unchanged after erroneous add_schema calls',
);
$js->add_schema('https://uri4.com', +{ $document->schema->%* });
cmp_result(
{ $js->_resource_index },
{
%$main_resource_index,
'https://uri4.com' => {
path => '',
canonical_uri => str('https://uri4.com/my_dir/'),
document => shallow($document),
%more_configs,
anchors => {
my_anchor => {
path => '',
canonical_uri => str('https://uri4.com/my_dir/'),
},
},
},
'https://uri4.com/my_dir/' => {
path => '',
canonical_uri => str('https://uri4.com/my_dir/'),
document => shallow($document),
%more_configs,
anchors => {
my_anchor => {
path => '',
canonical_uri => str('https://uri4.com/my_dir/'),
},
},
},
'https://uri4.com/my_dir/my_dir2' => {
path => '/$defs/foo',
canonical_uri => str('https://uri4.com/my_dir/my_dir2'),
document => shallow($document),
%more_configs,
},
},
'adding the same schema content again is permitted',
);
};
subtest 'register a document against multiple uris, with no root uri' => sub {
my $js = JSON::Schema::Modern->new;
my $document = JSON::Schema::Modern::Document->new(
schema => {
'$anchor' => 'my_anchor',
maximum => 1,
'$defs' => {
foo => {
'$id' => 'my_dir',
allOf => [ true ],
},
},
});
my %more_configs;
cmp_result(
{ $document->resource_index },
my $doc_resource_index = {
'' => {
path => '',
canonical_uri => str(''),
do { %more_configs = (
specification_version => 'draft2020-12',
vocabularies => $vocabularies{'draft2020-12'},
) },
anchors => {
my_anchor => {
path => '',
canonical_uri => str(''),
},
},
},
'my_dir' => {
path => '/$defs/foo',
canonical_uri => str('my_dir'),
%more_configs,
},
},
'identifiers stored for the document',
);
$js->add_document($document);
cmp_result(
{ $js->_resource_index },
my $main_resource_index = {
'' => {
path => '',
canonical_uri => str(''),
document => shallow($document),
%more_configs,
anchors => {
my_anchor => {
path => '',
canonical_uri => str(''),
},
},
},
'my_dir' => {
path => '/$defs/foo',
canonical_uri => str('my_dir'),
document => shallow($document),
%more_configs,
},
},
'resource index from the document is copied to the main object',
);
$js->add_document('https://uri2.com', $document);
cmp_result(
{ $js->_resource_index },
$main_resource_index = {
%$main_resource_index,
'https://uri2.com' => {
path => '',
canonical_uri => str('https://uri2.com'),
document => shallow($document),
%more_configs,
anchors => {
my_anchor => {
path => '',
canonical_uri => str('https://uri2.com'),
},
},
},
'https://uri2.com/my_dir' => {
path => '/$defs/foo',
canonical_uri => str('https://uri2.com/my_dir'),
document => shallow($document),
%more_configs,
},
},
'add a secondary (absolute) uri for the same document',
);
cmp_result(
{ $document->resource_index },
$doc_resource_index,
'secondary uri not also added to the document',
);
like(
exception { $js->add_schema('https://uri2.com', { x => 1 }) },
qr!^\Quri "https://uri2.com" conflicts with an existing schema resource\E!,
'cannot call add_schema with the same URI as for another schema',
);
like(
exception { $js->add_schema('https://uri3.com', { '$id' => 'https://uri2.com', x => 1 }) },
qr!^\Quri "https://uri2.com" conflicts with an existing schema resource\E!,
'cannot reuse the same $id in another document',
);
cmp_result(
{ $js->_resource_index },
$main_resource_index,
'resource index remains unchanged after erroneous add_schema calls',
);
$js->add_schema('https://uri4.com', +{ $document->schema->%* });
cmp_result(
{ $js->_resource_index },
{
%$main_resource_index,
'https://uri4.com' => {
path => '',
canonical_uri => str('https://uri4.com'),
document => shallow($document),
%more_configs,
anchors => {
my_anchor => {
path => '',
canonical_uri => str('https://uri4.com'),
},
},
},
'https://uri4.com/my_dir' => {
path => '/$defs/foo',
canonical_uri => str('https://uri4.com/my_dir'),
document => shallow($document),
%more_configs,
},
},
'adding the same schema content again is permitted',
);
};
subtest 'external resource with externally-supplied uri; main resource with multiple uris' => sub {
my $js = JSON::Schema::Modern->new;
$js->add_schema('http://localhost:1234/integer.json', { type => 'integer' });
$js->add_schema(
'https://secondary.com',
my $schema = {
'$id' => 'https://main.com',
'$ref' => 'http://localhost:1234/integer.json',
type => 'object',
},
);
cmp_result(
my $result = $js->evaluate('string', 'https://secondary.com')->TO_JSON,
{
valid => false,
errors => [
{
instanceLocation => '',
keywordLocation => '/$ref/type',
absoluteKeywordLocation => 'http://localhost:1234/integer.json#/type',
error => 'got string, not integer',
},
{
instanceLocation => '',
keywordLocation => '/type',
absoluteKeywordLocation => 'https://main.com#/type',
error => 'got string, not object',
},
],
},
'all uris in result are correct, using secondary uri as the target',
);
cmp_result(
$js->evaluate('string', 'https://main.com')->TO_JSON,
$result,
'all uris in result are correct, using main uri as the target',
);
};
subtest 'document with no canonical URI, but assigned a URI through add_schema' => sub {
my $js = JSON::Schema::Modern->new;
# the document itself doesn't know about this URI, but the evaluator does
$js->add_schema(
'https://localhost:1234/mydef.json',
my $def_schema = { '$defs' => { integer => { type => 'integer' } } },
);
cmp_result(
$js->evaluate(
{ foo => 'string' },
my $schema = {
# no $id here!
type => 'object',
additionalProperties => {
'$ref' => 'https://localhost:1234/mydef.json#/$defs/integer',
},
},
)->TO_JSON,
{
valid => false,
errors => [
{
instanceLocation => '/foo',
keywordLocation => '/additionalProperties/$ref/type',
# the canonical URI is what the evaluator knows it as, even if the document doesn't know
absoluteKeywordLocation => 'https://localhost:1234/mydef.json#/$defs/integer/type',
error => 'got string, not integer',
},
{
instanceLocation => '',
keywordLocation => '/additionalProperties',
error => 'not all additional properties are valid',
},
],
},
'evaluate a schema referencing a document given an ad-hoc uri',
);
# start over with a new evaluator...
$js = JSON::Schema::Modern->new;
$js->add_document(
'https://localhost:1234/mydef.json',
JSON::Schema::Modern::Document->new(schema => {
'$id' => 'https://otherhost.com/mydef.json',
%$def_schema,
}),
);
cmp_result(
$js->evaluate(
{ foo => 'string' },
$schema,
)->TO_JSON,
{
valid => false,
errors => [
{
instanceLocation => '/foo',
keywordLocation => '/additionalProperties/$ref/type',
absoluteKeywordLocation => 'https://otherhost.com/mydef.json#/$defs/integer/type',
error => 'got string, not integer',
},
{
instanceLocation => '',
keywordLocation => '/additionalProperties',
error => 'not all additional properties are valid',
},
],
},
'adding a uri to an existing document does not change its canonical uri',
);
};
had_no_warnings if $ENV{AUTHOR_TESTING};
done_testing;
|