| 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 lib 't/lib'; |
| 15 |
use Helper; |
| 16 |
use JSON::Schema::Modern::Utilities 'canonical_uri'; |
| 17 |
|
| 18 |
subtest 'traversal with callbacks' => sub { |
| 19 |
my $schema = { |
| 20 |
'$id' => 'https://foo.com', |
| 21 |
'$defs' => { |
| 22 |
foo => { |
| 23 |
'$id' => 'recursive_subschema', |
| 24 |
type => [ 'integer', 'object' ], |
| 25 |
additionalProperties => { '$ref' => 'recursive_subschema' }, |
| 26 |
}, |
| 27 |
bar => { |
| 28 |
properties => { |
| 29 |
description => { |
| 30 |
'$ref' => '#/$defs/foo', |
| 31 |
const => { '$ref' => 'this is not a real ref' }, |
| 32 |
}, |
| 33 |
}, |
| 34 |
}, |
| 35 |
}, |
| 36 |
if => 1, # bad subschema |
| 37 |
allOf => [ |
| 38 |
{}, |
| 39 |
{ '$ref' => '#/$defs/foo' }, |
| 40 |
{ '$ref' => '#/$defs/bar' }, |
| 41 |
], |
| 42 |
}; |
| 43 |
|
| 44 |
my %refs; |
| 45 |
my $if_callback_called; |
| 46 |
my $js = JSON::Schema::Modern->new; |
| 47 |
my $state = $js->traverse($schema, { callbacks => { |
| 48 |
'$ref' => sub ($schema, $state) { |
| 49 |
my $canonical_uri = canonical_uri($state); |
| 50 |
my $ref_uri = Mojo::URL->new($schema->{'$ref'}); |
| 51 |
$ref_uri = $ref_uri->to_abs($canonical_uri) if not $ref_uri->is_abs; |
| 52 |
$refs{$state->{traversed_keyword_path}.$state->{keyword_path}} = $ref_uri->to_string; |
| 53 |
}, |
| 54 |
if => sub { $if_callback_called = 1; }, |
| 55 |
}}); |
| 56 |
|
| 57 |
is_equal( |
| 58 |
[ map $_->TO_JSON, $state->{errors}->@* ], |
| 59 |
[ |
| 60 |
{ |
| 61 |
keywordLocation => '/if', |
| 62 |
absoluteKeywordLocation => 'https://foo.com#/if', |
| 63 |
error => 'invalid schema type: integer', |
| 64 |
}, |
| 65 |
], |
| 66 |
'errors encountered during traversal are returned', |
| 67 |
); |
| 68 |
|
| 69 |
ok(!$if_callback_called, 'callback for erroneous keyword was not called'); |
| 70 |
|
| 71 |
is_equal( |
| 72 |
\%refs, |
| 73 |
{ |
| 74 |
'/$defs/foo/additionalProperties' => 'https://foo.com/recursive_subschema', |
| 75 |
'/$defs/bar/properties/description' => 'https://foo.com#/$defs/foo', |
| 76 |
# no entry for 'if' -- callbacks are not called for keywords with errors |
| 77 |
'/allOf/1' => 'https://foo.com#/$defs/foo', |
| 78 |
'/allOf/2' => 'https://foo.com#/$defs/bar', |
| 79 |
}, |
| 80 |
'extracted all the real $refs out of the schema, with locations and canonical targets', |
| 81 |
); |
| 82 |
|
| 83 |
cmp_result( |
| 84 |
$state->{subschemas}, |
| 85 |
bag( |
| 86 |
'', |
| 87 |
'/$defs/bar', |
| 88 |
'/$defs/bar/properties/description', |
| 89 |
'/$defs/foo', |
| 90 |
'/$defs/foo/additionalProperties', |
| 91 |
'/allOf/0', |
| 92 |
'/allOf/1', |
| 93 |
'/allOf/2', |
| 94 |
'/if', |
| 95 |
), |
| 96 |
'identified all subschemas', |
| 97 |
); |
| 98 |
}; |
| 99 |
|
| 100 |
subtest 'errors when parsing $schema keyword' => sub { |
| 101 |
my $js = JSON::Schema::Modern->new; |
| 102 |
|
| 103 |
my $state = $js->traverse({ '$schema' => true }); |
| 104 |
is_equal( |
| 105 |
[ map $_->TO_JSON, $state->{errors}->@* ], |
| 106 |
[ |
| 107 |
{ |
| 108 |
keywordLocation => '/$schema', |
| 109 |
error => '$schema value is not a string', |
| 110 |
}, |
| 111 |
], |
| 112 |
'$schema is not a string', |
| 113 |
); |
| 114 |
|
| 115 |
$state = $js->traverse({ '$schema' => 'whargarbl' }); |
| 116 |
is_equal( |
| 117 |
[ map $_->TO_JSON, $state->{errors}->@* ], |
| 118 |
[ |
| 119 |
{ |
| 120 |
keywordLocation => '/$schema', |
| 121 |
error => '"whargarbl" is not a valid URI', |
| 122 |
}, |
| 123 |
], |
| 124 |
'$schema is not a URI', |
| 125 |
); |
| 126 |
}; |
| 127 |
|
| 128 |
subtest 'default metaschema' => sub { |
| 129 |
my $js = JSON::Schema::Modern->new; |
| 130 |
my $state = $js->traverse( |
| 131 |
{ |
| 132 |
'$defs' => { |
| 133 |
foo => { |
| 134 |
properties => 'not an object', |
| 135 |
}, |
| 136 |
}, |
| 137 |
}, |
| 138 |
); |
| 139 |
|
| 140 |
cmp_result( |
| 141 |
$state, |
| 142 |
superhashof({ |
| 143 |
specification_version => 'draft2020-12', |
| 144 |
metaschema_uri => str(JSON::Schema::Modern::METASCHEMA_URIS->{'draft2020-12'}), |
| 145 |
initial_schema_uri => str(''), |
| 146 |
vocabularies => [ |
| 147 |
map 'JSON::Schema::Modern::Vocabulary::'.$_, |
| 148 |
qw(Core Validation FormatAnnotation Applicator Content MetaData Unevaluated), |
| 149 |
], |
| 150 |
}), |
| 151 |
'dialect is properly determined', |
| 152 |
); |
| 153 |
|
| 154 |
is_equal( |
| 155 |
[ map $_->TO_JSON, $state->{errors}->@* ], |
| 156 |
[ |
| 157 |
{ |
| 158 |
keywordLocation => '/$defs/foo/properties', |
| 159 |
error => 'properties value is not an object', |
| 160 |
}, |
| 161 |
], |
| 162 |
'error within $defs is found, showing both Core and Applicator vocabularies are used', |
| 163 |
); |
| 164 |
}; |
| 165 |
|
| 166 |
subtest 'traversing a dialect with different core keywords' => sub { |
| 167 |
my $js = JSON::Schema::Modern->new; |
| 168 |
|
| 169 |
my $state = $js->traverse( |
| 170 |
{ |
| 171 |
'$id' => 'http://localhost:1234/root', |
| 172 |
'$schema' => 'http://json-schema.org/draft-07/schema#', |
| 173 |
definitions => { |
| 174 |
alpha => 1, |
| 175 |
}, |
| 176 |
}, |
| 177 |
); |
| 178 |
is_equal( |
| 179 |
[ map $_->TO_JSON, $state->{errors}->@* ], |
| 180 |
[ |
| 181 |
{ |
| 182 |
keywordLocation => '/definitions/alpha', |
| 183 |
absoluteKeywordLocation => 'http://localhost:1234/root#/definitions/alpha', |
| 184 |
error => 'invalid schema type: integer', |
| 185 |
}, |
| 186 |
], |
| 187 |
'dialect changes at root, with $id - dialect is switched in time to get a new keyword list for the core vocabulary', |
| 188 |
); |
| 189 |
|
| 190 |
cmp_result( |
| 191 |
$state, |
| 192 |
superhashof({ |
| 193 |
metaschema_uri => 'http://json-schema.org/draft-07/schema', |
| 194 |
specification_version => 'draft7', |
| 195 |
vocabularies => [ map 'JSON::Schema::Modern::Vocabulary::'.$_, |
| 196 |
qw(Core Validation FormatAnnotation Applicator Content MetaData) ], |
| 197 |
}), |
| 198 |
'other $state information is correct', |
| 199 |
); |
| 200 |
|
| 201 |
$state = $js->traverse( |
| 202 |
{ |
| 203 |
'$id' => '#hello', |
| 204 |
'$schema' => 'http://json-schema.org/draft-07/schema#', |
| 205 |
definitions => { |
| 206 |
bloop => { |
| 207 |
'$id' => '/bloop', |
| 208 |
type => 'object', |
| 209 |
}, |
| 210 |
}, |
| 211 |
}, |
| 212 |
); |
| 213 |
|
| 214 |
is_equal($state->{errors}, [], 'no errors when parsing this schema'); |
| 215 |
cmp_result( |
| 216 |
$state, |
| 217 |
superhashof({ |
| 218 |
identifiers => { |
| 219 |
'' => { |
| 220 |
path => '', |
| 221 |
canonical_uri => str(''), |
| 222 |
specification_version => 'draft7', |
| 223 |
vocabularies => [ |
| 224 |
map 'JSON::Schema::Modern::Vocabulary::'.$_, |
| 225 |
qw(Core Validation FormatAnnotation Applicator Content MetaData), |
| 226 |
], |
| 227 |
anchors => { |
| 228 |
hello => { |
| 229 |
path => '', |
| 230 |
canonical_uri => str(''), |
| 231 |
}, |
| 232 |
}, |
| 233 |
}, |
| 234 |
'/bloop' => { |
| 235 |
path => '/definitions/bloop', |
| 236 |
canonical_uri => str('/bloop'), |
| 237 |
specification_version => 'draft7', |
| 238 |
vocabularies => [ |
| 239 |
map 'JSON::Schema::Modern::Vocabulary::'.$_, |
| 240 |
qw(Core Validation FormatAnnotation Applicator Content MetaData), |
| 241 |
], |
| 242 |
}, |
| 243 |
}, |
| 244 |
metaschema_uri => 'http://json-schema.org/draft-07/schema', |
| 245 |
specification_version => 'draft7', |
| 246 |
vocabularies => [ map 'JSON::Schema::Modern::Vocabulary::'.$_, |
| 247 |
qw(Core Validation FormatAnnotation Applicator Content MetaData) ], |
| 248 |
}), |
| 249 |
'switched dialect in time to extract all identifiers, from root and definition', |
| 250 |
); |
| 251 |
|
| 252 |
$state = $js->traverse( |
| 253 |
{ |
| 254 |
'$schema' => 'http://json-schema.org/draft-07/schema#', |
| 255 |
definitions => { |
| 256 |
alpha => 1, |
| 257 |
}, |
| 258 |
}, |
| 259 |
); |
| 260 |
is_equal( |
| 261 |
[ map $_->TO_JSON, $state->{errors}->@* ], |
| 262 |
[ |
| 263 |
{ |
| 264 |
keywordLocation => '/definitions/alpha', |
| 265 |
error => 'invalid schema type: integer', |
| 266 |
}, |
| 267 |
], |
| 268 |
'dialect changes at root, no $id - dialect is switched in time to get a new keyword list for the core vocabulary', |
| 269 |
); |
| 270 |
|
| 271 |
$state = $js->traverse( |
| 272 |
{ |
| 273 |
'$defs' => { |
| 274 |
alpha => { |
| 275 |
'$id' => 'http://localhost:1234/inner', |
| 276 |
'$schema' => 'http://json-schema.org/draft-07/schema#', |
| 277 |
definitions => { |
| 278 |
alpha => 1, |
| 279 |
}, |
| 280 |
}, |
| 281 |
}, |
| 282 |
}, |
| 283 |
); |
| 284 |
is_equal( |
| 285 |
[ map $_->TO_JSON, $state->{errors}->@* ], |
| 286 |
[ |
| 287 |
{ |
| 288 |
keywordLocation => '/$defs/alpha/definitions/alpha', |
| 289 |
absoluteKeywordLocation => 'http://localhost:1234/inner#/definitions/alpha', |
| 290 |
error => 'invalid schema type: integer', |
| 291 |
}, |
| 292 |
], |
| 293 |
'dialect changes below root - dialect is switched in time to get a new keyword list for the core vocabulary', |
| 294 |
); |
| 295 |
}; |
| 296 |
|
| 297 |
subtest '$schema without an $id, below the root' => sub { |
| 298 |
my $js = JSON::Schema::Modern->new; |
| 299 |
|
| 300 |
my $state = $js->traverse( |
| 301 |
{ |
| 302 |
'$defs' => { |
| 303 |
alpha => { |
| 304 |
'$schema' => 'https://json-schema.org/draft/2019-09/schema', |
| 305 |
minimum => 2, |
| 306 |
}, |
| 307 |
}, |
| 308 |
}, |
| 309 |
); |
| 310 |
is_equal( |
| 311 |
[ map $_->TO_JSON, $state->{errors}->@* ], |
| 312 |
[ |
| 313 |
{ |
| 314 |
keywordLocation => '/$defs/alpha/$schema', |
| 315 |
error => '$schema can only appear at the schema resource root', |
| 316 |
}, |
| 317 |
], |
| 318 |
'$schema cannot exist without an $id, or at the root', |
| 319 |
); |
| 320 |
}; |
| 321 |
|
| 322 |
subtest 'duplicate identifiers' => sub { |
| 323 |
my $js = JSON::Schema::Modern->new; |
| 324 |
my $state = $js->traverse({ |
| 325 |
'$id' => 'https://base.com', |
| 326 |
allOf => [ |
| 327 |
{ '$id' => 'https://foo.com' }, |
| 328 |
{ '$id' => 'https://foo.com' }, |
| 329 |
], |
| 330 |
}); |
| 331 |
|
| 332 |
is_equal( |
| 333 |
[ map $_->TO_JSON, $state->{errors}->@* ], |
| 334 |
[ |
| 335 |
{ |
| 336 |
keywordLocation => '/allOf/1/$id', |
| 337 |
absoluteKeywordLocation => 'https://base.com#/allOf/1/$id', |
| 338 |
error => 'duplicate canonical uri "https://foo.com" found (original at path "/allOf/0")', |
| 339 |
}, |
| 340 |
], |
| 341 |
'detected colliding $ids within a single schema', |
| 342 |
); |
| 343 |
|
| 344 |
$state = $js->traverse({ |
| 345 |
'$id' => 'https://base.com', |
| 346 |
allOf => [ |
| 347 |
{ '$id' => 'dir1', '$anchor' => 'foo' }, |
| 348 |
{ '$id' => 'dir2', '$anchor' => 'foo' }, |
| 349 |
], |
| 350 |
}); |
| 351 |
is_equal( |
| 352 |
[ map $_->TO_JSON, $state->{errors}->@* ], |
| 353 |
[], |
| 354 |
'two anchors with different base uris are acceptable', |
| 355 |
); |
| 356 |
|
| 357 |
$state = $js->traverse({ |
| 358 |
'$id' => 'https://base.com', |
| 359 |
allOf => [ |
| 360 |
{ '$anchor' => 'foo' }, |
| 361 |
{ '$anchor' => 'foo' }, |
| 362 |
], |
| 363 |
}); |
| 364 |
is_equal( |
| 365 |
[ map $_->TO_JSON, $state->{errors}->@* ], |
| 366 |
[ |
| 367 |
{ |
| 368 |
keywordLocation => '/allOf/1/$anchor', |
| 369 |
absoluteKeywordLocation => 'https://base.com#/allOf/1/$anchor', |
| 370 |
error => 'duplicate anchor uri "https://base.com#foo" found (original at path "/allOf/0")', |
| 371 |
}, |
| 372 |
], |
| 373 |
'detected colliding $anchors within a single schema', |
| 374 |
); |
| 375 |
}; |
| 376 |
|
| 377 |
subtest '$anchor without $id' => sub { |
| 378 |
my $js = JSON::Schema::Modern->new; |
| 379 |
|
| 380 |
my $state = $js->traverse({ |
| 381 |
'$anchor' => 'root_anchor', |
| 382 |
}); |
| 383 |
cmp_result( |
| 384 |
$state->{identifiers}, |
| 385 |
{ |
| 386 |
'' => { |
| 387 |
path => '', |
| 388 |
canonical_uri => str(''), |
| 389 |
specification_version => 'draft2020-12', |
| 390 |
vocabularies => ignore, |
| 391 |
anchors => { |
| 392 |
root_anchor => { |
| 393 |
path => '', |
| 394 |
canonical_uri => str(''), |
| 395 |
}, |
| 396 |
}, |
| 397 |
}, |
| 398 |
}, |
| 399 |
'found anchor at root, without an $id to pre-populate the identifiers hash', |
| 400 |
); |
| 401 |
|
| 402 |
$state = $js->traverse({ |
| 403 |
properties => { |
| 404 |
foo => { |
| 405 |
'$anchor' => 'foo_anchor', |
| 406 |
}, |
| 407 |
}, |
| 408 |
}); |
| 409 |
cmp_result( |
| 410 |
$state->{identifiers}, |
| 411 |
{ |
| 412 |
'' => { |
| 413 |
path => '', |
| 414 |
canonical_uri => str(''), |
| 415 |
specification_version => 'draft2020-12', |
| 416 |
vocabularies => ignore, |
| 417 |
anchors => { |
| 418 |
foo_anchor => { |
| 419 |
path => '/properties/foo', |
| 420 |
canonical_uri => str('#/properties/foo'), |
| 421 |
}, |
| 422 |
}, |
| 423 |
}, |
| 424 |
}, |
| 425 |
'found anchor within schema, without an $id to pre-populate the identifiers hash', |
| 426 |
); |
| 427 |
}; |
| 428 |
|
| 429 |
subtest 'traverse with overridden specification_version' => sub { |
| 430 |
my $js = JSON::Schema::Modern->new(specification_version => 'draft7'); |
| 431 |
|
| 432 |
my $state = $js->traverse({}); |
| 433 |
cmp_result( |
| 434 |
$state, |
| 435 |
superhashof({ |
| 436 |
errors => [], |
| 437 |
metaschema_uri => 'http://json-schema.org/draft-07/schema', |
| 438 |
specification_version => 'draft7', |
| 439 |
vocabularies => [ map 'JSON::Schema::Modern::Vocabulary::'.$_, |
| 440 |
qw(Core Validation FormatAnnotation Applicator Content MetaData) ], |
| 441 |
}), |
| 442 |
'$state is correct with no $schema keyword, no overrides' |
| 443 |
); |
| 444 |
|
| 445 |
$state = $js->traverse({ '$schema' => 'https://json-schema.org/draft/2020-12/schema'}); |
| 446 |
cmp_result( |
| 447 |
$state, |
| 448 |
superhashof({ |
| 449 |
errors => [], |
| 450 |
metaschema_uri => 'https://json-schema.org/draft/2020-12/schema', |
| 451 |
specification_version => 'draft2020-12', |
| 452 |
vocabularies => [ map 'JSON::Schema::Modern::Vocabulary::'.$_, |
| 453 |
qw(Core Validation FormatAnnotation Applicator Content MetaData Unevaluated) ], |
| 454 |
}), |
| 455 |
'$state is correct with a $schema keyword, no overrides' |
| 456 |
); |
| 457 |
|
| 458 |
$state = $js->traverse({}, { specification_version => 'draft2019-09' }); |
| 459 |
cmp_result( |
| 460 |
$state, |
| 461 |
superhashof({ |
| 462 |
errors => [], |
| 463 |
metaschema_uri => 'https://json-schema.org/draft/2019-09/schema', |
| 464 |
specification_version => 'draft2019-09', |
| 465 |
vocabularies => [ map 'JSON::Schema::Modern::Vocabulary::'.$_, |
| 466 |
qw(Core Validation FormatAnnotation Applicator Content MetaData) ], |
| 467 |
}), |
| 468 |
'$state is correct with no $schema keyword, and an overridden specification_version' |
| 469 |
); |
| 470 |
|
| 471 |
$state = $js->traverse( |
| 472 |
{ '$schema' => 'http://json-schema.org/draft-04/schema#' }, |
| 473 |
{ specification_version => 'draft2020-12' }); |
| 474 |
cmp_result( |
| 475 |
$state, |
| 476 |
superhashof({ |
| 477 |
errors => [], |
| 478 |
metaschema_uri => 'http://json-schema.org/draft-04/schema', |
| 479 |
specification_version => 'draft4', |
| 480 |
vocabularies => [ map 'JSON::Schema::Modern::Vocabulary::'.$_, |
| 481 |
qw(Core Validation FormatAnnotation Applicator MetaData) ], |
| 482 |
}), |
| 483 |
'$state is correct with a $schema keyword, and an overridden specification_version' |
| 484 |
); |
| 485 |
}; |
| 486 |
|
| 487 |
subtest 'traverse with overridden metaschema_uri' => sub { |
| 488 |
my $js = JSON::Schema::Modern->new; |
| 489 |
|
| 490 |
my $state = $js->traverse({}, { metaschema_uri => 'https://unknown/metaschema' }); |
| 491 |
|
| 492 |
is_equal( |
| 493 |
[ map $_->TO_JSON, $state->{errors}->@* ], |
| 494 |
my $errors = [ |
| 495 |
{ |
| 496 |
keywordLocation => '', |
| 497 |
error => 'EXCEPTION: unable to find resource "https://unknown/metaschema"', |
| 498 |
}, |
| 499 |
], |
| 500 |
'metaschema_uri is not a known uri', |
| 501 |
); |
| 502 |
|
| 503 |
|
| 504 |
$js->add_schema({ |
| 505 |
'$id' => 'https://metaschema/with/wrong/spec', |
| 506 |
'$vocabulary' => { |
| 507 |
'https://unknown' => true, |
| 508 |
'https://unknown2' => false, |
| 509 |
}, |
| 510 |
}); |
| 511 |
|
| 512 |
|
| 513 |
$state = $js->traverse(true, { metaschema_uri => 'https://metaschema/with/wrong/spec' }); |
| 514 |
|
| 515 |
is_equal( |
| 516 |
[ map $_->TO_JSON, $state->{errors}->@* ], |
| 517 |
$errors = [ |
| 518 |
{ |
| 519 |
keywordLocation => jsonp(qw(/$vocabulary https://unknown)), |
| 520 |
absoluteKeywordLocation => 'https://metaschema/with/wrong/spec#'.jsonp(qw(/$vocabulary https://unknown)), |
| 521 |
error => '"https://unknown" is not a known vocabulary', |
| 522 |
}, |
| 523 |
{ |
| 524 |
keywordLocation => '/$vocabulary', |
| 525 |
absoluteKeywordLocation => 'https://metaschema/with/wrong/spec#/$vocabulary', |
| 526 |
error => 'the first vocabulary (by evaluation_order) must be Core', |
| 527 |
}, |
| 528 |
{ |
| 529 |
keywordLocation => '', |
| 530 |
error => '"https://metaschema/with/wrong/spec" is not a valid metaschema', |
| 531 |
}, |
| 532 |
], |
| 533 |
'boolean schema: metaschema_uri is overridden with a bad schema: same errors are returned', |
| 534 |
); |
| 535 |
|
| 536 |
|
| 537 |
$state = $js->traverse( |
| 538 |
{ '$id' => 'https://my/bad/schema' }, |
| 539 |
{ metaschema_uri => 'https://metaschema/with/wrong/spec' }); |
| 540 |
|
| 541 |
is_equal( |
| 542 |
[ map $_->TO_JSON, $state->{errors}->@* ], |
| 543 |
$errors, |
| 544 |
'object schema: metaschema_uri is overridden with a bad schema: same errors are returned', |
| 545 |
); |
| 546 |
|
| 547 |
|
| 548 |
# simulation of parsing a schema with a custom keyword that sets the metaschema uri |
| 549 |
# (see OpenAPI's jsonSchemaDialect keyword) |
| 550 |
$state = $js->traverse( |
| 551 |
true, |
| 552 |
{ |
| 553 |
metaschema_uri => 'https://metaschema/with/wrong/spec', |
| 554 |
initial_schema_uri => 'https://my-poor-schema/foo.json#/$my_dialect_is', |
| 555 |
traversed_keyword_path => '/$ref/$ref/some_keyword/$ref/$my_dialect_is', |
| 556 |
}); |
| 557 |
|
| 558 |
is_equal( |
| 559 |
[ map $_->TO_JSON, $state->{errors}->@* ], |
| 560 |
[ |
| 561 |
{ |
| 562 |
$errors->[0]->%*, |
| 563 |
keywordLocation => '/$ref/$ref/some_keyword/$ref/$my_dialect_is'.$errors->[0]{keywordLocation}, |
| 564 |
}, |
| 565 |
{ |
| 566 |
$errors->[1]->%*, |
| 567 |
keywordLocation => '/$ref/$ref/some_keyword/$ref/$my_dialect_is'.$errors->[1]{keywordLocation}, |
| 568 |
}, |
| 569 |
{ |
| 570 |
$errors->[2]->%*, |
| 571 |
keywordLocation => '/$ref/$ref/some_keyword/$ref/$my_dialect_is'.$errors->[2]{keywordLocation}, |
| 572 |
absoluteKeywordLocation => 'https://my-poor-schema/foo.json#/$my_dialect_is', |
| 573 |
}, |
| 574 |
], |
| 575 |
'metaschema_uri is overridden with a bad schema and there is a traversal path: errors contain the right locations', |
| 576 |
); |
| 577 |
|
| 578 |
|
| 579 |
$js->add_schema({ |
| 580 |
'$id' => 'https://my/first/metaschema', |
| 581 |
'$schema' => 'https://json-schema.org/draft/2019-09/schema', |
| 582 |
'$vocabulary' => { |
| 583 |
'https://json-schema.org/draft/2019-09/vocab/applicator' => true, |
| 584 |
'https://json-schema.org/draft/2019-09/vocab/core' => true, |
| 585 |
# note: no validation! |
| 586 |
}, |
| 587 |
}); |
| 588 |
|
| 589 |
$state = $js->traverse( |
| 590 |
{ |
| 591 |
'$id' => my $id = 'https://my/first/schema/with/custom/metaschema', |
| 592 |
# note: no $schema keyword! |
| 593 |
}, |
| 594 |
{ metaschema_uri => 'https://my/first/metaschema' }, |
| 595 |
); |
| 596 |
|
| 597 |
cmp_result( |
| 598 |
$state, |
| 599 |
superhashof({ |
| 600 |
identifiers => { |
| 601 |
$id => { |
| 602 |
canonical_uri => str($id), |
| 603 |
path => '', |
| 604 |
specification_version => 'draft2019-09', |
| 605 |
vocabularies => [ map 'JSON::Schema::Modern::Vocabulary::'.$_, qw(Core Applicator) ], |
| 606 |
}, |
| 607 |
}, |
| 608 |
metaschema_uri => 'https://my/first/metaschema', |
| 609 |
specification_version => 'draft2019-09', |
| 610 |
vocabularies => [ map 'JSON::Schema::Modern::Vocabulary::'.$_, qw(Core Applicator) ], |
| 611 |
}), |
| 612 |
'determined specification version and vocabularies to use for this schema from override', |
| 613 |
); |
| 614 |
|
| 615 |
|
| 616 |
$state = $js->traverse( |
| 617 |
{ |
| 618 |
'$id' => $id = 'https://my/second/schema/with/custom/metaschema', |
| 619 |
'$schema' => 'http://json-schema.org/draft-07/schema', |
| 620 |
}, |
| 621 |
{ metaschema_uri => 'https://my/first/metaschema' }, |
| 622 |
); |
| 623 |
|
| 624 |
cmp_result( |
| 625 |
my $state_copy = $state, |
| 626 |
superhashof({ |
| 627 |
identifiers => { |
| 628 |
$id => { |
| 629 |
canonical_uri => str($id), |
| 630 |
path => '', |
| 631 |
specification_version => 'draft7', |
| 632 |
vocabularies => [ map 'JSON::Schema::Modern::Vocabulary::'.$_, |
| 633 |
qw(Core Validation FormatAnnotation Applicator Content MetaData) ], |
| 634 |
}, |
| 635 |
}, |
| 636 |
metaschema_uri => 'http://json-schema.org/draft-07/schema', |
| 637 |
specification_version => 'draft7', |
| 638 |
vocabularies => [ map 'JSON::Schema::Modern::Vocabulary::'.$_, |
| 639 |
qw(Core Validation FormatAnnotation Applicator Content MetaData) ], |
| 640 |
}), |
| 641 |
'determined specification version and vocabularies to use for this schema from $schema keyword', |
| 642 |
); |
| 643 |
|
| 644 |
|
| 645 |
$state = $js->traverse( |
| 646 |
{ |
| 647 |
'$id' => my $third_id = 'https://my/third/schema/with/custom/metaschema', |
| 648 |
'$schema' => 'http://json-schema.org/draft-07/schema', |
| 649 |
}, |
| 650 |
{ metaschema_uri => 'https://metaschema/with/wrong/spec' }, |
| 651 |
); |
| 652 |
|
| 653 |
cmp_result( |
| 654 |
$state, |
| 655 |
superhashof({ |
| 656 |
identifiers => { |
| 657 |
$third_id => { $state_copy->{identifiers}{$id}->%*, canonical_uri => str($third_id) }, |
| 658 |
}, |
| 659 |
$state_copy->%{qw(metaschema_uri specification_version vocabularies)}, |
| 660 |
}), |
| 661 |
'when $schema keyword is used, custom metaschema_uri is never parsed, so there are no errors', |
| 662 |
); |
| 663 |
}; |
| 664 |
|
| 665 |
subtest 'start traversing below the document root' => sub { |
| 666 |
my $js = JSON::Schema::Modern->new; |
| 667 |
|
| 668 |
# Remember: at this point the $document object may exist, but its constructor hasn't finished yet |
| 669 |
# (until traverse() returns), and we have no idea where in the document we are, and the evaluator |
| 670 |
# isn't provided the document object yet. |
| 671 |
# The document data might not even be a JSON Schema. |
| 672 |
# Let's say the document actually looks like: |
| 673 |
# { |
| 674 |
# $self => 'my_document.yaml', |
| 675 |
# openapi => '3.1.1', |
| 676 |
# components => { |
| 677 |
# schemas => { |
| 678 |
# alpha => { |
| 679 |
# *** SUBSCHEMA BELOW *** |
| 680 |
# }, |
| 681 |
# }, |
| 682 |
# }, |
| 683 |
# } |
| 684 |
my $state = $js->traverse( |
| 685 |
{ |
| 686 |
properties => { |
| 687 |
myprop => { |
| 688 |
allOf => [ |
| 689 |
{ |
| 690 |
'$id' => 'inner_document', |
| 691 |
properties => { |
| 692 |
foo => 'not a valid schema', |
| 693 |
}, |
| 694 |
}, |
| 695 |
], |
| 696 |
}, |
| 697 |
}, |
| 698 |
type => 'not a valid type', |
| 699 |
}, |
| 700 |
{ |
| 701 |
initial_schema_uri => 'dir/my_subdocument#/subid', |
| 702 |
traversed_keyword_path => '/components/alpha/subid', |
| 703 |
}, |
| 704 |
); |
| 705 |
|
| 706 |
is_equal( |
| 707 |
[ map $_->TO_JSON, $state->{errors}->@* ], |
| 708 |
[ |
| 709 |
{ |
| 710 |
keywordLocation => '/components/alpha/subid/type', |
| 711 |
absoluteKeywordLocation => 'dir/my_subdocument#/subid/type', |
| 712 |
error => 'unrecognized type "not a valid type"', |
| 713 |
}, |
| 714 |
{ |
| 715 |
keywordLocation => '/components/alpha/subid/properties/myprop/allOf/0/properties/foo', |
| 716 |
absoluteKeywordLocation => 'dir/inner_document#/properties/foo', |
| 717 |
error => 'invalid schema type: string', |
| 718 |
}, |
| 719 |
], |
| 720 |
'identified the overridden location of all errors during traverse', |
| 721 |
); |
| 722 |
|
| 723 |
|
| 724 |
$state = $js->traverse( |
| 725 |
{ |
| 726 |
properties => { |
| 727 |
myprop => { |
| 728 |
allOf => [ |
| 729 |
{ |
| 730 |
'$id' => 'inner_document', # resolves to dir/inner_document |
| 731 |
properties => { foo => true }, |
| 732 |
}, |
| 733 |
], |
| 734 |
}, |
| 735 |
}, |
| 736 |
}, |
| 737 |
{ |
| 738 |
initial_schema_uri => 'dir/my_subdocument#/subid', |
| 739 |
traversed_keyword_path => '/components/alpha/subid', |
| 740 |
}, |
| 741 |
); |
| 742 |
|
| 743 |
cmp_result( |
| 744 |
$state->{identifiers}, |
| 745 |
{ |
| 746 |
'dir/inner_document' => { |
| 747 |
canonical_uri => str('dir/inner_document'), |
| 748 |
path => '/components/alpha/subid/properties/myprop/allOf/0', |
| 749 |
specification_version => 'draft2020-12', |
| 750 |
vocabularies => ignore, |
| 751 |
}, |
| 752 |
}, |
| 753 |
'identifiers are correctly extracted when traversing below the document root', |
| 754 |
); |
| 755 |
|
| 756 |
$state = $js->traverse( |
| 757 |
{ |
| 758 |
# the path at this position is /components/alpha/subid |
| 759 |
properties => { |
| 760 |
alpha => { |
| 761 |
'$id' => 'alpha_id', # resolves to dir/alpha_id |
| 762 |
properties => { |
| 763 |
alpha_one => { |
| 764 |
'$id' => 'alpha_one_id', # resolves to dir/alpha_one_id |
| 765 |
}, |
| 766 |
alpha_two => { |
| 767 |
'$anchor' => 'alpha_two_anchor', # resolves to dir/alpha_id#alpha_two_anchor |
| 768 |
}, |
| 769 |
alpha_three => { |
| 770 |
'$anchor' => 'alpha_three_anchor', # resolves to dir/alpha_id#alpha_three_anchor |
| 771 |
}, |
| 772 |
}, |
| 773 |
}, |
| 774 |
beta => { |
| 775 |
# produces anchor definition: |
| 776 |
# base uri is dir/my_subdocument, |
| 777 |
# canonical uri is dir/my_subdocument#/subid/properties/beta |
| 778 |
# path is /components/alpha/subid/properties/beta |
| 779 |
'$anchor' => 'beta_anchor', # resolves to dir/my_subdocument#beta_anchor |
| 780 |
}, |
| 781 |
}, |
| 782 |
}, |
| 783 |
{ |
| 784 |
# this is used for adjusting canonical_uri in extracted 'identifiers'; and for errors. |
| 785 |
# we can infer that there is an identifier 'dir/mysubdocument' at path '/components/alpha' |
| 786 |
initial_schema_uri => 'dir/my_subdocument#/subid', |
| 787 |
traversed_keyword_path => '/components/alpha/subid', |
| 788 |
}, |
| 789 |
); |
| 790 |
|
| 791 |
cmp_result( |
| 792 |
$state->{identifiers}, |
| 793 |
{ |
| 794 |
'dir/alpha_id' => { |
| 795 |
canonical_uri => str('dir/alpha_id'), |
| 796 |
path => '/components/alpha/subid/properties/alpha', |
| 797 |
specification_version => 'draft2020-12', |
| 798 |
vocabularies => ignore, |
| 799 |
anchors => { |
| 800 |
alpha_two_anchor => { |
| 801 |
canonical_uri => str('dir/alpha_id#/properties/alpha_two'), |
| 802 |
path => '/components/alpha/subid/properties/alpha/properties/alpha_two', |
| 803 |
}, |
| 804 |
alpha_three_anchor => { |
| 805 |
canonical_uri => str('dir/alpha_id#/properties/alpha_three'), |
| 806 |
path => '/components/alpha/subid/properties/alpha/properties/alpha_three', |
| 807 |
}, |
| 808 |
}, |
| 809 |
}, |
| 810 |
'dir/alpha_one_id' => { |
| 811 |
canonical_uri => str('dir/alpha_one_id'), |
| 812 |
path => '/components/alpha/subid/properties/alpha/properties/alpha_one', |
| 813 |
specification_version => 'draft2020-12', |
| 814 |
vocabularies => ignore, |
| 815 |
}, |
| 816 |
'dir/my_subdocument' => { # this is inferred when we process "$anchor": "beta_anchor" |
| 817 |
canonical_uri => str('dir/my_subdocument'), |
| 818 |
path => '/components/alpha', |
| 819 |
specification_version => 'draft2020-12', |
| 820 |
vocabularies => ignore, |
| 821 |
anchors => { |
| 822 |
beta_anchor => { |
| 823 |
canonical_uri => str('dir/my_subdocument#/subid/properties/beta'), |
| 824 |
path => '/components/alpha/subid/properties/beta', |
| 825 |
}, |
| 826 |
}, |
| 827 |
}, |
| 828 |
}, |
| 829 |
'identifiers are correctly extracted when traversing below the document root, with anchor', |
| 830 |
); |
| 831 |
}; |
| 832 |
|
| 833 |
done_testing; |