| 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 Test::Deep::UnorderedPairs; |
| 18 |
use Test::Memory::Cycle; |
| 19 |
use YAML::PP; |
| 20 |
|
| 21 |
# spec version -> vocab classes |
| 22 |
my %vocabularies = unpairs(JSON::Schema::Modern->new->__all_metaschema_vocabulary_classes); |
| 23 |
|
| 24 |
my %dialect = ( |
| 25 |
specification_version => 'draft2020-12', |
| 26 |
vocabularies => $vocabularies{'draft2020-12'}, |
| 27 |
); |
| 28 |
|
| 29 |
subtest 'boolean document' => sub { |
| 30 |
cmp_result( |
| 31 |
JSON::Schema::Modern::Document->new(schema => false), |
| 32 |
listmethods( |
| 33 |
resource_index => [ |
| 34 |
'' => { |
| 35 |
path => '', |
| 36 |
canonical_uri => str(''), |
| 37 |
%dialect, |
| 38 |
}, |
| 39 |
], |
| 40 |
original_uri => [ str('') ], |
| 41 |
canonical_uri => [ str('') ], |
| 42 |
metaschema_uri => [ str(JSON::Schema::Modern::METASCHEMA_URIS->{'draft2020-12'}) ], |
| 43 |
_entities => [ { '' => 0 } ], |
| 44 |
), |
| 45 |
'boolean schema with no canonical_uri', |
| 46 |
); |
| 47 |
|
| 48 |
like( |
| 49 |
dies { |
| 50 |
JSON::Schema::Modern::Document->new( |
| 51 |
canonical_uri => Mojo::URL->new('https://foo.com#/x/y/z'), |
| 52 |
schema => false, |
| 53 |
) |
| 54 |
}, |
| 55 |
qr/^Reference .*did not pass type constraint/, |
| 56 |
'boolean schema with invalid canonical_uri (fragment)', |
| 57 |
); |
| 58 |
|
| 59 |
cmp_result( |
| 60 |
JSON::Schema::Modern::Document->new( |
| 61 |
canonical_uri => Mojo::URL->new('https://foo.com'), |
| 62 |
schema => false, |
| 63 |
), |
| 64 |
listmethods( |
| 65 |
resource_index => [ |
| 66 |
'https://foo.com' => { |
| 67 |
path => '', |
| 68 |
canonical_uri => str('https://foo.com'), |
| 69 |
%dialect, |
| 70 |
}, |
| 71 |
], |
| 72 |
canonical_uri => [ str('https://foo.com') ], |
| 73 |
metaschema_uri => [ str(JSON::Schema::Modern::METASCHEMA_URIS->{'draft2020-12'}) ], |
| 74 |
_entities => [ { '' => 0 } ], |
| 75 |
), |
| 76 |
'boolean schema with valid canonical_uri', |
| 77 |
); |
| 78 |
}; |
| 79 |
|
| 80 |
subtest 'object document' => sub { |
| 81 |
cmp_result( |
| 82 |
JSON::Schema::Modern::Document->new( |
| 83 |
defined $_ ? (canonical_uri => $_) : (), |
| 84 |
schema => {}, |
| 85 |
), |
| 86 |
listmethods( |
| 87 |
resource_index => [ |
| 88 |
str($_//'') => { |
| 89 |
path => '', |
| 90 |
canonical_uri => str($_//''), |
| 91 |
%dialect, |
| 92 |
}, |
| 93 |
], |
| 94 |
original_uri => [ str($_//'') ], |
| 95 |
canonical_uri => [ str($_//'') ], |
| 96 |
_entities => [ { '' => 0 } ], |
| 97 |
), |
| 98 |
'object schema with originally provided uri = \''.($_//'<undef>').'\' and no root $id', |
| 99 |
) |
| 100 |
foreach (undef, '', '0', Mojo::URL->new, Mojo::URL->new(''), Mojo::URL->new('0')); |
| 101 |
|
| 102 |
cmp_result( |
| 103 |
JSON::Schema::Modern::Document->new( |
| 104 |
canonical_uri => Mojo::URL->new('https://foo.com'), |
| 105 |
schema => {}, |
| 106 |
), |
| 107 |
listmethods( |
| 108 |
resource_index => [ |
| 109 |
# note: no '' entry! |
| 110 |
'https://foo.com' => { |
| 111 |
path => '', |
| 112 |
canonical_uri => str('https://foo.com'), |
| 113 |
%dialect, |
| 114 |
}, |
| 115 |
], |
| 116 |
original_uri => [ str('https://foo.com') ], |
| 117 |
canonical_uri => [ str('https://foo.com') ], |
| 118 |
metaschema_uri => [ str(JSON::Schema::Modern::METASCHEMA_URIS->{'draft2020-12'}) ], |
| 119 |
_entities => [ { '' => 0 } ], |
| 120 |
), |
| 121 |
'object schema with valid canonical_uri, no root $id', |
| 122 |
); |
| 123 |
|
| 124 |
cmp_result( |
| 125 |
JSON::Schema::Modern::Document->new( |
| 126 |
defined $_ ? (canonical_uri => $_) : (), |
| 127 |
schema => { '$id' => 'https://foo.com' }, |
| 128 |
), |
| 129 |
listmethods( |
| 130 |
resource_index => [ |
| 131 |
# note: no '' entry |
| 132 |
'https://foo.com' => { |
| 133 |
path => '', |
| 134 |
canonical_uri => str('https://foo.com'), |
| 135 |
%dialect, |
| 136 |
}, |
| 137 |
], |
| 138 |
original_uri => [ str($_//'') ], |
| 139 |
canonical_uri => [ str('https://foo.com') ], # note canonical_uri has been overwritten |
| 140 |
metaschema_uri => [ str(JSON::Schema::Modern::METASCHEMA_URIS->{'draft2020-12'}) ], |
| 141 |
_entities => [ { '' => 0 } ], |
| 142 |
), |
| 143 |
'object schema with originally provided uri = \''.($_//'<undef>').'\' and absolute root $id', |
| 144 |
) |
| 145 |
foreach (undef, '', Mojo::URL->new); |
| 146 |
|
| 147 |
cmp_result( |
| 148 |
JSON::Schema::Modern::Document->new( |
| 149 |
canonical_uri => $_, |
| 150 |
schema => { '$id' => 'https://bar.com' }, |
| 151 |
), |
| 152 |
listmethods( |
| 153 |
resource_index => [ |
| 154 |
# note: no '' entry |
| 155 |
'https://bar.com' => { |
| 156 |
path => '', |
| 157 |
canonical_uri => str('https://bar.com'), |
| 158 |
%dialect, |
| 159 |
}, |
| 160 |
], |
| 161 |
original_uri => [ str($_) ], |
| 162 |
canonical_uri => [ str('https://bar.com') ], # note canonical_uri has been overwritten |
| 163 |
metaschema_uri => [ str(JSON::Schema::Modern::METASCHEMA_URIS->{'draft2020-12'}) ], |
| 164 |
_entities => [ { '' => 0 } ], |
| 165 |
), |
| 166 |
'originally provided uri is not indexed when overridden by an absolute root $id', |
| 167 |
) |
| 168 |
foreach ('0', Mojo::URL->new('0'), 'https://foo.com'); |
| 169 |
|
| 170 |
cmp_result( |
| 171 |
JSON::Schema::Modern::Document->new( |
| 172 |
schema => { |
| 173 |
'$defs' => { foo => {} }, |
| 174 |
}, |
| 175 |
canonical_uri => 'https://example.com', |
| 176 |
), |
| 177 |
listmethods( |
| 178 |
resource_index => [ |
| 179 |
'https://example.com' => { |
| 180 |
path => '', |
| 181 |
canonical_uri => str('https://example.com'), |
| 182 |
%dialect, |
| 183 |
}, |
| 184 |
], |
| 185 |
), |
| 186 |
'when canonical_uri provided, the empty uri is not added as a referenceable uri', |
| 187 |
); |
| 188 |
|
| 189 |
cmp_result( |
| 190 |
JSON::Schema::Modern::Document->new( |
| 191 |
canonical_uri => Mojo::URL->new('https://foo.com'), |
| 192 |
schema => { '$id' => 'https://foo.com' }, |
| 193 |
), |
| 194 |
listmethods( |
| 195 |
resource_index => [ |
| 196 |
'https://foo.com' => { |
| 197 |
path => '', |
| 198 |
canonical_uri => str('https://foo.com'), |
| 199 |
%dialect, |
| 200 |
}, |
| 201 |
], |
| 202 |
original_uri => [ str('https://foo.com') ], |
| 203 |
canonical_uri => [ str('https://foo.com') ], |
| 204 |
_entities => [ { '' => 0 } ], |
| 205 |
), |
| 206 |
'object schema with originally provided uri equal to root $id', |
| 207 |
); |
| 208 |
|
| 209 |
cmp_result( |
| 210 |
JSON::Schema::Modern::Document->new( |
| 211 |
canonical_uri => Mojo::URL->new('https://foo.com'), |
| 212 |
schema => { |
| 213 |
'$id' => 'https://bar.com', |
| 214 |
allOf => [ |
| 215 |
{ '$anchor' => 'my_anchor' }, |
| 216 |
{ '$id' => 'x/y/z.json' }, |
| 217 |
], |
| 218 |
}, |
| 219 |
), |
| 220 |
listmethods( |
| 221 |
resource_index => unordered_pairs( |
| 222 |
'https://bar.com' => { |
| 223 |
path => '', |
| 224 |
canonical_uri => str('https://bar.com'), |
| 225 |
%dialect, |
| 226 |
anchors => { |
| 227 |
my_anchor => { |
| 228 |
path => '/allOf/0', |
| 229 |
canonical_uri => str('https://bar.com#/allOf/0'), |
| 230 |
}, |
| 231 |
}, |
| 232 |
}, |
| 233 |
'https://bar.com/x/y/z.json' => { |
| 234 |
path => '/allOf/1', |
| 235 |
canonical_uri => str('https://bar.com/x/y/z.json'), |
| 236 |
%dialect, |
| 237 |
}, |
| 238 |
), |
| 239 |
original_uri => [ str('https://foo.com') ], |
| 240 |
canonical_uri => [ str('https://bar.com') ], |
| 241 |
metaschema_uri => [ str(JSON::Schema::Modern::METASCHEMA_URIS->{'draft2020-12'}) ], |
| 242 |
_entities => [ { map +($_ => 0), '', '/allOf/0', '/allOf/1' } ], |
| 243 |
), |
| 244 |
'object schema with canonical_uri and root $id, and additional resource schemas as well', |
| 245 |
); |
| 246 |
|
| 247 |
cmp_result( |
| 248 |
JSON::Schema::Modern::Document->new( |
| 249 |
schema => { |
| 250 |
'$id' => 'relative', |
| 251 |
}, |
| 252 |
canonical_uri => 'https://my-base.com', |
| 253 |
), |
| 254 |
listmethods( |
| 255 |
resource_index => [ |
| 256 |
'https://my-base.com/relative' => { |
| 257 |
path => '', |
| 258 |
canonical_uri => str('https://my-base.com/relative'), |
| 259 |
%dialect, |
| 260 |
}, |
| 261 |
], |
| 262 |
original_uri => [ str('https://my-base.com') ], |
| 263 |
canonical_uri => [ str('https://my-base.com/relative') ], |
| 264 |
metaschema_uri => [ str(JSON::Schema::Modern::METASCHEMA_URIS->{'draft2020-12'}) ], |
| 265 |
_entities => [ { '' => 0 } ], |
| 266 |
), |
| 267 |
'relative $id at root is resolved against provided canonical_id', |
| 268 |
); |
| 269 |
|
| 270 |
cmp_result( |
| 271 |
JSON::Schema::Modern::Document->new( |
| 272 |
schema => { |
| 273 |
'$defs' => { |
| 274 |
foo => { |
| 275 |
'$id' => 'my_foo', |
| 276 |
const => 'foo value', |
| 277 |
}, |
| 278 |
}, |
| 279 |
'$ref' => 'my_foo', |
| 280 |
}, |
| 281 |
), |
| 282 |
listmethods( |
| 283 |
resource_index => unordered_pairs( |
| 284 |
'' => { |
| 285 |
path => '', canonical_uri => str(''), |
| 286 |
%dialect, |
| 287 |
}, |
| 288 |
'my_foo' => { |
| 289 |
path => '/$defs/foo', |
| 290 |
canonical_uri => str('my_foo'), |
| 291 |
%dialect, |
| 292 |
}, |
| 293 |
), |
| 294 |
original_uri => [ str('') ], |
| 295 |
canonical_uri => [ str('') ], |
| 296 |
metaschema_uri => [ str(JSON::Schema::Modern::METASCHEMA_URIS->{'draft2020-12'}) ], |
| 297 |
_entities => [ { map +($_ => 0), '', '/$defs/foo' } ], |
| 298 |
), |
| 299 |
'relative uri for inner $id', |
| 300 |
); |
| 301 |
|
| 302 |
cmp_result( |
| 303 |
JSON::Schema::Modern::Document->new( |
| 304 |
schema => { |
| 305 |
'$defs' => { |
| 306 |
foo => { |
| 307 |
'$id' => 'http://localhost:4242/my_foo', |
| 308 |
const => 'foo value', |
| 309 |
}, |
| 310 |
}, |
| 311 |
}, |
| 312 |
), |
| 313 |
listmethods( |
| 314 |
resource_index => unordered_pairs( |
| 315 |
'' => { |
| 316 |
path => '', canonical_uri => str(''), |
| 317 |
%dialect, |
| 318 |
}, |
| 319 |
'http://localhost:4242/my_foo' => { |
| 320 |
path => '/$defs/foo', |
| 321 |
canonical_uri => str('http://localhost:4242/my_foo'), |
| 322 |
%dialect, |
| 323 |
}, |
| 324 |
), |
| 325 |
original_uri => [ str('') ], |
| 326 |
canonical_uri => [ str('') ], |
| 327 |
metaschema_uri => [ str(JSON::Schema::Modern::METASCHEMA_URIS->{'draft2020-12'}) ], |
| 328 |
_entities => [ { map +($_ => 0), '', '/$defs/foo' } ], |
| 329 |
), |
| 330 |
'no root $id; absolute uri with path in subschema resource', |
| 331 |
); |
| 332 |
|
| 333 |
cmp_result( |
| 334 |
JSON::Schema::Modern::Document->new( |
| 335 |
schema => { |
| 336 |
'$anchor' => 'my_anchor', |
| 337 |
}, |
| 338 |
), |
| 339 |
listmethods( |
| 340 |
resource_index => [ |
| 341 |
'' => { |
| 342 |
path => '', |
| 343 |
canonical_uri => str(''), |
| 344 |
%dialect, |
| 345 |
anchors => { |
| 346 |
my_anchor => { |
| 347 |
path => '', |
| 348 |
canonical_uri => str(''), |
| 349 |
}, |
| 350 |
}, |
| 351 |
}, |
| 352 |
], |
| 353 |
original_uri => [ str('') ], |
| 354 |
canonical_uri => [ str('') ], |
| 355 |
), |
| 356 |
'no root $id or canonical_uri provided; anchor is indexed at the root', |
| 357 |
); |
| 358 |
|
| 359 |
cmp_result( |
| 360 |
JSON::Schema::Modern::Document->new( |
| 361 |
schema => { |
| 362 |
'$anchor' => 'my_anchor', |
| 363 |
}, |
| 364 |
canonical_uri => 'https://example.com', |
| 365 |
), |
| 366 |
listmethods( |
| 367 |
resource_index => [ |
| 368 |
'https://example.com' => { |
| 369 |
path => '', |
| 370 |
canonical_uri => str('https://example.com'), |
| 371 |
%dialect, |
| 372 |
anchors => { |
| 373 |
my_anchor => { |
| 374 |
path => '', |
| 375 |
canonical_uri => str('https://example.com'), |
| 376 |
}, |
| 377 |
}, |
| 378 |
}, |
| 379 |
], |
| 380 |
original_uri => [ str('https://example.com') ], |
| 381 |
canonical_uri => [ str('https://example.com') ], |
| 382 |
metaschema_uri => [ str(JSON::Schema::Modern::METASCHEMA_URIS->{'draft2020-12'}) ], |
| 383 |
), |
| 384 |
'canonical_uri provided; empty uri not added as a referenceable uri when an anchor exists', |
| 385 |
); |
| 386 |
|
| 387 |
cmp_result( |
| 388 |
JSON::Schema::Modern::Document->new( |
| 389 |
schema => { |
| 390 |
'$id' => 'https://my-base.com', |
| 391 |
'$anchor' => 'my_anchor', |
| 392 |
}, |
| 393 |
), |
| 394 |
listmethods( |
| 395 |
resource_index => [ |
| 396 |
'https://my-base.com' => { |
| 397 |
path => '', |
| 398 |
canonical_uri => str('https://my-base.com'), |
| 399 |
%dialect, |
| 400 |
anchors => { |
| 401 |
my_anchor => { |
| 402 |
path => '', |
| 403 |
canonical_uri => str('https://my-base.com'), |
| 404 |
}, |
| 405 |
}, |
| 406 |
}, |
| 407 |
], |
| 408 |
original_uri => [ str('') ], |
| 409 |
canonical_uri => [ str('https://my-base.com') ], |
| 410 |
metaschema_uri => [ str(JSON::Schema::Modern::METASCHEMA_URIS->{'draft2020-12'}) ], |
| 411 |
), |
| 412 |
'absolute uri provided at root; adjacent anchor has the same canonical uri', |
| 413 |
); |
| 414 |
|
| 415 |
cmp_result( |
| 416 |
JSON::Schema::Modern::Document->new( |
| 417 |
schema => { |
| 418 |
'$id' => 'https://my-base.com', |
| 419 |
'$defs' => { |
| 420 |
foo => { |
| 421 |
'$anchor' => 'my_anchor', |
| 422 |
}, |
| 423 |
}, |
| 424 |
}, |
| 425 |
), |
| 426 |
listmethods( |
| 427 |
resource_index => [ |
| 428 |
'https://my-base.com' => { |
| 429 |
path => '', |
| 430 |
canonical_uri => str('https://my-base.com'), |
| 431 |
%dialect, |
| 432 |
anchors => { |
| 433 |
my_anchor => { |
| 434 |
path => '/$defs/foo', |
| 435 |
canonical_uri => str('https://my-base.com#/$defs/foo'), |
| 436 |
}, |
| 437 |
}, |
| 438 |
}, |
| 439 |
], |
| 440 |
original_uri => [ str('') ], |
| 441 |
canonical_uri => [ str('https://my-base.com') ], |
| 442 |
metaschema_uri => [ str(JSON::Schema::Modern::METASCHEMA_URIS->{'draft2020-12'}) ], |
| 443 |
), |
| 444 |
'absolute uri provided at root; anchor lower down has its own canonical uri', |
| 445 |
); |
| 446 |
}; |
| 447 |
|
| 448 |
subtest '$id and $anchor as properties' => sub { |
| 449 |
cmp_result( |
| 450 |
JSON::Schema::Modern::Document->new( |
| 451 |
schema => { |
| 452 |
type => 'object', |
| 453 |
properties => { |
| 454 |
'$id' => { type => 'string' }, |
| 455 |
'$anchor' => { type => 'string' }, |
| 456 |
}, |
| 457 |
}, |
| 458 |
), |
| 459 |
listmethods( |
| 460 |
resource_index => [ |
| 461 |
'' => { |
| 462 |
path => '', |
| 463 |
canonical_uri => str(''), |
| 464 |
%dialect, |
| 465 |
}, |
| 466 |
], |
| 467 |
_entities => [ { map +($_ => 0), '', '/properties/$id', '/properties/$anchor' } ], |
| 468 |
), |
| 469 |
'did not index the $id and $anchor properties as if they were identifier keywords', |
| 470 |
); |
| 471 |
}; |
| 472 |
|
| 473 |
subtest '$id with an empty fragment' => sub { |
| 474 |
cmp_result( |
| 475 |
JSON::Schema::Modern::Document->new( |
| 476 |
schema => { |
| 477 |
'$defs' => { |
| 478 |
foo => { |
| 479 |
'$id' => 'http://localhost:4242/my_foo#', |
| 480 |
type => 'string', |
| 481 |
}, |
| 482 |
}, |
| 483 |
}, |
| 484 |
), |
| 485 |
listmethods( |
| 486 |
resource_index => unordered_pairs( |
| 487 |
'' => { |
| 488 |
path => '', canonical_uri => str(''), |
| 489 |
%dialect, |
| 490 |
}, |
| 491 |
'http://localhost:4242/my_foo' => { |
| 492 |
path => '/$defs/foo', |
| 493 |
canonical_uri => str('http://localhost:4242/my_foo'), |
| 494 |
%dialect, |
| 495 |
}, |
| 496 |
), |
| 497 |
_entities => [ { map +($_ => 0), '', '/$defs/foo' } ], |
| 498 |
), |
| 499 |
'$id is stored with the empty fragment stripped', |
| 500 |
); |
| 501 |
}; |
| 502 |
|
| 503 |
subtest '$id with a non-empty fragment' => sub { |
| 504 |
my $doc = JSON::Schema::Modern::Document->new( |
| 505 |
schema => { |
| 506 |
'$id' => 'http://main.com', |
| 507 |
'$defs' => { |
| 508 |
foo => { |
| 509 |
'$id' => 'http://secondary.com', |
| 510 |
properties => { |
| 511 |
bar => { |
| 512 |
'$id' => 'http://localhost:4242/my_foo#hello', |
| 513 |
}, |
| 514 |
}, |
| 515 |
}, |
| 516 |
}, |
| 517 |
}, |
| 518 |
); |
| 519 |
is_equal( |
| 520 |
[ map $_->TO_JSON, $doc->errors ], |
| 521 |
[ |
| 522 |
{ |
| 523 |
keywordLocation => '/$defs/foo/properties/bar/$id', |
| 524 |
absoluteKeywordLocation => 'http://secondary.com#/properties/bar/$id', |
| 525 |
error => '$id value "http://localhost:4242/my_foo#hello" cannot have a non-empty fragment', |
| 526 |
}, |
| 527 |
], |
| 528 |
'did not index the $id with a non-empty fragment, nor use it as the base for other identifiers', |
| 529 |
); |
| 530 |
|
| 531 |
cmp_result($doc->canonical_uri, str('http://main.com'), 'canonical_uri'); |
| 532 |
is_equal([ $doc->resource_index ], [], 'nothing was indexed'); |
| 533 |
}; |
| 534 |
|
| 535 |
subtest '$anchor not conforming to syntax' => sub { |
| 536 |
my $doc = JSON::Schema::Modern::Document->new( |
| 537 |
schema => { |
| 538 |
'$defs' => { |
| 539 |
foo => { |
| 540 |
'$anchor' => 'my_#bad_anchor', |
| 541 |
}, |
| 542 |
}, |
| 543 |
}, |
| 544 |
); |
| 545 |
is_equal( |
| 546 |
[ map $_->TO_JSON, $doc->errors ], |
| 547 |
[ |
| 548 |
{ |
| 549 |
keywordLocation => '/$defs/foo/$anchor', |
| 550 |
error => '$anchor value "my_#bad_anchor" does not match required syntax', |
| 551 |
}, |
| 552 |
], |
| 553 |
'did not index an $anchor with invalid characters', |
| 554 |
); |
| 555 |
is_equal([ $doc->resource_index ], [], 'nothing was indexed'); |
| 556 |
|
| 557 |
$doc = JSON::Schema::Modern::Document->new( |
| 558 |
specification_version => 'draft2020-12', |
| 559 |
schema => { |
| 560 |
'$defs' => { |
| 561 |
foo => { |
| 562 |
'$anchor' => 'my:bad_anchor', # legal in earlier drafts |
| 563 |
}, |
| 564 |
qux => { |
| 565 |
'$id' => 'https://foo.com#my_bad_id', |
| 566 |
}, |
| 567 |
}, |
| 568 |
}, |
| 569 |
); |
| 570 |
is_equal( |
| 571 |
[ map $_->TO_JSON, $doc->errors ], |
| 572 |
[ |
| 573 |
{ |
| 574 |
keywordLocation => '/$defs/foo/$anchor', |
| 575 |
error => '$anchor value "my:bad_anchor" does not match required syntax', |
| 576 |
}, |
| 577 |
{ |
| 578 |
keywordLocation => '/$defs/qux/$id', |
| 579 |
error => '$id value "https://foo.com#my_bad_id" cannot have a non-empty fragment', |
| 580 |
}, |
| 581 |
], |
| 582 |
'did not index a draft2020-12 $anchor with invalid characters, or non-fragment-only $id', |
| 583 |
); |
| 584 |
is_equal([ $doc->resource_index ], [], 'nothing was indexed'); |
| 585 |
|
| 586 |
$doc = JSON::Schema::Modern::Document->new( |
| 587 |
specification_version => 'draft2019-09', |
| 588 |
schema => { |
| 589 |
'$defs' => { |
| 590 |
foo => { |
| 591 |
'$anchor' => '_my_bad_anchor', # legal in draft2020-12 |
| 592 |
}, |
| 593 |
qux => { |
| 594 |
'$id' => 'https://foo.com#my_bad_id', |
| 595 |
}, |
| 596 |
}, |
| 597 |
}, |
| 598 |
); |
| 599 |
is_equal( |
| 600 |
[ map $_->TO_JSON, $doc->errors ], |
| 601 |
[ |
| 602 |
{ |
| 603 |
keywordLocation => '/$defs/foo/$anchor', |
| 604 |
error => '$anchor value "_my_bad_anchor" does not match required syntax', |
| 605 |
}, |
| 606 |
{ |
| 607 |
keywordLocation => '/$defs/qux/$id', |
| 608 |
error => '$id value "https://foo.com#my_bad_id" cannot have a non-empty fragment', |
| 609 |
}, |
| 610 |
], |
| 611 |
'did not index a draft2019-09 $anchor with invalid characters, or non-fragment-only $id', |
| 612 |
); |
| 613 |
is_equal([ $doc->resource_index ], [], 'nothing was indexed'); |
| 614 |
|
| 615 |
foreach my $version (qw(draft6 draft7)) { |
| 616 |
$doc = JSON::Schema::Modern::Document->new( |
| 617 |
specification_version => $version, |
| 618 |
schema => { |
| 619 |
definitions => { |
| 620 |
foo => { |
| 621 |
'$id' => '#_my_bad_anchor', # legal in draft2020-12 |
| 622 |
}, |
| 623 |
qux => { |
| 624 |
'$id' => 'https://foo.com#my_bad_id', |
| 625 |
}, |
| 626 |
}, |
| 627 |
}, |
| 628 |
); |
| 629 |
is_equal( |
| 630 |
[ map $_->TO_JSON, $doc->errors ], |
| 631 |
[ |
| 632 |
{ |
| 633 |
keywordLocation => '/definitions/foo/$id', |
| 634 |
error => '$id value "#_my_bad_anchor" does not match required syntax', |
| 635 |
}, |
| 636 |
{ |
| 637 |
keywordLocation => '/definitions/qux/$id', |
| 638 |
error => '$id cannot change the base uri at the same time as declaring an anchor', |
| 639 |
}, |
| 640 |
], |
| 641 |
'did not index a '.$version.' fragment-only $id with invalid characters, or non-fragment-only $id', |
| 642 |
); |
| 643 |
is_equal([ $doc->resource_index ], [], 'nothing was indexed'); |
| 644 |
} |
| 645 |
|
| 646 |
$doc = JSON::Schema::Modern::Document->new( |
| 647 |
specification_version => 'draft4', |
| 648 |
schema => { |
| 649 |
definitions => { |
| 650 |
foo => { |
| 651 |
id => '#_my_bad_anchor', # legal in draft2020-12 |
| 652 |
}, |
| 653 |
}, |
| 654 |
}, |
| 655 |
); |
| 656 |
is_equal( |
| 657 |
[ map $_->TO_JSON, $doc->errors ], |
| 658 |
[ |
| 659 |
{ |
| 660 |
keywordLocation => '/definitions/foo/id', |
| 661 |
error => 'id value "#_my_bad_anchor" does not match required syntax', |
| 662 |
}, |
| 663 |
], |
| 664 |
'did not index a draft4 fragment-only id with invalid characters', |
| 665 |
); |
| 666 |
is_equal([ $doc->resource_index ], [], 'nothing was indexed'); |
| 667 |
|
| 668 |
$doc = JSON::Schema::Modern::Document->new( |
| 669 |
specification_version => 'draft4', |
| 670 |
schema => { |
| 671 |
id => 'https://foo.com', |
| 672 |
definitions => { |
| 673 |
qux => { |
| 674 |
id => 'blah#weird_but_legal', |
| 675 |
}, |
| 676 |
}, |
| 677 |
}, |
| 678 |
); |
| 679 |
|
| 680 |
cmp_result([ map $_->TO_JSON, $doc->errors ], [], 'no errors'); |
| 681 |
cmp_result( |
| 682 |
$doc, |
| 683 |
listmethods( |
| 684 |
resource_index => unordered_pairs( |
| 685 |
'https://foo.com' => { |
| 686 |
path => '', |
| 687 |
canonical_uri => str('https://foo.com'), |
| 688 |
specification_version => 'draft4', |
| 689 |
vocabularies => $vocabularies{'draft4'}, |
| 690 |
}, |
| 691 |
'https://foo.com/blah' => { |
| 692 |
path => '/definitions/qux', |
| 693 |
canonical_uri => str('https://foo.com/blah'), |
| 694 |
specification_version => 'draft4', |
| 695 |
vocabularies => $vocabularies{'draft4'}, |
| 696 |
anchors => { |
| 697 |
weird_but_legal => { |
| 698 |
path => '/definitions/qux', |
| 699 |
canonical_uri => str('https://foo.com/blah'), |
| 700 |
}, |
| 701 |
}, |
| 702 |
}, |
| 703 |
)), |
| 704 |
'can combine a canonical identifier with an anchor in draft4', |
| 705 |
); |
| 706 |
}; |
| 707 |
|
| 708 |
subtest '$schema not conforming to syntax' => sub { |
| 709 |
cmp_result( |
| 710 |
JSON::Schema::Modern::Document->new( |
| 711 |
schema => { '$schema' => 'foo' }, |
| 712 |
), |
| 713 |
listmethods( |
| 714 |
canonical_uri => [ str('') ], |
| 715 |
metaschema_uri => [ str('https://json-schema.org/draft/2020-12/schema') ], |
| 716 |
resource_index => [], |
| 717 |
errors => [ |
| 718 |
methods(TO_JSON => { |
| 719 |
keywordLocation => '/$schema', |
| 720 |
error => '"foo" is not a valid URI', |
| 721 |
}), |
| 722 |
], |
| 723 |
), |
| 724 |
'invalid $schema is detected', |
| 725 |
); |
| 726 |
}; |
| 727 |
|
| 728 |
subtest '$anchor and $id below an $id that is not at the document root' => sub { |
| 729 |
cmp_result( |
| 730 |
JSON::Schema::Modern::Document->new( |
| 731 |
canonical_uri => Mojo::URL->new('https://foo.com'), |
| 732 |
schema => { |
| 733 |
allOf => [ |
| 734 |
{ |
| 735 |
'$id' => 'https://bar.com', |
| 736 |
'$anchor' => 'my_anchor', |
| 737 |
not => { |
| 738 |
'$anchor' => 'my_not', |
| 739 |
not => { '$id' => 'inner_id' }, |
| 740 |
}, |
| 741 |
}, |
| 742 |
], |
| 743 |
}, |
| 744 |
), |
| 745 |
listmethods( |
| 746 |
resource_index => unordered_pairs( |
| 747 |
'https://foo.com' => { |
| 748 |
path => '', canonical_uri => str('https://foo.com'), |
| 749 |
%dialect, |
| 750 |
}, |
| 751 |
'https://bar.com' => { |
| 752 |
path => '/allOf/0', canonical_uri => str('https://bar.com'), |
| 753 |
%dialect, |
| 754 |
anchors => { |
| 755 |
my_anchor => { |
| 756 |
path => '/allOf/0', |
| 757 |
canonical_uri => str('https://bar.com'), |
| 758 |
}, |
| 759 |
my_not => { |
| 760 |
path => '/allOf/0/not', |
| 761 |
canonical_uri => str('https://bar.com#/not'), |
| 762 |
}, |
| 763 |
}, |
| 764 |
}, |
| 765 |
'https://bar.com/inner_id' => { |
| 766 |
path => '/allOf/0/not/not', canonical_uri => str('https://bar.com/inner_id'), |
| 767 |
%dialect, |
| 768 |
}, |
| 769 |
), |
| 770 |
_entities => [ { map +($_ => 0), '', '/allOf/0', '/allOf/0/not', '/allOf/0/not/not' } ], |
| 771 |
), |
| 772 |
'canonical_uri uses the path from the innermost $id, not document root $id', |
| 773 |
); |
| 774 |
}; |
| 775 |
|
| 776 |
subtest 'JSON pointer and URI escaping' => sub { |
| 777 |
cmp_result( |
| 778 |
my $doc = JSON::Schema::Modern::Document->new( |
| 779 |
schema => { |
| 780 |
'$defs' => { |
| 781 |
foo => { |
| 782 |
patternProperties => { |
| 783 |
'~' => { |
| 784 |
'$id' => 'http://localhost:4242/~username', |
| 785 |
properties => { |
| 786 |
'~/' => { |
| 787 |
'$anchor' => 'tilde', |
| 788 |
}, |
| 789 |
}, |
| 790 |
}, |
| 791 |
'/' => { |
| 792 |
'$id' => 'http://localhost:4242/my_slash', |
| 793 |
properties => { |
| 794 |
'~/' => { |
| 795 |
'$anchor' => 'slash', |
| 796 |
}, |
| 797 |
}, |
| 798 |
}, |
| 799 |
'[~/]' => { |
| 800 |
'$id' => 'http://localhost:4242/~username/my_slash', |
| 801 |
properties => { |
| 802 |
'~/' => { |
| 803 |
'$anchor' => 'tildeslash', |
| 804 |
}, |
| 805 |
}, |
| 806 |
}, |
| 807 |
}, |
| 808 |
}, |
| 809 |
}, |
| 810 |
}, |
| 811 |
), |
| 812 |
listmethods( |
| 813 |
resource_index => unordered_pairs( |
| 814 |
'' => { |
| 815 |
path => '', canonical_uri => str(''), |
| 816 |
%dialect, |
| 817 |
}, |
| 818 |
'http://localhost:4242/~username' => { |
| 819 |
path => '/$defs/foo/patternProperties/~0', |
| 820 |
canonical_uri => str('http://localhost:4242/~username'), |
| 821 |
%dialect, |
| 822 |
anchors => { |
| 823 |
tilde => { |
| 824 |
path => '/$defs/foo/patternProperties/~0/properties/~0~1', |
| 825 |
canonical_uri => str('http://localhost:4242/~username#/properties/~0~1'), |
| 826 |
}, |
| 827 |
}, |
| 828 |
}, |
| 829 |
'http://localhost:4242/my_slash' => { |
| 830 |
path => '/$defs/foo/patternProperties/~1', |
| 831 |
canonical_uri => str('http://localhost:4242/my_slash'), |
| 832 |
%dialect, |
| 833 |
anchors => { |
| 834 |
slash => { |
| 835 |
path => '/$defs/foo/patternProperties/~1/properties/~0~1', |
| 836 |
canonical_uri => str('http://localhost:4242/my_slash#/properties/~0~1'), |
| 837 |
}, |
| 838 |
}, |
| 839 |
}, |
| 840 |
'http://localhost:4242/~username/my_slash' => { |
| 841 |
path => '/$defs/foo/patternProperties/[~0~1]', |
| 842 |
canonical_uri => str('http://localhost:4242/~username/my_slash'), |
| 843 |
%dialect, |
| 844 |
anchors => { |
| 845 |
tildeslash => { |
| 846 |
path => '/$defs/foo/patternProperties/[~0~1]/properties/~0~1', |
| 847 |
canonical_uri => str('http://localhost:4242/~username/my_slash#/properties/~0~1'), |
| 848 |
}, |
| 849 |
}, |
| 850 |
}, |
| 851 |
), |
| 852 |
_entities => [ { map +($_ => 0), |
| 853 |
my @locations = ( |
| 854 |
'', |
| 855 |
'/$defs/foo', |
| 856 |
'/$defs/foo/patternProperties/~0', |
| 857 |
'/$defs/foo/patternProperties/~0/properties/~0~1', |
| 858 |
'/$defs/foo/patternProperties/~1', |
| 859 |
'/$defs/foo/patternProperties/~1/properties/~0~1', |
| 860 |
'/$defs/foo/patternProperties/[~0~1]', |
| 861 |
'/$defs/foo/patternProperties/[~0~1]/properties/~0~1', |
| 862 |
) |
| 863 |
}], |
| 864 |
), |
| 865 |
'properly escaped special characters in JSON pointers and URIs', |
| 866 |
); |
| 867 |
is($doc->get_entity_at_location('/$defs/foo/patternProperties/~0'), 'schema', 'schema locations are tracked'); |
| 868 |
is($doc->get_entity_at_location('/$defs/foo/patternProperties'), '', 'non-schema locations are also tracked'); |
| 869 |
|
| 870 |
cmp_result( |
| 871 |
[ $doc->get_entity_locations('schema') ], |
| 872 |
bag(@locations), |
| 873 |
'schema locations can be queried', |
| 874 |
); |
| 875 |
}; |
| 876 |
|
| 877 |
subtest 'resource collisions' => sub { |
| 878 |
ok( |
| 879 |
lives { |
| 880 |
JSON::Schema::Modern::Document->new( |
| 881 |
canonical_uri => Mojo::URL->new('https://foo.com/x/y/z'), |
| 882 |
schema => { '$id' => '/x/y/z' }, |
| 883 |
); |
| 884 |
}, |
| 885 |
'no collision when adding an identical resource (after resolving with base uri)', |
| 886 |
); |
| 887 |
|
| 888 |
like( |
| 889 |
dies { |
| 890 |
JSON::Schema::Modern::Document->new( |
| 891 |
canonical_uri => Mojo::URL->new('https://foo.com/x/y/z'), |
| 892 |
schema => { |
| 893 |
allOf => [ |
| 894 |
{ '$id' => '/x/y/z' }, |
| 895 |
{ '$id' => '/a/b/c' }, |
| 896 |
], |
| 897 |
}, |
| 898 |
); |
| 899 |
}, |
| 900 |
qr{^\Quri "https://foo.com/x/y/z" conflicts with an existing schema resource\E}, |
| 901 |
'detected collision between document\'s initial uri and a subschema\'s uri', |
| 902 |
); |
| 903 |
|
| 904 |
cmp_result( |
| 905 |
JSON::Schema::Modern::Document->new( |
| 906 |
canonical_uri => Mojo::URL->new('https://foo.com'), |
| 907 |
schema => { |
| 908 |
allOf => [ |
| 909 |
{ '$id' => '/x/y/z' }, |
| 910 |
{ '$id' => '/x/y/z' }, |
| 911 |
], |
| 912 |
}, |
| 913 |
), |
| 914 |
all( |
| 915 |
listmethods( |
| 916 |
resource_index => [], |
| 917 |
errors => [ |
| 918 |
methods(TO_JSON => { |
| 919 |
keywordLocation => '/allOf/1/$id', |
| 920 |
absoluteKeywordLocation => 'https://foo.com#/allOf/1/$id', |
| 921 |
error => 'duplicate canonical uri "https://foo.com/x/y/z" found (original at path "/allOf/0")', |
| 922 |
}), |
| 923 |
], |
| 924 |
), |
| 925 |
), |
| 926 |
'detected collision between two subschema uris in a document', |
| 927 |
); |
| 928 |
|
| 929 |
|
| 930 |
my $doc1 = JSON::Schema::Modern::Document->new(schema => { '$id' => 'a/b' }); |
| 931 |
my $doc2 = JSON::Schema::Modern::Document->new(schema => { '$id' => 'b' }); |
| 932 |
my $js = JSON::Schema::Modern->new; |
| 933 |
|
| 934 |
ok( |
| 935 |
# id resolves to https://foo.com/a/b |
| 936 |
lives { $js->add_document('https://foo.com' => $doc1) }, |
| 937 |
'add first document, resolving resources to a base uri', |
| 938 |
); |
| 939 |
|
| 940 |
like( |
| 941 |
# id resolves to https://foo.com/a/b |
| 942 |
dies { $js->add_document('https://foo.com/a/' => $doc2) }, |
| 943 |
qr{^uri "https://foo.com/a/b" conflicts with an existing schema resource}, |
| 944 |
'the resource in the second document resolves to the same uri as from the first document', |
| 945 |
); |
| 946 |
|
| 947 |
|
| 948 |
ok( |
| 949 |
lives { |
| 950 |
JSON::Schema::Modern::Document->new( |
| 951 |
canonical_uri => Mojo::URL->new('https://foo.com/x/y/z'), |
| 952 |
schema => { |
| 953 |
examples => [ |
| 954 |
{ '$id' => '/x/y/z' }, |
| 955 |
{ '$id' => 'https://foo.com/x/y/z' }, |
| 956 |
], |
| 957 |
default => { |
| 958 |
allOf => [ |
| 959 |
{ '$id' => '/x/y/z' }, |
| 960 |
{ '$id' => 'https://foo.com/x/y/z' }, |
| 961 |
], |
| 962 |
}, |
| 963 |
}, |
| 964 |
); |
| 965 |
}, |
| 966 |
'ignored "duplicate" uris embedded in non-schemas', |
| 967 |
); |
| 968 |
}; |
| 969 |
|
| 970 |
subtest 'create document with explicit canonical_uri set to the same as root $id' => sub { |
| 971 |
cmp_result( |
| 972 |
JSON::Schema::Modern::Document->new( |
| 973 |
canonical_uri => 'https://foo.com/x/y/z', |
| 974 |
schema => { '$id' => 'https://foo.com/x/y/z' }, |
| 975 |
), |
| 976 |
listmethods( |
| 977 |
resource_index => [ |
| 978 |
'https://foo.com/x/y/z' => { |
| 979 |
path => '', |
| 980 |
canonical_uri => str('https://foo.com/x/y/z'), |
| 981 |
%dialect, |
| 982 |
}, |
| 983 |
], |
| 984 |
canonical_uri => [ str('https://foo.com/x/y/z') ], |
| 985 |
), |
| 986 |
'there is one single uri indexed to the document', |
| 987 |
); |
| 988 |
}; |
| 989 |
|
| 990 |
subtest 'canonical_uri identification from a document with errors' => sub { |
| 991 |
cmp_result( |
| 992 |
JSON::Schema::Modern::Document->new( |
| 993 |
canonical_uri => 'https://foo.com/x/y/z', |
| 994 |
schema => { |
| 995 |
'$id' => 'https://bar.com', |
| 996 |
allOf => [ |
| 997 |
{ |
| 998 |
'$id' => 'https://baz.com', |
| 999 |
oneOf => [ |
| 1000 |
{ '$id' => 'https://quux.com' }, |
| 1001 |
[ 'not a subschema' ], |
| 1002 |
], |
| 1003 |
}, |
| 1004 |
], |
| 1005 |
}, |
| 1006 |
), |
| 1007 |
listmethods( |
| 1008 |
canonical_uri => [ str('https://bar.com') ], |
| 1009 |
errors => [ |
| 1010 |
methods(TO_JSON => { |
| 1011 |
keywordLocation => '/allOf/0/oneOf/1', |
| 1012 |
absoluteKeywordLocation => 'https://baz.com#/oneOf/1', |
| 1013 |
error => 'invalid schema type: array', |
| 1014 |
}), |
| 1015 |
], |
| 1016 |
), |
| 1017 |
'error lower down in document does not result in an inner identifier being used as canonical_uri', |
| 1018 |
); |
| 1019 |
}; |
| 1020 |
|
| 1021 |
subtest 'custom metaschema_uri' => sub { |
| 1022 |
my $js = JSON::Schema::Modern->new; |
| 1023 |
$js->add_schema({ |
| 1024 |
'$id' => 'https://my/first/metaschema', |
| 1025 |
'$vocabulary' => { |
| 1026 |
'https://json-schema.org/draft/2020-12/vocab/applicator' => true, |
| 1027 |
'https://json-schema.org/draft/2020-12/vocab/core' => true, |
| 1028 |
# note: no validation! |
| 1029 |
}, |
| 1030 |
}); |
| 1031 |
|
| 1032 |
my $doc = $js->add_document(JSON::Schema::Modern::Document->new( |
| 1033 |
schema => { |
| 1034 |
'$id' => my $id = 'https://my/first/schema/with/custom/metaschema', |
| 1035 |
# note: no $schema keyword! |
| 1036 |
allOf => [ { minimum => 'not even an integer' } ], |
| 1037 |
}, |
| 1038 |
metaschema_uri => 'https://my/first/metaschema', |
| 1039 |
evaluator => $js, # needed in order to find the metaschema |
| 1040 |
)); |
| 1041 |
|
| 1042 |
cmp_result( |
| 1043 |
$js->{_resource_index}{$id}{document}, |
| 1044 |
methods( |
| 1045 |
canonical_uri => str($id), |
| 1046 |
metaschema_uri => str('https://my/first/metaschema'), |
| 1047 |
), |
| 1048 |
'document contains correct values', |
| 1049 |
); |
| 1050 |
|
| 1051 |
cmp_result( |
| 1052 |
$js->{_resource_index}{$id}, |
| 1053 |
{ |
| 1054 |
canonical_uri => str($id), |
| 1055 |
path => '', |
| 1056 |
specification_version => 'draft2020-12', |
| 1057 |
document => $doc, |
| 1058 |
vocabularies => [ |
| 1059 |
map 'JSON::Schema::Modern::Vocabulary::'.$_, |
| 1060 |
qw(Core Applicator), |
| 1061 |
], |
| 1062 |
}, |
| 1063 |
'determined vocabularies to use for this schema', |
| 1064 |
); |
| 1065 |
|
| 1066 |
is_equal( |
| 1067 |
$js->evaluate(1, $id)->TO_JSON, |
| 1068 |
{ valid => true }, |
| 1069 |
'validation succeeds because "minimum" never gets run', |
| 1070 |
); |
| 1071 |
is_equal( |
| 1072 |
$js->evaluate(1, Mojo::URL->new($id)->fragment('/allOf/0'))->TO_JSON, |
| 1073 |
{ valid => true }, |
| 1074 |
'can evaluate at a subschema as well, with the same vocabularies', |
| 1075 |
); |
| 1076 |
|
| 1077 |
is_equal( |
| 1078 |
$doc->validate->TO_JSON, |
| 1079 |
{ |
| 1080 |
valid => false, |
| 1081 |
errors => [ |
| 1082 |
{ |
| 1083 |
instanceLocation => '', |
| 1084 |
keywordLocation => '', |
| 1085 |
error => 'EXCEPTION: unable to find resource "https://my/first/metaschema" (did you forget to provide "evaluator" to ->validate?)', |
| 1086 |
}, |
| 1087 |
], |
| 1088 |
}, |
| 1089 |
'when not providing the original evaluator, the metaschema cannot be found (and a hint is provided)', |
| 1090 |
); |
| 1091 |
|
| 1092 |
is_equal( |
| 1093 |
$doc->validate(evaluator => $js)->TO_JSON, |
| 1094 |
{ valid => true }, |
| 1095 |
'using the proper evaluator, schema validates against its metaschema, and "minimum" is ignored', |
| 1096 |
); |
| 1097 |
|
| 1098 |
memory_cycle_ok($js, 'no leaks in the evaluator object'); |
| 1099 |
}; |
| 1100 |
|
| 1101 |
subtest 'multiple uris used for resolution and identification, and original_uri' => sub { |
| 1102 |
my $js = JSON::Schema::Modern->new; |
| 1103 |
my $doc = $js->add_document( |
| 1104 |
'https://example.com/api/' => JSON::Schema::Modern::Document->new( |
| 1105 |
canonical_uri => 'staging/', |
| 1106 |
schema => { |
| 1107 |
'$id' => 'alpha.json', # https://example.com/staging/alpha.json |
| 1108 |
properties => { |
| 1109 |
foo => { '$id' => 'beta', not => true }, # https://example.com/staging/beta |
| 1110 |
}, |
| 1111 |
not => true, |
| 1112 |
}, |
| 1113 |
evaluator => $js, |
| 1114 |
) |
| 1115 |
); |
| 1116 |
|
| 1117 |
cmp_result( |
| 1118 |
$doc, |
| 1119 |
listmethods( |
| 1120 |
original_uri => [ str('staging/') ], |
| 1121 |
canonical_uri => [ str('staging/alpha.json') ], |
| 1122 |
resource_index => unordered_pairs( |
| 1123 |
'staging/alpha.json' => { |
| 1124 |
path => '', |
| 1125 |
canonical_uri => str('staging/alpha.json'), |
| 1126 |
%dialect, |
| 1127 |
}, |
| 1128 |
'staging/beta' => { |
| 1129 |
canonical_uri => str('staging/beta'), |
| 1130 |
path => '/properties/foo', |
| 1131 |
%dialect, |
| 1132 |
}, |
| 1133 |
), |
| 1134 |
), |
| 1135 |
'document has correct resources, resolved against the provided base uri', |
| 1136 |
); |
| 1137 |
|
| 1138 |
cmp_result( |
| 1139 |
$js->{_resource_index}, |
| 1140 |
my $resource_index = { |
| 1141 |
'https://example.com/api/' => { |
| 1142 |
path => '', |
| 1143 |
canonical_uri => str('https://example.com/api/staging/alpha.json'), |
| 1144 |
document => $doc, |
| 1145 |
%dialect, |
| 1146 |
}, |
| 1147 |
'https://example.com/api/staging/alpha.json' => { |
| 1148 |
path => '', |
| 1149 |
canonical_uri => str('https://example.com/api/staging/alpha.json'), |
| 1150 |
document => $doc, |
| 1151 |
%dialect, |
| 1152 |
}, |
| 1153 |
'https://example.com/api/staging/beta' => { |
| 1154 |
path => '/properties/foo', |
| 1155 |
canonical_uri => str('https://example.com/api/staging/beta'), |
| 1156 |
document => $doc, |
| 1157 |
%dialect, |
| 1158 |
}, |
| 1159 |
}, |
| 1160 |
'evaluator has correct resources, resolved against the provided base uri', |
| 1161 |
); |
| 1162 |
|
| 1163 |
is_equal( |
| 1164 |
$js->evaluate({ foo => 1 }, 'https://example.com/api/staging/alpha.json')->TO_JSON, |
| 1165 |
{ |
| 1166 |
valid => false, |
| 1167 |
errors => [ |
| 1168 |
{ |
| 1169 |
instanceLocation => '', |
| 1170 |
keywordLocation => '/not', |
| 1171 |
absoluteKeywordLocation => 'https://example.com/api/staging/alpha.json#/not', |
| 1172 |
error => 'subschema is true', |
| 1173 |
}, |
| 1174 |
{ |
| 1175 |
instanceLocation => '/foo', |
| 1176 |
keywordLocation => '/properties/foo/not', |
| 1177 |
absoluteKeywordLocation => 'https://example.com/api/staging/beta#/not', |
| 1178 |
error => 'subschema is true', |
| 1179 |
}, |
| 1180 |
{ |
| 1181 |
instanceLocation => '', |
| 1182 |
keywordLocation => '/properties', |
| 1183 |
absoluteKeywordLocation => 'https://example.com/api/staging/alpha.json#/properties', |
| 1184 |
error => 'not all properties are valid', |
| 1185 |
}, |
| 1186 |
], |
| 1187 |
}, |
| 1188 |
'when evaluating the document using the canonical uri, error locations use the canonical uri', |
| 1189 |
); |
| 1190 |
|
| 1191 |
is_equal( |
| 1192 |
$js->evaluate({ foo => 1 }, 'https://example.com/api/')->TO_JSON, |
| 1193 |
{ |
| 1194 |
valid => false, |
| 1195 |
errors => [ |
| 1196 |
{ |
| 1197 |
instanceLocation => '', |
| 1198 |
keywordLocation => '/not', |
| 1199 |
absoluteKeywordLocation => 'https://example.com/api/staging/alpha.json#/not', |
| 1200 |
error => 'subschema is true', |
| 1201 |
}, |
| 1202 |
{ |
| 1203 |
instanceLocation => '/foo', |
| 1204 |
keywordLocation => '/properties/foo/not', |
| 1205 |
absoluteKeywordLocation => 'https://example.com/api/staging/beta#/not', |
| 1206 |
error => 'subschema is true', |
| 1207 |
}, |
| 1208 |
{ |
| 1209 |
instanceLocation => '', |
| 1210 |
keywordLocation => '/properties', |
| 1211 |
absoluteKeywordLocation => 'https://example.com/api/staging/alpha.json#/properties', |
| 1212 |
error => 'not all properties are valid', |
| 1213 |
}, |
| 1214 |
], |
| 1215 |
}, |
| 1216 |
'when evaluating the document using a retrieval uri, error locations still use the canonical uri', |
| 1217 |
); |
| 1218 |
|
| 1219 |
my $doc2 = $js->add_document('file:///usr/local/share/api.json' => $doc); |
| 1220 |
is($doc2, $doc, 'same document is added a second time'); |
| 1221 |
|
| 1222 |
cmp_result( |
| 1223 |
$js->{_resource_index}, |
| 1224 |
{ |
| 1225 |
%$resource_index, # original entries |
| 1226 |
|
| 1227 |
'file:///usr/local/share/api.json' => { |
| 1228 |
path => '', |
| 1229 |
canonical_uri => str('file:///usr/local/share/staging/alpha.json'), |
| 1230 |
document => $doc, |
| 1231 |
%dialect, |
| 1232 |
}, |
| 1233 |
'file:///usr/local/share/staging/alpha.json' => { |
| 1234 |
path => '', |
| 1235 |
canonical_uri => str('file:///usr/local/share/staging/alpha.json'), |
| 1236 |
document => $doc, |
| 1237 |
%dialect, |
| 1238 |
}, |
| 1239 |
'file:///usr/local/share/staging/beta' => { |
| 1240 |
path => '/properties/foo', |
| 1241 |
canonical_uri => str('file:///usr/local/share/staging/beta'), |
| 1242 |
document => $doc, |
| 1243 |
%dialect, |
| 1244 |
}, |
| 1245 |
}, |
| 1246 |
'document resources are added using the new base, which appears in their canonical_uri values', |
| 1247 |
); |
| 1248 |
|
| 1249 |
is_equal( |
| 1250 |
$js->evaluate({ foo => 1 }, 'https://example.com/api/')->TO_JSON, |
| 1251 |
{ |
| 1252 |
valid => false, |
| 1253 |
errors => [ |
| 1254 |
{ |
| 1255 |
instanceLocation => '', |
| 1256 |
keywordLocation => '/not', |
| 1257 |
absoluteKeywordLocation => 'https://example.com/api/staging/alpha.json#/not', |
| 1258 |
error => 'subschema is true', |
| 1259 |
}, |
| 1260 |
{ |
| 1261 |
instanceLocation => '/foo', |
| 1262 |
keywordLocation => '/properties/foo/not', |
| 1263 |
absoluteKeywordLocation => 'https://example.com/api/staging/beta#/not', |
| 1264 |
error => 'subschema is true', |
| 1265 |
}, |
| 1266 |
{ |
| 1267 |
instanceLocation => '', |
| 1268 |
keywordLocation => '/properties', |
| 1269 |
absoluteKeywordLocation => 'https://example.com/api/staging/alpha.json#/properties', |
| 1270 |
error => 'not all properties are valid', |
| 1271 |
}, |
| 1272 |
], |
| 1273 |
}, |
| 1274 |
'when evaluating using the first base uri, error locations are relative to the provided base uri', |
| 1275 |
); |
| 1276 |
|
| 1277 |
# there are multiple resources mapped to the same document+path locations, but we want error |
| 1278 |
# locations to be using the set that we used in the evaluation call. |
| 1279 |
is_equal( |
| 1280 |
$js->evaluate({ foo => 1 }, 'file:///usr/local/share/api.json')->TO_JSON, |
| 1281 |
{ |
| 1282 |
valid => false, |
| 1283 |
errors => [ |
| 1284 |
{ |
| 1285 |
instanceLocation => '', |
| 1286 |
keywordLocation => '/not', |
| 1287 |
absoluteKeywordLocation => 'file:///usr/local/share/staging/alpha.json#/not', |
| 1288 |
error => 'subschema is true', |
| 1289 |
}, |
| 1290 |
{ |
| 1291 |
instanceLocation => '/foo', |
| 1292 |
keywordLocation => '/properties/foo/not', |
| 1293 |
absoluteKeywordLocation => 'file:///usr/local/share/staging/beta#/not', |
| 1294 |
error => 'subschema is true', |
| 1295 |
}, |
| 1296 |
{ |
| 1297 |
instanceLocation => '', |
| 1298 |
keywordLocation => '/properties', |
| 1299 |
absoluteKeywordLocation => 'file:///usr/local/share/staging/alpha.json#/properties', |
| 1300 |
error => 'not all properties are valid', |
| 1301 |
}, |
| 1302 |
], |
| 1303 |
}, |
| 1304 |
'when evaluating using the second base uri, error locations are relative to the original evaluation location', |
| 1305 |
); |
| 1306 |
}; |
| 1307 |
|
| 1308 |
subtest 'bad references' => sub { |
| 1309 |
my $js = JSON::Schema::Modern->new; |
| 1310 |
|
| 1311 |
my $doc1 = JSON::Schema::Modern::Document->new( |
| 1312 |
canonical_uri => 'http://example.com/api1', |
| 1313 |
evaluator => $js, |
| 1314 |
schema => my $schema1 = YAML::PP->new(boolean => 'JSON::PP')->load_string(<<'YAML')); |
| 1315 |
$defs: |
| 1316 |
schema00: { type: string } |
| 1317 |
schema01: { $anchor: my_schema } |
| 1318 |
schema02: |
| 1319 |
$id: subschema1 |
| 1320 |
properties: |
| 1321 |
foo: true |
| 1322 |
bar: false |
| 1323 |
baz: { $anchor: another_subschema } |
| 1324 |
YAML |
| 1325 |
|
| 1326 |
is_equal([ map $_->TO_JSON, $doc1->errors ], [], 'no errors from first document'); |
| 1327 |
$js->add_document($doc1); |
| 1328 |
|
| 1329 |
my $doc2 = JSON::Schema::Modern::Document->new( |
| 1330 |
canonical_uri => 'http://example.com/api2', |
| 1331 |
evaluator => $js, |
| 1332 |
schema => my $schema2 = YAML::PP->new(boolean => 'JSON::PP')->load_string(<<'YAML')); |
| 1333 |
$defs: |
| 1334 |
schema00: { type: string } |
| 1335 |
schema01: { $anchor: my_schema } |
| 1336 |
schema02: { $id: subschema2, properties: { foo: true, bar: false } } |
| 1337 |
schema03: { $dynamicAnchor: foo } |
| 1338 |
|
| 1339 |
# valid references |
| 1340 |
schema10: { $ref: '#/$defs/schema00' } |
| 1341 |
schema11: { $ref: '#my_schema' } |
| 1342 |
schema12: { $ref: http://example.com/api1 } |
| 1343 |
schema13: { $ref: http://example.com/api1#/$defs/schema00 } |
| 1344 |
schema14: { $ref: http://example.com/subschema1#another_subschema } |
| 1345 |
schema15: { $ref: http://example.com/api2#/$defs/schema00 } |
| 1346 |
schema16: { $ref: http://example.com/api2#my_schema } |
| 1347 |
schema17: { $dynamicRef: '#foo' } |
| 1348 |
|
| 1349 |
schema20: { $ref: http://example2.com/api } # unknown remote |
| 1350 |
schema21: { $ref: http://example2.com/api#/$defs/schema00 } # "" |
| 1351 |
schema22: { $ref: http://example2.com/api#my_schema } # "" |
| 1352 |
schema23: { $ref: http://example2.com/api#/$defs/does_not_exist } # "" |
| 1353 |
schema24: { $ref: http://example2.com/api#does_not_exist } # "" |
| 1354 |
|
| 1355 |
# invalid references |
| 1356 |
schema30: { $ref: '#/$defs/does_not_exist' } # local DNE, json pointer |
| 1357 |
schema31: { $ref: '#does_not_exist' } # local DNE, anchor |
| 1358 |
schema32: { $ref: '#/$defs' } # local non-entity |
| 1359 |
schema33: { $ref: http://example.com/subschema1#/$defs/does_not_exist } # remote DNE, json pointer |
| 1360 |
schema34: { $ref: http://example.com/subschema1#does_not_exist } # remote DNE, anchor |
| 1361 |
schema35: { $ref: http://example.com/api1#/$defs/schema02/properties } # remote non-entity |
| 1362 |
schema36: { $ref: http://example.com/subschema2#/$defs/schema00 } # exists at root, not sub$id |
| 1363 |
schema37: { $dynamicRef: '#bar' } # local DNE |
| 1364 |
schema38: { $dynamicRef: http://example.com/api1#foo } # remote DNE |
| 1365 |
YAML |
| 1366 |
|
| 1367 |
is_equal( |
| 1368 |
[ map $_->TO_JSON, $doc2->errors ], |
| 1369 |
my $doc2_errors = [ |
| 1370 |
{ |
| 1371 |
keywordLocation => '/$defs/schema30/$ref', |
| 1372 |
absoluteKeywordLocation => 'http://example.com/api2#/$defs/schema30/$ref', |
| 1373 |
error => 'schema target "http://example.com/api2#/$defs/does_not_exist" is a non-existent location', |
| 1374 |
}, |
| 1375 |
{ |
| 1376 |
keywordLocation => '/$defs/schema31/$ref', |
| 1377 |
absoluteKeywordLocation => 'http://example.com/api2#/$defs/schema31/$ref', |
| 1378 |
error => 'schema target "http://example.com/api2#does_not_exist" is a non-existent location', |
| 1379 |
}, |
| 1380 |
{ |
| 1381 |
keywordLocation => '/$defs/schema32/$ref', |
| 1382 |
absoluteKeywordLocation => 'http://example.com/api2#/$defs/schema32/$ref', |
| 1383 |
error => 'schema target "http://example.com/api2#/$defs" is not a referenceable location', |
| 1384 |
}, |
| 1385 |
{ |
| 1386 |
keywordLocation => '/$defs/schema33/$ref', |
| 1387 |
absoluteKeywordLocation => 'http://example.com/api2#/$defs/schema33/$ref', |
| 1388 |
error => 'schema target "http://example.com/subschema1#/$defs/does_not_exist" is a non-existent location', |
| 1389 |
}, |
| 1390 |
{ |
| 1391 |
keywordLocation => '/$defs/schema34/$ref', |
| 1392 |
absoluteKeywordLocation => 'http://example.com/api2#/$defs/schema34/$ref', |
| 1393 |
error => 'schema target "http://example.com/subschema1#does_not_exist" is a non-existent location', |
| 1394 |
}, |
| 1395 |
{ |
| 1396 |
keywordLocation => '/$defs/schema35/$ref', |
| 1397 |
absoluteKeywordLocation => 'http://example.com/api2#/$defs/schema35/$ref', |
| 1398 |
error => 'schema target "http://example.com/api1#/$defs/schema02/properties" is not a referenceable location', |
| 1399 |
}, |
| 1400 |
{ |
| 1401 |
keywordLocation => '/$defs/schema36/$ref', |
| 1402 |
absoluteKeywordLocation => 'http://example.com/api2#/$defs/schema36/$ref', |
| 1403 |
error => 'schema target "http://example.com/subschema2#/$defs/schema00" is a non-existent location', |
| 1404 |
}, |
| 1405 |
{ |
| 1406 |
keywordLocation => '/$defs/schema37/$dynamicRef', |
| 1407 |
absoluteKeywordLocation => 'http://example.com/api2#/$defs/schema37/$dynamicRef', |
| 1408 |
error => 'schema target "http://example.com/api2#bar" is a non-existent location', |
| 1409 |
}, |
| 1410 |
{ |
| 1411 |
keywordLocation => '/$defs/schema38/$dynamicRef', |
| 1412 |
absoluteKeywordLocation => 'http://example.com/api2#/$defs/schema38/$dynamicRef', |
| 1413 |
error => 'schema target "http://example.com/api1#foo" is a non-existent location', |
| 1414 |
}, |
| 1415 |
], |
| 1416 |
'bad references to local and known remote destinations are identified', |
| 1417 |
); |
| 1418 |
|
| 1419 |
cmp_result( |
| 1420 |
$doc2->_deferred_references, |
| 1421 |
[ |
| 1422 |
# keyword, path_location, abs_target, expected_entity |
| 1423 |
[ '$ref', '/$defs/schema20', str('http://example2.com/api'), 'schema' ], |
| 1424 |
[ '$ref', '/$defs/schema21', str('http://example2.com/api#/$defs/schema00'), 'schema' ], |
| 1425 |
[ '$ref', '/$defs/schema22', str('http://example2.com/api#my_schema'), 'schema' ], |
| 1426 |
[ '$ref', '/$defs/schema23', str('http://example2.com/api#/$defs/does_not_exist'), 'schema' ], |
| 1427 |
[ '$ref', '/$defs/schema24', str('http://example2.com/api#does_not_exist'), 'schema' ], |
| 1428 |
], |
| 1429 |
'unresolved references to unknown documents are set aside', |
| 1430 |
); |
| 1431 |
|
| 1432 |
my $other = JSON::Schema::Modern::Document->new( |
| 1433 |
canonical_uri => 'http://example.com/other', |
| 1434 |
evaluator => $js, |
| 1435 |
schema => $schema2, |
| 1436 |
skip_ref_checks => 1, |
| 1437 |
); |
| 1438 |
|
| 1439 |
is_equal([ map $_->TO_JSON, $other->errors ], [], 'no errors when skipping ref checks'); |
| 1440 |
|
| 1441 |
my $doc3 = JSON::Schema::Modern::Document->new( |
| 1442 |
canonical_uri => 'http://example2.com/api', |
| 1443 |
evaluator => $js, |
| 1444 |
schema => $schema1, |
| 1445 |
); |
| 1446 |
$js->add_document($doc3); |
| 1447 |
|
| 1448 |
is_equal([ map $_->TO_JSON, $other->errors ], [], 'no errors when checking this document'); |
| 1449 |
|
| 1450 |
$doc2->verify_references($js); |
| 1451 |
|
| 1452 |
is_equal($doc2->_deferred_references, [], 'deferred references are now all undeferred'); |
| 1453 |
|
| 1454 |
is_equal( |
| 1455 |
[ map $_->TO_JSON, $doc2->errors ], |
| 1456 |
[ |
| 1457 |
$doc2_errors->@*, |
| 1458 |
{ |
| 1459 |
keywordLocation => '/$defs/schema23/$ref', |
| 1460 |
absoluteKeywordLocation => 'http://example.com/api2#/$defs/schema23/$ref', |
| 1461 |
error => 'schema target "http://example2.com/api#/$defs/does_not_exist" is a non-existent location', |
| 1462 |
}, |
| 1463 |
{ |
| 1464 |
keywordLocation => '/$defs/schema24/$ref', |
| 1465 |
absoluteKeywordLocation => 'http://example.com/api2#/$defs/schema24/$ref', |
| 1466 |
error => 'schema target "http://example2.com/api#does_not_exist" is a non-existent location', |
| 1467 |
}, |
| 1468 |
], |
| 1469 |
'new errors are found from the previously-deferred references', |
| 1470 |
); |
| 1471 |
}; |
| 1472 |
|
| 1473 |
done_testing; |