| 1 |
# vim: set ft=perl ts=8 sts=2 sw=2 tw=100 et : |
| 2 |
use strictures 2; |
| 3 |
use 5.020; |
| 4 |
use stable 0.031 'postderef'; |
| 5 |
use experimental 'signatures'; |
| 6 |
no autovivification warn => qw(fetch store exists delete); |
| 7 |
use if "$]" >= 5.022, experimental => 're_strict'; |
| 8 |
no if "$]" >= 5.031009, feature => 'indirect'; |
| 9 |
no if "$]" >= 5.033001, feature => 'multidimensional'; |
| 10 |
no if "$]" >= 5.033006, feature => 'bareword_filehandles'; |
| 11 |
no if "$]" >= 5.041009, feature => 'smartmatch'; |
| 12 |
no feature 'switch'; |
| 13 |
use open ':std', ':encoding(UTF-8)'; # force stdin, stdout, stderr into utf8 |
| 14 |
|
| 15 |
use builtin::compat 'load_module'; |
| 16 |
use lib 't/lib'; |
| 17 |
use Helper; |
| 18 |
|
| 19 |
subtest 'draft7' => sub { |
| 20 |
like( |
| 21 |
dies { |
| 22 |
JSON::Schema::Modern->new(collect_annotations => 1, specification_version => 'draft7'); |
| 23 |
}, |
| 24 |
qr/^collect_annotations cannot be used with specification_version draft7/, |
| 25 |
'user cannot enable annotations for draft7', |
| 26 |
); |
| 27 |
|
| 28 |
is_equal( |
| 29 |
JSON::Schema::Modern->new(specification_version => 'draft7') |
| 30 |
->evaluate( |
| 31 |
1, |
| 32 |
{ |
| 33 |
title => 'draft7 title', |
| 34 |
allOf => [ { '$ref' => '#/definitions/draft2020-12-schema' } ], |
| 35 |
definitions => { |
| 36 |
'draft2020-12-schema' => { |
| 37 |
'$id' => 'my_draft2020-12_schema', |
| 38 |
'$schema' => 'https://json-schema.org/draft/2020-12/schema', |
| 39 |
title => 'draft2020-12 title' |
| 40 |
}, |
| 41 |
}, |
| 42 |
}, |
| 43 |
{ collect_annotations => 1 }, |
| 44 |
)->TO_JSON, |
| 45 |
{ |
| 46 |
valid => true, |
| 47 |
annotations => [ |
| 48 |
{ |
| 49 |
instanceLocation => '', |
| 50 |
keywordLocation => '/allOf/0/$ref/title', |
| 51 |
absoluteKeywordLocation => 'my_draft2020-12_schema#/title', |
| 52 |
annotation => 'draft2020-12 title', |
| 53 |
}, |
| 54 |
], |
| 55 |
}, |
| 56 |
'annotation collection can be enabled even when defaulting to draft7, but they are still only collected for later drafts', |
| 57 |
); |
| 58 |
}; |
| 59 |
|
| 60 |
my $js = JSON::Schema::Modern->new(collect_annotations => 1, short_circuit => 0); |
| 61 |
|
| 62 |
my $initial_state = { |
| 63 |
depth => 0, |
| 64 |
short_circuit => 0, |
| 65 |
collect_annotations => 1<<8, |
| 66 |
initial_schema_uri => Mojo::URL->new, |
| 67 |
data_path => '', |
| 68 |
keyword_path => '', |
| 69 |
traversed_keyword_path => '', |
| 70 |
specification_version => 'draft2019-09', |
| 71 |
vocabularies => [ |
| 72 |
(map load_module($_), |
| 73 |
map 'JSON::Schema::Modern::Vocabulary::'.$_, qw(Applicator Validation MetaData)), |
| 74 |
], |
| 75 |
evaluator => $js, |
| 76 |
}; |
| 77 |
|
| 78 |
subtest 'allOf' => sub { |
| 79 |
my $state = { |
| 80 |
%$initial_state, |
| 81 |
keyword => 'allOf', |
| 82 |
annotations => [], |
| 83 |
errors => [], |
| 84 |
}; |
| 85 |
|
| 86 |
my $fail_schema = { |
| 87 |
allOf => [ |
| 88 |
false, # fails; creates errors |
| 89 |
{ title => 'allOf title' }, # passes; creates annotations |
| 90 |
], |
| 91 |
}; |
| 92 |
|
| 93 |
ok( |
| 94 |
!$state->{vocabularies}[0]->_eval_keyword_allOf(1, $fail_schema, $state), |
| 95 |
'evaluation of the allOf keyword fails', |
| 96 |
); |
| 97 |
|
| 98 |
cmp_result( |
| 99 |
$state, |
| 100 |
my $new_state = { |
| 101 |
%$state, |
| 102 |
initial_schema_uri => str(''), |
| 103 |
annotations => [ |
| 104 |
superhashof({ |
| 105 |
instance_location => '', |
| 106 |
keyword_location => '/allOf/1/title', |
| 107 |
annotation => 'allOf title', |
| 108 |
}), |
| 109 |
], |
| 110 |
errors => [ |
| 111 |
methods(TO_JSON => { instanceLocation => '', keywordLocation => '/allOf/0', error => 'subschema is false' }), |
| 112 |
methods(TO_JSON => { instanceLocation => '', keywordLocation => '/allOf', error => 'subschema 0 is not valid' }), |
| 113 |
], |
| 114 |
}, |
| 115 |
'failing allOf: state is correct after evaluating', |
| 116 |
); |
| 117 |
|
| 118 |
my $pass_schema = { |
| 119 |
allOf => [ |
| 120 |
true, |
| 121 |
{ title => 'allOf title' }, # passes; creates annotations |
| 122 |
true, |
| 123 |
], |
| 124 |
}; |
| 125 |
$state->{annotations} = []; |
| 126 |
$state->{errors} = []; |
| 127 |
|
| 128 |
ok( |
| 129 |
$state->{vocabularies}[0]->_eval_keyword_allOf(1, $pass_schema, $state), |
| 130 |
'evaluation of the allOf keyword succeeds', |
| 131 |
); |
| 132 |
|
| 133 |
cmp_result( |
| 134 |
$state, |
| 135 |
{ |
| 136 |
%$new_state, |
| 137 |
annotations => [ |
| 138 |
superhashof({ |
| 139 |
instance_location => '', |
| 140 |
keyword_location => '/allOf/1/title', |
| 141 |
annotation => 'allOf title', |
| 142 |
}), |
| 143 |
], |
| 144 |
errors => [], |
| 145 |
}, |
| 146 |
'passing allOf: state is correct after evaluating', |
| 147 |
); |
| 148 |
|
| 149 |
is_equal( |
| 150 |
$js->evaluate(1, $pass_schema, { collect_annotations => 0 })->TO_JSON, |
| 151 |
{ valid => true }, |
| 152 |
'annotation collection can be turned off in evaluate()', |
| 153 |
); |
| 154 |
|
| 155 |
ok($js->collect_annotations, '...but the value is still true on the object'); |
| 156 |
|
| 157 |
{ |
| 158 |
my $js = JSON::Schema::Modern->new; |
| 159 |
ok(!$js->collect_annotations, 'collect_annotations defaults to false'); |
| 160 |
is_equal( |
| 161 |
$js->evaluate(1, $pass_schema, { collect_annotations => 1 })->TO_JSON, |
| 162 |
{ |
| 163 |
valid => true, |
| 164 |
annotations => [ |
| 165 |
{ |
| 166 |
instanceLocation => '', |
| 167 |
keywordLocation => '/allOf/1/title', |
| 168 |
annotation => 'allOf title', |
| 169 |
}, |
| 170 |
], |
| 171 |
}, |
| 172 |
'annotation collection can be turned on in evaluate() also', |
| 173 |
); |
| 174 |
} |
| 175 |
}; |
| 176 |
|
| 177 |
subtest 'oneOf' => sub { |
| 178 |
my $state = { |
| 179 |
%$initial_state, |
| 180 |
keyword => 'oneOf', |
| 181 |
annotations => [], |
| 182 |
errors => [], |
| 183 |
}; |
| 184 |
|
| 185 |
my $fail_schema = { |
| 186 |
oneOf => [ |
| 187 |
false, # fails; creates errors |
| 188 |
{ title => 'oneOf title' }, # passes; creates annotations |
| 189 |
{ title => 'oneOf title2' }, # passes; creates annotations |
| 190 |
], |
| 191 |
}; |
| 192 |
|
| 193 |
ok( |
| 194 |
!$state->{vocabularies}[0]->_eval_keyword_oneOf(1, $fail_schema, $state), |
| 195 |
'evaluation of the oneOf keyword fails', |
| 196 |
); |
| 197 |
|
| 198 |
cmp_result( |
| 199 |
$state, |
| 200 |
my $new_state = { |
| 201 |
%$state, |
| 202 |
initial_schema_uri => str(''), |
| 203 |
annotations => [ |
| 204 |
superhashof({ |
| 205 |
instance_location => '', |
| 206 |
keyword_location => '/oneOf/1/title', |
| 207 |
annotation => 'oneOf title', |
| 208 |
}), |
| 209 |
superhashof({ |
| 210 |
instance_location => '', |
| 211 |
keyword_location => '/oneOf/2/title', |
| 212 |
annotation => 'oneOf title2', |
| 213 |
}), |
| 214 |
], |
| 215 |
errors => [ |
| 216 |
methods(TO_JSON => { instanceLocation => '', keywordLocation => '/oneOf', error => 'multiple subschemas are valid: 1, 2' }), |
| 217 |
], |
| 218 |
}, |
| 219 |
'failing oneOf: state is correct after evaluating', |
| 220 |
); |
| 221 |
|
| 222 |
my $pass_schema = { |
| 223 |
oneOf => [ |
| 224 |
false, |
| 225 |
{ title => 'oneOf title' }, # passes; creates annotations |
| 226 |
false, |
| 227 |
], |
| 228 |
}; |
| 229 |
|
| 230 |
$state->{annotations} = []; |
| 231 |
$state->{errors} = []; |
| 232 |
|
| 233 |
ok( |
| 234 |
$state->{vocabularies}[0]->_eval_keyword_oneOf(1, $pass_schema, $state), |
| 235 |
'evaluation of the oneOf keyword succeeds', |
| 236 |
); |
| 237 |
|
| 238 |
cmp_result( |
| 239 |
$state, |
| 240 |
{ |
| 241 |
%$new_state, |
| 242 |
annotations => [ |
| 243 |
superhashof({ |
| 244 |
instance_location => '', |
| 245 |
keyword_location => '/oneOf/1/title', |
| 246 |
annotation => 'oneOf title', |
| 247 |
}), |
| 248 |
], |
| 249 |
errors => [], |
| 250 |
}, |
| 251 |
'passing oneOf: state is correct after evaluating', |
| 252 |
); |
| 253 |
}; |
| 254 |
|
| 255 |
subtest 'not' => sub { |
| 256 |
my $state = { |
| 257 |
%$initial_state, |
| 258 |
keyword => 'not', |
| 259 |
annotations => [], |
| 260 |
errors => [], |
| 261 |
}; |
| 262 |
|
| 263 |
my $fail_schema = { |
| 264 |
not => { title => 'not title' }, # passes; skips annotations because nothing needs them |
| 265 |
}; |
| 266 |
|
| 267 |
ok( |
| 268 |
!$state->{vocabularies}[0]->_eval_keyword_not(1, $fail_schema, $state), |
| 269 |
'evaluation of the not keyword fails', |
| 270 |
); |
| 271 |
|
| 272 |
cmp_result( |
| 273 |
$state, |
| 274 |
my $new_state = { |
| 275 |
%$state, |
| 276 |
initial_schema_uri => str(''), |
| 277 |
annotations => [], |
| 278 |
errors => [ |
| 279 |
methods(TO_JSON => { instanceLocation => '', keywordLocation => '/not', error => 'subschema is valid' }), |
| 280 |
], |
| 281 |
}, |
| 282 |
'failing not: state is correct after evaluating', |
| 283 |
); |
| 284 |
|
| 285 |
|
| 286 |
$state = { |
| 287 |
%$initial_state, |
| 288 |
keyword => 'not', |
| 289 |
annotations => [], |
| 290 |
errors => [], |
| 291 |
}; |
| 292 |
|
| 293 |
$fail_schema = { |
| 294 |
not => { |
| 295 |
properties => { foo => true }, |
| 296 |
unevaluatedProperties => false, |
| 297 |
}, # passes; annotations are collected because unevaluated* needs them |
| 298 |
}; |
| 299 |
|
| 300 |
ok( |
| 301 |
!$state->{vocabularies}[0]->_eval_keyword_not(1, $fail_schema, $state), |
| 302 |
'evaluation of the not keyword fails', |
| 303 |
); |
| 304 |
|
| 305 |
cmp_result( |
| 306 |
$state, |
| 307 |
$new_state = { |
| 308 |
%$state, |
| 309 |
initial_schema_uri => str(''), |
| 310 |
annotations => [], |
| 311 |
errors => [ |
| 312 |
methods(TO_JSON => { instanceLocation => '', keywordLocation => '/not', error => 'subschema is valid' }), |
| 313 |
], |
| 314 |
}, |
| 315 |
'failing not: state is correct after evaluating (annotations will be ultimately discarded)', |
| 316 |
); |
| 317 |
|
| 318 |
|
| 319 |
my $pass_schema = { |
| 320 |
not => { not => { title => 'not title' } }, |
| 321 |
}; |
| 322 |
|
| 323 |
$state->{annotations} = []; |
| 324 |
$state->{errors} = []; |
| 325 |
|
| 326 |
ok( |
| 327 |
$state->{vocabularies}[0]->_eval_keyword_not(1, $pass_schema, $state), |
| 328 |
'evaluation of the not keyword succeeds', |
| 329 |
); |
| 330 |
|
| 331 |
cmp_result( |
| 332 |
$state, |
| 333 |
{ |
| 334 |
%$new_state, |
| 335 |
annotations => [], |
| 336 |
errors => [], |
| 337 |
}, |
| 338 |
'passing not: state is correct after evaluating', |
| 339 |
); |
| 340 |
|
| 341 |
is_equal( |
| 342 |
$js->evaluate( |
| 343 |
{ foo => 1 }, |
| 344 |
{ |
| 345 |
not => { |
| 346 |
not => { |
| 347 |
'$comment' => 'this subschema must still produce annotations internally, even though the "not" will ultimately discard them', |
| 348 |
anyOf => [ |
| 349 |
true, |
| 350 |
{ properties => { foo => true } }, |
| 351 |
], |
| 352 |
unevaluatedProperties => false, |
| 353 |
}, |
| 354 |
}, |
| 355 |
}, |
| 356 |
)->TO_JSON, |
| 357 |
{ |
| 358 |
valid => true, |
| 359 |
}, |
| 360 |
'annotations are still collected inside a "not", otherwise the unevaluatedProperties would have returned false', |
| 361 |
); |
| 362 |
}; |
| 363 |
|
| 364 |
subtest 'prefixItems' => sub { |
| 365 |
my $state = { |
| 366 |
%$initial_state, |
| 367 |
keyword => 'prefixItems', |
| 368 |
annotations => [], |
| 369 |
errors => [], |
| 370 |
}; |
| 371 |
|
| 372 |
ok( |
| 373 |
$state->{vocabularies}[0]->_eval_keyword_prefixItems([], { prefixItems => [ true ] }, $state), |
| 374 |
'no items means that "prefixItems" succeeds', |
| 375 |
); |
| 376 |
|
| 377 |
cmp_result( |
| 378 |
$state, |
| 379 |
my $new_state = { |
| 380 |
%$state, |
| 381 |
initial_schema_uri => str(''), |
| 382 |
annotations => [], |
| 383 |
errors => [], |
| 384 |
}, |
| 385 |
'no items: no annotation is produced by prefixItems', |
| 386 |
); |
| 387 |
|
| 388 |
$state = { |
| 389 |
%$initial_state, |
| 390 |
keyword => 'prefixItems', |
| 391 |
annotations => [], |
| 392 |
errors => [], |
| 393 |
}; |
| 394 |
|
| 395 |
ok( |
| 396 |
$state->{vocabularies}[0]->_eval_keyword_prefixItems([ 1 ], { prefixItems => [ true ] }, $state), |
| 397 |
'one item', |
| 398 |
); |
| 399 |
|
| 400 |
cmp_result( |
| 401 |
$state, |
| 402 |
{ |
| 403 |
%$state, |
| 404 |
initial_schema_uri => str(''), |
| 405 |
annotations => [ |
| 406 |
superhashof({ |
| 407 |
instance_location => '', |
| 408 |
keyword_location => '/prefixItems', |
| 409 |
annotation => true, |
| 410 |
}), |
| 411 |
], |
| 412 |
errors => [], |
| 413 |
}, |
| 414 |
'passing prefixItems: one item is annotated', |
| 415 |
); |
| 416 |
|
| 417 |
$state = { |
| 418 |
%$initial_state, |
| 419 |
keyword => 'prefixItems', |
| 420 |
annotations => [], |
| 421 |
errors => [], |
| 422 |
}; |
| 423 |
|
| 424 |
ok( |
| 425 |
!$state->{vocabularies}[0]->_eval_keyword_prefixItems( |
| 426 |
[ 1, 5, 9 ], |
| 427 |
{ prefixItems => [ { title => 'hi', maximum => 3 }, { title => 'hi', maximum => 3 } ] }, |
| 428 |
$state), |
| 429 |
'two items, one failing', |
| 430 |
); |
| 431 |
|
| 432 |
cmp_result( |
| 433 |
$state, |
| 434 |
{ |
| 435 |
%$state, |
| 436 |
initial_schema_uri => str(''), |
| 437 |
annotations => [ |
| 438 |
superhashof({ |
| 439 |
instance_location => '/0', |
| 440 |
keyword_location => '/prefixItems/0/title', |
| 441 |
annotation => 'hi', |
| 442 |
}), |
| 443 |
superhashof({ |
| 444 |
instance_location => '', |
| 445 |
keyword_location => '/prefixItems', |
| 446 |
annotation => 1, |
| 447 |
}), |
| 448 |
], |
| 449 |
errors => [ |
| 450 |
methods(TO_JSON => { |
| 451 |
instanceLocation => '/1', |
| 452 |
keywordLocation => '/prefixItems/1/maximum', |
| 453 |
error => 'value is greater than 3', |
| 454 |
}), |
| 455 |
methods(TO_JSON => { |
| 456 |
instanceLocation => '', |
| 457 |
keywordLocation => '/prefixItems', |
| 458 |
error => 'not all items are valid', |
| 459 |
}), |
| 460 |
], |
| 461 |
}, |
| 462 |
'failing prefixItems still collects annotations', |
| 463 |
); |
| 464 |
}; |
| 465 |
|
| 466 |
subtest 'schema-items' => sub { |
| 467 |
my $state = { |
| 468 |
%$initial_state, |
| 469 |
keyword => 'items', |
| 470 |
annotations => [], |
| 471 |
errors => [], |
| 472 |
}; |
| 473 |
|
| 474 |
ok( |
| 475 |
$state->{vocabularies}[0]->_eval_keyword_items([], { items => true }, $state), |
| 476 |
'no items means that "items" succeeds', |
| 477 |
); |
| 478 |
|
| 479 |
cmp_result( |
| 480 |
$state, |
| 481 |
my $new_state = { |
| 482 |
%$state, |
| 483 |
initial_schema_uri => str(''), |
| 484 |
annotations => [], |
| 485 |
errors => [], |
| 486 |
}, |
| 487 |
'no items: no annotation is produced by items', |
| 488 |
); |
| 489 |
|
| 490 |
$state = { |
| 491 |
%$initial_state, |
| 492 |
keyword => 'items', |
| 493 |
annotations => [], |
| 494 |
errors => [], |
| 495 |
}; |
| 496 |
|
| 497 |
ok( |
| 498 |
$state->{vocabularies}[0]->_eval_keyword_items([ 1 ], { items => true }, $state), |
| 499 |
'one item', |
| 500 |
); |
| 501 |
|
| 502 |
cmp_result( |
| 503 |
$state, |
| 504 |
{ |
| 505 |
%$state, |
| 506 |
initial_schema_uri => str(''), |
| 507 |
annotations => [ |
| 508 |
superhashof({ |
| 509 |
instance_location => '', |
| 510 |
keyword_location => '/items', |
| 511 |
annotation => true, |
| 512 |
}), |
| 513 |
], |
| 514 |
errors => [], |
| 515 |
}, |
| 516 |
'passing items: one item is annotated', |
| 517 |
); |
| 518 |
|
| 519 |
$state = { |
| 520 |
%$initial_state, |
| 521 |
keyword => 'items', |
| 522 |
annotations => [], |
| 523 |
errors => [], |
| 524 |
}; |
| 525 |
|
| 526 |
ok( |
| 527 |
!$state->{vocabularies}[0]->_eval_keyword_items( |
| 528 |
[ 1, 5 ], |
| 529 |
{ items => { title => 'hi', maximum => 3 } }, |
| 530 |
$state), |
| 531 |
'two items, one failing', |
| 532 |
); |
| 533 |
|
| 534 |
cmp_result( |
| 535 |
$state, |
| 536 |
{ |
| 537 |
%$state, |
| 538 |
initial_schema_uri => str(''), |
| 539 |
annotations => [ |
| 540 |
superhashof({ |
| 541 |
instance_location => '/0', |
| 542 |
keyword_location => '/items/title', |
| 543 |
annotation => 'hi', |
| 544 |
}), |
| 545 |
superhashof({ |
| 546 |
instance_location => '', |
| 547 |
keyword_location => '/items', |
| 548 |
annotation => true, |
| 549 |
}), |
| 550 |
], |
| 551 |
errors => [ |
| 552 |
methods(TO_JSON => { |
| 553 |
instanceLocation => '/1', |
| 554 |
keywordLocation => '/items/maximum', |
| 555 |
error => 'value is greater than 3', |
| 556 |
}), |
| 557 |
methods(TO_JSON => { |
| 558 |
instanceLocation => '', |
| 559 |
keywordLocation => '/items', |
| 560 |
error => 'subschema is not valid against all items', |
| 561 |
}), |
| 562 |
], |
| 563 |
}, |
| 564 |
'failing items still collects annotations', |
| 565 |
); |
| 566 |
}; |
| 567 |
|
| 568 |
subtest 'additionalItems' => sub { |
| 569 |
my $state = { |
| 570 |
%$initial_state, |
| 571 |
keyword => 'additionalItems', |
| 572 |
annotations => [], |
| 573 |
errors => [], |
| 574 |
}; |
| 575 |
|
| 576 |
ok( |
| 577 |
$state->{vocabularies}[0]->_eval_keyword_items([], { additionalItems => true }, $state), |
| 578 |
'no items means that "additionalItems" succeeds', |
| 579 |
); |
| 580 |
|
| 581 |
cmp_result( |
| 582 |
$state, |
| 583 |
my $new_state = { |
| 584 |
%$state, |
| 585 |
initial_schema_uri => str(''), |
| 586 |
annotations => [], |
| 587 |
errors => [], |
| 588 |
}, |
| 589 |
'no items: no annotation is produced by additionaltems', |
| 590 |
); |
| 591 |
|
| 592 |
$state = { |
| 593 |
%$initial_state, |
| 594 |
keyword => 'additionalItems', |
| 595 |
annotations => [], |
| 596 |
errors => [], |
| 597 |
}; |
| 598 |
|
| 599 |
ok( |
| 600 |
$state->{vocabularies}[0]->_eval_keyword_additionalItems([ 1 ], { additionalItems => false }, $state), |
| 601 |
'one item', |
| 602 |
); |
| 603 |
|
| 604 |
cmp_result( |
| 605 |
$state, |
| 606 |
{ |
| 607 |
%$state, |
| 608 |
initial_schema_uri => str(''), |
| 609 |
annotations => [], |
| 610 |
errors => [], |
| 611 |
}, |
| 612 |
'additionalItems does nothing without items', |
| 613 |
); |
| 614 |
}; |
| 615 |
|
| 616 |
subtest 'properties' => sub { |
| 617 |
my $state = { |
| 618 |
%$initial_state, |
| 619 |
keyword => 'properties', |
| 620 |
annotations => [], |
| 621 |
errors => [], |
| 622 |
}; |
| 623 |
|
| 624 |
ok( |
| 625 |
$state->{vocabularies}[0]->_eval_keyword_properties({}, { properties => { foo => true } }, $state), |
| 626 |
'no items means that "properties" succeeds', |
| 627 |
); |
| 628 |
|
| 629 |
cmp_result( |
| 630 |
$state, |
| 631 |
my $new_state = { |
| 632 |
%$state, |
| 633 |
initial_schema_uri => str(''), |
| 634 |
annotations => [ |
| 635 |
superhashof({ |
| 636 |
instance_location => '', |
| 637 |
keyword_location => '/properties', |
| 638 |
annotation => [], |
| 639 |
}), |
| 640 |
], |
| 641 |
errors => [], |
| 642 |
}, |
| 643 |
'no properties: annotation is still produced by properties', |
| 644 |
); |
| 645 |
|
| 646 |
$state = { |
| 647 |
%$initial_state, |
| 648 |
keyword => 'properties', |
| 649 |
annotations => [], |
| 650 |
errors => [], |
| 651 |
}; |
| 652 |
|
| 653 |
ok( |
| 654 |
$state->{vocabularies}[0]->_eval_keyword_properties({ foo => 1 }, { properties => { foo => true } }, $state), |
| 655 |
'one property', |
| 656 |
); |
| 657 |
|
| 658 |
cmp_result( |
| 659 |
$state, |
| 660 |
{ |
| 661 |
%$state, |
| 662 |
initial_schema_uri => str(''), |
| 663 |
annotations => [ |
| 664 |
superhashof({ |
| 665 |
instance_location => '', |
| 666 |
keyword_location => '/properties', |
| 667 |
annotation => [ 'foo' ], |
| 668 |
}), |
| 669 |
], |
| 670 |
errors => [], |
| 671 |
}, |
| 672 |
'passing properties: one property is annotated', |
| 673 |
); |
| 674 |
|
| 675 |
$state = { |
| 676 |
%$initial_state, |
| 677 |
keyword => 'properties', |
| 678 |
annotations => [], |
| 679 |
errors => [], |
| 680 |
}; |
| 681 |
|
| 682 |
ok( |
| 683 |
!$state->{vocabularies}[0]->_eval_keyword_properties( |
| 684 |
{ foo => 1, bar => 5 }, |
| 685 |
{ properties => { |
| 686 |
foo => { title => 'hi', maximum => 3 }, |
| 687 |
bar => { title => 'hi', maximum => 3 }, |
| 688 |
}, |
| 689 |
}, |
| 690 |
$state), |
| 691 |
'two properties, one failing', |
| 692 |
); |
| 693 |
|
| 694 |
cmp_result( |
| 695 |
$state, |
| 696 |
{ |
| 697 |
%$state, |
| 698 |
initial_schema_uri => str(''), |
| 699 |
annotations => [ |
| 700 |
superhashof({ |
| 701 |
instance_location => '/foo', |
| 702 |
keyword_location => '/properties/foo/title', |
| 703 |
annotation => 'hi', |
| 704 |
}), |
| 705 |
superhashof({ |
| 706 |
instance_location => '', |
| 707 |
keyword_location => '/properties', |
| 708 |
annotation => [ qw(bar foo) ], |
| 709 |
}), |
| 710 |
], |
| 711 |
errors => [ |
| 712 |
methods(TO_JSON => { |
| 713 |
instanceLocation => '/bar', |
| 714 |
keywordLocation => '/properties/bar/maximum', |
| 715 |
error => 'value is greater than 3', |
| 716 |
}), |
| 717 |
methods(TO_JSON => { |
| 718 |
instanceLocation => '', |
| 719 |
keywordLocation => '/properties', |
| 720 |
error => 'not all properties are valid', |
| 721 |
}), |
| 722 |
], |
| 723 |
}, |
| 724 |
'failing properties still collects annotations', |
| 725 |
); |
| 726 |
}; |
| 727 |
|
| 728 |
subtest 'patternProperties' => sub { |
| 729 |
my $state = { |
| 730 |
%$initial_state, |
| 731 |
keyword => 'patternProperties', |
| 732 |
annotations => [], |
| 733 |
errors => [], |
| 734 |
}; |
| 735 |
|
| 736 |
ok( |
| 737 |
$state->{vocabularies}[0]->_eval_keyword_patternProperties({}, { patternProperties => { foo => true } }, $state), |
| 738 |
'no items means that "patternProperties" succeeds', |
| 739 |
); |
| 740 |
|
| 741 |
cmp_result( |
| 742 |
$state, |
| 743 |
my $new_state = { |
| 744 |
%$state, |
| 745 |
initial_schema_uri => str(''), |
| 746 |
annotations => [ |
| 747 |
superhashof({ |
| 748 |
instance_location => '', |
| 749 |
keyword_location => '/patternProperties', |
| 750 |
annotation => [], |
| 751 |
}), |
| 752 |
], |
| 753 |
errors => [], |
| 754 |
}, |
| 755 |
'no pProperties: annotation is still produced by patternProperties', |
| 756 |
); |
| 757 |
|
| 758 |
$state = { |
| 759 |
%$initial_state, |
| 760 |
keyword => 'patternProperties', |
| 761 |
annotations => [], |
| 762 |
errors => [], |
| 763 |
}; |
| 764 |
|
| 765 |
ok( |
| 766 |
$state->{vocabularies}[0]->_eval_keyword_patternProperties({ foo => 1 }, { patternProperties => { foo => true } }, $state), |
| 767 |
'one property', |
| 768 |
); |
| 769 |
|
| 770 |
cmp_result( |
| 771 |
$state, |
| 772 |
{ |
| 773 |
%$state, |
| 774 |
initial_schema_uri => str(''), |
| 775 |
annotations => [ |
| 776 |
superhashof({ |
| 777 |
instance_location => '', |
| 778 |
keyword_location => '/patternProperties', |
| 779 |
annotation => [ 'foo' ], |
| 780 |
}), |
| 781 |
], |
| 782 |
errors => [], |
| 783 |
}, |
| 784 |
'passing properties: one property is annotated', |
| 785 |
); |
| 786 |
|
| 787 |
$state = { |
| 788 |
%$initial_state, |
| 789 |
keyword => 'patternProperties', |
| 790 |
annotations => [], |
| 791 |
errors => [], |
| 792 |
}; |
| 793 |
|
| 794 |
ok( |
| 795 |
!$state->{vocabularies}[0]->_eval_keyword_patternProperties( |
| 796 |
{ foo => 1, bar => 5 }, |
| 797 |
{ patternProperties => { |
| 798 |
foo => { title => 'hi', maximum => 3 }, |
| 799 |
bar => { title => 'hi', maximum => 3 }, |
| 800 |
}, |
| 801 |
}, |
| 802 |
$state), |
| 803 |
'two properties, one failing', |
| 804 |
); |
| 805 |
|
| 806 |
cmp_result( |
| 807 |
$state, |
| 808 |
{ |
| 809 |
%$state, |
| 810 |
initial_schema_uri => str(''), |
| 811 |
annotations => [ |
| 812 |
superhashof({ |
| 813 |
instance_location => '/foo', |
| 814 |
keyword_location => '/patternProperties/foo/title', |
| 815 |
annotation => 'hi', |
| 816 |
}), |
| 817 |
superhashof({ |
| 818 |
instance_location => '', |
| 819 |
keyword_location => '/patternProperties', |
| 820 |
annotation => [ qw(bar foo) ], |
| 821 |
}), |
| 822 |
], |
| 823 |
errors => [ |
| 824 |
methods(TO_JSON => { |
| 825 |
instanceLocation => '/bar', |
| 826 |
keywordLocation => '/patternProperties/bar/maximum', |
| 827 |
error => 'value is greater than 3', |
| 828 |
}), |
| 829 |
methods(TO_JSON => { |
| 830 |
instanceLocation => '', |
| 831 |
keywordLocation => '/patternProperties', |
| 832 |
error => 'not all properties are valid', |
| 833 |
}), |
| 834 |
], |
| 835 |
}, |
| 836 |
'failing patternProperties still collects annotations', |
| 837 |
); |
| 838 |
}; |
| 839 |
|
| 840 |
subtest 'additionalProperties' => sub { |
| 841 |
my $state = { |
| 842 |
%$initial_state, |
| 843 |
keyword => 'additionalProperties', |
| 844 |
annotations => [], |
| 845 |
errors => [], |
| 846 |
}; |
| 847 |
|
| 848 |
ok( |
| 849 |
$state->{vocabularies}[0]->_eval_keyword_additionalProperties([], { additionalProperties => true }, $state), |
| 850 |
'no items means that "additionalProperties" succeeds', |
| 851 |
); |
| 852 |
|
| 853 |
cmp_result( |
| 854 |
$state, |
| 855 |
my $new_state = { |
| 856 |
%$state, |
| 857 |
initial_schema_uri => str(''), |
| 858 |
annotations => [], |
| 859 |
errors => [], |
| 860 |
}, |
| 861 |
'no properties: no annotation is produced by additionalProperties', |
| 862 |
); |
| 863 |
|
| 864 |
$state = { |
| 865 |
%$initial_state, |
| 866 |
keyword => 'additionalProperties', |
| 867 |
annotations => [], |
| 868 |
errors => [], |
| 869 |
}; |
| 870 |
|
| 871 |
ok( |
| 872 |
$state->{vocabularies}[0]->_eval_keyword_additionalProperties({ foo => 1 }, { additionalProperties => true }, $state), |
| 873 |
'one property', |
| 874 |
); |
| 875 |
|
| 876 |
cmp_result( |
| 877 |
$state, |
| 878 |
{ |
| 879 |
%$state, |
| 880 |
initial_schema_uri => str(''), |
| 881 |
annotations => [ |
| 882 |
superhashof({ |
| 883 |
instance_location => '', |
| 884 |
keyword_location => '/additionalProperties', |
| 885 |
annotation => [ 'foo' ], |
| 886 |
}), |
| 887 |
], |
| 888 |
errors => [], |
| 889 |
}, |
| 890 |
'passing additionalProperties: one property is annotated', |
| 891 |
); |
| 892 |
|
| 893 |
$state = { |
| 894 |
%$initial_state, |
| 895 |
keyword => 'additionalProperties', |
| 896 |
annotations => [], |
| 897 |
errors => [], |
| 898 |
}; |
| 899 |
|
| 900 |
ok( |
| 901 |
!$state->{vocabularies}[0]->_eval_keyword_additionalProperties( |
| 902 |
{ foo => 1, bar => 3, baz => 5 }, |
| 903 |
{ |
| 904 |
properties => { foo => true }, |
| 905 |
additionalProperties => { title => 'hi', maximum => 3 }, |
| 906 |
}, |
| 907 |
$state), |
| 908 |
'two properties, one failing', |
| 909 |
); |
| 910 |
|
| 911 |
cmp_result( |
| 912 |
$state, |
| 913 |
{ |
| 914 |
%$state, |
| 915 |
initial_schema_uri => str(''), |
| 916 |
annotations => [ |
| 917 |
superhashof({ |
| 918 |
instance_location => '/bar', |
| 919 |
keyword_location => '/additionalProperties/title', |
| 920 |
annotation => 'hi', |
| 921 |
}), |
| 922 |
superhashof({ |
| 923 |
instance_location => '', |
| 924 |
keyword_location => '/additionalProperties', |
| 925 |
annotation => [ qw(bar baz) ], |
| 926 |
}), |
| 927 |
], |
| 928 |
errors => [ |
| 929 |
methods(TO_JSON => { |
| 930 |
instanceLocation => '/baz', |
| 931 |
keywordLocation => '/additionalProperties/maximum', |
| 932 |
error => 'value is greater than 3', |
| 933 |
}), |
| 934 |
methods(TO_JSON => { |
| 935 |
instanceLocation => '', |
| 936 |
keywordLocation => '/additionalProperties', |
| 937 |
error => 'not all additional properties are valid', |
| 938 |
}), |
| 939 |
], |
| 940 |
}, |
| 941 |
'failing properties still collects annotations', |
| 942 |
); |
| 943 |
}; |
| 944 |
|
| 945 |
subtest 'unevaluatedProperties' => sub { |
| 946 |
my $state = { |
| 947 |
%$initial_state, |
| 948 |
keyword => 'unevaluatedProperties', |
| 949 |
annotations => [], |
| 950 |
errors => [], |
| 951 |
}; |
| 952 |
|
| 953 |
ok( |
| 954 |
$state->{vocabularies}[0]->_eval_keyword_unevaluatedProperties([], { unevaluatedProperties => true }, $state), |
| 955 |
'no items means that "unevaluatedProperties" succeeds', |
| 956 |
); |
| 957 |
|
| 958 |
cmp_result( |
| 959 |
$state, |
| 960 |
my $new_state = { |
| 961 |
%$state, |
| 962 |
initial_schema_uri => str(''), |
| 963 |
annotations => [], |
| 964 |
errors => [], |
| 965 |
}, |
| 966 |
'no properties: no annotation is produced by unevaluatedProperties', |
| 967 |
); |
| 968 |
|
| 969 |
$state = { |
| 970 |
%$initial_state, |
| 971 |
keyword => 'unevaluatedProperties', |
| 972 |
annotations => [], |
| 973 |
errors => [], |
| 974 |
}; |
| 975 |
|
| 976 |
ok( |
| 977 |
$state->{vocabularies}[0]->_eval_keyword_unevaluatedProperties({ foo => 1 }, { unevaluatedProperties => true }, $state), |
| 978 |
'one property', |
| 979 |
); |
| 980 |
|
| 981 |
cmp_result( |
| 982 |
$state, |
| 983 |
{ |
| 984 |
%$state, |
| 985 |
initial_schema_uri => str(''), |
| 986 |
annotations => [ |
| 987 |
superhashof({ |
| 988 |
instance_location => '', |
| 989 |
keyword_location => '/unevaluatedProperties', |
| 990 |
annotation => [ 'foo' ], |
| 991 |
}), |
| 992 |
], |
| 993 |
errors => [], |
| 994 |
}, |
| 995 |
'passing unevaluatedProperties: one property is annotated', |
| 996 |
); |
| 997 |
|
| 998 |
$state = { |
| 999 |
%$initial_state, |
| 1000 |
keyword => 'unevaluatedProperties', |
| 1001 |
annotations => [], |
| 1002 |
errors => [], |
| 1003 |
}; |
| 1004 |
|
| 1005 |
ok( |
| 1006 |
!$state->{vocabularies}[0]->_eval_keyword_unevaluatedProperties( |
| 1007 |
{ foo => 1, bar => 3, baz => 5 }, |
| 1008 |
{ |
| 1009 |
properties => { foo => true }, |
| 1010 |
unevaluatedProperties => { title => 'hi', maximum => 3 }, |
| 1011 |
}, |
| 1012 |
$state), |
| 1013 |
'two properties, one failing', |
| 1014 |
); |
| 1015 |
|
| 1016 |
cmp_result( |
| 1017 |
$state, |
| 1018 |
{ |
| 1019 |
%$state, |
| 1020 |
initial_schema_uri => str(''), |
| 1021 |
annotations => [ |
| 1022 |
(map superhashof({ |
| 1023 |
instance_location => '/'.$_, |
| 1024 |
keyword_location => '/unevaluatedProperties/title', |
| 1025 |
annotation => 'hi', |
| 1026 |
}), qw(bar foo)), |
| 1027 |
superhashof({ |
| 1028 |
instance_location => '', |
| 1029 |
keyword_location => '/unevaluatedProperties', |
| 1030 |
annotation => [ qw(bar baz foo) ], |
| 1031 |
}), |
| 1032 |
], |
| 1033 |
errors => [ |
| 1034 |
methods(TO_JSON => { |
| 1035 |
instanceLocation => '/baz', |
| 1036 |
keywordLocation => '/unevaluatedProperties/maximum', |
| 1037 |
error => 'value is greater than 3', |
| 1038 |
}), |
| 1039 |
methods(TO_JSON => { |
| 1040 |
instanceLocation => '', |
| 1041 |
keywordLocation => '/unevaluatedProperties', |
| 1042 |
error => 'not all additional properties are valid', |
| 1043 |
}), |
| 1044 |
], |
| 1045 |
}, |
| 1046 |
'failing unevaluatedProperties still collects annotations', |
| 1047 |
); |
| 1048 |
}; |
| 1049 |
|
| 1050 |
subtest 'collect_annotations and unevaluated keywords' => sub { |
| 1051 |
my $js = JSON::Schema::Modern->new(collect_annotations => 0); |
| 1052 |
|
| 1053 |
is_equal( |
| 1054 |
$js->evaluate( |
| 1055 |
[ 1 ], |
| 1056 |
my $schema = { |
| 1057 |
'$id' => 'unevaluatedItems.json', |
| 1058 |
prefixItems => [ true ], |
| 1059 |
unevaluatedItems => false, |
| 1060 |
}, |
| 1061 |
)->TO_JSON, |
| 1062 |
{ valid => true }, |
| 1063 |
'when "collect_annotations" is explicitly set to false, unevaluatedItems can still be used (valid result, no annotations in result)', |
| 1064 |
); |
| 1065 |
|
| 1066 |
is_equal( |
| 1067 |
$js->evaluate( |
| 1068 |
[ 1, 2 ], |
| 1069 |
$schema, |
| 1070 |
)->TO_JSON, |
| 1071 |
{ |
| 1072 |
valid => false, |
| 1073 |
errors => [ |
| 1074 |
{ |
| 1075 |
instanceLocation => '/1', |
| 1076 |
keywordLocation => '/unevaluatedItems', |
| 1077 |
absoluteKeywordLocation => 'unevaluatedItems.json#/unevaluatedItems', |
| 1078 |
error => 'additional item not permitted', |
| 1079 |
}, |
| 1080 |
{ |
| 1081 |
instanceLocation => '', |
| 1082 |
keywordLocation => '/unevaluatedItems', |
| 1083 |
absoluteKeywordLocation => 'unevaluatedItems.json#/unevaluatedItems', |
| 1084 |
error => 'subschema is not valid against all additional items', |
| 1085 |
}, |
| 1086 |
], |
| 1087 |
}, |
| 1088 |
'when "collect_annotations" is explicitly set to false, unevaluatedItems can still be used (invalid result)', |
| 1089 |
); |
| 1090 |
|
| 1091 |
is_equal( |
| 1092 |
$js->evaluate( |
| 1093 |
{ foo => 1 }, |
| 1094 |
$schema = { |
| 1095 |
'$id' => 'unevaluatedProperties.json', |
| 1096 |
properties => { foo => true }, |
| 1097 |
unevaluatedProperties => false, |
| 1098 |
}, |
| 1099 |
)->TO_JSON, |
| 1100 |
{ valid => true }, |
| 1101 |
'when "collect_annotations" is explicitly set to false, unevaluatedProperties can still be used (valid result, no annotations)', |
| 1102 |
); |
| 1103 |
is_equal( |
| 1104 |
$js->evaluate( |
| 1105 |
{ foo => 1, bar => 2 }, |
| 1106 |
$schema, |
| 1107 |
)->TO_JSON, |
| 1108 |
{ |
| 1109 |
valid => false, |
| 1110 |
errors => [ |
| 1111 |
{ |
| 1112 |
instanceLocation => '/bar', |
| 1113 |
keywordLocation => '/unevaluatedProperties', |
| 1114 |
absoluteKeywordLocation => 'unevaluatedProperties.json#/unevaluatedProperties', |
| 1115 |
error => 'additional property not permitted', |
| 1116 |
}, |
| 1117 |
{ |
| 1118 |
instanceLocation => '', |
| 1119 |
keywordLocation => '/unevaluatedProperties', |
| 1120 |
absoluteKeywordLocation => 'unevaluatedProperties.json#/unevaluatedProperties', |
| 1121 |
error => 'not all additional properties are valid', |
| 1122 |
}, |
| 1123 |
], |
| 1124 |
}, |
| 1125 |
'when "collect_annotations" is explicitly set to false, unevaluatedProperties can still be used (invalid result)', |
| 1126 |
); |
| 1127 |
|
| 1128 |
is_equal( |
| 1129 |
$js->evaluate( |
| 1130 |
{ |
| 1131 |
item => [ 1 ], |
| 1132 |
property => { foo => 1 }, |
| 1133 |
}, |
| 1134 |
$schema = { |
| 1135 |
properties => { |
| 1136 |
item => { '$ref' => 'unevaluatedItems.json' }, |
| 1137 |
property => { '$ref' => 'unevaluatedProperties.json' }, |
| 1138 |
}, |
| 1139 |
}, |
| 1140 |
)->TO_JSON, |
| 1141 |
{ valid => true }, |
| 1142 |
'when "collect_annotations" is explicitly set to false, unevaluatedProperties still be used, even in other documents (valid result)', |
| 1143 |
); |
| 1144 |
|
| 1145 |
is_equal( |
| 1146 |
$js->evaluate( |
| 1147 |
{ |
| 1148 |
item => [ 1, 2 ], |
| 1149 |
property => { foo => 1, bar => 2 }, |
| 1150 |
}, |
| 1151 |
$schema, |
| 1152 |
)->TO_JSON, |
| 1153 |
{ |
| 1154 |
valid => false, |
| 1155 |
errors => [ |
| 1156 |
{ |
| 1157 |
instanceLocation => '/item/1', |
| 1158 |
keywordLocation => '/properties/item/$ref/unevaluatedItems', |
| 1159 |
absoluteKeywordLocation => 'unevaluatedItems.json#/unevaluatedItems', |
| 1160 |
error => 'additional item not permitted', |
| 1161 |
}, |
| 1162 |
{ |
| 1163 |
instanceLocation => '/item', |
| 1164 |
keywordLocation => '/properties/item/$ref/unevaluatedItems', |
| 1165 |
absoluteKeywordLocation => 'unevaluatedItems.json#/unevaluatedItems', |
| 1166 |
error => 'subschema is not valid against all additional items', |
| 1167 |
}, |
| 1168 |
{ |
| 1169 |
instanceLocation => '/property/bar', |
| 1170 |
keywordLocation => '/properties/property/$ref/unevaluatedProperties', |
| 1171 |
absoluteKeywordLocation => 'unevaluatedProperties.json#/unevaluatedProperties', |
| 1172 |
error => 'additional property not permitted', |
| 1173 |
}, |
| 1174 |
{ |
| 1175 |
instanceLocation => '/property', |
| 1176 |
keywordLocation => '/properties/property/$ref/unevaluatedProperties', |
| 1177 |
absoluteKeywordLocation => 'unevaluatedProperties.json#/unevaluatedProperties', |
| 1178 |
error => 'not all additional properties are valid', |
| 1179 |
}, |
| 1180 |
{ |
| 1181 |
instanceLocation => '', |
| 1182 |
keywordLocation => '/properties', |
| 1183 |
error => 'not all properties are valid', |
| 1184 |
}, |
| 1185 |
], |
| 1186 |
}, |
| 1187 |
'when "collect_annotations" is explicitly set to false, unevaluatedProperties still be used, even in other documents (invalid result)', |
| 1188 |
); |
| 1189 |
|
| 1190 |
|
| 1191 |
$js = JSON::Schema::Modern->new(collect_annotations => 1); |
| 1192 |
|
| 1193 |
is_equal( |
| 1194 |
$js->evaluate( |
| 1195 |
[ 1 ], |
| 1196 |
{ |
| 1197 |
prefixItems => [ true ], |
| 1198 |
unevaluatedItems => false, |
| 1199 |
}, |
| 1200 |
)->TO_JSON, |
| 1201 |
{ |
| 1202 |
valid => true, |
| 1203 |
annotations => [ |
| 1204 |
{ |
| 1205 |
instanceLocation => '', |
| 1206 |
keywordLocation => '/prefixItems', |
| 1207 |
annotation => true, |
| 1208 |
}, |
| 1209 |
], |
| 1210 |
}, |
| 1211 |
'when "collect_annotations" is set to true, unevaluatedItems works, and annotations are returned', |
| 1212 |
); |
| 1213 |
|
| 1214 |
is_equal( |
| 1215 |
$js->evaluate( |
| 1216 |
{ foo => 1 }, |
| 1217 |
{ |
| 1218 |
properties => { foo => true }, |
| 1219 |
unevaluatedProperties => false, |
| 1220 |
}, |
| 1221 |
)->TO_JSON, |
| 1222 |
{ |
| 1223 |
valid => true, |
| 1224 |
annotations => [ |
| 1225 |
{ |
| 1226 |
instanceLocation => '', |
| 1227 |
keywordLocation => '/properties', |
| 1228 |
annotation => [ 'foo' ], |
| 1229 |
}, |
| 1230 |
{ |
| 1231 |
instanceLocation => '', |
| 1232 |
keywordLocation => '/unevaluatedProperties', |
| 1233 |
annotation => [], |
| 1234 |
}, |
| 1235 |
], |
| 1236 |
}, |
| 1237 |
'when "collect_annotations" is set to true, unevaluatedProperties passes, and annotations are returned', |
| 1238 |
); |
| 1239 |
|
| 1240 |
$js = JSON::Schema::Modern->new; |
| 1241 |
|
| 1242 |
is_equal( |
| 1243 |
$js->evaluate( |
| 1244 |
[ 1 ], |
| 1245 |
{ |
| 1246 |
'$id' => 'unevaluatedItems.json', |
| 1247 |
prefixItems => [ true ], |
| 1248 |
unevaluatedItems => false, |
| 1249 |
}, |
| 1250 |
)->TO_JSON, |
| 1251 |
{ |
| 1252 |
valid => true, |
| 1253 |
}, |
| 1254 |
'when "collect_annotations" is not set, unevaluatedItems still works, but annotations are not returned', |
| 1255 |
); |
| 1256 |
|
| 1257 |
is_equal( |
| 1258 |
$js->evaluate( |
| 1259 |
{ foo => 1 }, |
| 1260 |
{ |
| 1261 |
'$id' => 'unevaluatedProperties.json', |
| 1262 |
properties => { foo => true }, |
| 1263 |
unevaluatedProperties => false, |
| 1264 |
}, |
| 1265 |
)->TO_JSON, |
| 1266 |
{ |
| 1267 |
valid => true, |
| 1268 |
}, |
| 1269 |
'when "collect_annotations" is not set, unevaluatedProperties still works, but annotations are not returned', |
| 1270 |
); |
| 1271 |
|
| 1272 |
is_equal( |
| 1273 |
$js->evaluate( |
| 1274 |
{ |
| 1275 |
item => [ 1 ], |
| 1276 |
property => { foo => 1 }, |
| 1277 |
}, |
| 1278 |
{ |
| 1279 |
properties => { |
| 1280 |
item => { '$ref' => 'unevaluatedItems.json' }, |
| 1281 |
property => { '$ref' => 'unevaluatedProperties.json' }, |
| 1282 |
}, |
| 1283 |
}, |
| 1284 |
)->TO_JSON, |
| 1285 |
{ |
| 1286 |
valid => true, |
| 1287 |
}, |
| 1288 |
'... still works when unevaluated keywords are in a separate document', |
| 1289 |
); |
| 1290 |
|
| 1291 |
my $doc_items = $js->add_schema('prefixItems.json', { prefixItems => [ true ] }); |
| 1292 |
|
| 1293 |
my $doc_properties = $js->add_schema('properties.json', { properties => { foo => true } }); |
| 1294 |
|
| 1295 |
is_equal( |
| 1296 |
$js->evaluate( |
| 1297 |
{ |
| 1298 |
item => [ 1 ], |
| 1299 |
property => { foo => 1 }, |
| 1300 |
}, |
| 1301 |
{ |
| 1302 |
properties => { |
| 1303 |
item => { |
| 1304 |
'$ref' => 'prefixItems.json', |
| 1305 |
unevaluatedItems => false, |
| 1306 |
}, |
| 1307 |
property => { |
| 1308 |
'$ref' => 'properties.json', |
| 1309 |
unevaluatedProperties => false, |
| 1310 |
}, |
| 1311 |
}, |
| 1312 |
}, |
| 1313 |
)->TO_JSON, |
| 1314 |
{ |
| 1315 |
valid => true, |
| 1316 |
}, |
| 1317 |
'referenced schemas still produce annotations internally when needed, even when not required to evaluate themselves in isolation', |
| 1318 |
); |
| 1319 |
}; |
| 1320 |
|
| 1321 |
subtest 'annotate unknown keywords' => sub { |
| 1322 |
my $data = { |
| 1323 |
item => [ 1 ], |
| 1324 |
property => { foo => 1 }, |
| 1325 |
}; |
| 1326 |
my $schema = { |
| 1327 |
properties => { |
| 1328 |
item => { |
| 1329 |
items => true, |
| 1330 |
unevaluatedItems => false, |
| 1331 |
bloop => 5, |
| 1332 |
}, |
| 1333 |
property => { |
| 1334 |
properties => { foo => true }, |
| 1335 |
unevaluatedProperties => false, |
| 1336 |
blap => { hi => 1 }, |
| 1337 |
}, |
| 1338 |
}, |
| 1339 |
blip => [ 1, 2, 3 ], |
| 1340 |
}; |
| 1341 |
|
| 1342 |
is_equal( |
| 1343 |
JSON::Schema::Modern->new->evaluate( |
| 1344 |
$data, |
| 1345 |
$schema, |
| 1346 |
)->TO_JSON, |
| 1347 |
{ |
| 1348 |
valid => true, |
| 1349 |
}, |
| 1350 |
'no annotations even when collect_annotations is false', |
| 1351 |
); |
| 1352 |
|
| 1353 |
is_equal( |
| 1354 |
(my $result = JSON::Schema::Modern->new(collect_annotations => 1)->evaluate( |
| 1355 |
$data, |
| 1356 |
$schema, |
| 1357 |
))->TO_JSON, |
| 1358 |
{ |
| 1359 |
valid => true, |
| 1360 |
annotations => [ |
| 1361 |
{ |
| 1362 |
instanceLocation => '/item', |
| 1363 |
keywordLocation => '/properties/item/items', |
| 1364 |
annotation => true, |
| 1365 |
}, |
| 1366 |
{ |
| 1367 |
instanceLocation => '/item', |
| 1368 |
keywordLocation => '/properties/item/bloop', |
| 1369 |
annotation => 5, |
| 1370 |
}, |
| 1371 |
{ |
| 1372 |
instanceLocation => '/property', |
| 1373 |
keywordLocation => '/properties/property/properties', |
| 1374 |
annotation => [ 'foo' ], |
| 1375 |
}, |
| 1376 |
{ |
| 1377 |
instanceLocation => '/property', |
| 1378 |
keywordLocation => '/properties/property/unevaluatedProperties', |
| 1379 |
annotation => [], |
| 1380 |
}, |
| 1381 |
{ |
| 1382 |
instanceLocation => '/property', |
| 1383 |
keywordLocation => '/properties/property/blap', |
| 1384 |
annotation => { hi => 1 }, |
| 1385 |
}, |
| 1386 |
{ |
| 1387 |
instanceLocation => '', |
| 1388 |
keywordLocation => '/properties', |
| 1389 |
annotation => [ 'item', 'property' ], |
| 1390 |
}, |
| 1391 |
{ |
| 1392 |
instanceLocation => '', |
| 1393 |
keywordLocation => '/blip', |
| 1394 |
annotation => [ 1, 2, 3 ], |
| 1395 |
}, |
| 1396 |
], |
| 1397 |
}, |
| 1398 |
'unknown keywords are collected as annotations', |
| 1399 |
); |
| 1400 |
|
| 1401 |
cmp_result( |
| 1402 |
[ $result->annotations ], |
| 1403 |
[ |
| 1404 |
methods(keyword => 'items', unknown => bool(0)), |
| 1405 |
methods(keyword => 'bloop', unknown => bool(1)), |
| 1406 |
methods(keyword => 'properties', unknown => bool(0)), |
| 1407 |
methods(keyword => 'unevaluatedProperties', unknown => bool(0)), |
| 1408 |
methods(keyword => 'blap', unknown => bool(1)), |
| 1409 |
methods(keyword => 'properties', unknown => bool(0)), |
| 1410 |
methods(keyword => 'blip', unknown => bool(1)), |
| 1411 |
], |
| 1412 |
'"unknown" keyword is set on the annotation objects for unknown keywords', |
| 1413 |
); |
| 1414 |
|
| 1415 |
is_equal( |
| 1416 |
JSON::Schema::Modern->new(specification_version => 'draft2019-09', collect_annotations => 1) |
| 1417 |
->evaluate( |
| 1418 |
$data, |
| 1419 |
$schema, |
| 1420 |
)->TO_JSON, |
| 1421 |
{ |
| 1422 |
valid => true, |
| 1423 |
annotations => [ |
| 1424 |
{ |
| 1425 |
instanceLocation => '/item', |
| 1426 |
keywordLocation => '/properties/item/items', |
| 1427 |
annotation => true, |
| 1428 |
}, |
| 1429 |
# no bloop |
| 1430 |
{ |
| 1431 |
instanceLocation => '/property', |
| 1432 |
keywordLocation => '/properties/property/properties', |
| 1433 |
annotation => [ 'foo' ], |
| 1434 |
}, |
| 1435 |
{ |
| 1436 |
instanceLocation => '/property', |
| 1437 |
keywordLocation => '/properties/property/unevaluatedProperties', |
| 1438 |
annotation => [], |
| 1439 |
}, |
| 1440 |
# no blap |
| 1441 |
{ |
| 1442 |
instanceLocation => '', |
| 1443 |
keywordLocation => '/properties', |
| 1444 |
annotation => [ 'item', 'property' ], |
| 1445 |
}, |
| 1446 |
# no blip |
| 1447 |
], |
| 1448 |
}, |
| 1449 |
'no annotations from unknown keywords in draft2019-09', |
| 1450 |
); |
| 1451 |
}; |
| 1452 |
|
| 1453 |
subtest 'items + additionalItems, prefixItems + items' => sub { |
| 1454 |
is_equal( |
| 1455 |
JSON::Schema::Modern->new(specification_version => 'draft2019-09', collect_annotations => 1) |
| 1456 |
->evaluate( |
| 1457 |
[ 1, 2, 3 ], |
| 1458 |
{ |
| 1459 |
items => { maximum => 5 }, |
| 1460 |
additionalItems => { maximum => 0 }, |
| 1461 |
} |
| 1462 |
)->TO_JSON, |
| 1463 |
{ |
| 1464 |
valid => true, |
| 1465 |
annotations => [ |
| 1466 |
{ |
| 1467 |
instanceLocation => '', |
| 1468 |
keywordLocation => '/items', |
| 1469 |
annotation => true, |
| 1470 |
}, |
| 1471 |
# no error nor annotation from additionalItems |
| 1472 |
], |
| 1473 |
}, |
| 1474 |
'schema-based items + additionalItems', |
| 1475 |
); |
| 1476 |
|
| 1477 |
is_equal( |
| 1478 |
JSON::Schema::Modern->new(collect_annotations => 1)->evaluate( |
| 1479 |
[ 1, 2, 3 ], |
| 1480 |
{ |
| 1481 |
prefixItems => [ { maximum => 5 }, { maximum => 5 }, { maximum => 5 } ], |
| 1482 |
items => { maximum => 0 }, |
| 1483 |
} |
| 1484 |
)->TO_JSON, |
| 1485 |
{ |
| 1486 |
valid => true, |
| 1487 |
annotations => [ |
| 1488 |
{ |
| 1489 |
instanceLocation => '', |
| 1490 |
keywordLocation => '/prefixItems', |
| 1491 |
annotation => true, |
| 1492 |
}, |
| 1493 |
# no error nor annotation from items |
| 1494 |
], |
| 1495 |
}, |
| 1496 |
'prefixItems + schema-based items', |
| 1497 |
); |
| 1498 |
}; |
| 1499 |
|
| 1500 |
done_testing; |