| 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 |
use JSON::Schema::Modern::Utilities qw(get_type); |
| 19 |
|
| 20 |
use Test::Without::Module 0.19 qw( |
| 21 |
Data::Validate::Domain |
| 22 |
Email::Address::XS |
| 23 |
Net::IDN::Encode |
| 24 |
Data::Validate::URI |
| 25 |
); |
| 26 |
|
| 27 |
use constant ALL_FORMATS => [ qw( |
| 28 |
date-time |
| 29 |
email |
| 30 |
hostname |
| 31 |
ipv4 |
| 32 |
ipv6 |
| 33 |
uri |
| 34 |
uri-reference |
| 35 |
uri-template |
| 36 |
json-pointer |
| 37 |
iri |
| 38 |
iri-reference |
| 39 |
idn-email |
| 40 |
idn-hostname |
| 41 |
relative-json-pointer |
| 42 |
regex |
| 43 |
date |
| 44 |
time |
| 45 |
duration |
| 46 |
uuid |
| 47 |
) ]; |
| 48 |
|
| 49 |
my ($annotation_result, $validation_result); |
| 50 |
subtest 'no validation' => sub { |
| 51 |
is_equal( |
| 52 |
JSON::Schema::Modern->new(collect_annotations => 1, validate_formats => 0) |
| 53 |
->evaluate('abc', { format => 'uuid' })->TO_JSON, |
| 54 |
$annotation_result = { |
| 55 |
valid => true, |
| 56 |
annotations => [ |
| 57 |
{ |
| 58 |
instanceLocation => '', |
| 59 |
keywordLocation => '/format', |
| 60 |
annotation => 'uuid', |
| 61 |
}, |
| 62 |
], |
| 63 |
}, |
| 64 |
'validate_formats=0 disables format assertion behaviour; annotation is still produced', |
| 65 |
); |
| 66 |
|
| 67 |
is_equal( |
| 68 |
JSON::Schema::Modern->new(collect_annotations => 1, validate_formats => 1) |
| 69 |
->evaluate('abc', { format => 'uuid' }, { validate_formats => 0 })->TO_JSON, |
| 70 |
$annotation_result, |
| 71 |
'format validation can be turned off in evaluate()', |
| 72 |
); |
| 73 |
}; |
| 74 |
|
| 75 |
subtest 'simple validation' => sub { |
| 76 |
my $js = JSON::Schema::Modern->new(collect_annotations => 1, validate_formats => 1); |
| 77 |
|
| 78 |
is_equal( |
| 79 |
$js->evaluate(123, { format => 'uuid' })->TO_JSON, |
| 80 |
$annotation_result, |
| 81 |
'non-string values are valid, and produce an annotation', |
| 82 |
); |
| 83 |
|
| 84 |
is_equal( |
| 85 |
$js->evaluate( |
| 86 |
'2eb8aa08-aa98-11ea-b4aa-73b441d16380', |
| 87 |
{ format => 'uuid' }, |
| 88 |
)->TO_JSON, |
| 89 |
$annotation_result, |
| 90 |
'simple success', |
| 91 |
); |
| 92 |
|
| 93 |
is_equal( |
| 94 |
$js->evaluate('123', { format => 'uuid' })->TO_JSON, |
| 95 |
$validation_result = { |
| 96 |
valid => false, |
| 97 |
errors => [ |
| 98 |
{ |
| 99 |
instanceLocation => '', |
| 100 |
keywordLocation => '/format', |
| 101 |
error => 'not a valid uuid string', |
| 102 |
}, |
| 103 |
], |
| 104 |
}, |
| 105 |
'simple failure', |
| 106 |
); |
| 107 |
|
| 108 |
$js = JSON::Schema::Modern->new(collect_annotations => 1); |
| 109 |
ok(!$js->validate_formats, 'format_validation defaults to false'); |
| 110 |
is_equal( |
| 111 |
$js->evaluate('123', { format => 'uuid' }, { validate_formats => 1 })->TO_JSON, |
| 112 |
$validation_result, |
| 113 |
'format validation can be turned on in evaluate()', |
| 114 |
); |
| 115 |
|
| 116 |
ok(!$js->validate_formats, '...but the value is still false on the object'); |
| 117 |
}; |
| 118 |
|
| 119 |
subtest 'override a format sub' => sub { |
| 120 |
like( |
| 121 |
dies { |
| 122 |
JSON::Schema::Modern->new( |
| 123 |
validate_formats => 1, |
| 124 |
format_validations => +{ uuid => 1 }, |
| 125 |
) |
| 126 |
}, |
| 127 |
qr/^Reference .* did not pass type constraint /, |
| 128 |
'check syntax of override to existing format via constructor', |
| 129 |
); |
| 130 |
|
| 131 |
my $js = JSON::Schema::Modern->new(validate_formats => 1); |
| 132 |
like( |
| 133 |
dies { $js->add_format_validation([] => 1) }, |
| 134 |
qr/^\s*Value .* did not pass type constraint /m, |
| 135 |
'check syntax of override format name to existing format via setter', |
| 136 |
); |
| 137 |
like( |
| 138 |
dies { $js->add_format_validation(uuid => 1) }, |
| 139 |
qr/^\s*Value .* did not pass type constraint /m, |
| 140 |
'check syntax of override definition value to existing format via setter', |
| 141 |
); |
| 142 |
|
| 143 |
like( |
| 144 |
dies { $js->add_format_validation(uuid => { sub => sub { 0 }}) }, |
| 145 |
qr/^Reference .* did not pass type constraint /, |
| 146 |
'type is required if passing a hashref', |
| 147 |
); |
| 148 |
|
| 149 |
like( |
| 150 |
dies { $js->add_format_validation(uuid => { type => 'number', sub => sub { 0 }}) }, |
| 151 |
qr/^Type for override of format uuid does not match original type/, |
| 152 |
'cannot override a core format to support a different data type', |
| 153 |
); |
| 154 |
|
| 155 |
$js->add_format_validation(uuid => sub { $_[0] =~ /^[a-z0-9-]+\z/ }); |
| 156 |
is_equal( |
| 157 |
$js->evaluate( |
| 158 |
[ |
| 159 |
0, |
| 160 |
1, |
| 161 |
[], |
| 162 |
{}, |
| 163 |
'a', |
| 164 |
'foobie!', |
| 165 |
], |
| 166 |
{ items => { format => 'uuid' } }, |
| 167 |
)->TO_JSON, |
| 168 |
{ |
| 169 |
valid => false, |
| 170 |
errors => [ |
| 171 |
{ |
| 172 |
instanceLocation => '/5', |
| 173 |
keywordLocation => '/items/format', |
| 174 |
error => 'not a valid uuid string', |
| 175 |
}, |
| 176 |
{ |
| 177 |
instanceLocation => '', |
| 178 |
keywordLocation => '/items', |
| 179 |
error => 'subschema is not valid against all items', |
| 180 |
}, |
| 181 |
], |
| 182 |
}, |
| 183 |
'can override a core format definition, as long as it uses the same type', |
| 184 |
); |
| 185 |
|
| 186 |
like( |
| 187 |
dies { |
| 188 |
JSON::Schema::Modern->new( |
| 189 |
validate_formats => 1, |
| 190 |
format_validations => +{ mult_5 => 1 }, |
| 191 |
) |
| 192 |
}, |
| 193 |
qr/\s*Value "1" did not pass type constraint "(Dict\[|Ref").../m, |
| 194 |
'check syntax of implementation for a new format', |
| 195 |
); |
| 196 |
|
| 197 |
$js = JSON::Schema::Modern->new( |
| 198 |
collect_annotations => 1, |
| 199 |
validate_formats => 1, |
| 200 |
format_validations => +{ |
| 201 |
uuid => sub { $_[0] =~ /^[A-Z]+\z/ }, |
| 202 |
mult_5 => +{ type => 'number', sub => sub { ($_[0] % 5) == 0 } }, |
| 203 |
}, |
| 204 |
); |
| 205 |
|
| 206 |
like( |
| 207 |
dies { $js->add_format_validation(uuid_bad => 1) }, |
| 208 |
qr/^\s*Value "1" did not pass type constraint "(Dict\[|Ref").../m, |
| 209 |
'check syntax of implementation when adding an override to existing format', |
| 210 |
); |
| 211 |
|
| 212 |
like( |
| 213 |
dies { $js->add_format_validation(mult_5_bad => 1) }, |
| 214 |
qr/^\s*Value "1" did not pass type constraint "(Dict\[|Ref").../m, |
| 215 |
'check syntax of implementation when adding a new format', |
| 216 |
); |
| 217 |
|
| 218 |
is_equal( |
| 219 |
$js->evaluate( |
| 220 |
[ |
| 221 |
{ uuid => '2eb8aa08-aa98-11ea-b4aa-73b441d16380', mult_5 => 3 }, |
| 222 |
{ uuid => 3, mult_5 => 'abc' }, |
| 223 |
], |
| 224 |
{ |
| 225 |
items => { |
| 226 |
properties => { |
| 227 |
uuid => { format => 'uuid' }, |
| 228 |
mult_5 => { format => 'mult_5' }, |
| 229 |
}, |
| 230 |
}, |
| 231 |
}, |
| 232 |
)->TO_JSON, |
| 233 |
{ |
| 234 |
valid => false, |
| 235 |
errors => [ |
| 236 |
{ |
| 237 |
instanceLocation => '/0/mult_5', |
| 238 |
keywordLocation => '/items/properties/mult_5/format', |
| 239 |
error => 'not a valid mult_5 number', |
| 240 |
}, |
| 241 |
{ |
| 242 |
instanceLocation => '/0/uuid', |
| 243 |
keywordLocation => '/items/properties/uuid/format', |
| 244 |
error => 'not a valid uuid string', |
| 245 |
}, |
| 246 |
{ |
| 247 |
instanceLocation => '/0', |
| 248 |
keywordLocation => '/items/properties', |
| 249 |
error => 'not all properties are valid', |
| 250 |
}, |
| 251 |
{ |
| 252 |
instanceLocation => '', |
| 253 |
keywordLocation => '/items', |
| 254 |
error => 'subschema is not valid against all items', |
| 255 |
}, |
| 256 |
], |
| 257 |
}, |
| 258 |
'swapping out format implementation turns success into failure; wrong types are still valid', |
| 259 |
); |
| 260 |
|
| 261 |
# create a new format definition which uses a different type |
| 262 |
$js->add_format_validation(keys_mult_2 => +{ type => 'object', sub => sub { keys($_[0]->%*) > 2 } }); |
| 263 |
|
| 264 |
is_equal( |
| 265 |
$js->evaluate( |
| 266 |
[ |
| 267 |
{}, |
| 268 |
{ a => 1 }, |
| 269 |
{ a => 1, b => 2 }, |
| 270 |
{ a => 1, b => 2, c => 3 }, |
| 271 |
[], |
| 272 |
'a', |
| 273 |
], |
| 274 |
{ items => { format => 'keys_mult_2' } }, |
| 275 |
)->TO_JSON, |
| 276 |
{ |
| 277 |
valid => false, |
| 278 |
errors => [ |
| 279 |
(map +{ |
| 280 |
instanceLocation => '/'.$_, |
| 281 |
keywordLocation => '/items/format', |
| 282 |
error => 'not a valid keys_mult_2 object', |
| 283 |
}, 0, 1, 2), |
| 284 |
{ |
| 285 |
instanceLocation => '', |
| 286 |
keywordLocation => '/items', |
| 287 |
error => 'subschema is not valid against all items', |
| 288 |
}, |
| 289 |
], |
| 290 |
}, |
| 291 |
'can create a custom format definition to use a different type', |
| 292 |
); |
| 293 |
}; |
| 294 |
|
| 295 |
subtest 'toggle validate_formats after adding schema' => sub { |
| 296 |
my $js = JSON::Schema::Modern->new; |
| 297 |
my $document = $js->add_schema(my $uri = 'http://localhost:1234/ipv4', { format => 'ipv4' }); |
| 298 |
|
| 299 |
is_equal( |
| 300 |
$js->evaluate('hello', $uri)->TO_JSON, |
| 301 |
{ valid => true }, |
| 302 |
'assertion behaviour is off initially', |
| 303 |
); |
| 304 |
|
| 305 |
is_equal( |
| 306 |
$js->evaluate('hello', $uri, { validate_formats => 1 })->TO_JSON, |
| 307 |
{ |
| 308 |
valid => false, |
| 309 |
errors => [ |
| 310 |
{ |
| 311 |
instanceLocation => '', |
| 312 |
keywordLocation => '/format', |
| 313 |
absoluteKeywordLocation => 'http://localhost:1234/ipv4#/format', |
| 314 |
error => 'not a valid ipv4 string', |
| 315 |
}, |
| 316 |
], |
| 317 |
}, |
| 318 |
'assertion behaviour can be enabled later with an already-loaded schema', |
| 319 |
); |
| 320 |
|
| 321 |
is_equal( |
| 322 |
$js->evaluate('127.0.0.1', $uri, { validate_formats => 1 })->TO_JSON, |
| 323 |
{ valid => true }, |
| 324 |
'valid assertion behaviour does not die', |
| 325 |
); |
| 326 |
|
| 327 |
my $js2 = JSON::Schema::Modern->new(validate_formats => 1); |
| 328 |
$js2->add_document($uri, $document); |
| 329 |
is_equal( |
| 330 |
$js2->evaluate('hello', $uri)->TO_JSON, |
| 331 |
{ |
| 332 |
valid => false, |
| 333 |
errors => [ |
| 334 |
{ |
| 335 |
instanceLocation => '', |
| 336 |
keywordLocation => '/format', |
| 337 |
absoluteKeywordLocation => 'http://localhost:1234/ipv4#/format', |
| 338 |
error => 'not a valid ipv4 string', |
| 339 |
}, |
| 340 |
], |
| 341 |
}, |
| 342 |
'a schema document can be used with another evaluator with assertion behaviour', |
| 343 |
); |
| 344 |
|
| 345 |
is_equal( |
| 346 |
$js2->evaluate('127.0.0.1', $uri)->TO_JSON, |
| 347 |
{ valid => true }, |
| 348 |
'valid assertion behaviour does not die', |
| 349 |
); |
| 350 |
}; |
| 351 |
|
| 352 |
subtest 'custom metaschemas' => sub { |
| 353 |
my $js = JSON::Schema::Modern->new; |
| 354 |
$js->add_schema({ |
| 355 |
'$id' => 'https://metaschema/format-assertion/false', |
| 356 |
'$vocabulary' => { |
| 357 |
'https://json-schema.org/draft/2020-12/vocab/core' => true, |
| 358 |
'https://json-schema.org/draft/2020-12/vocab/format-assertion' => false, |
| 359 |
}, |
| 360 |
}); |
| 361 |
$js->add_schema({ |
| 362 |
'$id' => 'https://metaschema/format-assertion/true', |
| 363 |
'$vocabulary' => { |
| 364 |
'https://json-schema.org/draft/2020-12/vocab/core' => true, |
| 365 |
'https://json-schema.org/draft/2020-12/vocab/format-assertion' => true, |
| 366 |
}, |
| 367 |
}); |
| 368 |
|
| 369 |
is_equal( |
| 370 |
$js->evaluate( |
| 371 |
'not-an-ip', |
| 372 |
{ |
| 373 |
'$id' => 'https://schema/ipv4/false', |
| 374 |
'$schema' => 'https://metaschema/format-assertion/false', |
| 375 |
type => 'string', |
| 376 |
format => 'ipv4', |
| 377 |
}, |
| 378 |
)->TO_JSON, |
| 379 |
{ |
| 380 |
valid => false, |
| 381 |
errors => [ |
| 382 |
{ |
| 383 |
instanceLocation => '', |
| 384 |
keywordLocation => '/format', |
| 385 |
absoluteKeywordLocation => 'https://schema/ipv4/false#/format', |
| 386 |
error => 'not a valid ipv4 string', |
| 387 |
}, |
| 388 |
], |
| 389 |
}, |
| 390 |
'custom metaschema using format-assertion=false validates formats', |
| 391 |
); |
| 392 |
|
| 393 |
is_equal( |
| 394 |
$js->evaluate( |
| 395 |
'not-an-ip', |
| 396 |
{ |
| 397 |
'$id' => 'https://schema/ipv4/true', |
| 398 |
'$schema' => 'https://metaschema/format-assertion/true', |
| 399 |
type => 'string', |
| 400 |
format => 'ipv4', |
| 401 |
}, |
| 402 |
)->TO_JSON, |
| 403 |
{ |
| 404 |
valid => false, |
| 405 |
errors => [ |
| 406 |
{ |
| 407 |
instanceLocation => '', |
| 408 |
keywordLocation => '/format', |
| 409 |
absoluteKeywordLocation => 'https://schema/ipv4/true#/format', |
| 410 |
error => 'not a valid ipv4 string', |
| 411 |
}, |
| 412 |
], |
| 413 |
}, |
| 414 |
'custom metaschema using format-assertion=true validates formats', |
| 415 |
); |
| 416 |
}; |
| 417 |
|
| 418 |
subtest 'core formats added after draft7' => sub { |
| 419 |
my $js = JSON::Schema::Modern->new(specification_version => 'draft7', validate_formats => 1); |
| 420 |
|
| 421 |
is_equal( |
| 422 |
$js->evaluate('123', { format => 'duration' })->TO_JSON, |
| 423 |
{ valid => true }, |
| 424 |
'duration is not implemented in draft7', |
| 425 |
); |
| 426 |
|
| 427 |
is_equal( |
| 428 |
$js->evaluate('123', { format => 'uuid' })->TO_JSON, |
| 429 |
{ valid => true }, |
| 430 |
'uuid is not implemented in draft7', |
| 431 |
); |
| 432 |
}; |
| 433 |
|
| 434 |
subtest 'unimplemented core formats' => sub { |
| 435 |
# all specification versions |
| 436 |
foreach my $spec_version (JSON::Schema::Modern::SPECIFICATION_VERSIONS_SUPPORTED->@*) { |
| 437 |
my $js = JSON::Schema::Modern->new(specification_version => $spec_version, validate_formats => 1); |
| 438 |
is_equal( |
| 439 |
my $res = $js->evaluate( |
| 440 |
'hello', |
| 441 |
{ |
| 442 |
format => 'uri-template', |
| 443 |
}, |
| 444 |
)->TO_JSON, |
| 445 |
{ valid => true }, |
| 446 |
$spec_version . ' with validate_formats = 1 and default dialect, no error when an unimplemented core format is used', |
| 447 |
); |
| 448 |
} |
| 449 |
|
| 450 |
# specification version draft2020-12 and later, format-assertion vocabulary |
| 451 |
foreach my $spec_version (JSON::Schema::Modern::SPECIFICATION_VERSIONS_SUPPORTED->@*) { |
| 452 |
next if $spec_version =~ /^draft(?:[467]|2019-09)\z/; |
| 453 |
my $js = JSON::Schema::Modern->new(specification_version => $spec_version); |
| 454 |
$js->add_schema({ |
| 455 |
'$id' => 'https://my_metaschema', |
| 456 |
'$schema' => JSON::Schema::Modern::METASCHEMA_URIS->{$spec_version}, |
| 457 |
'$vocabulary' => { |
| 458 |
JSON::Schema::Modern::METASCHEMA_URIS->{$spec_version} =~ s{schema\z}{vocab/core}r => true, |
| 459 |
JSON::Schema::Modern::METASCHEMA_URIS->{$spec_version} =~ s{schema\z}{vocab/applicator}r => true, |
| 460 |
JSON::Schema::Modern::METASCHEMA_URIS->{$spec_version} =~ s{schema\z}{vocab/format-assertion}r => true, |
| 461 |
}, |
| 462 |
'$ref' => JSON::Schema::Modern::METASCHEMA_URIS->{$spec_version}, |
| 463 |
}); |
| 464 |
|
| 465 |
is_equal( |
| 466 |
$js->evaluate( |
| 467 |
'hello', |
| 468 |
{ |
| 469 |
'$schema' => 'https://my_metaschema', |
| 470 |
format => 'uri-template', |
| 471 |
}, |
| 472 |
)->TO_JSON, |
| 473 |
{ |
| 474 |
valid => false, |
| 475 |
errors => [ |
| 476 |
{ |
| 477 |
keywordLocation => '/format', |
| 478 |
error => 'unimplemented core format "uri-template"', |
| 479 |
}, |
| 480 |
], |
| 481 |
}, |
| 482 |
$spec_version . ' with Format-Assertion vocabulary: error when using a core format that is unimplemented', |
| 483 |
); |
| 484 |
|
| 485 |
is_equal( |
| 486 |
$js->evaluate( |
| 487 |
'hello', |
| 488 |
{ |
| 489 |
'$schema' => 'https://my_metaschema', |
| 490 |
anyOf => [ |
| 491 |
{ minLength => 1 }, |
| 492 |
{ format => 'uri-template' }, |
| 493 |
], |
| 494 |
}, |
| 495 |
)->TO_JSON, |
| 496 |
{ |
| 497 |
valid => false, |
| 498 |
errors => [ |
| 499 |
{ |
| 500 |
keywordLocation => '/anyOf/1/format', |
| 501 |
error => 'unimplemented core format "uri-template"', |
| 502 |
}, |
| 503 |
], |
| 504 |
}, |
| 505 |
$spec_version . ' with Format-Assertion vocabulary: error is seen even when containing subschema would be true, and evaluation is short-circuited', |
| 506 |
); |
| 507 |
|
| 508 |
# add uri-template definition that allows lower-cased characters |
| 509 |
$js->add_format_validation('uri-template' => sub { $_[0] !~ /[A-Z]/ }); |
| 510 |
|
| 511 |
is_equal( |
| 512 |
$js->evaluate( |
| 513 |
'hello', |
| 514 |
{ |
| 515 |
'$schema' => 'https://my_metaschema', |
| 516 |
format => 'uri-template', |
| 517 |
}, |
| 518 |
)->TO_JSON, |
| 519 |
{ valid => true }, |
| 520 |
'unimplemented core format can have a custom definition provided', |
| 521 |
); |
| 522 |
} |
| 523 |
}; |
| 524 |
|
| 525 |
subtest 'unknown custom formats' => sub { |
| 526 |
# see https://json-schema.org/draft/2019-09/json-schema-validation.html#rfc.section.7.2.3 |
| 527 |
# "An implementation MUST NOT fail validation or cease processing due to an unknown format |
| 528 |
# attribute." |
| 529 |
foreach my $spec_version (JSON::Schema::Modern::SPECIFICATION_VERSIONS_SUPPORTED->@*) { |
| 530 |
my $js = JSON::Schema::Modern->new( |
| 531 |
specification_version => $spec_version, |
| 532 |
$spec_version !~ /^draft[467]\z/ ? (collect_annotations => 1) : (), |
| 533 |
validate_formats => 1, |
| 534 |
); |
| 535 |
|
| 536 |
is_equal( |
| 537 |
$js->evaluate('hello', { format => 'whargarbl' })->TO_JSON, |
| 538 |
{ |
| 539 |
valid => true, |
| 540 |
$spec_version =~ /^draft[467]\z/ ? () : (annotations => [ |
| 541 |
{ |
| 542 |
instanceLocation => '', |
| 543 |
keywordLocation => '/format', |
| 544 |
annotation => 'whargarbl', |
| 545 |
}, |
| 546 |
]), |
| 547 |
}, |
| 548 |
$spec_version . ': for format validation with the Format-Annotation vocabulary, unrecognized format attributes do not cause validation failure' |
| 549 |
. ($spec_version !~ /^draft[467]\z/ ? '; annotation is still produced' : ''), |
| 550 |
); |
| 551 |
} |
| 552 |
|
| 553 |
# see https://json-schema.org/draft/2020-12/json-schema-validation#section-7.2.3 |
| 554 |
# "When the Format-Assertion vocabulary is specified, implementations MUST fail upon encountering |
| 555 |
# unknown formats." |
| 556 |
foreach my $spec_version (JSON::Schema::Modern::SPECIFICATION_VERSIONS_SUPPORTED->@*) { |
| 557 |
next if $spec_version =~ /^draft[467]\z/ or $spec_version eq 'draft2019-09'; |
| 558 |
|
| 559 |
my $js = JSON::Schema::Modern->new(specification_version => $spec_version); |
| 560 |
$js->add_schema({ |
| 561 |
'$id' => 'https://my_metaschema', |
| 562 |
'$schema' => JSON::Schema::Modern::METASCHEMA_URIS->{$spec_version}, |
| 563 |
'$vocabulary' => { |
| 564 |
JSON::Schema::Modern::METASCHEMA_URIS->{$spec_version} =~ s{schema\z}{vocab/core}r => true, |
| 565 |
JSON::Schema::Modern::METASCHEMA_URIS->{$spec_version} =~ s{schema\z}{vocab/applicator}r => true, |
| 566 |
JSON::Schema::Modern::METASCHEMA_URIS->{$spec_version} =~ s{schema\z}{vocab/validation}r => true, |
| 567 |
JSON::Schema::Modern::METASCHEMA_URIS->{$spec_version} =~ s{schema\z}{vocab/format-assertion}r => true, |
| 568 |
}, |
| 569 |
'$ref' => JSON::Schema::Modern::METASCHEMA_URIS->{$spec_version}, |
| 570 |
}); |
| 571 |
|
| 572 |
my $doc = $js->add_schema({ |
| 573 |
'$schema' => 'https://my_metaschema', |
| 574 |
anyOf => [ |
| 575 |
{ minLength => 3 }, |
| 576 |
{ format => 'bloop' }, |
| 577 |
], |
| 578 |
}); |
| 579 |
is($doc->errors, 0, $spec_version . ': for format validation with the Format-Assertion vocabulary, no errors during traversal when using an unknown custom format'); |
| 580 |
|
| 581 |
is_equal( |
| 582 |
$js->evaluate('hi', '')->TO_JSON, |
| 583 |
{ |
| 584 |
valid => false, |
| 585 |
errors => [ |
| 586 |
{ |
| 587 |
instanceLocation => '', |
| 588 |
keywordLocation => '/anyOf/1/format', |
| 589 |
error => 'unimplemented custom format "bloop"', |
| 590 |
}, |
| 591 |
], |
| 592 |
}, |
| 593 |
$spec_version . ': for format validation with the Format-Assertion vocabulary, unrecognized custom formats are detected at evaluation time', |
| 594 |
); |
| 595 |
|
| 596 |
is_equal( |
| 597 |
$js->evaluate('hello', '', { short_circuit => 1 })->TO_JSON, |
| 598 |
{ valid => true }, |
| 599 |
'...but this error can be avoided if the keyword is never evaluated', |
| 600 |
); |
| 601 |
} |
| 602 |
}; |
| 603 |
|
| 604 |
subtest 'format: invalid base type(s)' => sub { |
| 605 |
my $js = JSON::Schema::Modern->new(validate_formats => 1); |
| 606 |
|
| 607 |
like( |
| 608 |
dies { $js->add_format_validation(my_integer => { type => 'integer', sub => sub {} }) }, |
| 609 |
qr/^\s*Value .* did not pass type constraint /m, |
| 610 |
'integer is not a valid base type for a format validation', |
| 611 |
); |
| 612 |
|
| 613 |
like( |
| 614 |
dies { $js->add_format_validation(my_integer => { type => [qw(integer string)], sub => sub {} }) }, |
| 615 |
qr/^Reference .* did not pass type constraint /, |
| 616 |
'integer, string is not a valid base type for a format validation', |
| 617 |
); |
| 618 |
}; |
| 619 |
|
| 620 |
subtest 'format: pure_integer' => sub { |
| 621 |
my $js = JSON::Schema::Modern->new( |
| 622 |
validate_formats => 1, |
| 623 |
format_validations => +{ |
| 624 |
pure_integer => +{ type => 'number', sub => sub ($value) { |
| 625 |
# note: Storable::dclone mutates flags: 3.0 starts off with just NOK set, but |
| 626 |
# afterwards it has both NOK and IOK set |
| 627 |
!(B::svref_2object(\$value)->FLAGS & B::SVf_NOK) && |
| 628 |
(B::svref_2object(\$value)->FLAGS & B::SVf_IOK) |
| 629 |
} }, |
| 630 |
}, |
| 631 |
); |
| 632 |
|
| 633 |
my $decoder = JSON::Schema::Modern::_JSON_BACKEND()->new->allow_nonref(1)->utf8(0); |
| 634 |
my $int = 5; |
| 635 |
is_equal( |
| 636 |
$js->evaluate( |
| 637 |
[ |
| 638 |
(map $decoder->decode($_), |
| 639 |
'"hello"', |
| 640 |
'3.1', |
| 641 |
'3.0', |
| 642 |
'3', |
| 643 |
), |
| 644 |
bless(\$int, 'Local::MyInteger'), |
| 645 |
], |
| 646 |
{ |
| 647 |
items => { |
| 648 |
type => 'integer', |
| 649 |
format => 'pure_integer', |
| 650 |
}, |
| 651 |
}, |
| 652 |
)->TO_JSON, |
| 653 |
{ |
| 654 |
valid => false, |
| 655 |
errors => [ |
| 656 |
{ |
| 657 |
instanceLocation => '/0', |
| 658 |
keywordLocation => '/items/type', |
| 659 |
error => 'got string, not integer', |
| 660 |
}, |
| 661 |
{ |
| 662 |
instanceLocation => '/1', |
| 663 |
keywordLocation => '/items/type', |
| 664 |
error => 'got number, not integer', |
| 665 |
}, |
| 666 |
{ |
| 667 |
instanceLocation => '/1', |
| 668 |
keywordLocation => '/items/format', |
| 669 |
error => 'not a valid pure_integer number', |
| 670 |
}, |
| 671 |
{ |
| 672 |
instanceLocation => '/2', |
| 673 |
keywordLocation => '/items/format', |
| 674 |
error => 'not a valid pure_integer number', |
| 675 |
}, |
| 676 |
{ |
| 677 |
instanceLocation => '/4', |
| 678 |
keywordLocation => '/items/type', |
| 679 |
error => 'got Local::MyInteger, not integer', |
| 680 |
}, |
| 681 |
{ |
| 682 |
instanceLocation => '', |
| 683 |
keywordLocation => '/items', |
| 684 |
error => 'subschema is not valid against all items', |
| 685 |
}, |
| 686 |
], |
| 687 |
}, |
| 688 |
'pure_integer format with type', |
| 689 |
); |
| 690 |
|
| 691 |
is_equal( |
| 692 |
$js->evaluate( |
| 693 |
[ |
| 694 |
(map $decoder->decode($_), |
| 695 |
'"hello"', # string, will not apply format |
| 696 |
'3.1', # number, will apply format |
| 697 |
'3.0', # "" |
| 698 |
'3', # "" |
| 699 |
), |
| 700 |
bless(\$int, 'Local::MyInteger'), # blessed type, will not apply format |
| 701 |
], |
| 702 |
{ |
| 703 |
items => { |
| 704 |
format => 'pure_integer', |
| 705 |
}, |
| 706 |
}, |
| 707 |
)->TO_JSON, |
| 708 |
{ |
| 709 |
valid => false, |
| 710 |
errors => [ |
| 711 |
# strings are not applied to the format |
| 712 |
{ |
| 713 |
instanceLocation => '/1', |
| 714 |
keywordLocation => '/items/format', |
| 715 |
error => 'not a valid pure_integer number', |
| 716 |
}, |
| 717 |
{ |
| 718 |
instanceLocation => '/2', |
| 719 |
keywordLocation => '/items/format', |
| 720 |
error => 'not a valid pure_integer number', |
| 721 |
}, |
| 722 |
{ |
| 723 |
instanceLocation => '', |
| 724 |
keywordLocation => '/items', |
| 725 |
error => 'subschema is not valid against all items', |
| 726 |
}, |
| 727 |
], |
| 728 |
}, |
| 729 |
'pure_integer format without type', |
| 730 |
); |
| 731 |
}; |
| 732 |
|
| 733 |
subtest 'formats supporting multiple core types' => sub { |
| 734 |
# this is int64 from the OAI format registry: https://spec.openapis.org/registry/format/ |
| 735 |
my $js = JSON::Schema::Modern->new( |
| 736 |
validate_formats => 1, |
| 737 |
format_validations => +{ |
| 738 |
# a signed 64-bit integer; see https://spec.openapis.org/api/format.json |
| 739 |
int64 => +{ type => ['number', 'string'], sub => sub ($value) { |
| 740 |
my $type = get_type($value); |
| 741 |
return if not grep $type eq $_, qw(integer number string); |
| 742 |
$value = Math::BigInt->new($value) if $type eq 'string'; |
| 743 |
return if $value eq 'NaN'; |
| 744 |
# using the literal numbers rather than -2**63, 2**63 -1 to maintain precision |
| 745 |
$value >= Math::BigInt->new('-9223372036854775808') && $value <= Math::BigInt->new('9223372036854775807'); |
| 746 |
} }, |
| 747 |
}, |
| 748 |
); |
| 749 |
|
| 750 |
my @values = ( |
| 751 |
'{}', # object is valid |
| 752 |
'[]', # array is valid |
| 753 |
'true', # boolean is valid |
| 754 |
'null', # null is valid |
| 755 |
|
| 756 |
# string |
| 757 |
'"-9223372036854775809"', # 4: out of bounds |
| 758 |
'"-9223372036854775808"', # minimum value |
| 759 |
'"-9223372036854775807"', # within bounds |
| 760 |
'"0"', |
| 761 |
'"9223372036854775806"', # within bounds |
| 762 |
'"9223372036854775807"', # maximum value |
| 763 |
'"9223372036854775808"', # out of bounds |
| 764 |
'"Inf"', |
| 765 |
'"NaN"', |
| 766 |
|
| 767 |
# number |
| 768 |
'-9223372036854775809', # 13: out of bounds |
| 769 |
'-9223372036854775808', # minimum value; difficult to use on most architectures without Math::BigInt |
| 770 |
'-9223372036854775807', # within bounds |
| 771 |
'0', |
| 772 |
'9223372036854775806', # within bounds |
| 773 |
'9223372036854775807', # maximum value |
| 774 |
'9223372036854775808', # 19: out of bounds |
| 775 |
# numeric Inf and NaN are not valid JSON |
| 776 |
); |
| 777 |
|
| 778 |
# note: results may vary on 32-bit architectures when not using Math::BigFloat |
| 779 |
foreach my $decoder ( |
| 780 |
JSON::Schema::Modern::_JSON_BACKEND()->new->allow_nonref(1)->utf8(0), |
| 781 |
JSON::Schema::Modern::_JSON_BACKEND()->new->allow_nonref(1)->utf8(0)->allow_bignum(1)) { |
| 782 |
is_equal( |
| 783 |
$js->evaluate( |
| 784 |
[ map $decoder->decode($_), @values ], |
| 785 |
{ |
| 786 |
items => { |
| 787 |
format => 'int64', |
| 788 |
}, |
| 789 |
}, |
| 790 |
)->TO_JSON, |
| 791 |
{ |
| 792 |
valid => false, |
| 793 |
errors => [ |
| 794 |
(map +{ |
| 795 |
instanceLocation => "/$_", |
| 796 |
keywordLocation => '/items/format', |
| 797 |
error => 'not a valid int64 number, string', |
| 798 |
}, |
| 799 |
4, 10, 11, 12, 13, 19), |
| 800 |
{ |
| 801 |
instanceLocation => '', |
| 802 |
keywordLocation => '/items', |
| 803 |
error => 'subschema is not valid against all items', |
| 804 |
}, |
| 805 |
], |
| 806 |
}, |
| 807 |
'int64 format without type - accepts both numbers and strings', |
| 808 |
); |
| 809 |
} |
| 810 |
}; |
| 811 |
|
| 812 |
subtest 'stringy numbers with a numeric format' => sub { |
| 813 |
my $js = JSON::Schema::Modern->new( |
| 814 |
validate_formats => 1, |
| 815 |
stringy_numbers => 1, |
| 816 |
format_validations => +{ |
| 817 |
mult_5 => +{ type => 'number', sub => sub { ($_[0] % 5) == 0 } }, |
| 818 |
}, |
| 819 |
); |
| 820 |
|
| 821 |
is_equal( |
| 822 |
my $res = $js->evaluate( |
| 823 |
[ |
| 824 |
3, |
| 825 |
'3', |
| 826 |
5, |
| 827 |
'5', |
| 828 |
'abc', |
| 829 |
], |
| 830 |
{ items => { format => 'mult_5' } }, |
| 831 |
)->TO_JSON, |
| 832 |
{ |
| 833 |
valid => false, |
| 834 |
errors => [ |
| 835 |
{ |
| 836 |
instanceLocation => '/0', |
| 837 |
keywordLocation => '/items/format', |
| 838 |
error => 'not a valid mult_5 number', |
| 839 |
}, |
| 840 |
{ |
| 841 |
instanceLocation => '/1', |
| 842 |
keywordLocation => '/items/format', |
| 843 |
error => 'not a valid mult_5 number', |
| 844 |
}, |
| 845 |
{ |
| 846 |
instanceLocation => '', |
| 847 |
keywordLocation => '/items', |
| 848 |
error => 'subschema is not valid against all items', |
| 849 |
}, |
| 850 |
], |
| 851 |
}, |
| 852 |
'FormatAnnotation+validate_formats: strings that look like numbers can be validated against a numeric format when stringy_numbers=1', |
| 853 |
); |
| 854 |
|
| 855 |
$js = JSON::Schema::Modern->new( |
| 856 |
stringy_numbers => 1, |
| 857 |
format_validations => +{ |
| 858 |
mult_5 => +{ type => 'number', sub => sub { ($_[0] % 5) == 0 } }, |
| 859 |
}, |
| 860 |
); |
| 861 |
my $spec_version = $js->SPECIFICATION_VERSION_DEFAULT; |
| 862 |
$js->add_schema({ |
| 863 |
'$id' => 'https://my_metaschema', |
| 864 |
'$schema' => JSON::Schema::Modern::METASCHEMA_URIS->{$spec_version}, |
| 865 |
'$vocabulary' => { |
| 866 |
JSON::Schema::Modern::METASCHEMA_URIS->{$spec_version} =~ s{schema\z}{vocab/core}r => true, |
| 867 |
JSON::Schema::Modern::METASCHEMA_URIS->{$spec_version} =~ s{schema\z}{vocab/applicator}r => true, |
| 868 |
JSON::Schema::Modern::METASCHEMA_URIS->{$spec_version} =~ s{schema\z}{vocab/format-assertion}r => true, |
| 869 |
}, |
| 870 |
'$ref' => JSON::Schema::Modern::METASCHEMA_URIS->{$spec_version}, |
| 871 |
}); |
| 872 |
|
| 873 |
is_equal( |
| 874 |
$js->evaluate( |
| 875 |
[ |
| 876 |
3, |
| 877 |
'3', |
| 878 |
5, |
| 879 |
'5', |
| 880 |
'abc', |
| 881 |
], |
| 882 |
{ |
| 883 |
'$schema' => 'https://my_metaschema', |
| 884 |
items => { format => 'mult_5' }, |
| 885 |
}, |
| 886 |
)->TO_JSON, |
| 887 |
{ |
| 888 |
valid => false, |
| 889 |
errors => [ |
| 890 |
{ |
| 891 |
instanceLocation => '/0', |
| 892 |
keywordLocation => '/items/format', |
| 893 |
error => 'not a valid mult_5 number', |
| 894 |
}, |
| 895 |
{ |
| 896 |
instanceLocation => '/1', |
| 897 |
keywordLocation => '/items/format', |
| 898 |
error => 'not a valid mult_5 number', |
| 899 |
}, |
| 900 |
{ |
| 901 |
instanceLocation => '', |
| 902 |
keywordLocation => '/items', |
| 903 |
error => 'subschema is not valid against all items', |
| 904 |
}, |
| 905 |
], |
| 906 |
}, |
| 907 |
'FormatAssertion: strings that look like numbers can be validated against a numeric format when stringy_numbers=1', |
| 908 |
); |
| 909 |
}; |
| 910 |
|
| 911 |
# we do have support for these formats, but we do not force that their dependencies be installed |
| 912 |
# unless the formats are actually to be used. |
| 913 |
# Therefore we will allow them to be tested against other data types (e.g. in acceptance tests) |
| 914 |
# even without these dependencies installed, without throwing an exception. |
| 915 |
|
| 916 |
subtest 'annotation formats using implementations that rely on optional dependencies' => sub { |
| 917 |
is_equal( |
| 918 |
# relying on default format-assertion behaviour |
| 919 |
JSON::Schema::Modern->new->evaluate( |
| 920 |
[ |
| 921 |
undef, |
| 922 |
true, |
| 923 |
{}, |
| 924 |
[], |
| 925 |
1 |
| 926 |
], |
| 927 |
{ items => { allOf => [ map +{ format => $_ }, ALL_FORMATS->@* ] } }, |
| 928 |
)->TO_JSON, |
| 929 |
{ valid => true }, |
| 930 |
'can annotate a non-string against formats without their optional dependencies, without dying', |
| 931 |
); |
| 932 |
}; |
| 933 |
|
| 934 |
subtest 'assertion formats using implementations that rely on optional dependencies' => sub { |
| 935 |
foreach my $spec_version (JSON::Schema::Modern::SPECIFICATION_VERSIONS_SUPPORTED->@*) { |
| 936 |
my $js = JSON::Schema::Modern->new( |
| 937 |
specification_version => $spec_version, |
| 938 |
validate_formats => 1, |
| 939 |
); |
| 940 |
|
| 941 |
is_equal( |
| 942 |
$js->evaluate( |
| 943 |
[ |
| 944 |
undef, |
| 945 |
true, |
| 946 |
{}, |
| 947 |
[], |
| 948 |
1 |
| 949 |
], |
| 950 |
{ |
| 951 |
type => 'array', |
| 952 |
items => { allOf => [ map +{ format => $_ }, ALL_FORMATS->@* ] } |
| 953 |
}, |
| 954 |
)->TO_JSON, |
| 955 |
{ valid => true }, |
| 956 |
$spec_version . ': for format validation with the Format-Annotation vocabulary, can assert a non-string against formats without their optional dependencies, without dying', |
| 957 |
); |
| 958 |
|
| 959 |
is_equal( |
| 960 |
$js->evaluate( |
| 961 |
'2025-01-01T00:00:00Z', |
| 962 |
{ |
| 963 |
type => 'string', |
| 964 |
allOf => [ |
| 965 |
{ format => 'date-time' }, |
| 966 |
$spec_version eq 'draft4' ? {} : true, |
| 967 |
], |
| 968 |
}, |
| 969 |
)->TO_JSON, |
| 970 |
{ valid => true }, |
| 971 |
$spec_version . ': for format validation with the Format-Annotation vocabulary, in assertion mode, we treat missing prereqs as the format being valid', |
| 972 |
); |
| 973 |
} |
| 974 |
|
| 975 |
foreach my $spec_version (JSON::Schema::Modern::SPECIFICATION_VERSIONS_SUPPORTED->@*) { |
| 976 |
next if $spec_version =~ /^draft[467]\z/ or $spec_version eq 'draft2019-09'; |
| 977 |
|
| 978 |
my $js = JSON::Schema::Modern->new(specification_version => $spec_version); |
| 979 |
$js->add_schema({ |
| 980 |
'$id' => 'https://my_metaschema', |
| 981 |
'$schema' => JSON::Schema::Modern::METASCHEMA_URIS->{$spec_version}, |
| 982 |
'$vocabulary' => { |
| 983 |
JSON::Schema::Modern::METASCHEMA_URIS->{$spec_version} =~ s{schema\z}{vocab/core}r => true, |
| 984 |
JSON::Schema::Modern::METASCHEMA_URIS->{$spec_version} =~ s{schema\z}{vocab/applicator}r => true, |
| 985 |
JSON::Schema::Modern::METASCHEMA_URIS->{$spec_version} =~ s{schema\z}{vocab/format-assertion}r => true, |
| 986 |
}, |
| 987 |
'$ref' => JSON::Schema::Modern::METASCHEMA_URIS->{$spec_version}, |
| 988 |
}); |
| 989 |
|
| 990 |
my $doc; |
| 991 |
my @warnings = warnings { |
| 992 |
$doc = $js->add_schema({ |
| 993 |
'$schema' => 'https://my_metaschema', |
| 994 |
allOf => [ |
| 995 |
{ format => 'hostname' }, |
| 996 |
true, |
| 997 |
], |
| 998 |
}); |
| 999 |
}; |
| 1000 |
|
| 1001 |
is($doc->errors, 0, $spec_version . ': for format validation with the Format-Assertion vocabulary, no errors during traversal when using a format with missing prerequisites'); |
| 1002 |
|
| 1003 |
cmp_result( |
| 1004 |
\@warnings, |
| 1005 |
[ re(qr{^Can't locate Data/Validate/Domain\.pm}) ], |
| 1006 |
'we warn for a missing module', |
| 1007 |
); |
| 1008 |
|
| 1009 |
cmp_result( |
| 1010 |
$js->evaluate('2025-01-01T00:00:00Z', $doc->canonical_uri)->TO_JSON, |
| 1011 |
{ |
| 1012 |
valid => false, |
| 1013 |
errors => [ |
| 1014 |
{ |
| 1015 |
error => re(qr{^EXCEPTION: Can't locate Data/Validate/Domain\.pm}), |
| 1016 |
instanceLocation => '', |
| 1017 |
keywordLocation => '/allOf/0/format', |
| 1018 |
}, |
| 1019 |
], |
| 1020 |
}, |
| 1021 |
$spec_version . ': for Format-Assertion vocabulary, we immediately abort when encountering a format that throws an exception', |
| 1022 |
); |
| 1023 |
} |
| 1024 |
}; |
| 1025 |
|
| 1026 |
had_no_warnings() if $ENV{AUTHOR_TESTING}; |
| 1027 |
done_testing; |