| 1 |
# vim: set ft=perl ts=8 sts=2 sw=2 tw=100 et : |
| 2 |
use strictures 2; |
| 3 |
use stable 0.031 'postderef'; |
| 4 |
use experimental 'signatures'; |
| 5 |
no autovivification warn => qw(fetch store exists delete); |
| 6 |
use if "$]" >= 5.022, experimental => 're_strict'; |
| 7 |
no if "$]" >= 5.031009, feature => 'indirect'; |
| 8 |
no if "$]" >= 5.033001, feature => 'multidimensional'; |
| 9 |
no if "$]" >= 5.033006, feature => 'bareword_filehandles'; |
| 10 |
no if "$]" >= 5.041009, feature => 'smartmatch'; |
| 11 |
no feature 'switch'; |
| 12 |
use open ':std', ':encoding(UTF-8)'; # force stdin, stdout, stderr into utf8 |
| 13 |
|
| 14 |
use builtin::compat 'load_module'; |
| 15 |
use Mojo::File 'path'; |
| 16 |
use lib 't/lib'; |
| 17 |
use Helper; |
| 18 |
|
| 19 |
my $DUMP = shift; |
| 20 |
|
| 21 |
# regenerate this by running the test file with argument '1' |
| 22 |
use constant KEYWORDS => { |
| 23 |
# draft4 -> http://json-schema.org/draft-04/schema# |
| 24 |
'draft4' => { |
| 25 |
Core => [qw( |
| 26 |
$schema |
| 27 |
id |
| 28 |
$ref |
| 29 |
definitions |
| 30 |
)], |
| 31 |
Validation => [qw( |
| 32 |
type |
| 33 |
enum |
| 34 |
multipleOf |
| 35 |
maximum |
| 36 |
exclusiveMaximum |
| 37 |
minimum |
| 38 |
exclusiveMinimum |
| 39 |
maxLength |
| 40 |
minLength |
| 41 |
pattern |
| 42 |
maxItems |
| 43 |
minItems |
| 44 |
uniqueItems |
| 45 |
maxProperties |
| 46 |
minProperties |
| 47 |
required |
| 48 |
)], |
| 49 |
FormatAnnotation => [qw( |
| 50 |
format |
| 51 |
)], |
| 52 |
Applicator => [qw( |
| 53 |
allOf |
| 54 |
anyOf |
| 55 |
oneOf |
| 56 |
not |
| 57 |
dependencies |
| 58 |
items |
| 59 |
additionalItems |
| 60 |
properties |
| 61 |
patternProperties |
| 62 |
additionalProperties |
| 63 |
)], |
| 64 |
MetaData => [qw( |
| 65 |
title |
| 66 |
description |
| 67 |
default |
| 68 |
)], |
| 69 |
}, |
| 70 |
# draft6 -> http://json-schema.org/draft-06/schema# |
| 71 |
'draft6' => { |
| 72 |
Core => [qw( |
| 73 |
$schema |
| 74 |
$id |
| 75 |
$ref |
| 76 |
definitions |
| 77 |
)], |
| 78 |
Validation => [qw( |
| 79 |
type |
| 80 |
enum |
| 81 |
const |
| 82 |
multipleOf |
| 83 |
maximum |
| 84 |
exclusiveMaximum |
| 85 |
minimum |
| 86 |
exclusiveMinimum |
| 87 |
maxLength |
| 88 |
minLength |
| 89 |
pattern |
| 90 |
maxItems |
| 91 |
minItems |
| 92 |
uniqueItems |
| 93 |
maxProperties |
| 94 |
minProperties |
| 95 |
required |
| 96 |
)], |
| 97 |
FormatAnnotation => [qw( |
| 98 |
format |
| 99 |
)], |
| 100 |
Applicator => [qw( |
| 101 |
allOf |
| 102 |
anyOf |
| 103 |
oneOf |
| 104 |
not |
| 105 |
dependencies |
| 106 |
items |
| 107 |
additionalItems |
| 108 |
contains |
| 109 |
properties |
| 110 |
patternProperties |
| 111 |
additionalProperties |
| 112 |
propertyNames |
| 113 |
)], |
| 114 |
MetaData => [qw( |
| 115 |
title |
| 116 |
description |
| 117 |
default |
| 118 |
examples |
| 119 |
)], |
| 120 |
}, |
| 121 |
# draft7 -> http://json-schema.org/draft-07/schema# |
| 122 |
'draft7' => { |
| 123 |
Core => [qw( |
| 124 |
$schema |
| 125 |
$id |
| 126 |
$ref |
| 127 |
definitions |
| 128 |
$comment |
| 129 |
)], |
| 130 |
Validation => [qw( |
| 131 |
type |
| 132 |
enum |
| 133 |
const |
| 134 |
multipleOf |
| 135 |
maximum |
| 136 |
exclusiveMaximum |
| 137 |
minimum |
| 138 |
exclusiveMinimum |
| 139 |
maxLength |
| 140 |
minLength |
| 141 |
pattern |
| 142 |
maxItems |
| 143 |
minItems |
| 144 |
uniqueItems |
| 145 |
maxProperties |
| 146 |
minProperties |
| 147 |
required |
| 148 |
)], |
| 149 |
FormatAnnotation => [qw( |
| 150 |
format |
| 151 |
)], |
| 152 |
Applicator => [qw( |
| 153 |
allOf |
| 154 |
anyOf |
| 155 |
oneOf |
| 156 |
not |
| 157 |
if |
| 158 |
then |
| 159 |
else |
| 160 |
dependencies |
| 161 |
items |
| 162 |
additionalItems |
| 163 |
contains |
| 164 |
properties |
| 165 |
patternProperties |
| 166 |
additionalProperties |
| 167 |
propertyNames |
| 168 |
)], |
| 169 |
Content => [qw( |
| 170 |
contentEncoding |
| 171 |
contentMediaType |
| 172 |
)], |
| 173 |
MetaData => [qw( |
| 174 |
title |
| 175 |
description |
| 176 |
default |
| 177 |
readOnly |
| 178 |
writeOnly |
| 179 |
examples |
| 180 |
)], |
| 181 |
}, |
| 182 |
# draft2019-09 -> https://json-schema.org/draft/2019-09/schema |
| 183 |
'draft2019-09' => { |
| 184 |
Core => [qw( |
| 185 |
$schema |
| 186 |
$id |
| 187 |
$anchor |
| 188 |
$recursiveAnchor |
| 189 |
$ref |
| 190 |
$recursiveRef |
| 191 |
$vocabulary |
| 192 |
$defs |
| 193 |
$comment |
| 194 |
)], |
| 195 |
Validation => [qw( |
| 196 |
type |
| 197 |
enum |
| 198 |
const |
| 199 |
multipleOf |
| 200 |
maximum |
| 201 |
exclusiveMaximum |
| 202 |
minimum |
| 203 |
exclusiveMinimum |
| 204 |
maxLength |
| 205 |
minLength |
| 206 |
pattern |
| 207 |
maxItems |
| 208 |
minItems |
| 209 |
uniqueItems |
| 210 |
maxContains |
| 211 |
minContains |
| 212 |
maxProperties |
| 213 |
minProperties |
| 214 |
required |
| 215 |
dependentRequired |
| 216 |
)], |
| 217 |
FormatAnnotation => [qw( |
| 218 |
format |
| 219 |
)], |
| 220 |
Applicator => [qw( |
| 221 |
allOf |
| 222 |
anyOf |
| 223 |
oneOf |
| 224 |
not |
| 225 |
if |
| 226 |
then |
| 227 |
else |
| 228 |
dependentSchemas |
| 229 |
items |
| 230 |
additionalItems |
| 231 |
contains |
| 232 |
properties |
| 233 |
patternProperties |
| 234 |
additionalProperties |
| 235 |
propertyNames |
| 236 |
unevaluatedItems |
| 237 |
unevaluatedProperties |
| 238 |
)], |
| 239 |
Content => [qw( |
| 240 |
contentEncoding |
| 241 |
contentMediaType |
| 242 |
contentSchema |
| 243 |
)], |
| 244 |
MetaData => [qw( |
| 245 |
title |
| 246 |
description |
| 247 |
default |
| 248 |
deprecated |
| 249 |
readOnly |
| 250 |
writeOnly |
| 251 |
examples |
| 252 |
)], |
| 253 |
}, |
| 254 |
# draft2020-12 -> https://json-schema.org/draft/2020-12/schema |
| 255 |
'draft2020-12' => { |
| 256 |
Core => [qw( |
| 257 |
$schema |
| 258 |
$id |
| 259 |
$anchor |
| 260 |
$dynamicAnchor |
| 261 |
$ref |
| 262 |
$dynamicRef |
| 263 |
$vocabulary |
| 264 |
$defs |
| 265 |
$comment |
| 266 |
)], |
| 267 |
Validation => [qw( |
| 268 |
type |
| 269 |
enum |
| 270 |
const |
| 271 |
multipleOf |
| 272 |
maximum |
| 273 |
exclusiveMaximum |
| 274 |
minimum |
| 275 |
exclusiveMinimum |
| 276 |
maxLength |
| 277 |
minLength |
| 278 |
pattern |
| 279 |
maxItems |
| 280 |
minItems |
| 281 |
uniqueItems |
| 282 |
maxContains |
| 283 |
minContains |
| 284 |
maxProperties |
| 285 |
minProperties |
| 286 |
required |
| 287 |
dependentRequired |
| 288 |
)], |
| 289 |
FormatAnnotation => [qw( |
| 290 |
format |
| 291 |
)], |
| 292 |
Applicator => [qw( |
| 293 |
allOf |
| 294 |
anyOf |
| 295 |
oneOf |
| 296 |
not |
| 297 |
if |
| 298 |
then |
| 299 |
else |
| 300 |
dependentSchemas |
| 301 |
prefixItems |
| 302 |
items |
| 303 |
contains |
| 304 |
properties |
| 305 |
patternProperties |
| 306 |
additionalProperties |
| 307 |
propertyNames |
| 308 |
)], |
| 309 |
Content => [qw( |
| 310 |
contentEncoding |
| 311 |
contentMediaType |
| 312 |
contentSchema |
| 313 |
)], |
| 314 |
MetaData => [qw( |
| 315 |
title |
| 316 |
description |
| 317 |
default |
| 318 |
deprecated |
| 319 |
readOnly |
| 320 |
writeOnly |
| 321 |
examples |
| 322 |
)], |
| 323 |
Unevaluated => [qw( |
| 324 |
unevaluatedItems |
| 325 |
unevaluatedProperties |
| 326 |
)], |
| 327 |
}, |
| 328 |
}; |
| 329 |
|
| 330 |
subtest 'valid keywords' => sub { |
| 331 |
if ($DUMP) { |
| 332 |
my $js = JSON::Schema::Modern->new; |
| 333 |
print STDERR "{\n"; |
| 334 |
foreach my $spec_version (sort { length($a) <=> length($b) || $a cmp $b } $js->SPECIFICATION_VERSIONS_SUPPORTED->@*) { |
| 335 |
# specification_version -> metaschema uri |
| 336 |
my $metaschema_uri = $js->METASCHEMA_URIS->{$spec_version}; |
| 337 |
print STDERR " # $spec_version -> $metaschema_uri\n"; |
| 338 |
|
| 339 |
# metaschema uri -> vocab list: [ specification_version, [ vocab classes ] ] |
| 340 |
foreach my $metaschema_info ($js->_get_metaschema_vocabulary_classes($metaschema_uri)) { |
| 341 |
print STDERR " '$spec_version' => {\n"; |
| 342 |
foreach my $class (sort $metaschema_info->[1]->@*) { |
| 343 |
my ($short_class) = $class =~ /::([^:]+)\z/; |
| 344 |
print STDERR " $short_class => [qw(\n"; |
| 345 |
print STDERR " $_\n" foreach $class->keywords($spec_version); |
| 346 |
print STDERR " )],\n"; |
| 347 |
} |
| 348 |
print STDERR " },\n"; |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
print STDERR "};\n\n"; |
| 353 |
pass('table dumped'); |
| 354 |
return; |
| 355 |
} |
| 356 |
|
| 357 |
my @classes = |
| 358 |
grep load_module($_)->does('JSON::Schema::Modern::Vocabulary'), |
| 359 |
map 'JSON::Schema::Modern::Vocabulary::'.$_, |
| 360 |
map $_->basename =~ s/\.pm\z//r, |
| 361 |
grep /\.pm\z/, |
| 362 |
path('lib/JSON/Schema/Modern/Vocabulary/')->list->each; |
| 363 |
|
| 364 |
my $table = { |
| 365 |
map do { |
| 366 |
my $spec_version = $_; |
| 367 |
$spec_version => { |
| 368 |
map do { |
| 369 |
my $class = $_; |
| 370 |
my @keywords = eval { $class->keywords($spec_version) }; |
| 371 |
@keywords ? (($class =~ /::([^:]+)\z/) => \@keywords) : (); |
| 372 |
}, @classes |
| 373 |
}; |
| 374 |
}, |
| 375 |
JSON::Schema::Modern->SPECIFICATION_VERSIONS_SUPPORTED->@* |
| 376 |
}; |
| 377 |
|
| 378 |
foreach my $spec_version (sort { length($a) <=> length($b) || $a cmp $b } keys KEYWORDS->%*) { |
| 379 |
foreach my $short_class (sort keys KEYWORDS->{$spec_version}->%*) { |
| 380 |
my $class = 'JSON::Schema::Modern::Vocabulary::'.$short_class; |
| 381 |
is_equal( |
| 382 |
[ $class->keywords($spec_version) ], |
| 383 |
KEYWORDS->{$spec_version}{$short_class}, |
| 384 |
"$spec_version, $short_class: calculated keyword list matches hardcoded table", |
| 385 |
); |
| 386 |
} |
| 387 |
} |
| 388 |
}; |
| 389 |
|
| 390 |
done_testing; |