| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
package JSON::Schema::Modern::Vocabulary::Applicator; |
| 4 |
# vim: set ts=8 sts=2 sw=2 tw=100 et : |
| 5 |
# ABSTRACT: Implementation of the JSON Schema Applicator vocabulary |
| 6 |
|
| 7 |
our $VERSION = '0.650'; |
| 8 |
|
| 9 |
use 5.020; |
| 10 |
use Moo; |
| 11 |
use strictures 2; |
| 12 |
use stable 0.031 'postderef'; |
| 13 |
use experimental 0.026 qw(signatures args_array_with_signatures); |
| 14 |
no autovivification warn => qw(fetch store exists delete); |
| 15 |
use if "$]" >= 5.022, experimental => 're_strict'; |
| 16 |
no if "$]" >= 5.031009, feature => 'indirect'; |
| 17 |
no if "$]" >= 5.033001, feature => 'multidimensional'; |
| 18 |
no if "$]" >= 5.033006, feature => 'bareword_filehandles'; |
| 19 |
no if "$]" >= 5.041009, feature => 'smartmatch'; |
| 20 |
no feature 'switch'; |
| 21 |
use List::Util 1.45 'uniqstr'; |
| 22 |
use if "$]" < 5.041010, 'List::Util' => 'any'; |
| 23 |
use if "$]" >= 5.041010, experimental => 'keyword_any'; |
| 24 |
use JSON::Schema::Modern::Utilities qw(is_type jsonp E A assert_keyword_type assert_pattern true is_elements_unique); |
| 25 |
use JSON::Schema::Modern::Vocabulary::Unevaluated; |
| 26 |
use namespace::clean; |
| 27 |
|
| 28 |
with 'JSON::Schema::Modern::Vocabulary'; |
| 29 |
|
| 30 |
sub vocabulary ($class) { |
| 31 |
'https://json-schema.org/draft/2019-09/vocab/applicator' => 'draft2019-09', |
| 32 |
'https://json-schema.org/draft/2020-12/vocab/applicator' => 'draft2020-12'; |
| 33 |
} |
| 34 |
|
| 35 |
sub evaluation_order ($class) { 3 } |
| 36 |
|
| 37 |
# the keyword order is arbitrary, except: |
| 38 |
# - if must be evaluated before then, else |
| 39 |
# - items must be evaluated before additionalItems |
| 40 |
# - in-place applicators (allOf, anyOf, oneOf, not, if/then/else, dependentSchemas) and items, |
| 41 |
# additionalItems must be evaluated before unevaluatedItems (in the Unevaluated vocabulary) |
| 42 |
# - properties and patternProperties must be evaluated before additionalProperties |
| 43 |
# - in-place applicators and properties, patternProperties, additionalProperties must be evaluated |
| 44 |
# before unevaluatedProperties (in the Unevaluated vocabulary) |
| 45 |
# - contains must be evaluated before maxContains, minContains (implemented here, rather than in the Validation vocabulary) |
| 46 |
sub keywords ($class, $spec_version) { |
| 47 |
return ( |
| 48 |
qw(allOf anyOf oneOf not), |
| 49 |
$spec_version !~ /^draft[46]\z/ ? qw(if then else) : (), |
| 50 |
$spec_version =~ /^draft[467]\z/ ? 'dependencies' : (), |
| 51 |
$spec_version !~ /^draft[467]\z/ ? 'dependentSchemas' : (), |
| 52 |
$spec_version !~ /^draft(?:[467]|2019-09)\z/ ? 'prefixItems' : (), |
| 53 |
'items', |
| 54 |
$spec_version =~ /^draft(?:[467]|2019-09)\z/ ? 'additionalItems' : (), |
| 55 |
$spec_version ne 'draft4' ? 'contains' : (), |
| 56 |
qw(properties patternProperties additionalProperties), |
| 57 |
$spec_version ne 'draft4' ? 'propertyNames' : (), |
| 58 |
$spec_version eq 'draft2019-09' ? qw(unevaluatedItems unevaluatedProperties) : (), |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
# in draft2019-09, the unevaluated keywords were part of the Applicator vocabulary |
| 63 |
foreach my $phase (qw(traverse eval)) { |
| 64 |
foreach my $type (qw(Items Properties)) { |
| 65 |
my $method = '_'.$phase.'_keyword_unevaluated'.$type; |
| 66 |
no strict 'refs'; |
| 67 |
*{__PACKAGE__.'::'.$method} = sub { |
| 68 |
shift; |
| 69 |
JSON::Schema::Modern::Vocabulary::Unevaluated->$method(@_); |
| 70 |
}; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
sub _traverse_keyword_allOf { shift->traverse_array_schemas(@_) } |
| 75 |
|
| 76 |
sub _eval_keyword_allOf ($class, $data, $schema, $state) { |
| 77 |
my @invalid; |
| 78 |
foreach my $idx (0 .. $schema->{allOf}->$#*) { |
| 79 |
if ($class->eval($data, $schema->{allOf}[$idx], +{ %$state, |
| 80 |
keyword_path => $state->{keyword_path}.'/allOf/'.$idx })) { |
| 81 |
} |
| 82 |
else { |
| 83 |
push @invalid, $idx; |
| 84 |
last if $state->{short_circuit}; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
return 1 if @invalid == 0; |
| 89 |
|
| 90 |
my $pl = @invalid > 1; |
| 91 |
return E($state, 'subschema%s %s %s not valid', $pl?'s':'', join(', ', @invalid), $pl?'are':'is'); |
| 92 |
} |
| 93 |
|
| 94 |
sub _traverse_keyword_anyOf { shift->traverse_array_schemas(@_) } |
| 95 |
|
| 96 |
sub _eval_keyword_anyOf ($class, $data, $schema, $state) { |
| 97 |
my $valid = 0; |
| 98 |
my @errors; |
| 99 |
foreach my $idx (0 .. $schema->{anyOf}->$#*) { |
| 100 |
next if not $class->eval($data, $schema->{anyOf}[$idx], |
| 101 |
+{ %$state, errors => \@errors, keyword_path => $state->{keyword_path}.'/anyOf/'.$idx }); |
| 102 |
++$valid; |
| 103 |
last if $state->{short_circuit} and not $state->{collect_annotations}; |
| 104 |
} |
| 105 |
|
| 106 |
return 1 if $valid; |
| 107 |
push $state->{errors}->@*, @errors; |
| 108 |
return E($state, 'no subschemas are valid'); |
| 109 |
} |
| 110 |
|
| 111 |
sub _traverse_keyword_oneOf { shift->traverse_array_schemas(@_) } |
| 112 |
|
| 113 |
sub _eval_keyword_oneOf ($class, $data, $schema, $state) { |
| 114 |
my (@valid, @errors); |
| 115 |
foreach my $idx (0 .. $schema->{oneOf}->$#*) { |
| 116 |
next if not $class->eval($data, $schema->{oneOf}[$idx], |
| 117 |
+{ %$state, errors => \@errors, keyword_path => $state->{keyword_path}.'/oneOf/'.$idx }); |
| 118 |
push @valid, $idx; |
| 119 |
last if @valid > 1 and $state->{short_circuit}; |
| 120 |
} |
| 121 |
|
| 122 |
return 1 if @valid == 1; |
| 123 |
|
| 124 |
if (not @valid) { |
| 125 |
push $state->{errors}->@*, @errors; |
| 126 |
return E($state, 'no subschemas are valid'); |
| 127 |
} |
| 128 |
else { |
| 129 |
return E($state, 'multiple subschemas are valid: '.join(', ', @valid)); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
sub _traverse_keyword_not { shift->traverse_subschema(@_) } |
| 134 |
|
| 135 |
sub _eval_keyword_not ($class, $data, $schema, $state) { |
| 136 |
return !$schema->{not} || E($state, 'subschema is true') if is_type('boolean', $schema->{not}); |
| 137 |
|
| 138 |
return 1 if not $class->eval($data, $schema->{not}, |
| 139 |
+{ %$state, keyword_path => $state->{keyword_path}.'/not', |
| 140 |
short_circuit => 1, # errors do not propagate upward from this subschema |
| 141 |
collect_annotations => 0, # nor do annotations |
| 142 |
errors => [] }); |
| 143 |
|
| 144 |
return E($state, 'subschema is valid'); |
| 145 |
} |
| 146 |
|
| 147 |
sub _traverse_keyword_if { shift->traverse_subschema(@_) } |
| 148 |
sub _traverse_keyword_then { shift->traverse_subschema(@_) } |
| 149 |
sub _traverse_keyword_else { shift->traverse_subschema(@_) } |
| 150 |
|
| 151 |
sub _eval_keyword_if ($class, $data, $schema, $state) { |
| 152 |
return 1 if not exists $schema->{then} and not exists $schema->{else} |
| 153 |
and not $state->{collect_annotations}; |
| 154 |
my $keyword = $class->eval($data, $schema->{if}, |
| 155 |
+{ %$state, keyword_path => $state->{keyword_path}.'/if', short_circuit => 1, errors => [] }) |
| 156 |
? 'then' : 'else'; |
| 157 |
|
| 158 |
return 1 if not exists $schema->{$keyword}; |
| 159 |
|
| 160 |
return $schema->{$keyword} || E({ %$state, keyword => $keyword }, 'subschema is false') |
| 161 |
if is_type('boolean', $schema->{$keyword}); |
| 162 |
|
| 163 |
return 1 if $class->eval($data, $schema->{$keyword}, |
| 164 |
+{ %$state, keyword_path => $state->{keyword_path}.'/'.$keyword }); |
| 165 |
return E({ %$state, keyword => $keyword }, 'subschema is not valid'); |
| 166 |
} |
| 167 |
|
| 168 |
sub _traverse_keyword_dependentSchemas { shift->traverse_object_schemas(@_) } |
| 169 |
|
| 170 |
sub _eval_keyword_dependentSchemas ($class, $data, $schema, $state) { |
| 171 |
return 1 if not is_type('object', $data); |
| 172 |
|
| 173 |
my $valid = 1; |
| 174 |
foreach my $property (sort keys $schema->{dependentSchemas}->%*) { |
| 175 |
next if not exists $data->{$property}; |
| 176 |
|
| 177 |
if ($class->eval($data, $schema->{dependentSchemas}{$property}, |
| 178 |
+{ %$state, keyword_path => jsonp($state->{keyword_path}, 'dependentSchemas', $property) })) { |
| 179 |
next; |
| 180 |
} |
| 181 |
|
| 182 |
$valid = 0; |
| 183 |
last if $state->{short_circuit}; |
| 184 |
} |
| 185 |
|
| 186 |
return E($state, 'not all dependencies are satisfied') if not $valid; |
| 187 |
return 1; |
| 188 |
} |
| 189 |
|
| 190 |
sub _traverse_keyword_dependencies ($class, $schema, $state) { |
| 191 |
return if not assert_keyword_type($state, $schema, 'object'); |
| 192 |
|
| 193 |
my $valid = 1; |
| 194 |
foreach my $property (sort keys $schema->{dependencies}->%*) { |
| 195 |
if (is_type('array', $schema->{dependencies}{$property})) { |
| 196 |
# as in dependentRequired |
| 197 |
|
| 198 |
foreach my $index (0..$schema->{dependencies}{$property}->$#*) { |
| 199 |
$valid = E({ %$state, _keyword_path_suffix => [ $property, $index ] }, 'element #%d is not a string', $index) |
| 200 |
if not is_type('string', $schema->{dependencies}{$property}[$index]); |
| 201 |
} |
| 202 |
|
| 203 |
$valid = E({ %$state, _keyword_path_suffix => $property }, 'elements are not unique') |
| 204 |
if not is_elements_unique($schema->{dependencies}{$property}); |
| 205 |
|
| 206 |
$valid = E($state, '"dependencies" array for %s is empty', $property) |
| 207 |
if $state->{specification_version} eq 'draft4' and not $schema->{dependencies}{$property}->@*; |
| 208 |
} |
| 209 |
else { |
| 210 |
# as in dependentSchemas |
| 211 |
$valid = 0 if not $class->traverse_property_schema($schema, $state, $property); |
| 212 |
} |
| 213 |
} |
| 214 |
return $valid; |
| 215 |
} |
| 216 |
|
| 217 |
sub _eval_keyword_dependencies ($class, $data, $schema, $state) { |
| 218 |
return 1 if not is_type('object', $data); |
| 219 |
|
| 220 |
my $valid = 1; |
| 221 |
foreach my $property (sort keys $schema->{dependencies}->%*) { |
| 222 |
next if not exists $data->{$property}; |
| 223 |
|
| 224 |
if (is_type('array', $schema->{dependencies}{$property})) { |
| 225 |
# as in dependentRequired |
| 226 |
if (my @missing = grep !exists($data->{$_}), $schema->{dependencies}{$property}->@*) { |
| 227 |
$valid = E({ %$state, _keyword_path_suffix => $property }, |
| 228 |
'object is missing propert%s: %s', @missing > 1 ? 'ies' : 'y', join(', ', @missing)); |
| 229 |
} |
| 230 |
} |
| 231 |
else { |
| 232 |
# as in dependentSchemas |
| 233 |
if ($class->eval($data, $schema->{dependencies}{$property}, |
| 234 |
+{ %$state, keyword_path => jsonp($state->{keyword_path}, 'dependencies', $property) })) { |
| 235 |
next; |
| 236 |
} |
| 237 |
|
| 238 |
$valid = 0; |
| 239 |
last if $state->{short_circuit}; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
return E($state, 'not all dependencies are satisfied') if not $valid; |
| 244 |
return 1; |
| 245 |
} |
| 246 |
|
| 247 |
sub _traverse_keyword_prefixItems { shift->traverse_array_schemas(@_) } |
| 248 |
|
| 249 |
sub _eval_keyword_prefixItems { goto \&_eval_keyword__items_array_schemas } |
| 250 |
|
| 251 |
# array- or schema-based before draft2020-12; schema-based only for draft2020-12+ |
| 252 |
sub _traverse_keyword_items ($class, $schema, $state) { |
| 253 |
if (ref $schema->{items} eq 'ARRAY') { |
| 254 |
return E($state, 'array form of "items" not supported in %s', $state->{specification_version}) |
| 255 |
if $state->{specification_version} !~ /^draft(?:[467]|2019-09)\z/; |
| 256 |
|
| 257 |
return $class->traverse_array_schemas($schema, $state); |
| 258 |
} |
| 259 |
|
| 260 |
$class->traverse_subschema($schema, $state); |
| 261 |
} |
| 262 |
|
| 263 |
sub _eval_keyword_items ($class, $data, $schema, $state) { |
| 264 |
goto \&_eval_keyword__items_array_schemas if ref $schema->{items} eq 'ARRAY'; |
| 265 |
goto \&_eval_keyword__items_schema; |
| 266 |
} |
| 267 |
|
| 268 |
# pre-draft2020-12 only |
| 269 |
sub _traverse_keyword_additionalItems { shift->traverse_subschema(@_) } |
| 270 |
|
| 271 |
sub _eval_keyword_additionalItems ($class, $data, $schema, $state) { |
| 272 |
return 1 if not exists $state->{_last_items_index}; |
| 273 |
goto \&_eval_keyword__items_schema; |
| 274 |
} |
| 275 |
|
| 276 |
# "prefixItems" (draft 2020-12), array-based "items" (pre-draft2020-12)) |
| 277 |
sub _eval_keyword__items_array_schemas ($class, $data, $schema, $state) { |
| 278 |
return 1 if not is_type('array', $data); |
| 279 |
|
| 280 |
my $valid = 1; |
| 281 |
|
| 282 |
foreach my $idx (0 .. $data->$#*) { |
| 283 |
last if $idx > $schema->{$state->{keyword}}->$#*; |
| 284 |
$state->{_last_items_index} = $idx; |
| 285 |
|
| 286 |
if (is_type('boolean', $schema->{$state->{keyword}}[$idx])) { |
| 287 |
next if $schema->{$state->{keyword}}[$idx]; |
| 288 |
$valid = E({ %$state, data_path => $state->{data_path}.'/'.$idx, |
| 289 |
_keyword_path_suffix => $idx, collect_annotations => $state->{collect_annotations} & ~1 }, |
| 290 |
'item not permitted'); |
| 291 |
} |
| 292 |
elsif ($class->eval($data->[$idx], $schema->{$state->{keyword}}[$idx], |
| 293 |
+{ %$state, data_path => $state->{data_path}.'/'.$idx, |
| 294 |
keyword_path => $state->{keyword_path}.'/'.$state->{keyword}.'/'.$idx, |
| 295 |
collect_annotations => $state->{collect_annotations} & ~1 })) { |
| 296 |
next; |
| 297 |
} |
| 298 |
|
| 299 |
$valid = 0; |
| 300 |
last if $state->{short_circuit} and not exists $schema->{ |
| 301 |
$state->{keyword} eq 'prefixItems' ? 'items' |
| 302 |
: $state->{keyword} eq 'items' ? 'additionalItems' : die |
| 303 |
}; |
| 304 |
} |
| 305 |
|
| 306 |
if ($state->{defaults}) { |
| 307 |
foreach my $idx ($data->$#*+1 .. $schema->{$state->{keyword}}->$#*) { |
| 308 |
$state->{defaults}{$state->{data_path}.'/'.$idx} = $schema->{$state->{keyword}}[$idx]{default} |
| 309 |
if $valid and ref $schema->{$state->{keyword}}[$idx] eq 'HASH' |
| 310 |
and exists $schema->{$state->{keyword}}[$idx]{default}; |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
A($state, $state->{_last_items_index} == $data->$#* ? true : $state->{_last_items_index}) |
| 315 |
if exists $state->{_last_items_index}; |
| 316 |
return E($state, 'not all items are valid') if not $valid; |
| 317 |
return 1; |
| 318 |
} |
| 319 |
|
| 320 |
# schema-based items (all drafts), and additionalItems (up to and including draft2019-09) |
| 321 |
sub _eval_keyword__items_schema ($class, $data, $schema, $state) { |
| 322 |
return 1 if not is_type('array', $data); |
| 323 |
return 1 if ($state->{_last_items_index}//-1) == $data->$#*; |
| 324 |
|
| 325 |
my $valid = 1; |
| 326 |
|
| 327 |
foreach my $idx (($state->{_last_items_index}//-1)+1 .. $data->$#*) { |
| 328 |
if (is_type('boolean', $schema->{$state->{keyword}})) { |
| 329 |
next if $schema->{$state->{keyword}}; |
| 330 |
$valid = E({ %$state, data_path => $state->{data_path}.'/'.$idx }, |
| 331 |
'%sitem not permitted', |
| 332 |
exists $schema->{prefixItems} || $state->{keyword} eq 'additionalItems' ? 'additional ' : ''); |
| 333 |
} |
| 334 |
else { |
| 335 |
if ($class->eval($data->[$idx], $schema->{$state->{keyword}}, |
| 336 |
+{ %$state, data_path => $state->{data_path}.'/'.$idx, |
| 337 |
keyword_path => $state->{keyword_path}.'/'.$state->{keyword}, |
| 338 |
collect_annotations => $state->{collect_annotations} & ~1 })) { |
| 339 |
next; |
| 340 |
} |
| 341 |
|
| 342 |
$valid = 0; |
| 343 |
} |
| 344 |
last if $state->{short_circuit}; |
| 345 |
} |
| 346 |
|
| 347 |
$state->{_last_items_index} = $data->$#*; |
| 348 |
|
| 349 |
A($state, true); |
| 350 |
return E($state, 'subschema is not valid against all %sitems', |
| 351 |
$state->{keyword} eq 'additionalItems' ? 'additional ' : '') if not $valid; |
| 352 |
return 1; |
| 353 |
} |
| 354 |
|
| 355 |
sub _traverse_keyword_contains { shift->traverse_subschema(@_) } |
| 356 |
|
| 357 |
sub _eval_keyword_contains ($class, $data, $schema, $state) { |
| 358 |
return 1 if not is_type('array', $data); |
| 359 |
|
| 360 |
$state->{_num_contains} = 0; |
| 361 |
my (@errors, @valid); |
| 362 |
|
| 363 |
foreach my $idx (0 .. $data->$#*) { |
| 364 |
if ($class->eval($data->[$idx], $schema->{contains}, |
| 365 |
+{ %$state, errors => \@errors, |
| 366 |
data_path => $state->{data_path}.'/'.$idx, |
| 367 |
keyword_path => $state->{keyword_path}.'/contains', |
| 368 |
collect_annotations => $state->{collect_annotations} & ~1 })) { |
| 369 |
++$state->{_num_contains}; |
| 370 |
push @valid, $idx; |
| 371 |
|
| 372 |
last if $state->{short_circuit} |
| 373 |
# must continue until maxContains fails, but once it does we are guaranteed to be invalid, |
| 374 |
# so can always stop evaluating immediately |
| 375 |
and (exists $schema->{maxContains} and $state->{_num_contains} > $schema->{maxContains}) |
| 376 |
# once minContains succeeds, we can stop evaluating if no unevaluatedItems present |
| 377 |
# (but only draft2020-12 collects annotations for "contains" evaluations) |
| 378 |
or (not exists $schema->{maxContains} |
| 379 |
and (not $state->{collect_annotations} or $state->{specification_version} ne 'draft2020-12') |
| 380 |
and $state->{_num_contains} >= ($schema->{minContains}//1)); |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
# note: no items contained is only valid when minContains is explicitly 0 |
| 385 |
if (not $state->{_num_contains} |
| 386 |
and (($schema->{minContains}//1) > 0 or $state->{specification_version} =~ /^draft[467]\z/)) { |
| 387 |
push $state->{errors}->@*, @errors; |
| 388 |
return E($state, 'subschema is not valid against any item'); |
| 389 |
} |
| 390 |
|
| 391 |
# only draft2020-12 and later can produce annotations |
| 392 |
A($state, @valid == @$data ? true : \@valid) if $state->{specification_version} !~ /^draft(?:[467]|2019-09)\z/; |
| 393 |
|
| 394 |
my $valid = 1; |
| 395 |
|
| 396 |
# 'maxContains' and 'minContains' are owned by the Validation vocabulary, but do nothing if the |
| 397 |
# Applicator vocabulary is omitted and depend on the result of 'contains', so they are implemented |
| 398 |
# here, to be evaluated after 'contains' |
| 399 |
if ($state->{specification_version} !~ /^draft[467]\z/ |
| 400 |
and grep $_ eq 'JSON::Schema::Modern::Vocabulary::Validation', $state->{vocabularies}->@*) { |
| 401 |
$valid = E($state, 'array contains more than %d matching item%s', $schema->{maxContains}, $schema->{maxContains} != 1 ? 's' : '') |
| 402 |
if exists $schema->{maxContains} and $state->{_num_contains} > $schema->{maxContains}; |
| 403 |
$valid = E($state, 'array contains fewer than %d matching item%s', $schema->{minContains}, $schema->{minContains} != 1 ? 's' : '') && $valid |
| 404 |
if exists $schema->{minContains} and $state->{_num_contains} < $schema->{minContains}; |
| 405 |
} |
| 406 |
|
| 407 |
return $valid; |
| 408 |
} |
| 409 |
|
| 410 |
sub _traverse_keyword_properties { shift->traverse_object_schemas(@_) } |
| 411 |
|
| 412 |
sub _eval_keyword_properties ($class, $data, $schema, $state) { |
| 413 |
return 1 if not is_type('object', $data); |
| 414 |
|
| 415 |
my $valid = 1; |
| 416 |
my @properties; |
| 417 |
foreach my $property (sort keys $schema->{properties}->%*) { |
| 418 |
if (not exists $data->{$property} and $state->{defaults}) { |
| 419 |
$state->{defaults}{jsonp($state->{data_path}, $property)} = $schema->{properties}{$property}{default} |
| 420 |
if $valid and $state->{defaults} |
| 421 |
and ref $schema->{properties}{$property} eq 'HASH' |
| 422 |
and exists $schema->{properties}{$property}{default}; |
| 423 |
|
| 424 |
next; |
| 425 |
} |
| 426 |
|
| 427 |
next if not exists $data->{$property}; |
| 428 |
push @properties, $property; |
| 429 |
|
| 430 |
if (is_type('boolean', $schema->{properties}{$property})) { |
| 431 |
next if $schema->{properties}{$property}; |
| 432 |
$valid = E({ %$state, data_path => jsonp($state->{data_path}, $property), |
| 433 |
_keyword_path_suffix => $property }, 'property not permitted'); |
| 434 |
} |
| 435 |
else { |
| 436 |
if ($class->eval($data->{$property}, $schema->{properties}{$property}, |
| 437 |
+{ %$state, data_path => jsonp($state->{data_path}, $property), |
| 438 |
keyword_path => jsonp($state->{keyword_path}, 'properties', $property), |
| 439 |
collect_annotations => $state->{collect_annotations} & ~1 })) { |
| 440 |
next; |
| 441 |
} |
| 442 |
|
| 443 |
$valid = 0; |
| 444 |
} |
| 445 |
last if $state->{short_circuit}; |
| 446 |
} |
| 447 |
|
| 448 |
A($state, \@properties); |
| 449 |
return E($state, 'not all properties are valid') if not $valid; |
| 450 |
return 1; |
| 451 |
} |
| 452 |
|
| 453 |
sub _traverse_keyword_patternProperties ($class, $schema, $state) { |
| 454 |
return if not $class->traverse_object_schemas($schema, $state); |
| 455 |
|
| 456 |
0+!grep !assert_pattern({ %$state, _keyword_path_suffix => $_ }, $_), |
| 457 |
sort keys $schema->{patternProperties}->%*; |
| 458 |
} |
| 459 |
|
| 460 |
sub _eval_keyword_patternProperties ($class, $data, $schema, $state) { |
| 461 |
return 1 if not is_type('object', $data); |
| 462 |
|
| 463 |
my $valid = 1; |
| 464 |
my @properties; |
| 465 |
foreach my $property_pattern (sort keys $schema->{patternProperties}->%*) { |
| 466 |
foreach my $property (sort grep m/(?:$property_pattern)/, keys %$data) { |
| 467 |
push @properties, $property; |
| 468 |
if (is_type('boolean', $schema->{patternProperties}{$property_pattern})) { |
| 469 |
next if $schema->{patternProperties}{$property_pattern}; |
| 470 |
$valid = E({ %$state, data_path => jsonp($state->{data_path}, $property), |
| 471 |
_keyword_path_suffix => $property_pattern }, 'property not permitted'); |
| 472 |
} |
| 473 |
else { |
| 474 |
if ($class->eval($data->{$property}, $schema->{patternProperties}{$property_pattern}, |
| 475 |
+{ %$state, data_path => jsonp($state->{data_path}, $property), |
| 476 |
keyword_path => jsonp($state->{keyword_path}, 'patternProperties', $property_pattern), |
| 477 |
collect_annotations => $state->{collect_annotations} & ~1 })) { |
| 478 |
next; |
| 479 |
} |
| 480 |
|
| 481 |
$valid = 0; |
| 482 |
} |
| 483 |
last if $state->{short_circuit}; |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
A($state, [ uniqstr @properties ]); |
| 488 |
return E($state, 'not all properties are valid') if not $valid; |
| 489 |
return 1; |
| 490 |
} |
| 491 |
|
| 492 |
sub _traverse_keyword_additionalProperties { shift->traverse_subschema(@_) } |
| 493 |
|
| 494 |
sub _eval_keyword_additionalProperties ($class, $data, $schema, $state) { |
| 495 |
return 1 if not is_type('object', $data); |
| 496 |
|
| 497 |
my $valid = 1; |
| 498 |
my @properties; |
| 499 |
foreach my $property (sort keys %$data) { |
| 500 |
next if exists $schema->{properties} and exists $schema->{properties}{$property}; |
| 501 |
next if exists $schema->{patternProperties} |
| 502 |
and any { $property =~ /(?:$_)/ } keys $schema->{patternProperties}->%*; |
| 503 |
|
| 504 |
push @properties, $property; |
| 505 |
if (is_type('boolean', $schema->{additionalProperties})) { |
| 506 |
next if $schema->{additionalProperties}; |
| 507 |
$valid = E({ %$state, data_path => jsonp($state->{data_path}, $property) }, |
| 508 |
'additional property not permitted'); |
| 509 |
} |
| 510 |
else { |
| 511 |
if ($class->eval($data->{$property}, $schema->{additionalProperties}, |
| 512 |
+{ %$state, data_path => jsonp($state->{data_path}, $property), |
| 513 |
keyword_path => $state->{keyword_path}.'/additionalProperties', |
| 514 |
collect_annotations => $state->{collect_annotations} & ~1 })) { |
| 515 |
next; |
| 516 |
} |
| 517 |
|
| 518 |
$valid = 0; |
| 519 |
} |
| 520 |
last if $state->{short_circuit}; |
| 521 |
} |
| 522 |
|
| 523 |
A($state, \@properties); |
| 524 |
return E($state, 'not all additional properties are valid') if not $valid; |
| 525 |
return 1; |
| 526 |
} |
| 527 |
|
| 528 |
sub _traverse_keyword_propertyNames { shift->traverse_subschema(@_) } |
| 529 |
|
| 530 |
sub _eval_keyword_propertyNames ($class, $data, $schema, $state) { |
| 531 |
return 1 if not is_type('object', $data); |
| 532 |
|
| 533 |
my $valid = 1; |
| 534 |
foreach my $property (sort keys %$data) { |
| 535 |
if ($class->eval($property, $schema->{propertyNames}, |
| 536 |
+{ %$state, data_path => jsonp($state->{data_path}, $property), |
| 537 |
keyword_path => $state->{keyword_path}.'/propertyNames', |
| 538 |
collect_annotations => $state->{collect_annotations} & ~1 })) { |
| 539 |
next; |
| 540 |
} |
| 541 |
|
| 542 |
$valid = 0; |
| 543 |
last if $state->{short_circuit}; |
| 544 |
} |
| 545 |
|
| 546 |
return E($state, 'not all property names are valid') if not $valid; |
| 547 |
return 1; |
| 548 |
} |
| 549 |
|
| 550 |
1; |
| 551 |
|
| 552 |
__END__ |
| 553 |
|
| 554 |
=pod |
| 555 |
|
| 556 |
=encoding UTF-8 |
| 557 |
|
| 558 |
=head1 NAME |
| 559 |
|
| 560 |
JSON::Schema::Modern::Vocabulary::Applicator - Implementation of the JSON Schema Applicator vocabulary |
| 561 |
|
| 562 |
=head1 VERSION |
| 563 |
|
| 564 |
version 0.650 |
| 565 |
|
| 566 |
I use a linearly-increasing version numbering scheme. No meaning should be |
| 567 |
presumed or inferred from the version being less than 1.0. |
| 568 |
|
| 569 |
=head1 DESCRIPTION |
| 570 |
|
| 571 |
=for Pod::Coverage vocabulary evaluation_order keywords |
| 572 |
|
| 573 |
=for stopwords metaschema |
| 574 |
|
| 575 |
Implementation of the JSON Schema Draft 2020-12 "Applicator" vocabulary, indicated in metaschemas |
| 576 |
with the URI C<https://json-schema.org/draft/2020-12/vocab/applicator> and formally specified in |
| 577 |
L<https://json-schema.org/draft/2020-12/json-schema-core.html#section-10>. |
| 578 |
|
| 579 |
Support is also provided for |
| 580 |
|
| 581 |
=over 4 |
| 582 |
|
| 583 |
=item * |
| 584 |
|
| 585 |
the equivalent Draft 2019-09 keywords, indicated in metaschemas with the URI C<https://json-schema.org/draft/2019-09/vocab/applicator> and formally specified in L<https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-02#section-9> (except for the C<unevaluatedItems> and C<unevaluatedProperties> keywords, which are implemented in L<JSON::Schema::Modern::Vocabulary::Unevaluated>); |
| 586 |
|
| 587 |
=item * |
| 588 |
|
| 589 |
the equivalent Draft 7 keywords that correspond to this vocabulary and are formally specified in L<https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-6>. |
| 590 |
|
| 591 |
=item * |
| 592 |
|
| 593 |
the equivalent Draft 6 keywords that correspond to this vocabulary and are formally specified in L<https://json-schema.org/draft-06/draft-wright-json-schema-validation-01#rfc.section.6>. |
| 594 |
|
| 595 |
=item * |
| 596 |
|
| 597 |
the equivalent Draft 4 keywords that correspond to this vocabulary and are formally specified in L<https://json-schema.org/draft-04/draft-fge-json-schema-validation-00#rfc.section.5>. |
| 598 |
|
| 599 |
=back |
| 600 |
|
| 601 |
=head1 GIVING THANKS |
| 602 |
|
| 603 |
=for stopwords MetaCPAN GitHub |
| 604 |
|
| 605 |
If you found this module to be useful, please show your appreciation by |
| 606 |
adding a +1 in L<MetaCPAN|https://metacpan.org/dist/JSON-Schema-Modern> |
| 607 |
and a star in L<GitHub|https://github.com/karenetheridge/JSON-Schema-Modern>. |
| 608 |
|
| 609 |
=head1 SUPPORT |
| 610 |
|
| 611 |
Bugs may be submitted through L<https://github.com/karenetheridge/JSON-Schema-Modern/issues>. |
| 612 |
|
| 613 |
I am also usually active on irc, as 'ether' at C<irc.perl.org> and C<irc.libera.chat>. |
| 614 |
|
| 615 |
=for stopwords OpenAPI |
| 616 |
|
| 617 |
You can also find me on the L<JSON Schema Slack server|https://json-schema.slack.com> and L<OpenAPI Slack |
| 618 |
server|https://open-api.slack.com>, which are also great resources for finding help. |
| 619 |
|
| 620 |
=head1 AUTHOR |
| 621 |
|
| 622 |
Karen Etheridge <[email protected]> |
| 623 |
|
| 624 |
=head1 COPYRIGHT AND LICENCE |
| 625 |
|
| 626 |
This software is copyright (c) 2020 by Karen Etheridge. |
| 627 |
|
| 628 |
This is free software; you can redistribute it and/or modify it under |
| 629 |
the same terms as the Perl 5 programming language system itself. |
| 630 |
|
| 631 |
Some schema files have their own licence, in share/LICENSE. |
| 632 |
|
| 633 |
=cut |