| 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 |
use Test2::Warnings qw(warnings :no_end_test had_no_warnings allow_warnings); |
| 18 |
|
| 19 |
my $js = JSON::Schema::Modern->new(short_circuit => 0, validate_formats => 1); |
| 20 |
|
| 21 |
subtest 'invalid use of the $schema keyword' => sub { |
| 22 |
is_equal( |
| 23 |
$js->evaluate( |
| 24 |
1, |
| 25 |
{ |
| 26 |
allOf => [ |
| 27 |
true, |
| 28 |
{ '$schema' => 'https://json-schema.org/draft/2019-09/schema' }, |
| 29 |
], |
| 30 |
}, |
| 31 |
)->TO_JSON, |
| 32 |
{ |
| 33 |
valid => false, |
| 34 |
errors => [ |
| 35 |
{ |
| 36 |
keywordLocation => '/allOf/1/$schema', |
| 37 |
error => '$schema can only appear at the schema resource root', |
| 38 |
}, |
| 39 |
], |
| 40 |
}, |
| 41 |
'$schema can only appear at the root of a schema, when there is no canonical URI', |
| 42 |
); |
| 43 |
|
| 44 |
is_equal( |
| 45 |
$js->evaluate( |
| 46 |
1, |
| 47 |
{ |
| 48 |
'$id' => 'https://bloop.com', |
| 49 |
allOf => [ |
| 50 |
true, |
| 51 |
{ '$schema' => 'https://json-schema.org/draft/2019-09/schema' }, |
| 52 |
], |
| 53 |
}, |
| 54 |
)->TO_JSON, |
| 55 |
{ |
| 56 |
valid => false, |
| 57 |
errors => [ |
| 58 |
{ |
| 59 |
keywordLocation => '/allOf/1/$schema', |
| 60 |
absoluteKeywordLocation => 'https://bloop.com#/allOf/1/$schema', |
| 61 |
error => '$schema can only appear at the schema resource root', |
| 62 |
}, |
| 63 |
], |
| 64 |
}, |
| 65 |
'$schema can only appear where the canonical URI has no fragment, when there is a canonical URI', |
| 66 |
); |
| 67 |
|
| 68 |
is_equal( |
| 69 |
$js->evaluate( |
| 70 |
1, |
| 71 |
{ |
| 72 |
'$id' => 'https://bloop3.com', |
| 73 |
'$defs' => { |
| 74 |
my_def => { |
| 75 |
'$schema' => 'https://json-schema.org/draft/2019-09/schema', |
| 76 |
}, |
| 77 |
}, |
| 78 |
'$ref' => '#/$defs/my_def', |
| 79 |
}, |
| 80 |
)->TO_JSON, |
| 81 |
{ |
| 82 |
valid => false, |
| 83 |
errors => [ |
| 84 |
{ |
| 85 |
keywordLocation => '/$defs/my_def/$schema', |
| 86 |
absoluteKeywordLocation => 'https://bloop3.com#/$defs/my_def/$schema', |
| 87 |
error => '$schema can only appear at the schema resource root', |
| 88 |
}, |
| 89 |
], |
| 90 |
}, |
| 91 |
'this is still not a resource root, even in a $ref target', |
| 92 |
); |
| 93 |
}; |
| 94 |
|
| 95 |
subtest 'defaults without a $schema keyword' => sub { |
| 96 |
is_equal( |
| 97 |
$js->evaluate(1, true)->TO_JSON, |
| 98 |
{ valid => true }, |
| 99 |
'boolean schema: no $id, no $schema', |
| 100 |
); |
| 101 |
cmp_result( |
| 102 |
$js->{_resource_index}{''}, |
| 103 |
superhashof({ |
| 104 |
specification_version => 'draft2020-12', |
| 105 |
vocabularies => ignore, # for boolean schemas, vocabularies do not matter |
| 106 |
}), |
| 107 |
'boolean schema: defaults to draft2020-12 without a $schema keyword', |
| 108 |
); |
| 109 |
|
| 110 |
is_equal( |
| 111 |
$js->evaluate( |
| 112 |
{ foo => 1 }, |
| 113 |
{ unevaluatedProperties => false }, |
| 114 |
)->TO_JSON, |
| 115 |
{ |
| 116 |
valid => false, |
| 117 |
errors => [ |
| 118 |
{ |
| 119 |
instanceLocation => '/foo', |
| 120 |
keywordLocation => '/unevaluatedProperties', |
| 121 |
error => 'additional property not permitted', |
| 122 |
}, |
| 123 |
{ |
| 124 |
instanceLocation => '', |
| 125 |
keywordLocation => '/unevaluatedProperties', |
| 126 |
error => 'not all additional properties are valid', |
| 127 |
}, |
| 128 |
], |
| 129 |
}, |
| 130 |
'object schema: no $id, no $schema', |
| 131 |
); |
| 132 |
cmp_result( |
| 133 |
$js->{_resource_index}{''}, |
| 134 |
superhashof({ |
| 135 |
specification_version => 'draft2020-12', |
| 136 |
vocabularies => [ map 'JSON::Schema::Modern::Vocabulary::'.$_, |
| 137 |
qw(Core Validation FormatAnnotation Applicator Content MetaData Unevaluated) ], |
| 138 |
}), |
| 139 |
'object schema: defaults to draft2020-12 without a $schema keyword', |
| 140 |
); |
| 141 |
|
| 142 |
is_equal( |
| 143 |
$js->evaluate( |
| 144 |
1, |
| 145 |
{ '$defs' => { foo => { not => 'invalid subschema' } } }, |
| 146 |
)->TO_JSON, |
| 147 |
{ |
| 148 |
valid => false, |
| 149 |
errors => [ |
| 150 |
{ |
| 151 |
keywordLocation => '/$defs/foo/not', |
| 152 |
error => 'invalid schema type: string', |
| 153 |
}, |
| 154 |
], |
| 155 |
}, |
| 156 |
'"not" keyword, from the Applicator vocabulary, is traversed at the root level', |
| 157 |
); |
| 158 |
|
| 159 |
|
| 160 |
is_equal( |
| 161 |
$js->evaluate( |
| 162 |
{ foo => 1 }, |
| 163 |
{ |
| 164 |
'$id' => 'https://id-no-schema1', |
| 165 |
unevaluatedProperties => false, |
| 166 |
}, |
| 167 |
)->TO_JSON, |
| 168 |
{ |
| 169 |
valid => false, |
| 170 |
errors => [ |
| 171 |
{ |
| 172 |
instanceLocation => '/foo', |
| 173 |
keywordLocation => '/unevaluatedProperties', |
| 174 |
absoluteKeywordLocation => 'https://id-no-schema1#/unevaluatedProperties', |
| 175 |
error => 'additional property not permitted', |
| 176 |
}, |
| 177 |
{ |
| 178 |
instanceLocation => '', |
| 179 |
keywordLocation => '/unevaluatedProperties', |
| 180 |
absoluteKeywordLocation => 'https://id-no-schema1#/unevaluatedProperties', |
| 181 |
error => 'not all additional properties are valid', |
| 182 |
}, |
| 183 |
], |
| 184 |
}, |
| 185 |
'object schema: $id, no $schema', |
| 186 |
); |
| 187 |
cmp_result( |
| 188 |
$js->{_resource_index}{'https://id-no-schema1'}, |
| 189 |
superhashof({ |
| 190 |
specification_version => 'draft2020-12', |
| 191 |
vocabularies => [ map 'JSON::Schema::Modern::Vocabulary::'.$_, |
| 192 |
qw(Core Validation FormatAnnotation Applicator Content MetaData Unevaluated) ], |
| 193 |
}), |
| 194 |
'named resource defaults to draft2020-12 without a $schema keyword', |
| 195 |
); |
| 196 |
|
| 197 |
|
| 198 |
my $js = JSON::Schema::Modern->new(short_circuit => 0, specification_version => 'draft7'); |
| 199 |
|
| 200 |
is_equal( |
| 201 |
$js->evaluate(1, true)->TO_JSON, |
| 202 |
{ valid => true }, |
| 203 |
'boolean schema: no $id, no $schema', |
| 204 |
); |
| 205 |
cmp_result( |
| 206 |
$js->{_resource_index}{''}, |
| 207 |
superhashof({ |
| 208 |
specification_version => 'draft7', |
| 209 |
vocabularies => ignore, # for boolean schemas, vocabularies do not matter |
| 210 |
}), |
| 211 |
'boolean schema: specification_version overridden', |
| 212 |
); |
| 213 |
|
| 214 |
is_equal( |
| 215 |
$js->evaluate( |
| 216 |
{ foo => 1 }, |
| 217 |
{ unevaluatedProperties => 'not a schema' }, |
| 218 |
)->TO_JSON, |
| 219 |
{ valid => true }, |
| 220 |
'object schema: no $id, no $schema, specification version overridden, other keywords are ignored during traversal', |
| 221 |
); |
| 222 |
|
| 223 |
is_equal( |
| 224 |
$js->evaluate( |
| 225 |
{ foo => 1 }, |
| 226 |
{ unevaluatedProperties => false }, |
| 227 |
)->TO_JSON, |
| 228 |
{ valid => true }, |
| 229 |
'object schema: no $id, no $schema, specification version overridden, other keywords are ignored during evaluation', |
| 230 |
); |
| 231 |
cmp_result( |
| 232 |
$js->{_resource_index}{''}, |
| 233 |
superhashof({ |
| 234 |
specification_version => 'draft7', |
| 235 |
vocabularies => [ map 'JSON::Schema::Modern::Vocabulary::'.$_, |
| 236 |
qw(Core Validation FormatAnnotation Applicator Content MetaData) ], |
| 237 |
}), |
| 238 |
'object schema: overridden to draft7', |
| 239 |
); |
| 240 |
|
| 241 |
|
| 242 |
is_equal( |
| 243 |
$js->evaluate( |
| 244 |
{ foo => 1 }, |
| 245 |
{ |
| 246 |
'$id' => 'https://id-no-schema2', |
| 247 |
unevaluatedProperties => 'not a schema', |
| 248 |
}, |
| 249 |
)->TO_JSON, |
| 250 |
{ valid => true }, |
| 251 |
'object schema: $id, no $schema, unrecognized+invalid keywords are ignored during traversal', |
| 252 |
); |
| 253 |
|
| 254 |
is_equal( |
| 255 |
$js->evaluate( |
| 256 |
{ foo => 1 }, |
| 257 |
{ |
| 258 |
'$id' => 'https://id-no-schema3', |
| 259 |
unevaluatedProperties => false, |
| 260 |
}, |
| 261 |
)->TO_JSON, |
| 262 |
{ valid => true }, |
| 263 |
'object schema: $id, no $schema', |
| 264 |
); |
| 265 |
cmp_result( |
| 266 |
$js->{_resource_index}{'https://id-no-schema3'}, |
| 267 |
superhashof({ |
| 268 |
specification_version => 'draft7', |
| 269 |
vocabularies => [ map 'JSON::Schema::Modern::Vocabulary::'.$_, |
| 270 |
qw(Core Validation FormatAnnotation Applicator Content MetaData) ], |
| 271 |
}), |
| 272 |
'object schema: overridden to draft7 and other keywords are ignored', |
| 273 |
); |
| 274 |
}; |
| 275 |
|
| 276 |
subtest 'behaviour with a $schema keyword' => sub { |
| 277 |
is_equal( |
| 278 |
$js->evaluate( |
| 279 |
{ foo => 1 }, |
| 280 |
{ |
| 281 |
'$schema' => 'http://json-schema.org/draft-07/schema#', |
| 282 |
unevaluatedProperties => 'not a schema', |
| 283 |
}, |
| 284 |
)->TO_JSON, |
| 285 |
{ valid => true }, |
| 286 |
'object schema: no $id, has $schema, unrecognized+invalid keywords are ignored during traversal', |
| 287 |
); |
| 288 |
|
| 289 |
is_equal( |
| 290 |
$js->evaluate( |
| 291 |
{ foo => 1 }, |
| 292 |
{ |
| 293 |
'$schema' => 'http://json-schema.org/draft-07/schema#', |
| 294 |
unevaluatedProperties => false, |
| 295 |
}, |
| 296 |
)->TO_JSON, |
| 297 |
{ valid => true }, |
| 298 |
'object schema: no $id, has $schema, unrecognized keywords are ignored during evaluation', |
| 299 |
); |
| 300 |
cmp_result( |
| 301 |
$js->{_resource_index}{''}, |
| 302 |
superhashof({ |
| 303 |
specification_version => 'draft7', |
| 304 |
vocabularies => [ map 'JSON::Schema::Modern::Vocabulary::'.$_, |
| 305 |
qw(Core Validation FormatAnnotation Applicator Content MetaData) ], |
| 306 |
}), |
| 307 |
'semantics can be changed to another draft version', |
| 308 |
); |
| 309 |
|
| 310 |
is_equal( |
| 311 |
$js->evaluate( |
| 312 |
{ foo => 1 }, |
| 313 |
{ |
| 314 |
'$schema' => 'http://json-schema.org/draft-07/schema', |
| 315 |
unevaluatedProperties => false, |
| 316 |
}, |
| 317 |
)->TO_JSON, |
| 318 |
{ valid => true }, |
| 319 |
'schema is accepted with $schema without an empty fragment', |
| 320 |
); |
| 321 |
cmp_result( |
| 322 |
$js->{_resource_index}{''}, |
| 323 |
superhashof({ |
| 324 |
specification_version => 'draft7', |
| 325 |
vocabularies => [ map 'JSON::Schema::Modern::Vocabulary::'.$_, |
| 326 |
qw(Core Validation FormatAnnotation Applicator Content MetaData) ], |
| 327 |
}), |
| 328 |
'..and is still recognized as draft7', |
| 329 |
); |
| 330 |
|
| 331 |
is_equal( |
| 332 |
$js->evaluate( |
| 333 |
{ foo => 1 }, |
| 334 |
{ |
| 335 |
'$id' => 'https://id-and-schema1', |
| 336 |
'$schema' => 'http://json-schema.org/draft-07/schema#', |
| 337 |
unevaluatedProperties => 'not a schema', |
| 338 |
}, |
| 339 |
)->TO_JSON, |
| 340 |
{ valid => true }, |
| 341 |
'$id and $schema, unrecognized+invalid keywords are ignored during traversal', |
| 342 |
); |
| 343 |
|
| 344 |
is_equal( |
| 345 |
$js->evaluate( |
| 346 |
{ foo => 1 }, |
| 347 |
{ |
| 348 |
'$id' => 'https://id-and-schema2', |
| 349 |
'$schema' => 'http://json-schema.org/draft-07/schema#', |
| 350 |
unevaluatedProperties => false, |
| 351 |
}, |
| 352 |
)->TO_JSON, |
| 353 |
{ valid => true }, |
| 354 |
'$id and $schema', |
| 355 |
); |
| 356 |
cmp_result( |
| 357 |
$js->{_resource_index}{'https://id-and-schema2'}, |
| 358 |
superhashof({ |
| 359 |
specification_version => 'draft7', |
| 360 |
vocabularies => [ map 'JSON::Schema::Modern::Vocabulary::'.$_, |
| 361 |
qw(Core Validation FormatAnnotation Applicator Content MetaData) ], |
| 362 |
}), |
| 363 |
'named resource can be changed to another draft version and other keywords are ignored', |
| 364 |
); |
| 365 |
|
| 366 |
|
| 367 |
my $js = JSON::Schema::Modern->new(short_circuit => 0, specification_version => 'draft2019-09'); |
| 368 |
|
| 369 |
is_equal( |
| 370 |
$js->evaluate( |
| 371 |
{ foo => 1 }, |
| 372 |
{ |
| 373 |
'$schema' => 'http://json-schema.org/draft-07/schema#', |
| 374 |
unevaluatedProperties => 'not a schema', |
| 375 |
}, |
| 376 |
)->TO_JSON, |
| 377 |
{ valid => true }, |
| 378 |
'no $id, specification version overridden twice; unrecognized+invalid keywords are ignored during traversal', |
| 379 |
); |
| 380 |
|
| 381 |
is_equal( |
| 382 |
$js->evaluate( |
| 383 |
{ foo => 1 }, |
| 384 |
{ |
| 385 |
'$schema' => 'http://json-schema.org/draft-07/schema#', |
| 386 |
unevaluatedProperties => false, |
| 387 |
}, |
| 388 |
)->TO_JSON, |
| 389 |
{ valid => true }, |
| 390 |
'no $id, specification version overridden twice, other keywords are ignored during evaluation', |
| 391 |
); |
| 392 |
cmp_result( |
| 393 |
$js->{_resource_index}{''}, |
| 394 |
superhashof({ |
| 395 |
specification_version => 'draft7', |
| 396 |
vocabularies => [ map 'JSON::Schema::Modern::Vocabulary::'.$_, |
| 397 |
qw(Core Validation FormatAnnotation Applicator Content MetaData) ], |
| 398 |
}), |
| 399 |
'unnamed resource can be changed to another draft version', |
| 400 |
); |
| 401 |
|
| 402 |
|
| 403 |
is_equal( |
| 404 |
$js->evaluate( |
| 405 |
{ foo => 1 }, |
| 406 |
{ |
| 407 |
'$id' => 'https://id-and-schema3', |
| 408 |
'$schema' => 'http://json-schema.org/draft-07/schema#', |
| 409 |
unevaluatedProperties => 'not a schema', |
| 410 |
}, |
| 411 |
)->TO_JSON, |
| 412 |
{ valid => true }, |
| 413 |
'no $id, specification version overridden twice; unrecognized+invalid keywords are ignored during traversal', |
| 414 |
); |
| 415 |
|
| 416 |
is_equal( |
| 417 |
$js->evaluate( |
| 418 |
{ foo => 1 }, |
| 419 |
{ |
| 420 |
'$id' => 'https://id-and-schema4', |
| 421 |
'$schema' => 'http://json-schema.org/draft-07/schema#', |
| 422 |
unevaluatedProperties => false, |
| 423 |
}, |
| 424 |
)->TO_JSON, |
| 425 |
{ valid => true }, |
| 426 |
'no $id, specification version overridden twice, other keywords are ignored during evaluation', |
| 427 |
); |
| 428 |
cmp_result( |
| 429 |
$js->{_resource_index}{''}, |
| 430 |
superhashof({ |
| 431 |
specification_version => 'draft7', |
| 432 |
vocabularies => [ map 'JSON::Schema::Modern::Vocabulary::'.$_, |
| 433 |
qw(Core Validation FormatAnnotation Applicator Content MetaData) ], |
| 434 |
}), |
| 435 |
'unnamed resource can be changed to another draft version', |
| 436 |
); |
| 437 |
}; |
| 438 |
|
| 439 |
subtest 'setting or changing specification versions in a single document' => sub { |
| 440 |
is_equal( |
| 441 |
$js->evaluate( |
| 442 |
1, |
| 443 |
{ |
| 444 |
'$id' => 'https://bloop2.com', |
| 445 |
allOf => [ |
| 446 |
true, |
| 447 |
{ |
| 448 |
'$id' => 'https://newid.com', |
| 449 |
'$schema' => 'https://json-schema.org/draft/2020-12/schema', |
| 450 |
}, |
| 451 |
], |
| 452 |
}, |
| 453 |
)->TO_JSON, |
| 454 |
{ valid => true }, |
| 455 |
'$schema can appear adjacent to any $id', |
| 456 |
); |
| 457 |
}; |
| 458 |
|
| 459 |
subtest 'changing specification versions across documents' => sub { |
| 460 |
my $expected = [ re(qr!^\Qno-longer-supported "dependencies" keyword present (at location "https://iam.draft2019-09.com")!) ]; |
| 461 |
$expected = superbagof(@$expected) if not $ENV{AUTHOR_TESTING}; |
| 462 |
cmp_result( |
| 463 |
[ warnings { |
| 464 |
$js->add_schema({ |
| 465 |
'$id' => 'https://iam.draft2019-09.com', |
| 466 |
'$schema' => 'https://json-schema.org/draft/2019-09/schema', |
| 467 |
'$ref' => 'https://iam.draft7.com', |
| 468 |
dependencies => { foo => false }, |
| 469 |
dependentSchemas => { foo => false }, |
| 470 |
additionalProperties => { format => 'ipv6' }, |
| 471 |
}) |
| 472 |
} ], |
| 473 |
$expected, |
| 474 |
'no unexpected warnings', |
| 475 |
); |
| 476 |
$js->add_schema({ |
| 477 |
'$id' => 'https://iam.draft7.com', |
| 478 |
'$schema' => 'http://json-schema.org/draft-07/schema#', |
| 479 |
dependencies => { foo => false }, |
| 480 |
dependentSchemas => { foo => false }, |
| 481 |
additionalProperties => { format => 'ipv4' }, |
| 482 |
unevaluatedProperties => false, # this should be ignored |
| 483 |
}); |
| 484 |
|
| 485 |
is_equal( |
| 486 |
$js->evaluate({ foo => 'hi' }, 'https://iam.draft2019-09.com')->TO_JSON, |
| 487 |
{ |
| 488 |
valid => false, |
| 489 |
errors => [ |
| 490 |
{ |
| 491 |
instanceLocation => '', |
| 492 |
keywordLocation => '/$ref/dependencies/foo', |
| 493 |
absoluteKeywordLocation => 'https://iam.draft7.com#/dependencies/foo', |
| 494 |
error => 'subschema is false', |
| 495 |
}, |
| 496 |
{ |
| 497 |
instanceLocation => '', |
| 498 |
keywordLocation => '/$ref/dependencies', |
| 499 |
absoluteKeywordLocation => 'https://iam.draft7.com#/dependencies', |
| 500 |
error => 'not all dependencies are satisfied', |
| 501 |
}, |
| 502 |
{ |
| 503 |
instanceLocation => '/foo', |
| 504 |
keywordLocation => '/$ref/additionalProperties/format', |
| 505 |
absoluteKeywordLocation => 'https://iam.draft7.com#/additionalProperties/format', |
| 506 |
error => 'not a valid ipv4 string', |
| 507 |
}, |
| 508 |
{ |
| 509 |
instanceLocation => '', |
| 510 |
keywordLocation => '/$ref/additionalProperties', |
| 511 |
absoluteKeywordLocation => 'https://iam.draft7.com#/additionalProperties', |
| 512 |
error => 'not all additional properties are valid', |
| 513 |
}, |
| 514 |
{ |
| 515 |
instanceLocation => '', |
| 516 |
keywordLocation => '/dependentSchemas/foo', |
| 517 |
absoluteKeywordLocation => 'https://iam.draft2019-09.com#/dependentSchemas/foo', |
| 518 |
error => 'subschema is false', |
| 519 |
}, |
| 520 |
{ |
| 521 |
instanceLocation => '', |
| 522 |
keywordLocation => '/dependentSchemas', |
| 523 |
absoluteKeywordLocation => 'https://iam.draft2019-09.com#/dependentSchemas', |
| 524 |
error => 'not all dependencies are satisfied', |
| 525 |
}, |
| 526 |
{ |
| 527 |
instanceLocation => '/foo', |
| 528 |
keywordLocation => '/additionalProperties/format', |
| 529 |
absoluteKeywordLocation => 'https://iam.draft2019-09.com#/additionalProperties/format', |
| 530 |
error => 'not a valid ipv6 string', |
| 531 |
}, |
| 532 |
{ |
| 533 |
instanceLocation => '', |
| 534 |
keywordLocation => '/additionalProperties', |
| 535 |
absoluteKeywordLocation => 'https://iam.draft2019-09.com#/additionalProperties', |
| 536 |
error => 'not all additional properties are valid', |
| 537 |
}, |
| 538 |
], |
| 539 |
}, |
| 540 |
'switching between specification versions is acceptable when crossing document boundaries', |
| 541 |
); |
| 542 |
cmp_result( |
| 543 |
$js->{_resource_index}{'https://iam.draft2019-09.com'}, |
| 544 |
superhashof({ |
| 545 |
specification_version => 'draft2019-09', |
| 546 |
vocabularies => [ map 'JSON::Schema::Modern::Vocabulary::'.$_, |
| 547 |
qw(Core Validation FormatAnnotation Applicator Content MetaData) ], |
| 548 |
}), |
| 549 |
'resources for top level schema', |
| 550 |
); |
| 551 |
cmp_result( |
| 552 |
$js->{_resource_index}{'https://iam.draft7.com'}, |
| 553 |
superhashof({ |
| 554 |
specification_version => 'draft7', |
| 555 |
vocabularies => [ map 'JSON::Schema::Modern::Vocabulary::'.$_, |
| 556 |
qw(Core Validation FormatAnnotation Applicator Content MetaData) ], |
| 557 |
}), |
| 558 |
'resources for subschema', |
| 559 |
); |
| 560 |
|
| 561 |
|
| 562 |
$expected = [ re(qr!^\Qno-longer-supported "dependencies" keyword present (at location "https://iam.draft2020-12-2.com")!) ]; |
| 563 |
$expected = superbagof(@$expected) if not $ENV{AUTHOR_TESTING}; |
| 564 |
$js->add_schema({ |
| 565 |
'$id' => 'https://iam.draft7-2.com', |
| 566 |
'$schema' => 'http://json-schema.org/draft-07/schema#', |
| 567 |
allOf => [ { '$ref' => 'https://iam.draft2020-12-2.com' } ], |
| 568 |
dependencies => { foo => false }, |
| 569 |
dependentSchemas => { foo => false }, |
| 570 |
additionalProperties => { format => 'ipv4' }, |
| 571 |
unevaluatedProperties => false, # this should be ignored |
| 572 |
}); |
| 573 |
cmp_result( |
| 574 |
[ warnings { |
| 575 |
$js->add_schema({ |
| 576 |
'$id' => 'https://iam.draft2020-12-2.com', |
| 577 |
'$schema' => 'https://json-schema.org/draft/2020-12/schema', |
| 578 |
dependencies => { foo => false }, |
| 579 |
dependentSchemas => { foo => false }, |
| 580 |
additionalProperties => { format => 'ipv6' }, |
| 581 |
}) |
| 582 |
} ], |
| 583 |
$expected, |
| 584 |
'no unexpected warnings', |
| 585 |
); |
| 586 |
|
| 587 |
is_equal( |
| 588 |
$js->evaluate({ foo => 'hi' }, 'https://iam.draft7-2.com')->TO_JSON, |
| 589 |
{ |
| 590 |
valid => false, |
| 591 |
errors => [ |
| 592 |
{ |
| 593 |
instanceLocation => '', |
| 594 |
keywordLocation => '/allOf/0/$ref/dependentSchemas/foo', |
| 595 |
absoluteKeywordLocation => 'https://iam.draft2020-12-2.com#/dependentSchemas/foo', |
| 596 |
error => 'subschema is false', |
| 597 |
}, |
| 598 |
{ |
| 599 |
instanceLocation => '', |
| 600 |
keywordLocation => '/allOf/0/$ref/dependentSchemas', |
| 601 |
absoluteKeywordLocation => 'https://iam.draft2020-12-2.com#/dependentSchemas', |
| 602 |
error => 'not all dependencies are satisfied', |
| 603 |
}, |
| 604 |
{ |
| 605 |
instanceLocation => '/foo', |
| 606 |
keywordLocation => '/allOf/0/$ref/additionalProperties/format', |
| 607 |
absoluteKeywordLocation => 'https://iam.draft2020-12-2.com#/additionalProperties/format', |
| 608 |
error => 'not a valid ipv6 string', |
| 609 |
}, |
| 610 |
{ |
| 611 |
instanceLocation => '', |
| 612 |
keywordLocation => '/allOf/0/$ref/additionalProperties', |
| 613 |
absoluteKeywordLocation => 'https://iam.draft2020-12-2.com#/additionalProperties', |
| 614 |
error => 'not all additional properties are valid', |
| 615 |
}, |
| 616 |
{ |
| 617 |
instanceLocation => '', |
| 618 |
keywordLocation => '/allOf', |
| 619 |
absoluteKeywordLocation => 'https://iam.draft7-2.com#/allOf', |
| 620 |
error => 'subschema 0 is not valid', |
| 621 |
}, |
| 622 |
{ |
| 623 |
instanceLocation => '', |
| 624 |
keywordLocation => '/dependencies/foo', |
| 625 |
absoluteKeywordLocation => 'https://iam.draft7-2.com#/dependencies/foo', |
| 626 |
error => 'subschema is false', |
| 627 |
}, |
| 628 |
{ |
| 629 |
instanceLocation => '', |
| 630 |
keywordLocation => '/dependencies', |
| 631 |
absoluteKeywordLocation => 'https://iam.draft7-2.com#/dependencies', |
| 632 |
error => 'not all dependencies are satisfied', |
| 633 |
}, |
| 634 |
{ |
| 635 |
instanceLocation => '/foo', |
| 636 |
keywordLocation => '/additionalProperties/format', |
| 637 |
absoluteKeywordLocation => 'https://iam.draft7-2.com#/additionalProperties/format', |
| 638 |
error => 'not a valid ipv4 string', |
| 639 |
}, |
| 640 |
{ |
| 641 |
instanceLocation => '', |
| 642 |
keywordLocation => '/additionalProperties', |
| 643 |
absoluteKeywordLocation => 'https://iam.draft7-2.com#/additionalProperties', |
| 644 |
error => 'not all additional properties are valid', |
| 645 |
}, |
| 646 |
], |
| 647 |
}, |
| 648 |
'switching between specification versions is acceptable when crossing document boundaries', |
| 649 |
); |
| 650 |
cmp_result( |
| 651 |
$js->{_resource_index}{'https://iam.draft7-2.com'}, |
| 652 |
superhashof({ |
| 653 |
specification_version => 'draft7', |
| 654 |
vocabularies => [ map 'JSON::Schema::Modern::Vocabulary::'.$_, |
| 655 |
qw(Core Validation FormatAnnotation Applicator Content MetaData) ], |
| 656 |
}), |
| 657 |
'resources for top level schema', |
| 658 |
); |
| 659 |
cmp_result( |
| 660 |
$js->{_resource_index}{'https://iam.draft2020-12-2.com'}, |
| 661 |
superhashof({ |
| 662 |
specification_version => 'draft2020-12', |
| 663 |
vocabularies => [ map 'JSON::Schema::Modern::Vocabulary::'.$_, |
| 664 |
qw(Core Validation FormatAnnotation Applicator Content MetaData Unevaluated) ], |
| 665 |
}), |
| 666 |
'resources for subschema', |
| 667 |
); |
| 668 |
}; |
| 669 |
|
| 670 |
subtest 'changing specification versions within documents' => sub { |
| 671 |
allow_warnings(1); |
| 672 |
is_equal( |
| 673 |
$js->evaluate( |
| 674 |
{ foo => 'hi' }, |
| 675 |
{ |
| 676 |
'$id' => 'https://iam.draft2019-09-3.com', |
| 677 |
'$schema' => 'https://json-schema.org/draft/2019-09/schema', |
| 678 |
allOf => [ |
| 679 |
{ |
| 680 |
'$id' => 'https://iam.draft7-3.com', |
| 681 |
'$schema' => 'http://json-schema.org/draft-07/schema#', |
| 682 |
dependencies => { foo => false }, |
| 683 |
dependentSchemas => 'blurp', # this should be ignored |
| 684 |
additionalProperties => { format => 'ipv4' }, |
| 685 |
unevaluatedProperties => 'blurp', # this should be ignored |
| 686 |
}, |
| 687 |
], |
| 688 |
dependencies => 'blurp', # this should be ignored |
| 689 |
dependentSchemas => { foo => false }, |
| 690 |
additionalProperties => { format => 'ipv6' }, |
| 691 |
}, |
| 692 |
)->TO_JSON, |
| 693 |
{ |
| 694 |
valid => false, |
| 695 |
errors => [ |
| 696 |
{ |
| 697 |
instanceLocation => '', |
| 698 |
keywordLocation => '/allOf/0/dependencies/foo', |
| 699 |
absoluteKeywordLocation => 'https://iam.draft7-3.com#/dependencies/foo', |
| 700 |
error => 'subschema is false', |
| 701 |
}, |
| 702 |
{ |
| 703 |
instanceLocation => '', |
| 704 |
keywordLocation => '/allOf/0/dependencies', |
| 705 |
absoluteKeywordLocation => 'https://iam.draft7-3.com#/dependencies', |
| 706 |
error => 'not all dependencies are satisfied', |
| 707 |
}, |
| 708 |
{ |
| 709 |
instanceLocation => '/foo', |
| 710 |
keywordLocation => '/allOf/0/additionalProperties/format', |
| 711 |
absoluteKeywordLocation => 'https://iam.draft7-3.com#/additionalProperties/format', |
| 712 |
error => 'not a valid ipv4 string', |
| 713 |
}, |
| 714 |
{ |
| 715 |
instanceLocation => '', |
| 716 |
keywordLocation => '/allOf/0/additionalProperties', |
| 717 |
absoluteKeywordLocation => 'https://iam.draft7-3.com#/additionalProperties', |
| 718 |
error => 'not all additional properties are valid', |
| 719 |
}, |
| 720 |
{ |
| 721 |
instanceLocation => '', |
| 722 |
keywordLocation => '/allOf', |
| 723 |
absoluteKeywordLocation => 'https://iam.draft2019-09-3.com#/allOf', |
| 724 |
error => 'subschema 0 is not valid', |
| 725 |
}, |
| 726 |
{ |
| 727 |
instanceLocation => '', |
| 728 |
keywordLocation => '/dependentSchemas/foo', |
| 729 |
absoluteKeywordLocation => 'https://iam.draft2019-09-3.com#/dependentSchemas/foo', |
| 730 |
error => 'subschema is false', |
| 731 |
}, |
| 732 |
{ |
| 733 |
instanceLocation => '', |
| 734 |
keywordLocation => '/dependentSchemas', |
| 735 |
absoluteKeywordLocation => 'https://iam.draft2019-09-3.com#/dependentSchemas', |
| 736 |
error => 'not all dependencies are satisfied', |
| 737 |
}, |
| 738 |
{ |
| 739 |
instanceLocation => '/foo', |
| 740 |
keywordLocation => '/additionalProperties/format', |
| 741 |
absoluteKeywordLocation => 'https://iam.draft2019-09-3.com#/additionalProperties/format', |
| 742 |
error => 'not a valid ipv6 string', |
| 743 |
}, |
| 744 |
{ |
| 745 |
instanceLocation => '', |
| 746 |
keywordLocation => '/additionalProperties', |
| 747 |
absoluteKeywordLocation => 'https://iam.draft2019-09-3.com#/additionalProperties', |
| 748 |
error => 'not all additional properties are valid', |
| 749 |
}, |
| 750 |
], |
| 751 |
}, |
| 752 |
'switching between specification versions is acceptable within a document, draft2019-09 -> draft7', |
| 753 |
); |
| 754 |
allow_warnings(0); |
| 755 |
|
| 756 |
cmp_result( |
| 757 |
$js->{_resource_index}{'https://iam.draft2019-09-3.com'}, |
| 758 |
superhashof({ |
| 759 |
specification_version => 'draft2019-09', |
| 760 |
vocabularies => [ map 'JSON::Schema::Modern::Vocabulary::'.$_, |
| 761 |
qw(Core Validation FormatAnnotation Applicator Content MetaData) ], |
| 762 |
}), |
| 763 |
'resources for top level schema', |
| 764 |
); |
| 765 |
cmp_result( |
| 766 |
$js->{_resource_index}{'https://iam.draft7-3.com'}, |
| 767 |
superhashof({ |
| 768 |
specification_version => 'draft7', |
| 769 |
vocabularies => [ map 'JSON::Schema::Modern::Vocabulary::'.$_, |
| 770 |
qw(Core Validation FormatAnnotation Applicator Content MetaData) ], |
| 771 |
}), |
| 772 |
'resources for subschema', |
| 773 |
); |
| 774 |
|
| 775 |
allow_warnings(1); |
| 776 |
is_equal( |
| 777 |
$js->evaluate( |
| 778 |
{ foo => 'hi' }, |
| 779 |
{ |
| 780 |
'$id' => 'https://iam.draft7-4.com', |
| 781 |
'$schema' => 'http://json-schema.org/draft-07/schema#', |
| 782 |
allOf => [ |
| 783 |
{ |
| 784 |
'$id' => 'https://iam.draft2020-12-4.com', |
| 785 |
'$schema' => 'https://json-schema.org/draft/2020-12/schema', |
| 786 |
dependencies => { foo => false }, # this should be ignored |
| 787 |
dependentSchemas => { foo => false }, |
| 788 |
additionalProperties => { format => 'ipv4' }, |
| 789 |
}, |
| 790 |
], |
| 791 |
dependencies => { foo => false }, |
| 792 |
dependentSchemas => { foo => false }, # this should be ignored |
| 793 |
additionalProperties => { format => 'ipv6' }, |
| 794 |
unevaluatedProperties => false, # this should be ignored |
| 795 |
}, |
| 796 |
)->TO_JSON, |
| 797 |
{ |
| 798 |
valid => false, |
| 799 |
errors => [ |
| 800 |
{ |
| 801 |
instanceLocation => '', |
| 802 |
keywordLocation => '/allOf/0/dependentSchemas/foo', |
| 803 |
absoluteKeywordLocation => 'https://iam.draft2020-12-4.com#/dependentSchemas/foo', |
| 804 |
error => 'subschema is false', |
| 805 |
}, |
| 806 |
{ |
| 807 |
instanceLocation => '', |
| 808 |
keywordLocation => '/allOf/0/dependentSchemas', |
| 809 |
absoluteKeywordLocation => 'https://iam.draft2020-12-4.com#/dependentSchemas', |
| 810 |
error => 'not all dependencies are satisfied', |
| 811 |
}, |
| 812 |
{ |
| 813 |
instanceLocation => '/foo', |
| 814 |
keywordLocation => '/allOf/0/additionalProperties/format', |
| 815 |
absoluteKeywordLocation => 'https://iam.draft2020-12-4.com#/additionalProperties/format', |
| 816 |
error => 'not a valid ipv4 string', |
| 817 |
}, |
| 818 |
{ |
| 819 |
instanceLocation => '', |
| 820 |
keywordLocation => '/allOf/0/additionalProperties', |
| 821 |
absoluteKeywordLocation => 'https://iam.draft2020-12-4.com#/additionalProperties', |
| 822 |
error => 'not all additional properties are valid', |
| 823 |
}, |
| 824 |
{ |
| 825 |
instanceLocation => '', |
| 826 |
keywordLocation => '/allOf', |
| 827 |
absoluteKeywordLocation => 'https://iam.draft7-4.com#/allOf', |
| 828 |
error => 'subschema 0 is not valid', |
| 829 |
}, |
| 830 |
{ |
| 831 |
instanceLocation => '', |
| 832 |
keywordLocation => '/dependencies/foo', |
| 833 |
absoluteKeywordLocation => 'https://iam.draft7-4.com#/dependencies/foo', |
| 834 |
error => 'subschema is false', |
| 835 |
}, |
| 836 |
{ |
| 837 |
instanceLocation => '', |
| 838 |
keywordLocation => '/dependencies', |
| 839 |
absoluteKeywordLocation => 'https://iam.draft7-4.com#/dependencies', |
| 840 |
error => 'not all dependencies are satisfied', |
| 841 |
}, |
| 842 |
{ |
| 843 |
instanceLocation => '/foo', |
| 844 |
keywordLocation => '/additionalProperties/format', |
| 845 |
absoluteKeywordLocation => 'https://iam.draft7-4.com#/additionalProperties/format', |
| 846 |
error => 'not a valid ipv6 string', |
| 847 |
}, |
| 848 |
{ |
| 849 |
instanceLocation => '', |
| 850 |
keywordLocation => '/additionalProperties', |
| 851 |
absoluteKeywordLocation => 'https://iam.draft7-4.com#/additionalProperties', |
| 852 |
error => 'not all additional properties are valid', |
| 853 |
}, |
| 854 |
], |
| 855 |
}, |
| 856 |
'switching between specification versions is acceptable within a document, draft7 -> draf2020-12', |
| 857 |
); |
| 858 |
allow_warnings(0); |
| 859 |
cmp_result( |
| 860 |
$js->{_resource_index}{'https://iam.draft7-4.com'}, |
| 861 |
superhashof({ |
| 862 |
specification_version => 'draft7', |
| 863 |
vocabularies => [ map 'JSON::Schema::Modern::Vocabulary::'.$_, |
| 864 |
qw(Core Validation FormatAnnotation Applicator Content MetaData) ], |
| 865 |
}), |
| 866 |
'resources for top level schema', |
| 867 |
); |
| 868 |
cmp_result( |
| 869 |
$js->{_resource_index}{'https://iam.draft2020-12-4.com'}, |
| 870 |
superhashof({ |
| 871 |
specification_version => 'draft2020-12', |
| 872 |
vocabularies => [ map 'JSON::Schema::Modern::Vocabulary::'.$_, |
| 873 |
qw(Core Validation FormatAnnotation Applicator Content MetaData Unevaluated) ], |
| 874 |
}), |
| 875 |
'resources for subschema', |
| 876 |
); |
| 877 |
|
| 878 |
allow_warnings(1); |
| 879 |
is_equal( |
| 880 |
$js->evaluate( |
| 881 |
{ foo => 'hi' }, |
| 882 |
{ |
| 883 |
'$id' => 'https://iam.draft2020-12-5.com', |
| 884 |
'$schema' => 'https://json-schema.org/draft/2020-12/schema', |
| 885 |
allOf => [ |
| 886 |
{ |
| 887 |
id => 'https://iam.draft4-5.com', |
| 888 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 889 |
definitions => { blah => { not => {} } }, |
| 890 |
dependencies => { foo => { not => {} } }, |
| 891 |
dependentSchemas => { foo => { not => {} } }, # this should be ignored |
| 892 |
allOf => [ { '$ref' => '#/definitions/blah' } ], |
| 893 |
additionalProperties => { format => 'ipv4' }, |
| 894 |
}, |
| 895 |
], |
| 896 |
dependencies => { foo => false }, # this should be ignored |
| 897 |
dependentSchemas => { foo => false }, |
| 898 |
additionalProperties => { format => 'ipv6' }, |
| 899 |
unevaluatedProperties => false, |
| 900 |
}, |
| 901 |
)->TO_JSON, |
| 902 |
{ |
| 903 |
valid => false, |
| 904 |
errors => [ |
| 905 |
{ |
| 906 |
instanceLocation => '', |
| 907 |
keywordLocation => '/allOf/0/allOf/0/$ref/not', |
| 908 |
absoluteKeywordLocation => 'https://iam.draft4-5.com#/definitions/blah/not', |
| 909 |
error => 'subschema is valid', |
| 910 |
}, |
| 911 |
{ |
| 912 |
instanceLocation => '', |
| 913 |
keywordLocation => '/allOf/0/allOf', |
| 914 |
absoluteKeywordLocation => 'https://iam.draft4-5.com#/allOf', |
| 915 |
error => 'subschema 0 is not valid', |
| 916 |
}, |
| 917 |
{ |
| 918 |
instanceLocation => '', |
| 919 |
keywordLocation => '/allOf/0/dependencies/foo/not', |
| 920 |
absoluteKeywordLocation => 'https://iam.draft4-5.com#/dependencies/foo/not', |
| 921 |
error => 'subschema is valid', |
| 922 |
}, |
| 923 |
{ |
| 924 |
instanceLocation => '', |
| 925 |
keywordLocation => '/allOf/0/dependencies', |
| 926 |
absoluteKeywordLocation => 'https://iam.draft4-5.com#/dependencies', |
| 927 |
error => 'not all dependencies are satisfied', |
| 928 |
}, |
| 929 |
{ |
| 930 |
instanceLocation => '/foo', |
| 931 |
keywordLocation => '/allOf/0/additionalProperties/format', |
| 932 |
absoluteKeywordLocation => 'https://iam.draft4-5.com#/additionalProperties/format', |
| 933 |
error => 'not a valid ipv4 string', |
| 934 |
}, |
| 935 |
{ |
| 936 |
instanceLocation => '', |
| 937 |
keywordLocation => '/allOf/0/additionalProperties', |
| 938 |
absoluteKeywordLocation => 'https://iam.draft4-5.com#/additionalProperties', |
| 939 |
error => 'not all additional properties are valid', |
| 940 |
}, |
| 941 |
{ |
| 942 |
instanceLocation => '', |
| 943 |
keywordLocation => '/allOf', |
| 944 |
absoluteKeywordLocation => 'https://iam.draft2020-12-5.com#/allOf', |
| 945 |
error => 'subschema 0 is not valid', |
| 946 |
}, |
| 947 |
{ |
| 948 |
instanceLocation => '', |
| 949 |
keywordLocation => '/dependentSchemas/foo', |
| 950 |
absoluteKeywordLocation => 'https://iam.draft2020-12-5.com#/dependentSchemas/foo', |
| 951 |
error => 'subschema is false', |
| 952 |
}, |
| 953 |
{ |
| 954 |
instanceLocation => '', |
| 955 |
keywordLocation => '/dependentSchemas', |
| 956 |
absoluteKeywordLocation => 'https://iam.draft2020-12-5.com#/dependentSchemas', |
| 957 |
error => 'not all dependencies are satisfied', |
| 958 |
}, |
| 959 |
{ |
| 960 |
instanceLocation => '/foo', |
| 961 |
keywordLocation => '/additionalProperties/format', |
| 962 |
absoluteKeywordLocation => 'https://iam.draft2020-12-5.com#/additionalProperties/format', |
| 963 |
error => 'not a valid ipv6 string', |
| 964 |
}, |
| 965 |
{ |
| 966 |
instanceLocation => '', |
| 967 |
keywordLocation => '/additionalProperties', |
| 968 |
absoluteKeywordLocation => 'https://iam.draft2020-12-5.com#/additionalProperties', |
| 969 |
error => 'not all additional properties are valid', |
| 970 |
}, |
| 971 |
], |
| 972 |
}, |
| 973 |
'switching between specification versions is acceptable within a document, draft2020-12 -> draft4', |
| 974 |
); |
| 975 |
allow_warnings(0); |
| 976 |
|
| 977 |
cmp_result( |
| 978 |
$js->{_resource_index}{'https://iam.draft2020-12-5.com'}, |
| 979 |
superhashof({ |
| 980 |
specification_version => 'draft2020-12', |
| 981 |
vocabularies => [ map 'JSON::Schema::Modern::Vocabulary::'.$_, |
| 982 |
qw(Core Validation FormatAnnotation Applicator Content MetaData Unevaluated) ], |
| 983 |
}), |
| 984 |
'resources for top level schema', |
| 985 |
); |
| 986 |
cmp_result( |
| 987 |
$js->{_resource_index}{'https://iam.draft4-5.com'}, |
| 988 |
superhashof({ |
| 989 |
specification_version => 'draft4', |
| 990 |
vocabularies => [ map 'JSON::Schema::Modern::Vocabulary::'.$_, |
| 991 |
qw(Core Validation FormatAnnotation Applicator MetaData) ], |
| 992 |
}), |
| 993 |
'resources for subschema', |
| 994 |
); |
| 995 |
}; |
| 996 |
|
| 997 |
undef $js; |
| 998 |
|
| 999 |
subtest '$vocabulary syntax' => sub { |
| 1000 |
is_equal( |
| 1001 |
JSON::Schema::Modern->new->evaluate( |
| 1002 |
1, |
| 1003 |
{ |
| 1004 |
'$vocabulary' => { |
| 1005 |
'https://json-schema.org/draft/2020-12/vocab/core' => true, |
| 1006 |
'#/notauri' => false, |
| 1007 |
'https://foo' => 1, |
| 1008 |
'https://json-schema.org/draft/2019-09/vocab/validation' => true, |
| 1009 |
'https://json-schema.org/draft/2020-12/vocab/applicator' => true, |
| 1010 |
'https://unknown' => true, # ignored.. for now |
| 1011 |
'https://unknown2' => false, # "" |
| 1012 |
}, |
| 1013 |
}, |
| 1014 |
)->TO_JSON, |
| 1015 |
{ |
| 1016 |
valid => false, |
| 1017 |
errors => [ |
| 1018 |
{ |
| 1019 |
keywordLocation => jsonp('/$vocabulary', '#/notauri'), |
| 1020 |
error => '"#/notauri" is not a valid URI', |
| 1021 |
}, |
| 1022 |
{ |
| 1023 |
keywordLocation => jsonp('/$vocabulary', 'https://foo'), |
| 1024 |
error => '$vocabulary value at "https://foo" is not a boolean', |
| 1025 |
}, |
| 1026 |
], |
| 1027 |
}, |
| 1028 |
'$vocabulary syntax checks', |
| 1029 |
); |
| 1030 |
|
| 1031 |
is_equal( |
| 1032 |
JSON::Schema::Modern->new->evaluate( |
| 1033 |
1, |
| 1034 |
{ |
| 1035 |
'$id' => 'http://mymetaschema', |
| 1036 |
items => { '$vocabulary' => { 'https://json-schema.org/draft/2020-12/vocab/applicator' => true } }, |
| 1037 |
}, |
| 1038 |
)->TO_JSON, |
| 1039 |
{ |
| 1040 |
valid => false, |
| 1041 |
errors => [ |
| 1042 |
{ |
| 1043 |
keywordLocation => '/items/$vocabulary', |
| 1044 |
absoluteKeywordLocation => 'http://mymetaschema#/items/$vocabulary', |
| 1045 |
error => '$vocabulary can only appear at the schema resource root', |
| 1046 |
}, |
| 1047 |
], |
| 1048 |
}, |
| 1049 |
'$vocabulary location check - resource root', |
| 1050 |
); |
| 1051 |
|
| 1052 |
is_equal( |
| 1053 |
JSON::Schema::Modern->new->evaluate( |
| 1054 |
1, |
| 1055 |
{ |
| 1056 |
items => { |
| 1057 |
'$id' => 'foobar', |
| 1058 |
'$vocabulary' => { 'https://json-schema.org/draft/2020-12/vocab/core' => true }, |
| 1059 |
}, |
| 1060 |
}, |
| 1061 |
)->TO_JSON, |
| 1062 |
{ valid => true }, |
| 1063 |
'$vocabulary location check - document root', |
| 1064 |
); |
| 1065 |
|
| 1066 |
|
| 1067 |
my $js = JSON::Schema::Modern->new; |
| 1068 |
is_equal( |
| 1069 |
$js->evaluate( |
| 1070 |
1, |
| 1071 |
{ |
| 1072 |
'$id' => 'http://mymetaschema', |
| 1073 |
'$vocabulary' => { |
| 1074 |
'https://json-schema.org/draft/2020-12/vocab/core' => true, |
| 1075 |
'https://json-schema.org/draft/2020-12/vocab/applicator' => false, |
| 1076 |
}, |
| 1077 |
}, |
| 1078 |
)->TO_JSON, |
| 1079 |
{ valid => true }, |
| 1080 |
'successfully evaluated a metaschema that specifies vocabularies', |
| 1081 |
); |
| 1082 |
|
| 1083 |
cmp_result( |
| 1084 |
$js->{_resource_index}{'http://mymetaschema'}, |
| 1085 |
{ |
| 1086 |
canonical_uri => str('http://mymetaschema'), |
| 1087 |
path => '', |
| 1088 |
specification_version => 'draft2020-12', |
| 1089 |
document => ignore, |
| 1090 |
vocabularies => [ |
| 1091 |
map 'JSON::Schema::Modern::Vocabulary::'.$_, |
| 1092 |
qw(Core Validation FormatAnnotation Applicator Content MetaData Unevaluated), |
| 1093 |
], |
| 1094 |
}, |
| 1095 |
'metaschemas are not saved on the resource', |
| 1096 |
); |
| 1097 |
|
| 1098 |
is_equal( |
| 1099 |
$js->evaluate(1, { '$schema' => 'http://mymetaschema' })->TO_JSON, |
| 1100 |
{ valid => true }, |
| 1101 |
'..but once we use the schema as a metaschema,', |
| 1102 |
); |
| 1103 |
|
| 1104 |
is_equal( |
| 1105 |
$js->{_metaschema_vocabulary_classes}{'http://mymetaschema'}, |
| 1106 |
[ |
| 1107 |
'draft2020-12', |
| 1108 |
[ |
| 1109 |
'JSON::Schema::Modern::Vocabulary::Core', |
| 1110 |
'JSON::Schema::Modern::Vocabulary::Applicator', |
| 1111 |
], |
| 1112 |
], |
| 1113 |
'... the vocabulary information is now cached in the evaluator', |
| 1114 |
); |
| 1115 |
}; |
| 1116 |
|
| 1117 |
subtest 'changing dialects (same specification version)' => sub { |
| 1118 |
my $js = JSON::Schema::Modern->new(collect_annotations => 1); |
| 1119 |
|
| 1120 |
$js->add_schema({ |
| 1121 |
'$id' => 'https://my_metaschema', |
| 1122 |
'$schema' => 'https://json-schema.org/draft/2020-12/schema', |
| 1123 |
'$vocabulary' => { |
| 1124 |
'https://json-schema.org/draft/2020-12/vocab/core' => true, |
| 1125 |
'https://json-schema.org/draft/2020-12/vocab/validation' => true, |
| 1126 |
# no applicator! |
| 1127 |
}, |
| 1128 |
}); |
| 1129 |
|
| 1130 |
$js->add_schema({ |
| 1131 |
'$id' => 'https://my_other_schema', |
| 1132 |
'$schema' => 'https://my_metaschema', |
| 1133 |
type => 'object', |
| 1134 |
properties => { bar => false }, # this keyword should only annotate |
| 1135 |
zeta => 1, |
| 1136 |
}); |
| 1137 |
|
| 1138 |
is_equal( |
| 1139 |
$js->evaluate( |
| 1140 |
{ foo => { bar => 1 } }, |
| 1141 |
{ |
| 1142 |
'$id' => 'https://example.com', |
| 1143 |
additionalProperties => { |
| 1144 |
'$ref' => 'https://my_other_schema', |
| 1145 |
}, |
| 1146 |
}, |
| 1147 |
)->TO_JSON, |
| 1148 |
{ |
| 1149 |
valid => true, |
| 1150 |
annotations => [ |
| 1151 |
{ |
| 1152 |
instanceLocation => '/foo', |
| 1153 |
keywordLocation => '/additionalProperties/$ref/properties', |
| 1154 |
absoluteKeywordLocation => 'https://my_other_schema#/properties', |
| 1155 |
annotation => { bar => false }, |
| 1156 |
}, |
| 1157 |
{ |
| 1158 |
instanceLocation => '/foo', |
| 1159 |
keywordLocation => '/additionalProperties/$ref/zeta', |
| 1160 |
absoluteKeywordLocation => 'https://my_other_schema#/zeta', |
| 1161 |
annotation => 1, |
| 1162 |
}, |
| 1163 |
{ |
| 1164 |
instanceLocation => '', |
| 1165 |
keywordLocation => '/additionalProperties', |
| 1166 |
absoluteKeywordLocation => 'https://example.com#/additionalProperties', |
| 1167 |
annotation => ['foo'], |
| 1168 |
}, |
| 1169 |
], |
| 1170 |
}, |
| 1171 |
'evaluation of the subschema in another document correctly uses the new $id and $schema', |
| 1172 |
); |
| 1173 |
|
| 1174 |
is_equal( |
| 1175 |
$js->evaluate( |
| 1176 |
{ foo => { bar => 1 } }, |
| 1177 |
{ |
| 1178 |
'$id' => 'https://example2.com', |
| 1179 |
'$defs' => { |
| 1180 |
'my_def' => { |
| 1181 |
'$id' => 'https://my_other_schema2', |
| 1182 |
'$schema' => 'https://my_metaschema', |
| 1183 |
type => 'object', |
| 1184 |
properties => { bar => false }, # this keyword should only annotate |
| 1185 |
zeta => 1, |
| 1186 |
}, |
| 1187 |
}, |
| 1188 |
additionalProperties => { |
| 1189 |
'$ref' => 'https://my_other_schema2', |
| 1190 |
}, |
| 1191 |
}, |
| 1192 |
)->TO_JSON, |
| 1193 |
{ |
| 1194 |
valid => true, |
| 1195 |
annotations => [ |
| 1196 |
{ |
| 1197 |
instanceLocation => '/foo', |
| 1198 |
keywordLocation => '/additionalProperties/$ref/properties', |
| 1199 |
absoluteKeywordLocation => 'https://my_other_schema2#/properties', |
| 1200 |
annotation => { bar => false }, |
| 1201 |
}, |
| 1202 |
{ |
| 1203 |
instanceLocation => '/foo', |
| 1204 |
keywordLocation => '/additionalProperties/$ref/zeta', |
| 1205 |
absoluteKeywordLocation => 'https://my_other_schema2#/zeta', |
| 1206 |
annotation => 1, |
| 1207 |
}, |
| 1208 |
{ |
| 1209 |
instanceLocation => '', |
| 1210 |
keywordLocation => '/additionalProperties', |
| 1211 |
absoluteKeywordLocation => 'https://example2.com#/additionalProperties', |
| 1212 |
annotation => ['foo'], |
| 1213 |
}, |
| 1214 |
], |
| 1215 |
}, |
| 1216 |
'evaluation of the subschema in the same document via a $ref correctly uses the new $id and $schema', |
| 1217 |
); |
| 1218 |
|
| 1219 |
is_equal( |
| 1220 |
$js->evaluate( |
| 1221 |
{ foo => { bar => 1 } }, |
| 1222 |
{ |
| 1223 |
'$id' => 'https://example3.com', |
| 1224 |
additionalProperties => { |
| 1225 |
'$id' => 'https://my_other_schema3', |
| 1226 |
'$schema' => 'https://my_metaschema', |
| 1227 |
type => 'object', |
| 1228 |
properties => { bar => false }, # this keyword should only annotate |
| 1229 |
zeta => 1, |
| 1230 |
}, |
| 1231 |
}, |
| 1232 |
)->TO_JSON, |
| 1233 |
{ |
| 1234 |
valid => true, |
| 1235 |
annotations => [ |
| 1236 |
{ |
| 1237 |
instanceLocation => '/foo', |
| 1238 |
keywordLocation => '/additionalProperties/properties', |
| 1239 |
absoluteKeywordLocation => 'https://my_other_schema3#/properties', |
| 1240 |
annotation => { bar => false }, |
| 1241 |
}, |
| 1242 |
{ |
| 1243 |
instanceLocation => '/foo', |
| 1244 |
keywordLocation => '/additionalProperties/zeta', |
| 1245 |
absoluteKeywordLocation => 'https://my_other_schema3#/zeta', |
| 1246 |
annotation => 1, |
| 1247 |
}, |
| 1248 |
{ |
| 1249 |
instanceLocation => '', |
| 1250 |
keywordLocation => '/additionalProperties', |
| 1251 |
absoluteKeywordLocation => 'https://example3.com#/additionalProperties', |
| 1252 |
annotation => ['foo'], |
| 1253 |
}, |
| 1254 |
], |
| 1255 |
}, |
| 1256 |
'evaluation of the subschema in the same document with no $ref correctly uses the new $id and $schema', |
| 1257 |
); |
| 1258 |
|
| 1259 |
cmp_result( |
| 1260 |
$js->traverse({ |
| 1261 |
'$id' => 'https://example4.com', |
| 1262 |
additionalProperties => { |
| 1263 |
'$id' => 'https://my_other_schema4', |
| 1264 |
'$schema' => 'https://my_metaschema', |
| 1265 |
type => 'object', |
| 1266 |
properties => 1, # this is not a real keyword as the assertion vocabulary is not present |
| 1267 |
}, |
| 1268 |
}), |
| 1269 |
superhashof({ errors => [] }), |
| 1270 |
'no errors found when traversing a document with a malformed keyword outside the dialect', |
| 1271 |
); |
| 1272 |
}; |
| 1273 |
|
| 1274 |
subtest 'standard metaschemas' => sub { |
| 1275 |
my $js = JSON::Schema::Modern->new; |
| 1276 |
my ($draft202012_metaschema) = $js->get('https://json-schema.org/draft/2020-12/schema'); |
| 1277 |
|
| 1278 |
is_equal( |
| 1279 |
$js->evaluate($draft202012_metaschema, 'https://json-schema.org/draft/2020-12/schema')->TO_JSON, |
| 1280 |
{ valid => true }, |
| 1281 |
'main metaschema evaluated against its own URI', |
| 1282 |
); |
| 1283 |
|
| 1284 |
is_equal( |
| 1285 |
$js->evaluate($draft202012_metaschema, $draft202012_metaschema)->TO_JSON, |
| 1286 |
{ valid => true }, |
| 1287 |
'main metaschema evaluated against its own content', |
| 1288 |
); |
| 1289 |
|
| 1290 |
my ($draft202012_core_metaschema) = $js->get('https://json-schema.org/draft/2020-12/meta/core'); |
| 1291 |
|
| 1292 |
is_equal( |
| 1293 |
$js->evaluate($draft202012_core_metaschema, 'https://json-schema.org/draft/2020-12/schema')->TO_JSON, |
| 1294 |
{ valid => true }, |
| 1295 |
'core metaschema evaluated against the main metaschema URI', |
| 1296 |
); |
| 1297 |
|
| 1298 |
is_equal( |
| 1299 |
$js->evaluate($draft202012_core_metaschema, $draft202012_core_metaschema)->TO_JSON, |
| 1300 |
{ valid => true }, |
| 1301 |
'core metaschema evaluated against its own content', |
| 1302 |
); |
| 1303 |
}; |
| 1304 |
|
| 1305 |
subtest 'custom metaschemas, without custom vocabularies' => sub { |
| 1306 |
my $js = JSON::Schema::Modern->new; |
| 1307 |
|
| 1308 |
my $metaschema_document = $js->add_schema(my $metaschema = { |
| 1309 |
'$id' => 'http://localhost:1234/my-meta-schema', |
| 1310 |
'$schema' => 'https://json-schema.org/draft/2019-09/schema', |
| 1311 |
type => 'object', |
| 1312 |
'$recursiveAnchor' => true, |
| 1313 |
allOf => [ { '$ref' => 'https://json-schema.org/draft/2019-09/schema' } ], |
| 1314 |
}); |
| 1315 |
|
| 1316 |
cmp_result( |
| 1317 |
$metaschema_document, |
| 1318 |
methods( |
| 1319 |
canonical_uri => str('http://localhost:1234/my-meta-schema'), |
| 1320 |
metaschema_uri => str('https://json-schema.org/draft/2019-09/schema'), |
| 1321 |
), |
| 1322 |
'document contains correct values', |
| 1323 |
); |
| 1324 |
|
| 1325 |
is($metaschema_document->_get_resource($metaschema->{'$id'})->{specification_version}, 'draft2019-09', |
| 1326 |
'specification version detected from standard metaschema URI'); |
| 1327 |
|
| 1328 |
is_equal( |
| 1329 |
$js->evaluate(false, 'http://localhost:1234/my-meta-schema')->TO_JSON, |
| 1330 |
{ |
| 1331 |
valid => false, |
| 1332 |
errors => [ |
| 1333 |
{ |
| 1334 |
instanceLocation => '', |
| 1335 |
keywordLocation => '/type', |
| 1336 |
absoluteKeywordLocation => 'http://localhost:1234/my-meta-schema#/type', |
| 1337 |
error => 'got boolean, not object', |
| 1338 |
}, |
| 1339 |
], |
| 1340 |
}, |
| 1341 |
'custom metaschema restricts schemas to objects', |
| 1342 |
); |
| 1343 |
|
| 1344 |
# the evaluation of $recursiveAnchor in the schema proves that the proper specification version |
| 1345 |
# was detected via the $schema keyword |
| 1346 |
is_equal( |
| 1347 |
$js->evaluate( |
| 1348 |
{ allOf => [ false ] }, |
| 1349 |
'http://localhost:1234/my-meta-schema', |
| 1350 |
)->TO_JSON, |
| 1351 |
{ |
| 1352 |
valid => false, |
| 1353 |
errors => [ |
| 1354 |
{ |
| 1355 |
instanceLocation => '/allOf/0', |
| 1356 |
keywordLocation => '/allOf/0/$ref/allOf/1/$ref/properties/allOf/$ref/items/$recursiveRef/type', |
| 1357 |
absoluteKeywordLocation => 'http://localhost:1234/my-meta-schema#/type', |
| 1358 |
error => 'got boolean, not object', |
| 1359 |
}, |
| 1360 |
{ |
| 1361 |
instanceLocation => '/allOf', |
| 1362 |
keywordLocation => '/allOf/0/$ref/allOf/1/$ref/properties/allOf/$ref/items', |
| 1363 |
absoluteKeywordLocation => 'https://json-schema.org/draft/2019-09/meta/applicator#/$defs/schemaArray/items', |
| 1364 |
error => 'subschema is not valid against all items', |
| 1365 |
}, |
| 1366 |
{ |
| 1367 |
instanceLocation => '', |
| 1368 |
keywordLocation => '/allOf/0/$ref/allOf/1/$ref/properties', |
| 1369 |
absoluteKeywordLocation => 'https://json-schema.org/draft/2019-09/meta/applicator#/properties', |
| 1370 |
error => 'not all properties are valid', |
| 1371 |
}, |
| 1372 |
{ |
| 1373 |
instanceLocation => '', |
| 1374 |
keywordLocation => '/allOf/0/$ref/allOf', |
| 1375 |
absoluteKeywordLocation => 'https://json-schema.org/draft/2019-09/schema#/allOf', |
| 1376 |
error => 'subschema 1 is not valid', |
| 1377 |
}, |
| 1378 |
{ |
| 1379 |
instanceLocation => '', |
| 1380 |
keywordLocation => '/allOf', |
| 1381 |
absoluteKeywordLocation => 'http://localhost:1234/my-meta-schema#/allOf', |
| 1382 |
error => 'subschema 0 is not valid', |
| 1383 |
}, |
| 1384 |
], |
| 1385 |
}, |
| 1386 |
'custom metaschema recurses to standard metaschema', |
| 1387 |
); |
| 1388 |
|
| 1389 |
is_equal( |
| 1390 |
$js->evaluate({ allOf => [ {} ] }, 'http://localhost:1234/my-meta-schema')->TO_JSON, |
| 1391 |
{ valid => true }, |
| 1392 |
'objects are acceptable schemas to this metaschema', |
| 1393 |
); |
| 1394 |
|
| 1395 |
is_equal( |
| 1396 |
$js->evaluate( |
| 1397 |
1, |
| 1398 |
{ |
| 1399 |
'$id' => 'https://localhost:1234/my-schema', |
| 1400 |
'$schema' => 'http://localhost:1234/my-meta-schema', |
| 1401 |
}, |
| 1402 |
)->TO_JSON, |
| 1403 |
{ valid => true }, |
| 1404 |
'metaschemas without $vocabulary can still be used in the $schema keyword', |
| 1405 |
); |
| 1406 |
cmp_result( |
| 1407 |
$js->{_resource_index}{'https://localhost:1234/my-schema'}, |
| 1408 |
superhashof({ |
| 1409 |
specification_version => 'draft2019-09', |
| 1410 |
vocabularies => [ map 'JSON::Schema::Modern::Vocabulary::'.$_, |
| 1411 |
qw(Core Validation FormatAnnotation Applicator Content MetaData) ], |
| 1412 |
}), |
| 1413 |
'..and schema uses the correct spec version and vocabularies', |
| 1414 |
); |
| 1415 |
}; |
| 1416 |
|
| 1417 |
subtest 'other $schema usage' => sub { |
| 1418 |
my $js = JSON::Schema::Modern->new; |
| 1419 |
|
| 1420 |
is_equal( |
| 1421 |
$js->evaluate(1, { '$schema' => 20 })->TO_JSON, |
| 1422 |
{ |
| 1423 |
valid => false, |
| 1424 |
errors => [ |
| 1425 |
{ |
| 1426 |
keywordLocation => '/$schema', |
| 1427 |
error => '$schema value is not a string', |
| 1428 |
}, |
| 1429 |
], |
| 1430 |
}, |
| 1431 |
'$schema values must be strings', |
| 1432 |
); |
| 1433 |
|
| 1434 |
is_equal( |
| 1435 |
$js->evaluate(1, { '$schema' => '#/not_a_uri' })->TO_JSON, |
| 1436 |
{ |
| 1437 |
valid => false, |
| 1438 |
errors => [ |
| 1439 |
{ |
| 1440 |
keywordLocation => '/$schema', |
| 1441 |
error => '"#/not_a_uri" is not a valid URI', |
| 1442 |
}, |
| 1443 |
], |
| 1444 |
}, |
| 1445 |
'$schema values must be URIs', |
| 1446 |
); |
| 1447 |
|
| 1448 |
is_equal( |
| 1449 |
$js->evaluate(1, { '$schema' => 'https://unknown/metaschema' })->TO_JSON, |
| 1450 |
{ |
| 1451 |
valid => false, |
| 1452 |
errors => [ |
| 1453 |
{ |
| 1454 |
keywordLocation => '/$schema', |
| 1455 |
error => 'EXCEPTION: unable to find resource "https://unknown/metaschema"', |
| 1456 |
}, |
| 1457 |
], |
| 1458 |
}, |
| 1459 |
'custom metaschemas are okay, but the document must be known', |
| 1460 |
); |
| 1461 |
|
| 1462 |
|
| 1463 |
$js->add_schema({ |
| 1464 |
'$id' => 'https://example.com/hello', |
| 1465 |
'$defs' => { |
| 1466 |
metaschema => { |
| 1467 |
'$comment' => 'a metaschema based on draft2020-12 that requires a minimum of 5 keywords', |
| 1468 |
'$dynamicAnchor' => 'meta', |
| 1469 |
type => 'object', |
| 1470 |
minProperties => 5, |
| 1471 |
'$ref' => 'https://json-schema.org/draft/2020-12/schema', |
| 1472 |
}, |
| 1473 |
}, |
| 1474 |
}); |
| 1475 |
|
| 1476 |
my $doc = $js->add_schema({ |
| 1477 |
'$id' => 'my_schema', |
| 1478 |
'$schema' => 'https://example.com/hello#/$defs/metaschema' |
| 1479 |
}); |
| 1480 |
is_equal([ map $_->TO_JSON, $doc->errors ], [], 'no document errors'); |
| 1481 |
|
| 1482 |
is_equal( |
| 1483 |
$doc->validate(evaluator => $js)->TO_JSON, |
| 1484 |
{ |
| 1485 |
valid => false, |
| 1486 |
errors => [ |
| 1487 |
{ |
| 1488 |
instanceLocation => '', |
| 1489 |
keywordLocation => '/minProperties', |
| 1490 |
absoluteKeywordLocation => 'https://example.com/hello#/$defs/metaschema/minProperties', |
| 1491 |
error => 'object has fewer than 5 properties', |
| 1492 |
}, |
| 1493 |
], |
| 1494 |
}, |
| 1495 |
'$schema keyword can use a URI with a fragment', |
| 1496 |
); |
| 1497 |
}; |
| 1498 |
|
| 1499 |
subtest 'custom metaschemas, with custom vocabularies' => sub { |
| 1500 |
my $js = JSON::Schema::Modern->new; |
| 1501 |
|
| 1502 |
$js->add_schema({ |
| 1503 |
'$id' => 'https://metaschema/with/misplaced/vocabulary/keyword/base', |
| 1504 |
items => { |
| 1505 |
'$id' => 'subschema', |
| 1506 |
'$vocabulary' => { 'https://json-schema.org/draft/2020-12/vocab/core' => true }, |
| 1507 |
}, |
| 1508 |
}); |
| 1509 |
is_equal( |
| 1510 |
$js->evaluate(1, { '$schema' => 'https://metaschema/with/misplaced/vocabulary/keyword/subschema' })->TO_JSON, |
| 1511 |
{ |
| 1512 |
valid => false, |
| 1513 |
errors => [ |
| 1514 |
{ |
| 1515 |
keywordLocation => '/$schema/$vocabulary', |
| 1516 |
absoluteKeywordLocation => 'https://metaschema/with/misplaced/vocabulary/keyword/subschema#/$vocabulary', |
| 1517 |
error => '$vocabulary can only appear at the document root', |
| 1518 |
}, |
| 1519 |
{ |
| 1520 |
keywordLocation => '/$schema', |
| 1521 |
error => '"https://metaschema/with/misplaced/vocabulary/keyword/subschema" is not a valid metaschema', |
| 1522 |
}, |
| 1523 |
], |
| 1524 |
}, |
| 1525 |
'$vocabulary location check - document root', |
| 1526 |
); |
| 1527 |
|
| 1528 |
|
| 1529 |
$js->add_schema('https://metaschema/with/no/id', |
| 1530 |
{ '$vocabulary' => { 'https://json-schema.org/draft/2020-12/vocab/core' => true } }); |
| 1531 |
is_equal( |
| 1532 |
$js->evaluate(1, { '$schema' => 'https://metaschema/with/no/id' })->TO_JSON, |
| 1533 |
{ |
| 1534 |
valid => false, |
| 1535 |
errors => [ |
| 1536 |
{ |
| 1537 |
keywordLocation => '/$schema/$vocabulary', |
| 1538 |
absoluteKeywordLocation => 'https://metaschema/with/no/id#/$vocabulary', |
| 1539 |
error => 'metaschemas must have an $id', |
| 1540 |
}, |
| 1541 |
{ |
| 1542 |
keywordLocation => '/$schema', |
| 1543 |
error => '"https://metaschema/with/no/id" is not a valid metaschema', |
| 1544 |
}, |
| 1545 |
], |
| 1546 |
}, |
| 1547 |
'metaschemas must have an i$id', |
| 1548 |
); |
| 1549 |
|
| 1550 |
$js->add_schema({ |
| 1551 |
'$id' => 'https://metaschema/with/wrong/spec', |
| 1552 |
'$vocabulary' => { |
| 1553 |
'https://json-schema.org/draft/2020-12/vocab/core' => true, |
| 1554 |
'https://json-schema.org/draft/2019-09/vocab/validation' => true, |
| 1555 |
'https://json-schema.org/draft/2020-12/vocab/applicator' => true, |
| 1556 |
'https://unknown' => true, |
| 1557 |
'https://unknown2' => false, |
| 1558 |
}, |
| 1559 |
}); |
| 1560 |
is_equal( |
| 1561 |
$js->evaluate(1, { '$schema' => 'https://metaschema/with/wrong/spec' })->TO_JSON, |
| 1562 |
{ |
| 1563 |
valid => false, |
| 1564 |
errors => [ |
| 1565 |
{ |
| 1566 |
keywordLocation => jsonp(qw(/$schema $vocabulary https://json-schema.org/draft/2019-09/vocab/validation)), |
| 1567 |
absoluteKeywordLocation => 'https://metaschema/with/wrong/spec#'.jsonp(qw(/$vocabulary https://json-schema.org/draft/2019-09/vocab/validation)), |
| 1568 |
error => '"https://json-schema.org/draft/2019-09/vocab/validation" uses draft2019-09, but the metaschema itself uses draft2020-12', |
| 1569 |
}, |
| 1570 |
{ |
| 1571 |
keywordLocation => jsonp(qw(/$schema $vocabulary https://unknown)), |
| 1572 |
absoluteKeywordLocation => 'https://metaschema/with/wrong/spec#'.jsonp(qw(/$vocabulary https://unknown)), |
| 1573 |
error => '"https://unknown" is not a known vocabulary', |
| 1574 |
}, |
| 1575 |
{ |
| 1576 |
keywordLocation => '/$schema', |
| 1577 |
error => '"https://metaschema/with/wrong/spec" is not a valid metaschema', |
| 1578 |
}, |
| 1579 |
], |
| 1580 |
}, |
| 1581 |
'$vocabulary validation that must be deferred until used as a metaschema', |
| 1582 |
); |
| 1583 |
|
| 1584 |
$js->add_schema({ |
| 1585 |
'$id' => 'https://my/mismatched/metaschema', |
| 1586 |
'$schema' => 'https://json-schema.org/draft/2019-09/schema', |
| 1587 |
'$vocabulary' => { |
| 1588 |
'https://json-schema.org/draft/2019-09/vocab/core' => true, |
| 1589 |
'https://json-schema.org/draft/2020-12/vocab/applicator' => true, |
| 1590 |
# note: no validation! |
| 1591 |
}, |
| 1592 |
}); |
| 1593 |
|
| 1594 |
is_equal( |
| 1595 |
$js->evaluate(1, { '$schema' => 'https://my/mismatched/metaschema' })->TO_JSON, |
| 1596 |
{ |
| 1597 |
valid => false, |
| 1598 |
errors => [ |
| 1599 |
{ |
| 1600 |
keywordLocation => jsonp(qw(/$schema $vocabulary https://json-schema.org/draft/2020-12/vocab/applicator)), |
| 1601 |
absoluteKeywordLocation => 'https://my/mismatched/metaschema#'.jsonp(qw(/$vocabulary https://json-schema.org/draft/2020-12/vocab/applicator)), |
| 1602 |
error => '"https://json-schema.org/draft/2020-12/vocab/applicator" uses draft2020-12, but the metaschema itself uses draft2019-09', |
| 1603 |
}, |
| 1604 |
{ |
| 1605 |
keywordLocation => '/$schema', |
| 1606 |
error => '"https://my/mismatched/metaschema" is not a valid metaschema', |
| 1607 |
}, |
| 1608 |
], |
| 1609 |
}, |
| 1610 |
'vocabularies in the metaschema must match the $schema version', |
| 1611 |
); |
| 1612 |
|
| 1613 |
$js->add_schema({ |
| 1614 |
'$id' => 'https://metaschema/missing/vocabs', |
| 1615 |
'$vocabulary' => {}, |
| 1616 |
}); |
| 1617 |
is_equal( |
| 1618 |
$js->evaluate(1, { '$schema' => 'https://metaschema/missing/vocabs' })->TO_JSON, |
| 1619 |
{ |
| 1620 |
valid => false, |
| 1621 |
errors => [ |
| 1622 |
{ |
| 1623 |
keywordLocation => '/$schema/$vocabulary', |
| 1624 |
absoluteKeywordLocation => 'https://metaschema/missing/vocabs#/$vocabulary', |
| 1625 |
error => 'the first vocabulary (by evaluation_order) must be Core', |
| 1626 |
}, |
| 1627 |
{ |
| 1628 |
keywordLocation => '/$schema', |
| 1629 |
error => '"https://metaschema/missing/vocabs" is not a valid metaschema', |
| 1630 |
}, |
| 1631 |
], |
| 1632 |
}, |
| 1633 |
'metaschemas using "$vocabulary" must contain vocabularies', |
| 1634 |
); |
| 1635 |
|
| 1636 |
$js->add_schema({ |
| 1637 |
'$id' => 'https://metaschema/missing/core', |
| 1638 |
'$vocabulary' => { |
| 1639 |
'https://json-schema.org/draft/2020-12/vocab/applicator' => true, |
| 1640 |
}, |
| 1641 |
}); |
| 1642 |
is_equal( |
| 1643 |
$js->evaluate(1, { '$schema' => 'https://metaschema/missing/core' })->TO_JSON, |
| 1644 |
{ |
| 1645 |
valid => false, |
| 1646 |
errors => [ |
| 1647 |
{ |
| 1648 |
keywordLocation => '/$schema/$vocabulary', |
| 1649 |
absoluteKeywordLocation => 'https://metaschema/missing/core#/$vocabulary', |
| 1650 |
error => 'the first vocabulary (by evaluation_order) must be Core', |
| 1651 |
}, |
| 1652 |
{ |
| 1653 |
keywordLocation => '/$schema', |
| 1654 |
error => '"https://metaschema/missing/core" is not a valid metaschema', |
| 1655 |
}, |
| 1656 |
], |
| 1657 |
}, |
| 1658 |
'metaschemas must contain the Core vocabulary', |
| 1659 |
); |
| 1660 |
|
| 1661 |
|
| 1662 |
$js->add_schema({ |
| 1663 |
'$id' => 'https://my/first/metaschema', |
| 1664 |
'$vocabulary' => { |
| 1665 |
'https://json-schema.org/draft/2020-12/vocab/applicator' => true, |
| 1666 |
'https://json-schema.org/draft/2020-12/vocab/core' => true, |
| 1667 |
# note: no validation! |
| 1668 |
}, |
| 1669 |
}); |
| 1670 |
is_equal( |
| 1671 |
$js->evaluate( |
| 1672 |
1, |
| 1673 |
{ |
| 1674 |
'$id' => my $id = 'https://my/first/schema/with/custom/metaschema', |
| 1675 |
'$schema' => 'https://my/first/metaschema', |
| 1676 |
minimum => 10, |
| 1677 |
}, |
| 1678 |
)->TO_JSON, |
| 1679 |
{ valid => true }, |
| 1680 |
'validation succeeds because "minimum" never gets run', |
| 1681 |
); |
| 1682 |
cmp_result( |
| 1683 |
$js->{_resource_index}{$id}{document}, |
| 1684 |
methods( |
| 1685 |
canonical_uri => str($id), |
| 1686 |
metaschema_uri => str('https://my/first/metaschema'), |
| 1687 |
), |
| 1688 |
'document contains correct values', |
| 1689 |
); |
| 1690 |
cmp_result( |
| 1691 |
$js->{_resource_index}{$id}, |
| 1692 |
{ |
| 1693 |
canonical_uri => str($id), |
| 1694 |
path => '', |
| 1695 |
specification_version => 'draft2020-12', |
| 1696 |
document => ignore, |
| 1697 |
vocabularies => [ |
| 1698 |
map 'JSON::Schema::Modern::Vocabulary::'.$_, |
| 1699 |
qw(Core Applicator), |
| 1700 |
], |
| 1701 |
}, |
| 1702 |
'determined vocabularies to use for this schema', |
| 1703 |
); |
| 1704 |
}; |
| 1705 |
|
| 1706 |
subtest 'custom vocabulary classes with add_vocabulary()' => sub { |
| 1707 |
my $js = JSON::Schema::Modern->new; |
| 1708 |
|
| 1709 |
like( |
| 1710 |
dies { $js->add_vocabulary('MyVocabulary::Does::Not::Exist') }, |
| 1711 |
qr{^Can't locate MyVocabulary/Does/Not/Exist.pm in \@INC}, |
| 1712 |
'vocabulary class must exist', |
| 1713 |
); |
| 1714 |
|
| 1715 |
like( |
| 1716 |
dies { $js->add_vocabulary('MyVocabulary::MissingRole') }, |
| 1717 |
qr/^Value "MyVocabulary::MissingRole" did not pass type constraint/, |
| 1718 |
'vocabulary class must implement the role', |
| 1719 |
); |
| 1720 |
|
| 1721 |
like( |
| 1722 |
dies { $js->add_vocabulary('MyVocabulary::MissingSub') }, |
| 1723 |
qr/^Can't apply JSON::Schema::Modern::Vocabulary to MyVocabulary::MissingSub - missing vocabulary, keywords/, |
| 1724 |
'vocabulary class must implement some subs', |
| 1725 |
); |
| 1726 |
|
| 1727 |
cmp_result( |
| 1728 |
[ warnings { |
| 1729 |
like( |
| 1730 |
dies { $js->add_vocabulary('MyVocabulary::BadVocabularySub1') }, |
| 1731 |
qr/^Undef did not pass type constraint/, |
| 1732 |
'vocabulary() sub in the vocabulary class must return uri => specification_version pairs', |
| 1733 |
) |
| 1734 |
} ], |
| 1735 |
[ re(qr/^Odd number of elements in pairs/) ], |
| 1736 |
'parse error from bad vocab sub', |
| 1737 |
); |
| 1738 |
|
| 1739 |
like( |
| 1740 |
dies { $js->add_vocabulary('MyVocabulary::BadVocabularySub2') }, |
| 1741 |
qr!^Value "https://some/uri#/invalid/uri" did not pass type constraint!, |
| 1742 |
'vocabulary() sub in the vocabulary class must contain valid absolute, fragmentless URIs', |
| 1743 |
); |
| 1744 |
|
| 1745 |
like( |
| 1746 |
dies { $js->add_vocabulary('MyVocabulary::BadVocabularySub3') }, |
| 1747 |
qr/^Value "wrongdraft" did not pass type constraint/, |
| 1748 |
'vocabulary() sub in the vocabulary class must reference a known specification version', |
| 1749 |
); |
| 1750 |
|
| 1751 |
|
| 1752 |
ok( |
| 1753 |
lives { $js->add_vocabulary('MyVocabulary::BadEvaluationOrder') }, |
| 1754 |
'added a vocabulary sub', |
| 1755 |
); |
| 1756 |
|
| 1757 |
cmp_result( |
| 1758 |
$js->{_vocabulary_classes}, |
| 1759 |
superhashof({ 'https://vocabulary/with/bad/evaluation/order' => [ 'draft2020-12', 'MyVocabulary::BadEvaluationOrder' ] }), |
| 1760 |
'vocabulary was successfully added', |
| 1761 |
); |
| 1762 |
|
| 1763 |
$js->add_schema({ |
| 1764 |
'$id' => 'https://my/first/metaschema', |
| 1765 |
'$vocabulary' => { |
| 1766 |
'https://json-schema.org/draft/2020-12/vocab/core' => true, |
| 1767 |
'https://json-schema.org/draft/2020-12/vocab/validation' => true, |
| 1768 |
'https://vocabulary/with/bad/evaluation/order' => true, |
| 1769 |
}, |
| 1770 |
}); |
| 1771 |
is_equal( |
| 1772 |
$js->evaluate(1, { '$schema' => 'https://my/first/metaschema' })->TO_JSON, |
| 1773 |
{ |
| 1774 |
valid => false, |
| 1775 |
errors => [ |
| 1776 |
{ |
| 1777 |
keywordLocation => '/$schema/$vocabulary', |
| 1778 |
absoluteKeywordLocation => 'https://my/first/metaschema#/$vocabulary', |
| 1779 |
error => 'JSON::Schema::Modern::Vocabulary::Validation and MyVocabulary::BadEvaluationOrder have a conflicting evaluation_order', |
| 1780 |
}, |
| 1781 |
{ |
| 1782 |
keywordLocation => '/$schema', |
| 1783 |
error => '"https://my/first/metaschema" is not a valid metaschema', |
| 1784 |
}, |
| 1785 |
], |
| 1786 |
}, |
| 1787 |
'custom vocabulary class has a conflicting evaluation_order', |
| 1788 |
); |
| 1789 |
|
| 1790 |
ok( |
| 1791 |
lives { $js->add_vocabulary('MyVocabulary::StringComparison') }, |
| 1792 |
'added another vocabulary sub', |
| 1793 |
); |
| 1794 |
|
| 1795 |
$js->add_schema({ |
| 1796 |
'$id' => 'https://my/first/working/metaschema', |
| 1797 |
'$vocabulary' => { |
| 1798 |
'https://json-schema.org/draft/2020-12/vocab/core' => true, |
| 1799 |
'https://vocabulary/string/comparison' => true, |
| 1800 |
}, |
| 1801 |
}); |
| 1802 |
|
| 1803 |
is_equal( |
| 1804 |
$js->evaluate( |
| 1805 |
'bloop', |
| 1806 |
{ |
| 1807 |
'$id' => 'https://my/first/schema/with/custom/metaschema', |
| 1808 |
'$schema' => 'https://my/first/working/metaschema', |
| 1809 |
stringLessThan => 'alpha', |
| 1810 |
}, |
| 1811 |
)->TO_JSON, |
| 1812 |
{ |
| 1813 |
valid => false, |
| 1814 |
errors => [ |
| 1815 |
{ |
| 1816 |
instanceLocation => '', |
| 1817 |
keywordLocation => '/stringLessThan', |
| 1818 |
absoluteKeywordLocation => 'https://my/first/schema/with/custom/metaschema#/stringLessThan', |
| 1819 |
error => 'value is not stringwise less than alpha', |
| 1820 |
}, |
| 1821 |
], |
| 1822 |
}, |
| 1823 |
'custom vocabulary class used by a custom metaschema used by a schema', |
| 1824 |
); |
| 1825 |
|
| 1826 |
|
| 1827 |
like( |
| 1828 |
dies { $js->add_vocabulary('MyVocabulary::ReservedKeyword') }, |
| 1829 |
qr/^keywords starting with "\$" are reserved for core and cannot be used/, |
| 1830 |
'$ keywords are prohibited', |
| 1831 |
); |
| 1832 |
|
| 1833 |
|
| 1834 |
ok( |
| 1835 |
lives { $js->add_vocabulary('MyVocabulary::ConflictingKeyword') }, |
| 1836 |
'added another vocabulary sub', |
| 1837 |
); |
| 1838 |
|
| 1839 |
$js->add_schema({ |
| 1840 |
'$id' => 'https://colliding/keyword/metaschema', |
| 1841 |
'$vocabulary' => { |
| 1842 |
'https://json-schema.org/draft/2020-12/vocab/core' => true, |
| 1843 |
'https://json-schema.org/draft/2020-12/vocab/validation' => true, |
| 1844 |
'https://vocabulary/conflicting/keyword' => true, |
| 1845 |
}, |
| 1846 |
}); |
| 1847 |
|
| 1848 |
is_equal( |
| 1849 |
$js->evaluate( |
| 1850 |
'bloop', |
| 1851 |
{ |
| 1852 |
'$id' => 'https://my/second/schema/with/custom/metaschema', |
| 1853 |
'$schema' => 'https://colliding/keyword/metaschema', |
| 1854 |
minLength => 2, |
| 1855 |
}, |
| 1856 |
)->TO_JSON, |
| 1857 |
{ |
| 1858 |
valid => false, |
| 1859 |
errors => [ |
| 1860 |
{ |
| 1861 |
keywordLocation => '/$schema/$vocabulary', |
| 1862 |
absoluteKeywordLocation => 'https://colliding/keyword/metaschema#/$vocabulary', |
| 1863 |
error => 'MyVocabulary::ConflictingKeyword keyword "minLength" conflicts with keyword of the same name from JSON::Schema::Modern::Vocabulary::Validation', |
| 1864 |
}, |
| 1865 |
{ |
| 1866 |
keywordLocation => '/$schema', |
| 1867 |
error => '"https://colliding/keyword/metaschema" is not a valid metaschema', |
| 1868 |
}, |
| 1869 |
], |
| 1870 |
}, |
| 1871 |
'keywords cannot appear in more than one vocabulary in the same dialect', |
| 1872 |
); |
| 1873 |
}; |
| 1874 |
|
| 1875 |
subtest '$schema points to a boolean schema' => sub { |
| 1876 |
my $js = JSON::Schema::Modern->new; |
| 1877 |
$js->add_schema('https://my_boolean_schema' => true); |
| 1878 |
|
| 1879 |
is_equal( |
| 1880 |
$js->evaluate( |
| 1881 |
1, |
| 1882 |
{ |
| 1883 |
'$id' => '/foo', |
| 1884 |
'$schema' => 'https://my_boolean_schema', |
| 1885 |
}, |
| 1886 |
)->TO_JSON, |
| 1887 |
{ |
| 1888 |
valid => false, |
| 1889 |
errors => [ |
| 1890 |
{ |
| 1891 |
keywordLocation => '/$schema', |
| 1892 |
# we haven't processed $id yet, so we don't know the absolute location |
| 1893 |
error => 'metaschemas must be objects', |
| 1894 |
}, |
| 1895 |
{ |
| 1896 |
keywordLocation => '/$schema', |
| 1897 |
error => '"https://my_boolean_schema" is not a valid metaschema', |
| 1898 |
}, |
| 1899 |
], |
| 1900 |
}, |
| 1901 |
'$schema cannot reference a boolean schema', |
| 1902 |
); |
| 1903 |
}; |
| 1904 |
|
| 1905 |
subtest '$ref to a different dialect' => sub { |
| 1906 |
my $js = JSON::Schema::Modern->new(collect_annotations => 1); |
| 1907 |
|
| 1908 |
$js->add_schema({ |
| 1909 |
'$id' => 'https://my_metaschema', |
| 1910 |
'$schema' => 'https://json-schema.org/draft/2020-12/schema', |
| 1911 |
'$vocabulary' => { |
| 1912 |
'https://json-schema.org/draft/2020-12/vocab/core' => true, |
| 1913 |
'https://json-schema.org/draft/2020-12/vocab/validation' => true, |
| 1914 |
}, |
| 1915 |
}); |
| 1916 |
|
| 1917 |
is_equal( |
| 1918 |
$js->evaluate( |
| 1919 |
{ foo => { bar => 1 } }, |
| 1920 |
{ |
| 1921 |
'$id' => 'https://example.com', |
| 1922 |
'$defs' => { |
| 1923 |
subschema => { |
| 1924 |
'$id' => 'https://foo.com', |
| 1925 |
'$schema' => 'https://my_metaschema', |
| 1926 |
'$defs' => { |
| 1927 |
my_def => { type => 'object', blah => 1 }, |
| 1928 |
}, |
| 1929 |
'$ref' => '#/$defs/my_def', |
| 1930 |
bloop => 2, |
| 1931 |
properties => { bar => false }, # this keyword should only annotate |
| 1932 |
}, |
| 1933 |
}, |
| 1934 |
additionalProperties => { '$ref' => '#/$defs/subschema' }, |
| 1935 |
}, |
| 1936 |
)->TO_JSON, |
| 1937 |
{ |
| 1938 |
valid => true, |
| 1939 |
annotations => [ |
| 1940 |
{ |
| 1941 |
instanceLocation => '/foo', |
| 1942 |
keywordLocation => '/additionalProperties/$ref/$ref/blah', |
| 1943 |
absoluteKeywordLocation => 'https://foo.com#/$defs/my_def/blah', |
| 1944 |
annotation => 1, |
| 1945 |
}, |
| 1946 |
{ |
| 1947 |
instanceLocation => '/foo', |
| 1948 |
keywordLocation => '/additionalProperties/$ref/bloop', |
| 1949 |
absoluteKeywordLocation => 'https://foo.com#/bloop', |
| 1950 |
annotation => 2, |
| 1951 |
}, |
| 1952 |
{ |
| 1953 |
instanceLocation => '/foo', |
| 1954 |
keywordLocation => '/additionalProperties/$ref/properties', |
| 1955 |
absoluteKeywordLocation => 'https://foo.com#/properties', |
| 1956 |
annotation => { bar => false }, |
| 1957 |
}, |
| 1958 |
{ |
| 1959 |
instanceLocation => '', |
| 1960 |
keywordLocation => '/additionalProperties', |
| 1961 |
absoluteKeywordLocation => 'https://example.com#/additionalProperties', |
| 1962 |
annotation => [ 'foo' ], |
| 1963 |
}, |
| 1964 |
], |
| 1965 |
}, |
| 1966 |
'evaluation of the subschema correctly uses the new $id and $schema', |
| 1967 |
); |
| 1968 |
}; |
| 1969 |
|
| 1970 |
had_no_warnings() if $ENV{AUTHOR_TESTING}; |
| 1971 |
done_testing; |