| 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 lib 't/lib'; |
| 16 |
use Helper; |
| 17 |
|
| 18 |
my $js = JSON::Schema::Modern->new(short_circuit => 0); |
| 19 |
my $js_short = JSON::Schema::Modern->new(short_circuit => 1); |
| 20 |
|
| 21 |
subtest 'multiple types' => sub { |
| 22 |
my $result = $js->evaluate(true, { type => ['string','number'] }); |
| 23 |
ok(!$result->valid, 'type returned false'); |
| 24 |
is($result->error_count, 1, 'got error count'); |
| 25 |
|
| 26 |
cmp_result( |
| 27 |
[ $result->errors ], |
| 28 |
[ |
| 29 |
all( |
| 30 |
isa('JSON::Schema::Modern::Error'), |
| 31 |
methods( |
| 32 |
instance_location => '', |
| 33 |
keyword_location => '/type', |
| 34 |
absolute_keyword_location => undef, |
| 35 |
error => 'got boolean, not one of string, number', |
| 36 |
), |
| 37 |
), |
| 38 |
], |
| 39 |
'correct error generated from type', |
| 40 |
); |
| 41 |
|
| 42 |
is_equal( |
| 43 |
$result->TO_JSON, |
| 44 |
{ |
| 45 |
valid => false, |
| 46 |
errors => [ |
| 47 |
{ |
| 48 |
instanceLocation => '', |
| 49 |
keywordLocation => '/type', |
| 50 |
error => 'got boolean, not one of string, number', |
| 51 |
}, |
| 52 |
], |
| 53 |
}, |
| 54 |
'result object serializes correctly', |
| 55 |
); |
| 56 |
|
| 57 |
cmp_result( |
| 58 |
my $e = ($result->errors)[0]->clone(error => 'oh noes'), |
| 59 |
methods( |
| 60 |
instance_location => '', |
| 61 |
keyword_location => '/type', |
| 62 |
absolute_keyword_location => undef, |
| 63 |
error => 'oh noes', |
| 64 |
), |
| 65 |
'cloning leaves absolute_keyword_location as-is', |
| 66 |
); |
| 67 |
}; |
| 68 |
|
| 69 |
subtest 'multipleOf' => sub { |
| 70 |
is_equal( |
| 71 |
$js->evaluate(3, { multipleOf => 2 })->TO_JSON, |
| 72 |
{ |
| 73 |
valid => false, |
| 74 |
errors => [ |
| 75 |
{ |
| 76 |
instanceLocation => '', |
| 77 |
keywordLocation => '/multipleOf', |
| 78 |
error => 'value is not a multiple of 2', |
| 79 |
}, |
| 80 |
], |
| 81 |
}, |
| 82 |
'correct error generated from multipleOf', |
| 83 |
); |
| 84 |
}; |
| 85 |
|
| 86 |
subtest 'uniqueItems' => sub { |
| 87 |
is_equal( |
| 88 |
$js->evaluate([qw(a b c d c)], { uniqueItems => true })->TO_JSON, |
| 89 |
{ |
| 90 |
valid => false, |
| 91 |
errors => [ |
| 92 |
{ |
| 93 |
instanceLocation => '', |
| 94 |
keywordLocation => '/uniqueItems', |
| 95 |
error => 'items at indices 2 and 4 are not unique', |
| 96 |
}, |
| 97 |
], |
| 98 |
}, |
| 99 |
'correct error generated from uniqueItems', |
| 100 |
); |
| 101 |
}; |
| 102 |
|
| 103 |
subtest 'allOf, not, and false schema' => sub { |
| 104 |
is_equal( |
| 105 |
$js->evaluate( |
| 106 |
my $data = 1, |
| 107 |
my $schema = { allOf => [ true, false, { not => { not => false } } ] }, |
| 108 |
)->TO_JSON, |
| 109 |
{ |
| 110 |
valid => false, |
| 111 |
errors => [ |
| 112 |
{ |
| 113 |
instanceLocation => '', |
| 114 |
keywordLocation => '/allOf/1', |
| 115 |
error => 'subschema is false', |
| 116 |
}, |
| 117 |
{ |
| 118 |
instanceLocation => '', |
| 119 |
keywordLocation => '/allOf/2/not', |
| 120 |
error => 'subschema is valid', |
| 121 |
}, |
| 122 |
{ |
| 123 |
instanceLocation => '', |
| 124 |
keywordLocation => '/allOf', |
| 125 |
error => 'subschemas 1, 2 are not valid', |
| 126 |
}, |
| 127 |
], |
| 128 |
}, |
| 129 |
'correct errors with locations; did not collect errors inside "not"', |
| 130 |
); |
| 131 |
|
| 132 |
is_equal( |
| 133 |
$js_short->evaluate($data, $schema)->TO_JSON, |
| 134 |
{ |
| 135 |
valid => false, |
| 136 |
errors => [ |
| 137 |
{ |
| 138 |
instanceLocation => '', |
| 139 |
keywordLocation => '/allOf/1', |
| 140 |
error => 'subschema is false', |
| 141 |
}, |
| 142 |
{ |
| 143 |
instanceLocation => '', |
| 144 |
keywordLocation => '/allOf', |
| 145 |
error => 'subschema 1 is not valid', |
| 146 |
}, |
| 147 |
], |
| 148 |
}, |
| 149 |
'short-circuited results contain fewer errors', |
| 150 |
); |
| 151 |
}; |
| 152 |
|
| 153 |
subtest 'anyOf keeps all errors for false paths when invalid, discards errors for false paths when valid' => sub { |
| 154 |
is_equal( |
| 155 |
$js->evaluate( |
| 156 |
my $data = 1, |
| 157 |
my $schema = { anyOf => [ false, false ] }, |
| 158 |
)->TO_JSON, |
| 159 |
my $result = { |
| 160 |
valid => false, |
| 161 |
errors => [ |
| 162 |
{ |
| 163 |
instanceLocation => '', |
| 164 |
keywordLocation => '/anyOf/0', |
| 165 |
error => 'subschema is false', |
| 166 |
}, |
| 167 |
{ |
| 168 |
instanceLocation => '', |
| 169 |
keywordLocation => '/anyOf/1', |
| 170 |
error => 'subschema is false', |
| 171 |
}, |
| 172 |
{ |
| 173 |
instanceLocation => '', |
| 174 |
keywordLocation => '/anyOf', |
| 175 |
error => 'no subschemas are valid', |
| 176 |
}, |
| 177 |
], |
| 178 |
}, |
| 179 |
'correct errors with locations; did not collect errors inside "not"', |
| 180 |
); |
| 181 |
|
| 182 |
is_equal( |
| 183 |
$js_short->evaluate($data, $schema)->TO_JSON, |
| 184 |
$result, |
| 185 |
'short-circuited results contain the same errors (short-circuiting not possible)', |
| 186 |
); |
| 187 |
|
| 188 |
is_equal( |
| 189 |
$result = $js->evaluate(1, { anyOf => [ false, true ], not => true })->TO_JSON, |
| 190 |
{ |
| 191 |
valid => false, |
| 192 |
errors => [ |
| 193 |
{ |
| 194 |
instanceLocation => '', |
| 195 |
keywordLocation => '/not', |
| 196 |
error => 'subschema is true', |
| 197 |
}, |
| 198 |
], |
| 199 |
}, |
| 200 |
'did not collect errors from failure paths from successful anyOf', |
| 201 |
); |
| 202 |
|
| 203 |
is_equal( |
| 204 |
$js->evaluate(1, { anyOf => [ false, true ] })->TO_JSON, |
| 205 |
{ valid => true }, |
| 206 |
'no errors collected for true validation', |
| 207 |
); |
| 208 |
}; |
| 209 |
|
| 210 |
subtest 'applicators with non-boolean subschemas, discarding intermediary errors - items' => sub { |
| 211 |
my $result = $js->evaluate( |
| 212 |
my $data = [ 1, 2 ], |
| 213 |
my $schema = { |
| 214 |
items => { |
| 215 |
anyOf => [ |
| 216 |
{ minimum => 2 }, |
| 217 |
{ allOf => [ { maximum => -1 }, { maximum => 0 } ] }, |
| 218 |
] |
| 219 |
}, |
| 220 |
}, |
| 221 |
); |
| 222 |
# - evaluate /items on instance '' |
| 223 |
# - evaluate /items on instance /0 |
| 224 |
# - evaluate /items/anyOf on instance /0 |
| 225 |
# - evaluate /items/anyOf/0 on instance /0 |
| 226 |
# - evaluate /items/anyOf/0/minimum on instance /0 FAIL |
| 227 |
# /items/anyOf/0 FAILS |
| 228 |
# - evaluate /items/anyOf/1 on instance /0 |
| 229 |
# - evaluate /items/anyOf/1/allOf on instance /0 |
| 230 |
# - evaluate /items/anyOf/1/allOf/0 on instance /0 |
| 231 |
# - evaluate /items/anyOf/1/allOf/0/maximum on instance /0 FAIL |
| 232 |
# - evaluate /items/anyOf/1/allOf/1 on instance /0 |
| 233 |
# - evaluate /items/anyOf/1/allOf/1/maximum on instance /0 FAIL |
| 234 |
# /items/anyOf/1/allOf FAILS |
| 235 |
# /items/anyOf/1 FAILS (no message) |
| 236 |
# /items/anyOf FAILS |
| 237 |
# /items FAILS on instance /0 (no message) |
| 238 |
|
| 239 |
# - evaluate /items on instance /1 |
| 240 |
# - evaluate /items/anyOf on instance /1 |
| 241 |
# - evaluate /items/anyOf/0 on instance /1 |
| 242 |
# - evaluate /items/anyOf/0/minimum on instance /1 PASS |
| 243 |
# /items/anyOf/0 PASSES |
| 244 |
# - evaluate /items/anyOf/1 on instance /1 |
| 245 |
# - evaluate /items/anyOf/1/allOf on instance /1 |
| 246 |
# - evaluate /items/anyOf/1/allOf/0 on instance /1 |
| 247 |
# - evaluate /items/anyOf/1/allOf/0/maximum on instance /1 FAIL |
| 248 |
# - evaluate /items/anyOf/1/allOf/1 on instance /1 |
| 249 |
# - evaluate /items/anyOf/1/allOf/1/maximum on instance /1 FAIL |
| 250 |
# /items/anyOf/1/allOf FAILS |
| 251 |
# /items/anyOf/1 FAILS (no message) |
| 252 |
# /items/anyOf PASSES -- all failures above are discarded |
| 253 |
# /items PASSES on instance /1 |
| 254 |
# /items FAILS (across all instances) |
| 255 |
# entire schema FAILS |
| 256 |
|
| 257 |
is_equal( |
| 258 |
$result->TO_JSON, |
| 259 |
{ |
| 260 |
valid => false, |
| 261 |
errors => [ |
| 262 |
{ |
| 263 |
instanceLocation => '/0', |
| 264 |
keywordLocation => '/items/anyOf/0/minimum', |
| 265 |
error => 'value is less than 2', |
| 266 |
}, |
| 267 |
{ |
| 268 |
instanceLocation => '/0', |
| 269 |
keywordLocation => '/items/anyOf/1/allOf/0/maximum', |
| 270 |
error => 'value is greater than -1', |
| 271 |
}, |
| 272 |
{ |
| 273 |
instanceLocation => '/0', |
| 274 |
keywordLocation => '/items/anyOf/1/allOf/1/maximum', |
| 275 |
error => 'value is greater than 0', |
| 276 |
}, |
| 277 |
{ |
| 278 |
instanceLocation => '/0', |
| 279 |
keywordLocation => '/items/anyOf/1/allOf', |
| 280 |
error => 'subschemas 0, 1 are not valid', |
| 281 |
}, |
| 282 |
{ |
| 283 |
instanceLocation => '/0', |
| 284 |
keywordLocation => '/items/anyOf', |
| 285 |
error => 'no subschemas are valid', |
| 286 |
}, |
| 287 |
# these errors are discarded because /items/anyOf passes on instance /1 |
| 288 |
#{ |
| 289 |
# instanceLocation => '/1', |
| 290 |
# keywordLocation => '/items/anyOf/1/allOf/0/maximum', |
| 291 |
# error => 'value is greater than -1', |
| 292 |
#}, |
| 293 |
#{ |
| 294 |
# instanceLocation => '/1', |
| 295 |
# keywordLocation => '/items/anyOf/1/allOf/1/maximum', |
| 296 |
# error => 'value is greater than 0', |
| 297 |
#}, |
| 298 |
#{ |
| 299 |
# instanceLocation => '/1', |
| 300 |
# keywordLocation => '/items/anyOf/1/allOf', |
| 301 |
# error => 'subschemas 0, 1 are not valid', |
| 302 |
#}, |
| 303 |
{ |
| 304 |
instanceLocation => '', |
| 305 |
keywordLocation => '/items', |
| 306 |
error => 'subschema is not valid against all items', |
| 307 |
}, |
| 308 |
], |
| 309 |
}, |
| 310 |
'collected all errors from subschemas for failing branches only (passing branches discard errors)', |
| 311 |
); |
| 312 |
|
| 313 |
is_equal( |
| 314 |
$js_short->evaluate($data, $schema)->TO_JSON, |
| 315 |
{ |
| 316 |
valid => false, |
| 317 |
errors => [ |
| 318 |
{ |
| 319 |
instanceLocation => '/0', |
| 320 |
keywordLocation => '/items/anyOf/0/minimum', |
| 321 |
error => 'value is less than 2' |
| 322 |
}, |
| 323 |
{ |
| 324 |
instanceLocation => '/0', |
| 325 |
keywordLocation => '/items/anyOf/1/allOf/0/maximum', |
| 326 |
error => 'value is greater than -1', |
| 327 |
}, |
| 328 |
{ |
| 329 |
instanceLocation => '/0', |
| 330 |
keywordLocation => '/items/anyOf/1/allOf', |
| 331 |
error => 'subschema 0 is not valid', |
| 332 |
}, |
| 333 |
{ |
| 334 |
instanceLocation => '/0', |
| 335 |
keywordLocation => '/items/anyOf', |
| 336 |
error => 'no subschemas are valid', |
| 337 |
}, |
| 338 |
{ |
| 339 |
instanceLocation => '', |
| 340 |
keywordLocation => '/items', |
| 341 |
error => 'subschema is not valid against all items', |
| 342 |
}, |
| 343 |
], |
| 344 |
}, |
| 345 |
'short-circuited results contain fewer errors', |
| 346 |
); |
| 347 |
}; |
| 348 |
|
| 349 |
subtest 'applicators with non-boolean subschemas, discarding intermediary errors - contains' => sub { |
| 350 |
my $result = $js->evaluate( |
| 351 |
my $data = [ |
| 352 |
{ foo => 1 }, |
| 353 |
{ bar => 2 }, |
| 354 |
], |
| 355 |
my $schema = { |
| 356 |
not => true, |
| 357 |
contains => { |
| 358 |
properties => { |
| 359 |
foo => false, # if 'foo' is present, then we fail |
| 360 |
}, |
| 361 |
}, |
| 362 |
}, |
| 363 |
); |
| 364 |
|
| 365 |
# - evaluate /not on instance '' |
| 366 |
# - evaluate subschema "true" - PASS |
| 367 |
# /not FAILS. |
| 368 |
# - evaluate /contains on instance '' |
| 369 |
# - evaluate /contains on instance /0 |
| 370 |
# - evaluate /contains/properties on instance /0 |
| 371 |
# - evaluate /contains/properties/foo on instance /0/foo |
| 372 |
# schema is FALSE. |
| 373 |
# /contains/properties FAILS |
| 374 |
# /contains does not match on instance /0 |
| 375 |
# - evaluate /contains on instance /1 |
| 376 |
# - evaluate /contains/properties on instance /1 |
| 377 |
# - evaluate /contains/properties/foo on instance /1/foo - does not exist. |
| 378 |
# /contains/properties/foo PASSES |
| 379 |
# /contains/properties PASSES |
| 380 |
# /contains matches on instance /1 |
| 381 |
# /contains has at least 1 match; it PASSES |
| 382 |
# entire schema FAILS |
| 383 |
|
| 384 |
is_equal( |
| 385 |
$result->TO_JSON, |
| 386 |
{ |
| 387 |
valid => false, |
| 388 |
errors => [ |
| 389 |
{ |
| 390 |
instanceLocation => '', |
| 391 |
keywordLocation => '/not', |
| 392 |
error => 'subschema is true', |
| 393 |
}, |
| 394 |
# these errors are discarded because /contains passes on instance /1 |
| 395 |
#{ |
| 396 |
# instanceLocation => '/0/foo', |
| 397 |
# keywordLocation => '/contains/properties/foo', |
| 398 |
# error => 'subschema is false', |
| 399 |
#}, |
| 400 |
#{ |
| 401 |
# instanceLocation => '/0', |
| 402 |
# keywordLocation => '/contains/properties', |
| 403 |
# error => 'not all properties are valid', |
| 404 |
#}, |
| 405 |
], |
| 406 |
}, |
| 407 |
'collected all errors from subschemas for failing branches only (passing branches discard errors)', |
| 408 |
); |
| 409 |
|
| 410 |
is_equal( |
| 411 |
$js_short->evaluate($data, $schema)->TO_JSON, |
| 412 |
{ |
| 413 |
valid => false, |
| 414 |
errors => [ |
| 415 |
{ |
| 416 |
instanceLocation => '', |
| 417 |
keywordLocation => '/not', |
| 418 |
error => 'subschema is true', |
| 419 |
}, |
| 420 |
], |
| 421 |
}, |
| 422 |
'short-circuited results contain the same errors', |
| 423 |
); |
| 424 |
}; |
| 425 |
|
| 426 |
subtest 'errors with $refs' => sub { |
| 427 |
my $result = $js->evaluate( |
| 428 |
[ { x => 1 }, { x => 2 }, { x => 3 } ], |
| 429 |
{ |
| 430 |
'$defs' => { |
| 431 |
mydef => { |
| 432 |
type => 'integer', |
| 433 |
minimum => 5, |
| 434 |
'$ref' => '#/$defs/myint', |
| 435 |
}, |
| 436 |
myint => { |
| 437 |
multipleOf => 5, |
| 438 |
}, |
| 439 |
}, |
| 440 |
items => { |
| 441 |
properties => { |
| 442 |
x => { |
| 443 |
'$ref' => '#/$defs/mydef', |
| 444 |
maximum => 2, |
| 445 |
}, |
| 446 |
}, |
| 447 |
} |
| 448 |
}, |
| 449 |
); |
| 450 |
|
| 451 |
# evaluation order: |
| 452 |
# /items/properties/x/$ref (mydef) /$ref (myint) /multipleOf |
| 453 |
# /items/properties/x/$ref (mydef) /type |
| 454 |
# /items/properties/x/$ref (mydef) /minimum |
| 455 |
# /items/properties/x/maximum |
| 456 |
|
| 457 |
is_equal( |
| 458 |
$result->TO_JSON, |
| 459 |
{ |
| 460 |
valid => false, |
| 461 |
errors => [ |
| 462 |
{ |
| 463 |
instanceLocation => '/0/x', |
| 464 |
keywordLocation => '/items/properties/x/$ref/$ref/multipleOf', |
| 465 |
absoluteKeywordLocation => '#/$defs/myint/multipleOf', |
| 466 |
error => 'value is not a multiple of 5', |
| 467 |
}, |
| 468 |
{ |
| 469 |
instanceLocation => '/0/x', |
| 470 |
keywordLocation => '/items/properties/x/$ref/minimum', |
| 471 |
absoluteKeywordLocation => '#/$defs/mydef/minimum', |
| 472 |
error => 'value is less than 5', |
| 473 |
}, |
| 474 |
{ |
| 475 |
instanceLocation => '/0', |
| 476 |
keywordLocation => '/items/properties', |
| 477 |
error => 'not all properties are valid', |
| 478 |
}, |
| 479 |
{ |
| 480 |
instanceLocation => '/1/x', |
| 481 |
keywordLocation => '/items/properties/x/$ref/$ref/multipleOf', |
| 482 |
absoluteKeywordLocation => '#/$defs/myint/multipleOf', |
| 483 |
error => 'value is not a multiple of 5', |
| 484 |
}, |
| 485 |
{ |
| 486 |
instanceLocation => '/1/x', |
| 487 |
keywordLocation => '/items/properties/x/$ref/minimum', |
| 488 |
absoluteKeywordLocation => '#/$defs/mydef/minimum', |
| 489 |
error => 'value is less than 5', |
| 490 |
}, |
| 491 |
{ |
| 492 |
instanceLocation => '/1', |
| 493 |
keywordLocation => '/items/properties', |
| 494 |
error => 'not all properties are valid', |
| 495 |
}, |
| 496 |
{ |
| 497 |
instanceLocation => '/2/x', |
| 498 |
keywordLocation => '/items/properties/x/$ref/$ref/multipleOf', |
| 499 |
absoluteKeywordLocation => '#/$defs/myint/multipleOf', |
| 500 |
error => 'value is not a multiple of 5', |
| 501 |
}, |
| 502 |
{ |
| 503 |
instanceLocation => '/2/x', |
| 504 |
keywordLocation => '/items/properties/x/$ref/minimum', |
| 505 |
absoluteKeywordLocation => '#/$defs/mydef/minimum', |
| 506 |
error => 'value is less than 5', |
| 507 |
}, |
| 508 |
{ |
| 509 |
instanceLocation => '/2/x', |
| 510 |
keywordLocation => '/items/properties/x/maximum', |
| 511 |
error => 'value is greater than 2', |
| 512 |
}, |
| 513 |
{ |
| 514 |
instanceLocation => '/2', |
| 515 |
keywordLocation => '/items/properties', |
| 516 |
error => 'not all properties are valid', |
| 517 |
}, |
| 518 |
{ |
| 519 |
instanceLocation => '', |
| 520 |
keywordLocation => '/items', |
| 521 |
error => 'subschema is not valid against all items', |
| 522 |
}, |
| 523 |
], |
| 524 |
}, |
| 525 |
'errors have correct absolute keyword location via $ref', |
| 526 |
); |
| 527 |
}; |
| 528 |
|
| 529 |
subtest 'const and enum' => sub { |
| 530 |
is_equal( |
| 531 |
$js->evaluate( |
| 532 |
{ foo => { a => { b => { c => { d => 1 } } } } }, |
| 533 |
{ |
| 534 |
properties => { |
| 535 |
foo => { |
| 536 |
allOf => [ |
| 537 |
{ const => { a => { b => { c => { d => 2 } } } } }, |
| 538 |
{ enum => [ 0, 'whargarbl', { a => { b => { c => { d => 2 } } } } ] }, |
| 539 |
], |
| 540 |
} |
| 541 |
}, |
| 542 |
}, |
| 543 |
)->TO_JSON, |
| 544 |
{ |
| 545 |
valid => false, |
| 546 |
errors => [ |
| 547 |
{ |
| 548 |
instanceLocation => '/foo', |
| 549 |
keywordLocation => '/properties/foo/allOf/0/const', |
| 550 |
error => 'value does not match (at \'/a/b/c/d\': integers not equal)', |
| 551 |
}, |
| 552 |
{ |
| 553 |
instanceLocation => '/foo', |
| 554 |
keywordLocation => '/properties/foo/allOf/1/enum', |
| 555 |
error => 'value does not match (from enum 0: wrong type: object vs integer; from enum 1: wrong type: object vs string; from enum 2 at \'/a/b/c/d\': integers not equal)', |
| 556 |
}, |
| 557 |
{ |
| 558 |
instanceLocation => '/foo', |
| 559 |
keywordLocation => '/properties/foo/allOf', |
| 560 |
error => 'subschemas 0, 1 are not valid', |
| 561 |
}, |
| 562 |
{ |
| 563 |
instanceLocation => '', |
| 564 |
keywordLocation => '/properties', |
| 565 |
error => 'not all properties are valid', |
| 566 |
}, |
| 567 |
], |
| 568 |
}, |
| 569 |
'got details about object differences in errors from const and enum', |
| 570 |
); |
| 571 |
}; |
| 572 |
|
| 573 |
subtest 'exceptions' => sub { |
| 574 |
cmp_result( |
| 575 |
(my $result = $js->evaluate_json_string('[ 1, 2, 3, whargarbl ]', true))->TO_JSON, |
| 576 |
{ |
| 577 |
valid => false, |
| 578 |
errors => [ |
| 579 |
{ |
| 580 |
keywordLocation => '', |
| 581 |
error => re(qr/^malformed JSON string/), |
| 582 |
}, |
| 583 |
], |
| 584 |
}, |
| 585 |
'attempting to evaluate a json string returns the exception as an error', |
| 586 |
); |
| 587 |
ok($result->exception, 'exception flag is true on the result'); |
| 588 |
|
| 589 |
is_equal( |
| 590 |
($result = $js->evaluate( |
| 591 |
{ x => 'hello' }, |
| 592 |
{ |
| 593 |
allOf => [ |
| 594 |
{ properties => { x => 1 } }, |
| 595 |
{ properties => { x => 'hi' } }, |
| 596 |
], |
| 597 |
} |
| 598 |
))->TO_JSON, |
| 599 |
{ |
| 600 |
valid => false, |
| 601 |
errors => [ |
| 602 |
{ |
| 603 |
keywordLocation => '/allOf/0/properties/x', |
| 604 |
error => 'invalid schema type: integer', |
| 605 |
}, |
| 606 |
{ |
| 607 |
keywordLocation => '/allOf/1/properties/x', |
| 608 |
error => 'invalid schema type: string', |
| 609 |
}, |
| 610 |
], |
| 611 |
}, |
| 612 |
'a subschema of an invalid type returns an error at the right position, and traversal continues', |
| 613 |
); |
| 614 |
ok($result->exception, 'exception flag is true on the result'); |
| 615 |
|
| 616 |
is_equal( |
| 617 |
($result = $js->evaluate( |
| 618 |
1, |
| 619 |
{ |
| 620 |
allOf => [ |
| 621 |
{ type => 'whargarbl' }, |
| 622 |
{ type => 'whoops' }, |
| 623 |
], |
| 624 |
} |
| 625 |
))->TO_JSON, |
| 626 |
{ |
| 627 |
valid => false, |
| 628 |
errors => [ |
| 629 |
{ |
| 630 |
keywordLocation => '/allOf/0/type', |
| 631 |
error => 'unrecognized type "whargarbl"', |
| 632 |
}, |
| 633 |
{ |
| 634 |
keywordLocation => '/allOf/1/type', |
| 635 |
error => 'unrecognized type "whoops"', |
| 636 |
}, |
| 637 |
], |
| 638 |
}, |
| 639 |
'invalid argument to "type" returns an error at the right position, and evaluation continues', |
| 640 |
); |
| 641 |
ok($result->exception, 'exception flag is true on the result'); |
| 642 |
}; |
| 643 |
|
| 644 |
subtest 'errors after crossing multiple $refs using $id and $anchor' => sub { |
| 645 |
is_equal( |
| 646 |
$js->evaluate( |
| 647 |
1, |
| 648 |
{ |
| 649 |
'$id' => 'base.json', |
| 650 |
'$defs' => { |
| 651 |
def1 => { |
| 652 |
'$comment' => 'canonical uri: "def1.json"', |
| 653 |
'$id' => 'def1.json', |
| 654 |
'$ref' => 'base.json#/$defs/myint', |
| 655 |
type => 'integer', |
| 656 |
maximum => -1, |
| 657 |
minimum => 5, |
| 658 |
}, |
| 659 |
myint => { |
| 660 |
'$comment' => 'canonical uri: "def2.json"', |
| 661 |
'$id' => 'def2.json', |
| 662 |
'$ref' => 'base.json#my_not', |
| 663 |
multipleOf => 5, |
| 664 |
exclusiveMaximum => 1, |
| 665 |
}, |
| 666 |
mynot => { |
| 667 |
'$comment' => 'canonical uri: "base.json#/$defs/mynot"', |
| 668 |
'$anchor' => 'my_not', |
| 669 |
'$ref' => 'http://localhost:4242/object.json', |
| 670 |
not => true, |
| 671 |
}, |
| 672 |
myobject => { |
| 673 |
'$id' => 'http://localhost:4242/object.json', |
| 674 |
type => 'object', |
| 675 |
anyOf => [ false ], |
| 676 |
}, |
| 677 |
}, |
| 678 |
'$ref' => '#/$defs/def1', |
| 679 |
}, |
| 680 |
)->TO_JSON, |
| 681 |
{ |
| 682 |
valid => false, |
| 683 |
errors => [ |
| 684 |
{ |
| 685 |
instanceLocation => '', |
| 686 |
keywordLocation => '/$ref/$ref/$ref/$ref/type', |
| 687 |
absoluteKeywordLocation => 'http://localhost:4242/object.json#/type', |
| 688 |
error => 'got integer, not object', |
| 689 |
}, |
| 690 |
{ |
| 691 |
instanceLocation => '', |
| 692 |
keywordLocation => '/$ref/$ref/$ref/$ref/anyOf/0', |
| 693 |
absoluteKeywordLocation => 'http://localhost:4242/object.json#/anyOf/0', |
| 694 |
error => 'subschema is false', |
| 695 |
}, |
| 696 |
{ |
| 697 |
instanceLocation => '', |
| 698 |
keywordLocation => '/$ref/$ref/$ref/$ref/anyOf', |
| 699 |
absoluteKeywordLocation => 'http://localhost:4242/object.json#/anyOf', |
| 700 |
error => 'no subschemas are valid', |
| 701 |
}, |
| 702 |
{ |
| 703 |
instanceLocation => '', |
| 704 |
keywordLocation => '/$ref/$ref/$ref/not', |
| 705 |
absoluteKeywordLocation => 'base.json#/$defs/mynot/not', |
| 706 |
error => 'subschema is true', |
| 707 |
}, |
| 708 |
{ |
| 709 |
instanceLocation => '', |
| 710 |
keywordLocation => '/$ref/$ref/multipleOf', |
| 711 |
absoluteKeywordLocation => 'def2.json#/multipleOf', |
| 712 |
error => 'value is not a multiple of 5', |
| 713 |
}, |
| 714 |
{ |
| 715 |
instanceLocation => '', |
| 716 |
keywordLocation => '/$ref/$ref/exclusiveMaximum', |
| 717 |
absoluteKeywordLocation => 'def2.json#/exclusiveMaximum', |
| 718 |
error => 'value is greater than or equal to 1', |
| 719 |
}, |
| 720 |
{ |
| 721 |
instanceLocation => '', |
| 722 |
keywordLocation => '/$ref/maximum', |
| 723 |
absoluteKeywordLocation => 'def1.json#/maximum', |
| 724 |
error => 'value is greater than -1', |
| 725 |
}, |
| 726 |
{ |
| 727 |
instanceLocation => '', |
| 728 |
keywordLocation => '/$ref/minimum', |
| 729 |
absoluteKeywordLocation => 'def1.json#/minimum', |
| 730 |
error => 'value is less than 5', |
| 731 |
}, |
| 732 |
], |
| 733 |
}, |
| 734 |
'errors have correct absolute keyword location via $ref', |
| 735 |
); |
| 736 |
|
| 737 |
is_equal( |
| 738 |
$js->evaluate( |
| 739 |
1, |
| 740 |
{ |
| 741 |
'$id' => 'http://localhost:1234/hello', |
| 742 |
'$defs' => { |
| 743 |
foo => { |
| 744 |
'$id' => 'http://localhost:1234/a/b.json', |
| 745 |
'$defs' => { |
| 746 |
bar => { |
| 747 |
'$anchor' => 'my_anchor', |
| 748 |
'$defs' => { |
| 749 |
baz => { |
| 750 |
'$anchor' => 'another_anchor', |
| 751 |
not => true |
| 752 |
}, |
| 753 |
}, |
| 754 |
}, |
| 755 |
}, |
| 756 |
}, |
| 757 |
}, |
| 758 |
'$ref' => 'http://localhost:1234/hello#/$defs/foo/$defs/bar/$defs/baz', |
| 759 |
}, |
| 760 |
)->TO_JSON, |
| 761 |
{ |
| 762 |
valid => false, |
| 763 |
errors => [ |
| 764 |
{ |
| 765 |
instanceLocation => '', |
| 766 |
keywordLocation => '/$ref/not', |
| 767 |
absoluteKeywordLocation => 'http://localhost:1234/a/b.json#/$defs/bar/$defs/baz/not', |
| 768 |
error => 'subschema is true', |
| 769 |
}, |
| 770 |
], |
| 771 |
}, |
| 772 |
'absolute keyword location is correct, even when not used in the $ref', |
| 773 |
); |
| 774 |
}; |
| 775 |
|
| 776 |
subtest 'unresolvable $ref to a remote resource' => sub { |
| 777 |
# new evaluator, with no resources remembered |
| 778 |
my $js = JSON::Schema::Modern->new; |
| 779 |
|
| 780 |
is_equal( |
| 781 |
(my $result = $js->evaluate( |
| 782 |
1, |
| 783 |
{ |
| 784 |
'$id' => 'http://localhost:4242/foo/bar/top_id.json', |
| 785 |
'$ref' => '/baz/myint.json', |
| 786 |
'$defs' => { |
| 787 |
myint => { |
| 788 |
'$id' => '/baz/myint.json', |
| 789 |
'$ref' => 'does-not-exist.json', |
| 790 |
}, |
| 791 |
}, |
| 792 |
anyOf => [ false ], |
| 793 |
}, |
| 794 |
))->TO_JSON, |
| 795 |
{ |
| 796 |
valid => false, |
| 797 |
errors => [ |
| 798 |
{ |
| 799 |
instanceLocation => '', |
| 800 |
keywordLocation => '/$ref/$ref', |
| 801 |
absoluteKeywordLocation => 'http://localhost:4242/baz/myint.json#/$ref', |
| 802 |
error => 'EXCEPTION: unable to find resource "http://localhost:4242/baz/does-not-exist.json"', |
| 803 |
}, |
| 804 |
], |
| 805 |
}, |
| 806 |
'error for a bad $ref reports the correct absolute location that was referred to', |
| 807 |
); |
| 808 |
ok($result->exception, 'exception flag is true on the result'); |
| 809 |
}; |
| 810 |
|
| 811 |
subtest 'unresolvable $ref to remote plain-name fragment' => sub { |
| 812 |
is_equal( |
| 813 |
(my $result = $js->evaluate(1, { '$ref' => 'http://localhost:4242#nowhere' }))->TO_JSON, |
| 814 |
{ |
| 815 |
valid => false, |
| 816 |
errors => [ |
| 817 |
{ |
| 818 |
instanceLocation => '', |
| 819 |
keywordLocation => '/$ref', |
| 820 |
error => 'EXCEPTION: unable to find resource "http://localhost:4242#nowhere"', |
| 821 |
}, |
| 822 |
], |
| 823 |
}, |
| 824 |
'properly handled a bad $ref to an anchor', |
| 825 |
); |
| 826 |
ok($result->exception, 'exception flag is true on the result'); |
| 827 |
}; |
| 828 |
|
| 829 |
subtest 'abort due to a schema error' => sub { |
| 830 |
is_equal( |
| 831 |
$js->evaluate( |
| 832 |
1, |
| 833 |
{ |
| 834 |
oneOf => [ |
| 835 |
{ type => 'number' }, |
| 836 |
{ type => 'string' }, |
| 837 |
{ type => 'whargarbl' }, |
| 838 |
], |
| 839 |
} |
| 840 |
)->TO_JSON, |
| 841 |
{ |
| 842 |
valid => false, |
| 843 |
errors => [ |
| 844 |
{ |
| 845 |
keywordLocation => '/oneOf/2/type', |
| 846 |
error => 'unrecognized type "whargarbl"', |
| 847 |
}, |
| 848 |
], |
| 849 |
}, |
| 850 |
'exception inside a oneOf (where errors are localized) are still included in the result', |
| 851 |
); |
| 852 |
}; |
| 853 |
|
| 854 |
subtest 'sorted property names' => sub { |
| 855 |
is_equal( |
| 856 |
$js->evaluate( |
| 857 |
{ foo => 1, bar => 1, baz => 1, hello => 1 }, |
| 858 |
{ |
| 859 |
properties => { |
| 860 |
foo => false, |
| 861 |
bar => false, |
| 862 |
}, |
| 863 |
additionalProperties => false, |
| 864 |
} |
| 865 |
)->TO_JSON, |
| 866 |
{ |
| 867 |
valid => false, |
| 868 |
errors => [ |
| 869 |
{ |
| 870 |
instanceLocation => '/bar', |
| 871 |
keywordLocation => '/properties/bar', |
| 872 |
error => 'property not permitted', |
| 873 |
}, |
| 874 |
{ |
| 875 |
instanceLocation => '/foo', |
| 876 |
keywordLocation => '/properties/foo', |
| 877 |
error => 'property not permitted', |
| 878 |
}, |
| 879 |
{ |
| 880 |
instanceLocation => '', |
| 881 |
keywordLocation => '/properties', |
| 882 |
error => 'not all properties are valid', |
| 883 |
}, |
| 884 |
{ |
| 885 |
instanceLocation => '/baz', |
| 886 |
keywordLocation => '/additionalProperties', |
| 887 |
error => 'additional property not permitted', |
| 888 |
}, |
| 889 |
{ |
| 890 |
instanceLocation => '/hello', |
| 891 |
keywordLocation => '/additionalProperties', |
| 892 |
error => 'additional property not permitted', |
| 893 |
}, |
| 894 |
{ |
| 895 |
instanceLocation => '', |
| 896 |
keywordLocation => '/additionalProperties', |
| 897 |
error => 'not all additional properties are valid', |
| 898 |
}, |
| 899 |
], |
| 900 |
}, |
| 901 |
'property names are considered in sorted order', |
| 902 |
); |
| 903 |
}; |
| 904 |
|
| 905 |
subtest 'bad regex in schema' => sub { |
| 906 |
cmp_result( |
| 907 |
$js->evaluate( |
| 908 |
{ |
| 909 |
my_pattern => 'foo', |
| 910 |
my_patternProperties => { foo => 1 }, |
| 911 |
}, |
| 912 |
my $schema = { |
| 913 |
type => 'object', |
| 914 |
properties => { |
| 915 |
my_pattern => { |
| 916 |
type => 'string', |
| 917 |
pattern => '(', |
| 918 |
}, |
| 919 |
my_patternProperties => { |
| 920 |
type => 'object', |
| 921 |
patternProperties => { '(' => true }, |
| 922 |
additionalProperties => false, |
| 923 |
}, |
| 924 |
my_runtime_pattern => { |
| 925 |
type => 'string', |
| 926 |
pattern => '\p{main::IsFoo}', # qr/$pattern/ will not find this error, but m/$pattern/ will |
| 927 |
}, |
| 928 |
}, |
| 929 |
}, |
| 930 |
)->TO_JSON, |
| 931 |
{ |
| 932 |
valid => false, |
| 933 |
errors => [ |
| 934 |
{ |
| 935 |
keywordLocation => '/properties/my_pattern/pattern', |
| 936 |
error => re(qr/^Unmatched \( in regex/), |
| 937 |
}, |
| 938 |
{ |
| 939 |
keywordLocation => '/properties/my_patternProperties/patternProperties/(', |
| 940 |
error => re(qr/^Unmatched \( in regex/), |
| 941 |
}, |
| 942 |
# Note no error for missing IsFoo |
| 943 |
], |
| 944 |
}, |
| 945 |
'bad "pattern" and "patternProperties" regexes are properly noted in error', |
| 946 |
); |
| 947 |
|
| 948 |
cmp_result( |
| 949 |
$js->evaluate( |
| 950 |
{ my_runtime_pattern => 'foo' }, |
| 951 |
$schema = { |
| 952 |
$schema->%{type}, |
| 953 |
properties => +{ $schema->{properties}->%{my_runtime_pattern} }, |
| 954 |
}, |
| 955 |
)->TO_JSON, |
| 956 |
{ |
| 957 |
valid => false, |
| 958 |
errors => [ |
| 959 |
{ |
| 960 |
instanceLocation => '/my_runtime_pattern', |
| 961 |
keywordLocation => '/properties/my_runtime_pattern/pattern', |
| 962 |
# in 5.28 and earlier: Can't find Unicode property definition "IsFoo" |
| 963 |
# in 5.30 and later: Unknown user-defined property name \p{main::IsFoo} |
| 964 |
error => re(qr/^EXCEPTION: .*property.*IsFoo/), |
| 965 |
}, |
| 966 |
], |
| 967 |
}, |
| 968 |
'bad "pattern" regex is properly noted in error', |
| 969 |
); |
| 970 |
|
| 971 |
no warnings 'once'; |
| 972 |
*IsFoo = sub { "0066\n006F\n" }; # accepts 'f', 'o' |
| 973 |
|
| 974 |
is_equal( |
| 975 |
$js->evaluate( |
| 976 |
{ my_runtime_pattern => 'foo' }, |
| 977 |
$schema, |
| 978 |
)->TO_JSON, |
| 979 |
{ |
| 980 |
valid => true, |
| 981 |
}, |
| 982 |
'"pattern" regex is now valid, due to the Unicode property becoming defined', |
| 983 |
); |
| 984 |
}; |
| 985 |
|
| 986 |
subtest 'JSON pointer escaping' => sub { |
| 987 |
is_equal( |
| 988 |
$js->evaluate( |
| 989 |
{ '{}' => { 'my~tilde/slash-property' => 1 } }, |
| 990 |
my $schema = { |
| 991 |
'$defs' => { |
| 992 |
mydef => { |
| 993 |
properties => { |
| 994 |
'{}' => { |
| 995 |
properties => { |
| 996 |
'my~tilde/slash-property' => false, |
| 997 |
}, |
| 998 |
patternProperties => { |
| 999 |
'/' => { minimum => 6 }, |
| 1000 |
'[~/]' => { minimum => 7 }, |
| 1001 |
'~' => { minimum => 5 }, |
| 1002 |
'~.*/' => false, |
| 1003 |
}, |
| 1004 |
}, |
| 1005 |
}, |
| 1006 |
}, |
| 1007 |
}, |
| 1008 |
'$ref' => '#/$defs/mydef', |
| 1009 |
}, |
| 1010 |
)->TO_JSON, |
| 1011 |
{ |
| 1012 |
valid => false, |
| 1013 |
errors => my $errors = [ |
| 1014 |
{ |
| 1015 |
instanceLocation => '/{}/my~0tilde~1slash-property', |
| 1016 |
keywordLocation => '/$ref/properties/{}/properties/my~0tilde~1slash-property', |
| 1017 |
absoluteKeywordLocation => '#/$defs/mydef/properties/%7B%7D/properties/my~0tilde~1slash-property', |
| 1018 |
error => 'property not permitted', |
| 1019 |
}, |
| 1020 |
{ |
| 1021 |
instanceLocation => '/{}', |
| 1022 |
keywordLocation => '/$ref/properties/{}/properties', |
| 1023 |
absoluteKeywordLocation => '#/$defs/mydef/properties/%7B%7D/properties', |
| 1024 |
error => 'not all properties are valid', |
| 1025 |
}, |
| 1026 |
{ |
| 1027 |
instanceLocation => '/{}/my~0tilde~1slash-property', |
| 1028 |
keywordLocation => '/$ref/properties/{}/patternProperties/~1/minimum', |
| 1029 |
absoluteKeywordLocation => '#/$defs/mydef/properties/%7B%7D/patternProperties/~1/minimum', # / |
| 1030 |
error => 'value is less than 6', |
| 1031 |
}, |
| 1032 |
{ |
| 1033 |
instanceLocation => '/{}/my~0tilde~1slash-property', |
| 1034 |
keywordLocation => '/$ref/properties/{}/patternProperties/[~0~1]/minimum', |
| 1035 |
absoluteKeywordLocation => '#/$defs/mydef/properties/%7B%7D/patternProperties/%5B~0~1%5D/minimum', # [~/] |
| 1036 |
error => 'value is less than 7', |
| 1037 |
}, |
| 1038 |
{ |
| 1039 |
instanceLocation => '/{}/my~0tilde~1slash-property', |
| 1040 |
keywordLocation => '/$ref/properties/{}/patternProperties/~0/minimum', |
| 1041 |
absoluteKeywordLocation => '#/$defs/mydef/properties/%7B%7D/patternProperties/~0/minimum', # ~ |
| 1042 |
error => 'value is less than 5', |
| 1043 |
}, |
| 1044 |
{ |
| 1045 |
instanceLocation => '/{}/my~0tilde~1slash-property', |
| 1046 |
keywordLocation => '/$ref/properties/{}/patternProperties/~0.*~1', |
| 1047 |
absoluteKeywordLocation => '#/$defs/mydef/properties/%7B%7D/patternProperties/~0.*~1', # ~.*/ |
| 1048 |
error => 'property not permitted', |
| 1049 |
}, |
| 1050 |
{ |
| 1051 |
instanceLocation => '/{}', |
| 1052 |
keywordLocation => '/$ref/properties/{}/patternProperties', |
| 1053 |
absoluteKeywordLocation => '#/$defs/mydef/properties/%7B%7D/patternProperties', |
| 1054 |
error => 'not all properties are valid', |
| 1055 |
}, |
| 1056 |
{ |
| 1057 |
instanceLocation => '', |
| 1058 |
keywordLocation => '/$ref/properties', |
| 1059 |
absoluteKeywordLocation => '#/$defs/mydef/properties', |
| 1060 |
error => 'not all properties are valid', |
| 1061 |
}, |
| 1062 |
], |
| 1063 |
}, |
| 1064 |
'JSON pointers are properly escaped; URIs doubly so', |
| 1065 |
); |
| 1066 |
|
| 1067 |
is_equal( |
| 1068 |
$js->evaluate( |
| 1069 |
{ '{}' => { 'my~tilde/slash-property' => 1 } }, |
| 1070 |
$schema->{'$defs'}{mydef}, |
| 1071 |
)->TO_JSON, |
| 1072 |
{ |
| 1073 |
valid => false, |
| 1074 |
errors => [ |
| 1075 |
map +{ |
| 1076 |
error => $_->{error}, |
| 1077 |
instanceLocation => $_->{instanceLocation}, |
| 1078 |
keywordLocation => $_->{keywordLocation} =~ s{^/\$ref}{}r, |
| 1079 |
}, @$errors |
| 1080 |
], |
| 1081 |
}, |
| 1082 |
'absoluteKeywordLocation is omitted when paths are the same, not counting uri encoding', |
| 1083 |
); |
| 1084 |
|
| 1085 |
cmp_result( |
| 1086 |
$js->evaluate( |
| 1087 |
{ '{}' => { 'my~tilde/slash-property' => 1 } }, |
| 1088 |
{ |
| 1089 |
'$defs' => { |
| 1090 |
mydef => { |
| 1091 |
properties => { |
| 1092 |
'{}' => { |
| 1093 |
patternProperties => { |
| 1094 |
'a{' => { minimum => 2 }, # this is a broken regex |
| 1095 |
}, |
| 1096 |
}, |
| 1097 |
}, |
| 1098 |
}, |
| 1099 |
}, |
| 1100 |
'$ref' => '#/$defs/mydef', |
| 1101 |
}, |
| 1102 |
)->TO_JSON, |
| 1103 |
{ |
| 1104 |
valid => false, |
| 1105 |
errors => [ |
| 1106 |
{ |
| 1107 |
keywordLocation => '/$defs/mydef/properties/{}/patternProperties/a{', |
| 1108 |
error => re(qr/^Unescaped left brace in regex is (deprecated|illegal|passed through)/), |
| 1109 |
}, |
| 1110 |
], |
| 1111 |
}, |
| 1112 |
# all the other _keyword_path_suffix cases are tested in the earlier test case |
| 1113 |
'use of _keyword_path_suffix in a fatal error', |
| 1114 |
) if "$]" >= 5.022; |
| 1115 |
}; |
| 1116 |
|
| 1117 |
subtest 'absoluteKeywordLocation' => sub { |
| 1118 |
is_equal( |
| 1119 |
JSON::Schema::Modern->new(max_depth => 1)->evaluate( |
| 1120 |
[ [ 1 ] ], |
| 1121 |
{ items => { '$ref' => '#' } }, |
| 1122 |
)->TO_JSON, |
| 1123 |
{ |
| 1124 |
valid => false, |
| 1125 |
errors => [ |
| 1126 |
{ |
| 1127 |
instanceLocation => '/0', |
| 1128 |
keywordLocation => '/items/$ref', |
| 1129 |
absoluteKeywordLocation => '', |
| 1130 |
error => 'EXCEPTION: maximum evaluation depth (1) exceeded', |
| 1131 |
}, |
| 1132 |
], |
| 1133 |
}, |
| 1134 |
'absoluteKeywordLocation is included when different from instanceLocation, even when empty', |
| 1135 |
); |
| 1136 |
|
| 1137 |
is_equal( |
| 1138 |
$js->evaluate(1, { '$ref' => 'http://example.com#does_not_exist' })->TO_JSON, |
| 1139 |
{ |
| 1140 |
valid => false, |
| 1141 |
errors => [ |
| 1142 |
{ |
| 1143 |
instanceLocation => '', |
| 1144 |
keywordLocation => '/$ref', |
| 1145 |
error => 'EXCEPTION: unable to find resource "http://example.com#does_not_exist"', |
| 1146 |
}, |
| 1147 |
], |
| 1148 |
}, |
| 1149 |
'absoluteKeywordLocation is not included when the path equals keywordLocation, even if a $ref is present', |
| 1150 |
); |
| 1151 |
|
| 1152 |
$js->add_schema(false); |
| 1153 |
is_equal( |
| 1154 |
$js->evaluate(1, '#')->TO_JSON, |
| 1155 |
{ |
| 1156 |
valid => false, |
| 1157 |
errors => [ |
| 1158 |
{ |
| 1159 |
instanceLocation => '', |
| 1160 |
keywordLocation => '', |
| 1161 |
error => 'subschema is false', |
| 1162 |
}, |
| 1163 |
], |
| 1164 |
}, |
| 1165 |
'absoluteKeywordLocation is never "#"', |
| 1166 |
); |
| 1167 |
|
| 1168 |
is_equal( |
| 1169 |
$js->evaluate( |
| 1170 |
1, |
| 1171 |
my $schema = { |
| 1172 |
'$id' => 'https://localhost:1234/bloop', |
| 1173 |
allOf => [ |
| 1174 |
{ |
| 1175 |
'$id' => 'foo.json', |
| 1176 |
type => 'object', |
| 1177 |
}, |
| 1178 |
{ |
| 1179 |
'$id' => 'bar/', |
| 1180 |
allOf => [ |
| 1181 |
{ |
| 1182 |
'$id' => 'alpha', |
| 1183 |
type => 'object', |
| 1184 |
}, |
| 1185 |
], |
| 1186 |
}, |
| 1187 |
{ type => 'object' }, |
| 1188 |
], |
| 1189 |
}, |
| 1190 |
)->TO_JSON, |
| 1191 |
{ |
| 1192 |
valid => false, |
| 1193 |
errors => [ |
| 1194 |
{ |
| 1195 |
instanceLocation => '', |
| 1196 |
keywordLocation => '/allOf/0/type', |
| 1197 |
absoluteKeywordLocation => 'https://localhost:1234/foo.json#/type', |
| 1198 |
error => 'got integer, not object', |
| 1199 |
}, |
| 1200 |
{ |
| 1201 |
instanceLocation => '', |
| 1202 |
keywordLocation => '/allOf/1/allOf/0/type', |
| 1203 |
absoluteKeywordLocation => 'https://localhost:1234/bar/alpha#/type', |
| 1204 |
error => 'got integer, not object', |
| 1205 |
}, |
| 1206 |
{ |
| 1207 |
instanceLocation => '', |
| 1208 |
keywordLocation => '/allOf/1/allOf', |
| 1209 |
absoluteKeywordLocation => 'https://localhost:1234/bar/#/allOf', |
| 1210 |
error => 'subschema 0 is not valid', |
| 1211 |
}, |
| 1212 |
{ |
| 1213 |
instanceLocation => '', |
| 1214 |
keywordLocation => '/allOf/2/type', |
| 1215 |
absoluteKeywordLocation => 'https://localhost:1234/bloop#/allOf/2/type', |
| 1216 |
error => 'got integer, not object', |
| 1217 |
}, |
| 1218 |
{ |
| 1219 |
instanceLocation => '', |
| 1220 |
keywordLocation => '/allOf', |
| 1221 |
absoluteKeywordLocation => 'https://localhost:1234/bloop#/allOf', |
| 1222 |
error => 'subschemas 0, 1, 2 are not valid', |
| 1223 |
}, |
| 1224 |
], |
| 1225 |
}, |
| 1226 |
'absoluteKeywordLocation reflects the canonical schema uri as it changes when passing through $id', |
| 1227 |
); |
| 1228 |
|
| 1229 |
$schema->{'$id'} = 'https://example.com'; |
| 1230 |
$schema->{allOf}[2]{'$id'} = '#my_anchor2'; |
| 1231 |
is_equal( |
| 1232 |
JSON::Schema::Modern->new(specification_version => 'draft7')->evaluate(1, $schema)->TO_JSON, |
| 1233 |
{ |
| 1234 |
valid => false, |
| 1235 |
errors => [ |
| 1236 |
{ |
| 1237 |
instanceLocation => '', |
| 1238 |
keywordLocation => '/allOf/0/type', |
| 1239 |
absoluteKeywordLocation => 'https://example.com/foo.json#/type', |
| 1240 |
error => 'got integer, not object', |
| 1241 |
}, |
| 1242 |
{ |
| 1243 |
instanceLocation => '', |
| 1244 |
keywordLocation => '/allOf/1/allOf/0/type', |
| 1245 |
absoluteKeywordLocation => 'https://example.com/bar/alpha#/type', |
| 1246 |
error => 'got integer, not object', |
| 1247 |
}, |
| 1248 |
{ |
| 1249 |
instanceLocation => '', |
| 1250 |
keywordLocation => '/allOf/1/allOf', |
| 1251 |
absoluteKeywordLocation => 'https://example.com/bar/#/allOf', |
| 1252 |
error => 'subschema 0 is not valid', |
| 1253 |
}, |
| 1254 |
{ |
| 1255 |
instanceLocation => '', |
| 1256 |
keywordLocation => '/allOf/2/type', |
| 1257 |
absoluteKeywordLocation => 'https://example.com#/allOf/2/type', |
| 1258 |
error => 'got integer, not object', |
| 1259 |
}, |
| 1260 |
{ |
| 1261 |
instanceLocation => '', |
| 1262 |
keywordLocation => '/allOf', |
| 1263 |
absoluteKeywordLocation => 'https://example.com#/allOf', |
| 1264 |
error => 'subschemas 0, 1, 2 are not valid', |
| 1265 |
}, |
| 1266 |
], |
| 1267 |
}, |
| 1268 |
'plain-name fragment in $id does not change canonical schema uri', |
| 1269 |
); |
| 1270 |
}; |
| 1271 |
|
| 1272 |
subtest dependentRequired => sub { |
| 1273 |
is_equal( |
| 1274 |
$js->evaluate(1, { dependentRequired => { foo => [ 1 ] } })->TO_JSON, |
| 1275 |
{ |
| 1276 |
valid => false, |
| 1277 |
errors => [ |
| 1278 |
{ |
| 1279 |
keywordLocation => '/dependentRequired/foo/0', |
| 1280 |
error => 'element is not a string', |
| 1281 |
}, |
| 1282 |
], |
| 1283 |
}, |
| 1284 |
'dependentRequired traversal error', |
| 1285 |
); |
| 1286 |
}; |
| 1287 |
|
| 1288 |
subtest 'numbers in output' => sub { |
| 1289 |
is_equal( |
| 1290 |
$js->evaluate( |
| 1291 |
5, |
| 1292 |
{ |
| 1293 |
multipleOf => 1.23456789, |
| 1294 |
maximum => 4.23456789, |
| 1295 |
minimum => 6.23456789, |
| 1296 |
exclusiveMaximum => 4.23456789, |
| 1297 |
exclusiveMinimum => 6.23456789, |
| 1298 |
}, |
| 1299 |
)->TO_JSON, |
| 1300 |
{ |
| 1301 |
valid => false, |
| 1302 |
errors => [ |
| 1303 |
{ |
| 1304 |
instanceLocation => '', |
| 1305 |
keywordLocation => '/multipleOf', |
| 1306 |
error => 'value is not a multiple of 1.23456789', |
| 1307 |
}, |
| 1308 |
{ |
| 1309 |
instanceLocation => '', |
| 1310 |
keywordLocation => '/maximum', |
| 1311 |
error => 'value is greater than 4.23456789', |
| 1312 |
}, |
| 1313 |
{ |
| 1314 |
instanceLocation => '', |
| 1315 |
keywordLocation => '/exclusiveMaximum', |
| 1316 |
error => 'value is greater than or equal to 4.23456789', |
| 1317 |
}, |
| 1318 |
{ |
| 1319 |
instanceLocation => '', |
| 1320 |
keywordLocation => '/minimum', |
| 1321 |
error => 'value is less than 6.23456789', |
| 1322 |
}, |
| 1323 |
{ |
| 1324 |
instanceLocation => '', |
| 1325 |
keywordLocation => '/exclusiveMinimum', |
| 1326 |
error => 'value is less than or equal to 6.23456789', |
| 1327 |
}, |
| 1328 |
], |
| 1329 |
}, |
| 1330 |
'numbers in errors do not lose any digits of precision', |
| 1331 |
); |
| 1332 |
}; |
| 1333 |
|
| 1334 |
subtest 'overriding starting locations' => sub { |
| 1335 |
# evaluating this document from its root would do nothing, as it is only definitions |
| 1336 |
my $doc = $js->add_schema('/api', my $schema = { |
| 1337 |
'$defs' => { |
| 1338 |
alpha => { |
| 1339 |
items => { |
| 1340 |
'$ref' => '#/$defs/beta', |
| 1341 |
}, |
| 1342 |
}, |
| 1343 |
beta => { |
| 1344 |
const => 2, |
| 1345 |
}, |
| 1346 |
}, |
| 1347 |
}); |
| 1348 |
$js->add_document('https://example.com/api', $doc); |
| 1349 |
|
| 1350 |
is_equal( |
| 1351 |
$js->evaluate( |
| 1352 |
[ 5 ], |
| 1353 |
'https://example.com/api#/$defs/alpha', |
| 1354 |
{ |
| 1355 |
data_path => '/html/body/div/div/h1/div/p', # reported data location |
| 1356 |
traversed_keyword_path => '/some/other/document/$ref', # reported keywords passed through before we start |
| 1357 |
}, |
| 1358 |
)->TO_JSON, |
| 1359 |
{ |
| 1360 |
valid => false, |
| 1361 |
errors => [ |
| 1362 |
{ |
| 1363 |
instanceLocation => '/html/body/div/div/h1/div/p/0', |
| 1364 |
keywordLocation => '/some/other/document/$ref/items/$ref/const', |
| 1365 |
absoluteKeywordLocation => 'https://example.com/api#/$defs/beta/const', |
| 1366 |
error => 'value does not match (integers not equal)', |
| 1367 |
}, |
| 1368 |
{ |
| 1369 |
instanceLocation => '/html/body/div/div/h1/div/p', |
| 1370 |
keywordLocation => '/some/other/document/$ref/items', |
| 1371 |
absoluteKeywordLocation => 'https://example.com/api#/$defs/alpha/items', |
| 1372 |
error => 'subschema is not valid against all items', |
| 1373 |
}, |
| 1374 |
], |
| 1375 |
}, |
| 1376 |
'can alter locations with data_path, traversed_keyword_path, and add_schema()', |
| 1377 |
); |
| 1378 |
|
| 1379 |
is_equal( |
| 1380 |
(my $result = $js->evaluate( |
| 1381 |
[ 2 ], |
| 1382 |
'https://example.com/api#/$defs/alpha', |
| 1383 |
{ |
| 1384 |
data_path => '/html/body/div/div/h1/div/p', # reported data location |
| 1385 |
traversed_keyword_path => '/some/other/document/$ref', # reported keywords passed through before we start |
| 1386 |
}, |
| 1387 |
))->TO_JSON, |
| 1388 |
{ valid => true, }, |
| 1389 |
'successful result with data location adjusted', |
| 1390 |
); |
| 1391 |
is_equal( |
| 1392 |
$result->data, |
| 1393 |
{ html => { body => { div => { div => { h1 => { div => { p => [ 2 ] } } } } } } }, |
| 1394 |
'data is populated into the correct location (adjusted for data_path)', |
| 1395 |
); |
| 1396 |
}; |
| 1397 |
|
| 1398 |
subtest 'recommended_response' => sub { |
| 1399 |
is_equal( |
| 1400 |
JSON::Schema::Modern::Result->new(valid => 1)->recommended_response, |
| 1401 |
undef, |
| 1402 |
'recommended_response is not defined when there are no errors', |
| 1403 |
); |
| 1404 |
|
| 1405 |
my $result = $js->evaluate( |
| 1406 |
{ foo => 3 }, |
| 1407 |
{ |
| 1408 |
type => 'object', |
| 1409 |
properties => { |
| 1410 |
foo => { |
| 1411 |
type => 'integer', |
| 1412 |
minimum => 5, |
| 1413 |
}, |
| 1414 |
}, |
| 1415 |
}, |
| 1416 |
); |
| 1417 |
|
| 1418 |
cmp_result( |
| 1419 |
$result->recommended_response, |
| 1420 |
[ 400, q{'/foo': value is less than 5} ], |
| 1421 |
'recommended_response uses the first error in the result', |
| 1422 |
); |
| 1423 |
|
| 1424 |
my $result2 = $js->evaluate(1, { '$ref' => '#/$defs/does_not_exist' }); |
| 1425 |
|
| 1426 |
cmp_result( |
| 1427 |
$result2->recommended_response, |
| 1428 |
[ 500, 'Internal Server Error' ], |
| 1429 |
'recommended_response indicates an exception occurred', |
| 1430 |
); |
| 1431 |
|
| 1432 |
my $result3 = JSON::Schema::Modern::Result->new( |
| 1433 |
valid => 0, |
| 1434 |
errors => [ |
| 1435 |
$result->errors, |
| 1436 |
# TODO: I haven't implemented authentication in OpenAPI::Modern yet, so I'm not sure how |
| 1437 |
# exactly these errors are going to look |
| 1438 |
JSON::Schema::Modern::Error->new( |
| 1439 |
depth => 0, |
| 1440 |
mode => 'evaluate', |
| 1441 |
keyword => 'authentication', |
| 1442 |
instance_location => '/request/headers/Authentication', |
| 1443 |
keyword_location => '/paths/foo/get/security', |
| 1444 |
error => 'security check failed', |
| 1445 |
recommended_response => [ 401, 'Unauthorized' ], |
| 1446 |
), |
| 1447 |
], |
| 1448 |
); |
| 1449 |
|
| 1450 |
cmp_result( |
| 1451 |
$result3->recommended_response, |
| 1452 |
[ 401, 'Unauthorized' ], |
| 1453 |
'recommended_response uses the one from the error that is explicitly set', |
| 1454 |
); |
| 1455 |
|
| 1456 |
cmp_result( |
| 1457 |
my $e = ($result3->errors)[-1]->clone(error => 'oh noes'), |
| 1458 |
methods( |
| 1459 |
error => 'oh noes', |
| 1460 |
recommended_response => [ 401, 'Unauthorized' ], |
| 1461 |
), |
| 1462 |
'cloning copies recommended_response', |
| 1463 |
); |
| 1464 |
}; |
| 1465 |
|
| 1466 |
subtest 'exclusiveMaximum, exclusiveMinimum across drafts' => sub { |
| 1467 |
is_equal( |
| 1468 |
$js->evaluate(4, { maximum => 4, exclusiveMaximum => 4 })->TO_JSON, |
| 1469 |
{ |
| 1470 |
valid => false, |
| 1471 |
errors => [ |
| 1472 |
{ |
| 1473 |
instanceLocation => '', |
| 1474 |
keywordLocation => '/exclusiveMaximum', |
| 1475 |
error => 'value is greater than or equal to 4', |
| 1476 |
}, |
| 1477 |
], |
| 1478 |
}, |
| 1479 |
'later drafts; errors are produced separately from the keywords', |
| 1480 |
); |
| 1481 |
|
| 1482 |
is_equal( |
| 1483 |
$js->evaluate(5, { maximum => 4, exclusiveMaximum => 4 })->TO_JSON, |
| 1484 |
{ |
| 1485 |
valid => false, |
| 1486 |
errors => [ |
| 1487 |
{ |
| 1488 |
instanceLocation => '', |
| 1489 |
keywordLocation => '/maximum', |
| 1490 |
error => 'value is greater than 4', |
| 1491 |
}, |
| 1492 |
{ |
| 1493 |
instanceLocation => '', |
| 1494 |
keywordLocation => '/exclusiveMaximum', |
| 1495 |
error => 'value is greater than or equal to 4', |
| 1496 |
}, |
| 1497 |
], |
| 1498 |
}, |
| 1499 |
'later drafts; two errors can result', |
| 1500 |
); |
| 1501 |
|
| 1502 |
my $js = JSON::Schema::Modern->new(specification_version => 'draft4'); |
| 1503 |
|
| 1504 |
is_equal( |
| 1505 |
$js->evaluate(4, { maximum => 4, exclusiveMaximum => true })->TO_JSON, |
| 1506 |
{ |
| 1507 |
valid => false, |
| 1508 |
errors => [ |
| 1509 |
{ |
| 1510 |
instanceLocation => '', |
| 1511 |
keywordLocation => '/maximum', |
| 1512 |
error => 'value is greater than or equal to 4', |
| 1513 |
}, |
| 1514 |
], |
| 1515 |
}, |
| 1516 |
'draft4: one error comes from maximum, but includes the exclusiveMaximum check', |
| 1517 |
); |
| 1518 |
|
| 1519 |
is_equal( |
| 1520 |
$js->evaluate(5, { maximum => 4, exclusiveMaximum => true })->TO_JSON, |
| 1521 |
{ |
| 1522 |
valid => false, |
| 1523 |
errors => [ |
| 1524 |
{ |
| 1525 |
instanceLocation => '', |
| 1526 |
keywordLocation => '/maximum', |
| 1527 |
error => 'value is greater than or equal to 4', |
| 1528 |
}, |
| 1529 |
], |
| 1530 |
}, |
| 1531 |
'draft4: maximum + exclusiveMaximum checks are combined', |
| 1532 |
); |
| 1533 |
|
| 1534 |
is_equal( |
| 1535 |
$js->evaluate(4, { maximum => 4, exclusiveMaximum => false })->TO_JSON, |
| 1536 |
{ valid => true }, |
| 1537 |
'draft4: exclusive check uses the right boundary', |
| 1538 |
); |
| 1539 |
|
| 1540 |
is_equal( |
| 1541 |
$js->evaluate(5, { maximum => 4, exclusiveMaximum => false })->TO_JSON, |
| 1542 |
{ |
| 1543 |
valid => false, |
| 1544 |
errors => [ |
| 1545 |
{ |
| 1546 |
instanceLocation => '', |
| 1547 |
keywordLocation => '/maximum', |
| 1548 |
error => 'value is greater than 4', |
| 1549 |
}, |
| 1550 |
], |
| 1551 |
}, |
| 1552 |
'draft4: maximum check is correct', |
| 1553 |
); |
| 1554 |
}; |
| 1555 |
|
| 1556 |
subtest 'boolean schemas in draft4' => sub { |
| 1557 |
my $js = JSON::Schema::Modern->new(specification_version => 'draft4', strict => 1); |
| 1558 |
is_equal ( |
| 1559 |
$js->evaluate( |
| 1560 |
1, |
| 1561 |
{ |
| 1562 |
allOf => [ true ], |
| 1563 |
anyOf => [ true ], |
| 1564 |
oneOf => [ true ], |
| 1565 |
not => true, |
| 1566 |
items => true, |
| 1567 |
additionalItems => true, # ok |
| 1568 |
properties => { foo => true }, |
| 1569 |
patternProperties => { foo => false }, |
| 1570 |
additionalProperties => false, # ok |
| 1571 |
uniqueItems => true, # ok |
| 1572 |
}, |
| 1573 |
)->TO_JSON, |
| 1574 |
{ |
| 1575 |
valid => false, |
| 1576 |
errors => [ |
| 1577 |
(map +{ |
| 1578 |
keywordLocation => '/'.$_.'/0', |
| 1579 |
error => 'invalid schema type: boolean', |
| 1580 |
}, qw(allOf anyOf oneOf)), |
| 1581 |
(map +{ |
| 1582 |
keywordLocation => '/'.$_, |
| 1583 |
error => 'invalid schema type: boolean', |
| 1584 |
}, qw(not items)), |
| 1585 |
(map +{ |
| 1586 |
keywordLocation => '/'.$_.'/foo', |
| 1587 |
error => 'invalid schema type: boolean', |
| 1588 |
}, qw(properties patternProperties)), |
| 1589 |
], |
| 1590 |
}, |
| 1591 |
'got all traverse errors from use of booleans in schemas for draft4', |
| 1592 |
); |
| 1593 |
|
| 1594 |
is_equal( |
| 1595 |
$js->evaluate( |
| 1596 |
{ |
| 1597 |
array => [ 1, 1 ], |
| 1598 |
object => { foo => 1 }, |
| 1599 |
}, |
| 1600 |
my $schema = { |
| 1601 |
allOf => [ |
| 1602 |
{ |
| 1603 |
properties => { |
| 1604 |
array => { |
| 1605 |
type => 'array', |
| 1606 |
uniqueItems => true, |
| 1607 |
items => [ {} ], |
| 1608 |
additionalItems => false, |
| 1609 |
}, |
| 1610 |
object => { |
| 1611 |
type => 'object', |
| 1612 |
additionalProperties => false, |
| 1613 |
}, |
| 1614 |
}, |
| 1615 |
}, |
| 1616 |
], |
| 1617 |
}, |
| 1618 |
)->TO_JSON, |
| 1619 |
{ |
| 1620 |
valid => false, |
| 1621 |
errors => [ |
| 1622 |
{ |
| 1623 |
instanceLocation => '/array', |
| 1624 |
keywordLocation => '/allOf/0/properties/array/uniqueItems', |
| 1625 |
error => 'items at indices 0 and 1 are not unique', |
| 1626 |
}, |
| 1627 |
{ |
| 1628 |
instanceLocation => '/array/1', |
| 1629 |
keywordLocation => '/allOf/0/properties/array/additionalItems', |
| 1630 |
error => 'additional item not permitted', |
| 1631 |
}, |
| 1632 |
{ |
| 1633 |
instanceLocation => '/array', |
| 1634 |
keywordLocation => '/allOf/0/properties/array/additionalItems', |
| 1635 |
error => 'subschema is not valid against all additional items', |
| 1636 |
}, |
| 1637 |
{ |
| 1638 |
instanceLocation => '/object/foo', |
| 1639 |
keywordLocation => '/allOf/0/properties/object/additionalProperties', |
| 1640 |
error => 'additional property not permitted', |
| 1641 |
}, |
| 1642 |
{ |
| 1643 |
instanceLocation => '/object', |
| 1644 |
keywordLocation => '/allOf/0/properties/object/additionalProperties', |
| 1645 |
error => 'not all additional properties are valid', |
| 1646 |
}, |
| 1647 |
{ |
| 1648 |
instanceLocation => '', |
| 1649 |
keywordLocation => '/allOf/0/properties', |
| 1650 |
error => 'not all properties are valid', |
| 1651 |
}, |
| 1652 |
{ |
| 1653 |
instanceLocation => '', |
| 1654 |
keywordLocation => '/allOf', |
| 1655 |
error => 'subschema 0 is not valid', |
| 1656 |
}, |
| 1657 |
], |
| 1658 |
}, |
| 1659 |
'booleans are okay in uniqueItems, additionalItems, additionalProperties', |
| 1660 |
); |
| 1661 |
|
| 1662 |
push $schema->{allOf}->@*, false; |
| 1663 |
|
| 1664 |
is_equal( |
| 1665 |
$js->evaluate( |
| 1666 |
{ |
| 1667 |
array => [ 1 ], |
| 1668 |
object => { foo => 1 }, |
| 1669 |
}, |
| 1670 |
$schema, |
| 1671 |
)->TO_JSON, |
| 1672 |
{ |
| 1673 |
valid => false, |
| 1674 |
errors => [ |
| 1675 |
{ |
| 1676 |
keywordLocation => '/allOf/1', |
| 1677 |
error => 'invalid schema type: boolean', |
| 1678 |
}, |
| 1679 |
], |
| 1680 |
}, |
| 1681 |
'boolean schemas did not exist in draft4', |
| 1682 |
); |
| 1683 |
}; |
| 1684 |
|
| 1685 |
done_testing; |