| 1 | package JSV::Compiler; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use JSON; | ||
| 5 | use JSON::Pointer; | ||
| 6 | use URI; | ||
| 7 | use Path::Tiny; | ||
| 8 | use Carp; | ||
| 9 | use Storable 'dclone'; | ||
| 10 | use Data::Dumper; | ||
| 11 | use Regexp::Common('RE_ALL', 'Email::Address', 'URI', 'time'); | ||
| 12 | use Scalar::Util qw(looks_like_number blessed weaken reftype); | ||
| 13 | |||
| 14 | our $VERSION = "0.05"; | ||
| 15 | |||
| 16 | sub new { | ||
| 17 | my ($class, %args) = @_; | ||
| 18 | bless { | ||
| 19 | original_schema => {}, | ||
| 20 | full_schema => {}, | ||
| 21 | }, $class; | ||
| 22 | } | ||
| 23 | |||
| 24 | sub load_schema { | ||
| 25 | my ($self, $file) = @_; | ||
| 26 | if ('HASH' eq ref $file) { | ||
| 27 | $self->{original_schema} = $file; | ||
| 28 | } else { | ||
| 29 | croak "Unreadable file" if !-r $file; | ||
| 30 | if ($file =~ /\.yaml$/i || $file =~ /\.yml$/i) { | ||
| 31 | require YAML::XS; | ||
| 32 | $self->{original_schema} = YAML::XS::LoadFile($file); | ||
| 33 | } elsif ($file =~ /\.json/i) { | ||
| 34 | $self->{original_schema} = decode_json(path($file)->slurp_raw); | ||
| 35 | } else { | ||
| 36 | croak "Unknown file type: must be .json or .yaml"; | ||
| 37 | } | ||
| 38 | } | ||
| 39 | return $self->_resolve_references; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub _deep_walk { | ||
| 43 | my $visitor = shift; | ||
| 44 | my $recurse; | ||
| 45 | ## no critic (Variables::RequireInitializationForLocalVars) | ||
| 46 | local $_; | ||
| 47 | $recurse = sub { | ||
| 48 | my ($cnode) = @_; | ||
| 49 | my $ctype = reftype $cnode; | ||
| 50 | if ($ctype eq 'ARRAY') { | ||
| 51 | my $index = 0; | ||
| 52 | for (@$cnode) { | ||
| 53 | my $dtype = reftype $_; | ||
| 54 | if ($dtype && ($dtype eq 'HASH' || $dtype eq 'ARRAY')) { | ||
| 55 | $recurse->($_, $cnode); | ||
| 56 | } | ||
| 57 | $visitor->($ctype, $cnode, $index++); | ||
| 58 | } | ||
| 59 | } elsif ($ctype eq 'HASH') { | ||
| 60 | for my $k (keys %$cnode) { | ||
| 61 | local $_ = $cnode->{$k}; | ||
| 62 | my $dtype = reftype $_; | ||
| 63 | if ($dtype && ($dtype eq 'HASH' || $dtype eq 'ARRAY')) { | ||
| 64 | $recurse->($_, $cnode); | ||
| 65 | } | ||
| 66 | $visitor->($ctype, $cnode, $k); | ||
| 67 | } | ||
| 68 | } | ||
| 69 | }; | ||
| 70 | $recurse->($_[0]); | ||
| 71 | $_ = $_[0]; | ||
| 72 | $visitor->('ARRAY', \@_, 0); | ||
| 73 | } | ||
| 74 | |||
| 75 | sub _resolve_references { ## no critic (Subroutines::ProhibitExcessComplexity) | ||
| 76 | my $self = $_[0]; | ||
| 77 | $self->{full_schema} = dclone $self->{original_schema}; | ||
| 78 | my $base_uri = $self->{full_schema}{id} || $self->{full_schema}{'$id'}; | ||
| 79 | if ($base_uri) { | ||
| 80 | $base_uri = URI->new($base_uri)->canonical(); | ||
| 81 | $base_uri->fragment("") if not $base_uri->fragment; | ||
| 82 | $self->{schemas}{$base_uri} = $self->{full_schema}; | ||
| 83 | } | ||
| 84 | my @unresolved; | ||
| 85 | my %unresolved; | ||
| 86 | my $resolve = sub { | ||
| 87 | my ($ref) = @_; | ||
| 88 | my $uri = $base_uri ? URI->new_abs($ref, $base_uri)->canonical : URI->new($ref)->canonical; | ||
| 89 | return $self->{schemas}{$uri} if $self->{schemas}{$uri}; | ||
| 90 | my $su = $uri->clone; | ||
| 91 | $su->fragment(""); | ||
| 92 | if ($self->{schemas}{$su}) { | ||
| 93 | my $rs = JSON::Pointer->get($self->{schemas}{$su}, $uri->fragment); | ||
| 94 | return $rs if $rs; | ||
| 95 | } | ||
| 96 | push @unresolved, "$su" if not $unresolved{$su}++; | ||
| 97 | return undef; | ||
| 98 | }; | ||
| 99 | _deep_walk( | ||
| 100 | sub { | ||
| 101 | my ($ctype, $cnode, $index) = @_; | ||
| 102 | if ( $ctype eq 'ARRAY' | ||
| 103 | && 'HASH' eq ref $_ | ||
| 104 | && keys %$_ == 1 | ||
| 105 | && $_->{'$ref'} | ||
| 106 | && !ref($_->{'$ref'})) | ||
| 107 | { | ||
| 108 | weaken($cnode->[$index] = $resolve->($_->{'$ref'})); | ||
| 109 | } elsif ('HASH' eq ref $_) { | ||
| 110 | for my $k (keys %$_) { | ||
| 111 | my $v = $_->{$k}; | ||
| 112 | if ('HASH' eq ref($v) && keys(%$v) == 1 && $v->{'$ref'} && !ref($v->{'$ref'})) { | ||
| 113 | weaken($_->{$k} = $resolve->($v->{'$ref'})); | ||
| 114 | } elsif ($k eq '$ref' && !ref($_->{$k})) { | ||
| 115 | my $r = $resolve->($_->{$k}); | ||
| 116 | if ($r && 'HASH' eq ref $r) { | ||
| 117 | weaken($cnode->{$index} = $r); | ||
| 118 | } | ||
| 119 | } elsif (($k eq 'id' || $k eq '$id') && !ref($v)) { | ||
| 120 | my $id = $base_uri ? URI->new_abs($v, $base_uri)->canonical : URI->new($v)->canonical; | ||
| 121 | weaken($self->{schemas}{$id} = $_) if not $self->{schemas}{$id}; | ||
| 122 | } | ||
| 123 | } | ||
| 124 | } | ||
| 125 | }, | ||
| 126 | $self->{full_schema} | ||
| 127 | ); | ||
| 128 | return wantarray ? @unresolved : $self; | ||
| 129 | } | ||
| 130 | |||
| 131 | sub compile { | ||
| 132 | my ($self, %opts) = @_; | ||
| 133 | ## no critic (Variables::ProhibitLocalVars) | ||
| 134 | local $self->{coersion} = $opts{coersion} // 0; | ||
| 135 | local $self->{to_json} = $opts{to_json} // 0; | ||
| 136 | $self->{required_modules} = {}; | ||
| 137 | my $input_sym = $opts{input_symbole} // '$_[0]'; | ||
| 138 | my $schema = _norm_schema($self->{full_schema}); | ||
| 139 | my $type = $schema->{type} // _guess_schema_type($schema); | ||
| 140 | my $is_required = $opts{is_required} // $type eq 'object' || 0; | ||
| 141 | my $val_func = "_validate_$type"; | ||
| 142 | my $val_expr = $self->$val_func($input_sym, $schema, "", $is_required); | ||
| 143 | return | ||
| 144 | wantarray | ||
| 145 | ? ($val_expr, map {$_ => [sort keys %{$self->{required_modules}{$_}}]} keys %{$self->{required_modules}}) | ||
| 146 | : $val_expr; | ||
| 147 | } | ||
| 148 | |||
| 149 | # type: six primitive types ("null", "boolean", "object", "array", "number", or "string"), or "integer" | ||
| 150 | |||
| 151 | sub _norm_schema { | ||
| 152 | my $shmpt = $_[0]; | ||
| 153 | return +{ | ||
| 154 | type => _guess_schema_type($shmpt), | ||
| 155 | const => $shmpt | ||
| 156 | } if 'HASH' ne ref $shmpt; | ||
| 157 | $shmpt; | ||
| 158 | } | ||
| 159 | |||
| 160 | sub _guess_schema_type { ## no critic (Subroutines::ProhibitExcessComplexity) | ||
| 161 | my $shmpt = $_[0]; | ||
| 162 | if (my $class = blessed($shmpt)) { | ||
| 163 | if ($class =~ /bool/i) { | ||
| 164 | return 'boolean'; | ||
| 165 | } else { | ||
| 166 | return 'object'; | ||
| 167 | } | ||
| 168 | } | ||
| 169 | if ('HASH' ne ref $shmpt) { | ||
| 170 | return 'number' if looks_like_number($shmpt); | ||
| 171 | return 'string'; | ||
| 172 | } | ||
| 173 | return $shmpt->{type} if $shmpt->{type}; | ||
| 174 | return 'object' | ||
| 175 | if defined $shmpt->{additionalProperties} | ||
| 176 | or $shmpt->{patternProperties} | ||
| 177 | or $shmpt->{properties} | ||
| 178 | or defined $shmpt->{minProperties} | ||
| 179 | or defined $shmpt->{maxProperties}; | ||
| 180 | return 'array' | ||
| 181 | if defined $shmpt->{additionalItems} | ||
| 182 | or defined $shmpt->{uniqueItems} | ||
| 183 | or $shmpt->{items} | ||
| 184 | or defined $shmpt->{minItems} | ||
| 185 | or defined $shmpt->{maxItems}; | ||
| 186 | return 'number' | ||
| 187 | if defined $shmpt->{minimum} | ||
| 188 | or defined $shmpt->{maximum} | ||
| 189 | or exists $shmpt->{exclusiveMinimum} | ||
| 190 | or exists $shmpt->{exclusiveMaximum} | ||
| 191 | or defined $shmpt->{multipleOf}; | ||
| 192 | return 'string'; | ||
| 193 | } | ||
| 194 | |||
| 195 | sub _quote_var { | ||
| 196 | my $s = $_[0]; | ||
| 197 | my $d = Data::Dumper->new([$s]); | ||
| 198 | $d->Terse(1); | ||
| 199 | my $qs = $d->Dump; | ||
| 200 | substr($qs, -1, 1, '') if substr($qs, -1, 1) eq "\n"; | ||
| 201 | return $qs; | ||
| 202 | } | ||
| 203 | |||
| 204 | #<<< | ||
| 205 | my %formats = ( | ||
| 206 | 'date-time' => $RE{time}{iso}, | ||
| 207 | email => $RE{Email}{Address}, | ||
| 208 | uri => $RE{URI}, | ||
| 209 | hostname => '(?:(?:(?:(?:[a-zA-Z0-9][-a-zA-Z0-9]*)?[a-zA-Z0-9])[.])*' | ||
| 210 | . '(?:[a-zA-Z][-a-zA-Z0-9]*[a-zA-Z0-9]|[a-zA-Z])[.]?)', | ||
| 211 | ipv4 => $RE{net}{IPv4}, | ||
| 212 | ipv6 => $RE{net}{IPv6}, | ||
| 213 | ); | ||
| 214 | #>>> | ||
| 215 | |||
| 216 | ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines) | ||
| 217 | sub _validate_null { | ||
| 218 | my ($self, $sympt, $schmptm, $path) = @_; | ||
| 219 | my @sp = split /->/, $sympt; | ||
| 220 | my $el = pop @sp; | ||
| 221 | my $sh = join "->", @sp; | ||
| 222 | my $ec = $sh ? "|| ('HASH' eq ref($sh) && !exists ($sympt))" : ''; | ||
| 223 | return "push \@\$errors, \"$path must be null\" if defined($sympt) $ec;\n"; | ||
| 224 | } | ||
| 225 | |||
| 226 | sub _validate_boolean { | ||
| 227 | my ($self, $sympt, $schmpt, $path, $is_required) = @_; | ||
| 228 | $schmpt = _norm_schema($schmpt); | ||
| 229 | my $r = ''; | ||
| 230 | if (exists $schmpt->{default}) { | ||
| 231 | my $val = _quote_var($schmpt->{default}); | ||
| 232 | $r = "$sympt = $val if not defined $sympt;\n"; | ||
| 233 | } | ||
| 234 | $r .= "if(defined($sympt)) {\n"; | ||
| 235 | $r .= $self->_validate_schemas_array($sympt, $schmpt, $path); | ||
| 236 | if (defined $schmpt->{const}) { | ||
| 237 | $r .= " { no warnings 'uninitialized';\n"; | ||
| 238 | my $not = $schmpt->{const} ? 'not' : ""; | ||
| 239 | $r .= " push \@\$errors, \"$path must be \".($schmpt->{const}?'true':'false') if $not $sympt \n"; | ||
| 240 | $r .= " }\n"; | ||
| 241 | } | ||
| 242 | if ($self->{to_json}) { | ||
| 243 | $r .= " $sympt = (($sympt)? \\1: \\0);\n"; | ||
| 244 | } elsif ($self->{coersion}) { | ||
| 245 | $r .= " $sympt = (($sympt)? 1: 0);\n"; | ||
| 246 | } | ||
| 247 | $r .= "}\n"; | ||
| 248 | if ($is_required) { | ||
| 249 | $r .= "else {\n"; | ||
| 250 | $r .= " push \@\$errors, \"$path is required\";\n"; | ||
| 251 | $r .= "}\n"; | ||
| 252 | } | ||
| 253 | return $r; | ||
| 254 | } | ||
| 255 | |||
| 256 | sub _validate_string { | ||
| 257 | my ($self, $sympt, $schmpt, $path, $is_required) = @_; | ||
| 258 | $schmpt = _norm_schema($schmpt); | ||
| 259 | my $r = ''; | ||
| 260 | if (defined $schmpt->{default}) { | ||
| 261 | my $val = _quote_var($schmpt->{default}); | ||
| 262 | $r = "$sympt = $val if not defined $sympt;\n"; | ||
| 263 | } | ||
| 264 | $r .= "if(defined($sympt)) {\n"; | ||
| 265 | $r .= $self->_validate_schemas_array($sympt, $schmpt, $path); | ||
| 266 | if (defined $schmpt->{maxLength}) { | ||
| 267 | $r .= " push \@\$errors, '$path length must be not greater than "; | ||
| 268 | $r .= "$schmpt->{maxLength}' if length($sympt) > $schmpt->{maxLength};\n"; | ||
| 269 | } | ||
| 270 | if (defined $schmpt->{minLength}) { | ||
| 271 | $r .= " push \@\$errors, '$path length must be not less than "; | ||
| 272 | $r .= "$schmpt->{minLength}' if length($sympt) < $schmpt->{minLength};\n"; | ||
| 273 | } | ||
| 274 | if (defined $schmpt->{const}) { | ||
| 275 | my $val = _quote_var($schmpt->{const}); | ||
| 276 | $r .= " push \@\$errors, \"$path must be $schmpt->{const}\" if $sympt ne $val;\n"; | ||
| 277 | } | ||
| 278 | if (defined $schmpt->{pattern}) { | ||
| 279 | my $pattern = $schmpt->{pattern}; | ||
| 280 | $pattern =~ s/\\Q(.*?)\\E/quotemeta($1)/eg; | ||
| 281 | $pattern =~ s/\\Q(.*)$/quotemeta($1)/eg; | ||
| 282 | $pattern =~ s/"/\\"/g; | ||
| 283 | $pattern =~ s|/|\\/|g; | ||
| 284 | $r .= " push \@\$errors, \"$path does not match pattern\" if $sympt !~ /$pattern/;\n"; | ||
| 285 | } | ||
| 286 | if ($schmpt->{enum} && 'ARRAY' eq ref($schmpt->{enum}) && @{$schmpt->{enum}}) { | ||
| 287 | my $can_list = join ", ", map {_quote_var($_)} @{$schmpt->{enum}}; | ||
| 288 | $self->{required_modules}{'List::Util'}{none} = 1; | ||
| 289 | $r .= " push \@\$errors, \"$path must be on of $can_list\" if none {\$_ eq $sympt} ($can_list);\n"; | ||
| 290 | } | ||
| 291 | if ($schmpt->{format} && $formats{$schmpt->{format}}) { | ||
| 292 | $r .= " push \@\$errors, \"$path does not match format $schmpt->{format}\""; | ||
| 293 | $r .= " if $sympt !~ /^$formats{$schmpt->{format}}\$/;\n"; | ||
| 294 | } | ||
| 295 | if ($self->{to_json} || $self->{coersion}) { | ||
| 296 | $r .= " $sympt = \"$sympt\";\n"; | ||
| 297 | } | ||
| 298 | $r .= "}\n"; | ||
| 299 | if ($is_required) { | ||
| 300 | $r .= "else {\n"; | ||
| 301 | $r .= " push \@\$errors, \"$path is required\";\n"; | ||
| 302 | $r .= "}\n"; | ||
| 303 | } | ||
| 304 | return $r; | ||
| 305 | } | ||
| 306 | |||
| 307 | sub _validate_any_number { ## no critic (Subroutines::ProhibitManyArgs Subroutines::ProhibitExcessComplexity) | ||
| 308 | my ($self, $sympt, $schmpt, $path, $is_required, $re, $ntype) = @_; | ||
| 309 | $schmpt = _norm_schema($schmpt); | ||
| 310 | my $r = ''; | ||
| 311 | $ntype ||= ''; | ||
| 312 | if (defined $schmpt->{default}) { | ||
| 313 | my $val = _quote_var($schmpt->{default}); | ||
| 314 | $r = "$sympt = $val if not defined $sympt;\n"; | ||
| 315 | } | ||
| 316 | $r .= "if(defined($sympt)) {\n"; | ||
| 317 | $r .= $self->_validate_schemas_array($sympt, $schmpt, $path); | ||
| 318 | $r .= " {\n"; | ||
| 319 | $r .= " if($sympt !~ /^$re\$/){ push \@\$errors, '$path does not look like $ntype number'; last }\n"; | ||
| 320 | my ($minimum, $exclusiveMinimum, $maximum, $exclusiveMaximum) = | ||
| 321 | @{$schmpt}{qw(minimum exclusiveMinimum maximum exclusiveMaximum)}; | ||
| 322 | if (defined $minimum && $exclusiveMinimum) { | ||
| 323 | $exclusiveMinimum = $minimum; | ||
| 324 | undef $minimum; | ||
| 325 | } | ||
| 326 | if (defined $maximum && $exclusiveMaximum) { | ||
| 327 | $exclusiveMaximum = $maximum; | ||
| 328 | undef $maximum; | ||
| 329 | } | ||
| 330 | if (defined $minimum) { | ||
| 331 | $r .= " push \@\$errors, '$path must be not less than $minimum'"; | ||
| 332 | $r .= " if $sympt < $minimum;\n"; | ||
| 333 | } | ||
| 334 | if (defined $exclusiveMinimum) { | ||
| 335 | $r .= " push \@\$errors, '$path must be greater than $exclusiveMinimum'"; | ||
| 336 | $r .= " if $sympt <= $exclusiveMinimum;\n"; | ||
| 337 | } | ||
| 338 | if (defined $maximum) { | ||
| 339 | $r .= " push \@\$errors, '$path must be not greater than $maximum'"; | ||
| 340 | $r .= " if $sympt > $maximum;\n"; | ||
| 341 | } | ||
| 342 | if (defined $exclusiveMaximum) { | ||
| 343 | $r .= " push \@\$errors, '$path must be less than $exclusiveMaximum'"; | ||
| 344 | $r .= " if $sympt >= $exclusiveMaximum;\n"; | ||
| 345 | } | ||
| 346 | if (defined $schmpt->{const}) { | ||
| 347 | $r .= " push \@\$errors, '$path must be $schmpt->{const}' if $sympt != $schmpt->{const};\n"; | ||
| 348 | } | ||
| 349 | if ($schmpt->{multipleOf}) { | ||
| 350 | $self->{required_modules}{'POSIX'}{floor} = 1; | ||
| 351 | $r .= " push \@\$errors, '$path must be multiple of $schmpt->{multipleOf}'"; | ||
| 352 | $r .= " if $sympt / $schmpt->{multipleOf} != floor($sympt / $schmpt->{multipleOf});\n"; | ||
| 353 | } | ||
| 354 | if ($schmpt->{enum} && 'ARRAY' eq ref($schmpt->{enum}) && @{$schmpt->{enum}}) { | ||
| 355 | my $can_list = join ", ", map {_quote_var($_)} @{$schmpt->{enum}}; | ||
| 356 | $self->{required_modules}{'List::Util'}{none} = 1; | ||
| 357 | $r .= " push \@\$errors, '$path must be on of $can_list' if none {$_ == $sympt} ($can_list);\n"; | ||
| 358 | } | ||
| 359 | if ($schmpt->{format} && $formats{$schmpt->{format}}) { | ||
| 360 | $r .= " push \@\$errors, '$path does not match format $schmpt->{format}'"; | ||
| 361 | $r .= " if $sympt !~ /^$formats{$schmpt->{format}}\$/;\n"; | ||
| 362 | } | ||
| 363 | if ($self->{to_json} || $self->{coersion}) { | ||
| 364 | $r .= " $sympt += 0;\n"; | ||
| 365 | } | ||
| 366 | $r .= "} }\n"; | ||
| 367 | if ($is_required) { | ||
| 368 | $r .= "else {\n"; | ||
| 369 | $r .= " push \@\$errors, \"$path is required\";\n"; | ||
| 370 | $r .= "}\n"; | ||
| 371 | } | ||
| 372 | return $r; | ||
| 373 | |||
| 374 | } | ||
| 375 | |||
| 376 | sub _validate_number { | ||
| 377 | my ($self, $sympt, $schmpt, $path, $is_required) = @_; | ||
| 378 | return $self->_validate_any_number($sympt, $schmpt, $path, $is_required, $RE{num}{real}); | ||
| 379 | } | ||
| 380 | |||
| 381 | sub _validate_integer { | ||
| 382 | my ($self, $sympt, $schmpt, $path, $is_required) = @_; | ||
| 383 | return $self->_validate_any_number($sympt, $schmpt, $path, $is_required, $RE{num}{int}, "integer"); | ||
| 384 | } | ||
| 385 | |||
| 386 | sub _make_schemas_array { | ||
| 387 | my ($self, $schemas, $rpath, $type) = @_; | ||
| 388 | $schemas = [$schemas] if 'ARRAY' ne ref $schemas; | ||
| 389 | my @tfa; | ||
| 390 | for my $schm (@{$schemas}) { | ||
| 391 | my $subschm = _norm_schema($schm); | ||
| 392 | my $stype = $subschm->{type} // $type // _guess_schema_type($schm); | ||
| 393 | my $val_func = "_validate_$stype"; | ||
| 394 | my $ivf = $self->$val_func("\$_[0]", $subschm, "$rpath", "required"); | ||
| 395 | push @tfa, " sub {my \$errors = []; $ivf; \@\$errors == 0}\n"; | ||
| 396 | } | ||
| 397 | return "(" . join(",\n", @tfa) . ")"; | ||
| 398 | } | ||
| 399 | |||
| 400 | sub _validate_all_of { | ||
| 401 | my ($self, $schmpt, $sympt, $rpath) = @_; | ||
| 402 | my $r = ''; | ||
| 403 | $self->{required_modules}{'List::Util'}{notall} = 1; | ||
| 404 | $r .= " { my \@allOf = " . $self->_make_schemas_array($schmpt->{allOf}, $rpath, $schmpt->{type}) . ";\n"; | ||
| 405 | $r .= " my \$stored_arg = ${sympt};\n"; | ||
| 406 | $r .= " push \@\$errors, \"$rpath doesn't match all required schemas\" " | ||
| 407 | . "if notall { \$_->(\$stored_arg, \"$rpath\") } \@allOf;\n"; | ||
| 408 | $r .= " }\n"; | ||
| 409 | return $r; | ||
| 410 | } | ||
| 411 | |||
| 412 | sub _validate_any_of { | ||
| 413 | my ($self, $schmpt, $sympt, $rpath) = @_; | ||
| 414 | my $r = ''; | ||
| 415 | $self->{required_modules}{'List::Util'}{none} = 1; | ||
| 416 | $r .= " { my \@anyOf = " . $self->_make_schemas_array($schmpt->{anyOf}, $rpath, $schmpt->{type}) . ";\n"; | ||
| 417 | $r .= " my \$stored_arg = ${sympt};\n"; | ||
| 418 | $r .= " push \@\$errors, \"$rpath doesn't match any required schema\"" | ||
| 419 | . " if none { \$_->(\$stored_arg, \"$rpath\") } \@anyOf;\n"; | ||
| 420 | $r .= " }\n"; | ||
| 421 | return $r; | ||
| 422 | } | ||
| 423 | |||
| 424 | sub _validate_one_of { | ||
| 425 | my ($self, $schmpt, $sympt, $rpath) = @_; | ||
| 426 | my $r = ''; | ||
| 427 | $r .= " { my \@oneOf = " . $self->_make_schemas_array($schmpt->{oneOf}, $rpath, $schmpt->{type}) . ";\n"; | ||
| 428 | $r .= " my \$stored_arg = ${sympt};\n"; | ||
| 429 | $r .= " my \$m = 0; for my \$t (\@oneOf) { ++\$m if \$t->(\$stored_arg, \"$rpath\"); last if \$m > 1; }\n"; | ||
| 430 | $r .= " push \@\$errors, \"$rpath doesn't match exactly one required schema\" if \$m != 1;\n"; | ||
| 431 | $r .= " }\n"; | ||
| 432 | return $r; | ||
| 433 | } | ||
| 434 | |||
| 435 | sub _validate_not_of { | ||
| 436 | my ($self, $schmpt, $sympt, $rpath) = @_; | ||
| 437 | my $r = ''; | ||
| 438 | $self->{required_modules}{'List::Util'}{any} = 1; | ||
| 439 | $r .= " { my \@notOf = " . $self->_make_schemas_array($schmpt->{not}, $rpath, $schmpt->{type}) . ";\n"; | ||
| 440 | $r .= " my \$stored_arg = ${sympt};\n"; | ||
| 441 | $r .= " push \@\$errors, \"$rpath matches a schema when must not\" " | ||
| 442 | . " if any { \$_->(\$stored_arg, \"$rpath\") } \@notOf;\n"; | ||
| 443 | $r .= " }\n"; | ||
| 444 | return $r; | ||
| 445 | } | ||
| 446 | |||
| 447 | sub _validate_schemas_array { | ||
| 448 | my ($self, $sympt, $schmpt, $path) = @_; | ||
| 449 | my $rpath = !$path ? "(object)" : $path; | ||
| 450 | my $r = ''; | ||
| 451 | $r .= $self->_validate_any_of($schmpt, $sympt, $rpath) if defined $schmpt->{anyOf}; | ||
| 452 | $r .= $self->_validate_all_of($schmpt, $sympt, $rpath) if defined $schmpt->{allOf}; | ||
| 453 | $r .= $self->_validate_one_of($schmpt, $sympt, $rpath) if defined $schmpt->{oneOf}; | ||
| 454 | $r .= $self->_validate_not_of($schmpt, $sympt, $rpath) if defined $schmpt->{not}; | ||
| 455 | return $r; | ||
| 456 | } | ||
| 457 | |||
| 458 | sub _validate_object { ## no critic (Subroutines::ProhibitExcessComplexity) | ||
| 459 | my ($self, $sympt, $schmpt, $path, $is_required) = @_; | ||
| 460 | $schmpt = _norm_schema($schmpt); | ||
| 461 | my $rpath = !$path ? "(object)" : $path; | ||
| 462 | my $ppref = $path ? "$path/" : ""; | ||
| 463 | my $r = ''; | ||
| 464 | if ($schmpt->{default}) { | ||
| 465 | my $val = _quote_var($schmpt->{default}); | ||
| 466 | $r = " $sympt = $val if not defined $sympt;\n"; | ||
| 467 | } | ||
| 468 | $r .= "if('HASH' eq ref($sympt)) {\n"; | ||
| 469 | $r .= $self->_validate_schemas_array($sympt, $schmpt, $path); | ||
| 470 | if ($schmpt->{properties} && 'HASH' eq ref $schmpt->{properties}) { | ||
| 471 | my %required; | ||
| 472 | if ($schmpt->{required} && 'ARRAY' eq ref $schmpt->{required}) { | ||
| 473 | %required = map {$_ => 1} @{$schmpt->{required}}; | ||
| 474 | } | ||
| 475 | for my $k (keys %{$schmpt->{properties}}) { | ||
| 476 | my $type = 'string'; | ||
| 477 | if ('HASH' eq ref $schmpt->{properties}{$k}) { | ||
| 478 | $type = $schmpt->{properties}{$k}{type} // _guess_schema_type($schmpt->{properties}{$k}); | ||
| 479 | } | ||
| 480 | my $val_func = "_validate_$type"; | ||
| 481 | my $qk = _quote_var($k); | ||
| 482 | $r .= $self->$val_func("${sympt}->{$qk}", $schmpt->{properties}{$k}, "$ppref$k", $required{$k}); | ||
| 483 | } | ||
| 484 | } | ||
| 485 | if (defined $schmpt->{minProperties}) { | ||
| 486 | $schmpt->{minProperties} += 0; | ||
| 487 | $r .= " push \@\$errors, '$rpath must contain not less than $schmpt->{minProperties} properties'"; | ||
| 488 | $r .= " if keys %{$sympt} < $schmpt->{minProperties};\n"; | ||
| 489 | } | ||
| 490 | if (defined $schmpt->{maxProperties}) { | ||
| 491 | $schmpt->{maxProperties} += 0; | ||
| 492 | $r .= " push \@\$errors, '$rpath must contain not more than $schmpt->{maxProperties} properties'"; | ||
| 493 | $r .= " if keys %{$sympt} > $schmpt->{minProperties};\n"; | ||
| 494 | } | ||
| 495 | my @pt; | ||
| 496 | if (defined $schmpt->{patternProperties}) { | ||
| 497 | for my $pt (keys %{$schmpt->{patternProperties}}) { | ||
| 498 | my $type; | ||
| 499 | $type = $schmpt->{patternProperties}{$pt}{type} | ||
| 500 | // _guess_schema_type($schmpt->{patternProperties}{$pt}); | ||
| 501 | my $val_func = "_validate_$type"; | ||
| 502 | (my $upt = $pt) =~ s/"/\\"/g; | ||
| 503 | $upt =~ s/\\Q(.*?)\\E/quotemeta($1)/eg; | ||
| 504 | $upt =~ s/\\Q(.*)$/quotemeta($1)/eg; | ||
| 505 | $upt =~ s|/|\\/|g; | ||
| 506 | push @pt, $upt; | ||
| 507 | my $ivf = $self->$val_func("\$_[0]", $schmpt->{patternProperties}{$pt}, "\$_[1]", "required"); | ||
| 508 | $r .= " { my \@props = grep {/$upt/} keys %{${sympt}};"; | ||
| 509 | |||
| 510 | if ($schmpt->{properties} && 'HASH' eq ref $schmpt->{properties}) { | ||
| 511 | my %apr = map {_quote_var($_) => undef} keys %{$schmpt->{properties}}; | ||
| 512 | $r .= " my %defined_props = (" . join(", ", map {$_ => "undef"} keys %apr) . ");\n"; | ||
| 513 | $r .= " \@props = grep {!exists \$defined_props{\$_} } \@props;\n"; | ||
| 514 | } | ||
| 515 | $r .= " my \$tf = sub { $ivf };\n"; | ||
| 516 | $r .= " for my \$prop (\@props) {\n"; | ||
| 517 | $r .= " \$tf->(${sympt}->{\$prop}, \"$ppref\${prop}\");\n"; | ||
| 518 | $r .= " };\n"; | ||
| 519 | $r .= " }\n"; | ||
| 520 | } | ||
| 521 | } | ||
| 522 | if (defined $schmpt->{additionalProperties}) { | ||
| 523 | if (!ref($schmpt->{additionalProperties}) && !$schmpt->{additionalProperties}) { | ||
| 524 | my %apr; | ||
| 525 | $r .= " {\n"; | ||
| 526 | if ($schmpt->{properties} && 'HASH' eq ref $schmpt->{properties}) { | ||
| 527 | %apr = map {_quote_var($_) => undef} keys %{$schmpt->{properties}}; | ||
| 528 | $r .= " my %allowed_props = (" . join(", ", map {$_ => "undef"} keys %apr) . ");\n"; | ||
| 529 | $r .= " my \@unallowed_props = grep {!exists \$allowed_props{\$_} } keys %{${sympt}};\n"; | ||
| 530 | if (@pt) { | ||
| 531 | $r .= | ||
| 532 | " \@unallowed_props = grep { " | ||
| 533 | . join("&&", map {"!/$_/"} @pt) | ||
| 534 | . " } \@unallowed_props;\n"; | ||
| 535 | } | ||
| 536 | $r .= " push \@\$errors, \"$rpath contains not allowed properties: \@unallowed_props\" "; | ||
| 537 | $r .= " if \@unallowed_props;\n"; | ||
| 538 | } else { | ||
| 539 | $r .= " push \@\$errors, \"$rpath can't contain properties\" if %{${sympt}};\n"; | ||
| 540 | } | ||
| 541 | $r .= " }\n"; | ||
| 542 | } | ||
| 543 | } | ||
| 544 | $r .= "}\n"; | ||
| 545 | if ($is_required) { | ||
| 546 | $r .= "else {\n"; | ||
| 547 | $r .= " push \@\$errors, \"$rpath is required\";\n"; | ||
| 548 | $r .= "}\n"; | ||
| 549 | } | ||
| 550 | return $r; | ||
| 551 | } | ||
| 552 | |||
| 553 | sub _validate_array { | ||
| 554 | my ($self, $sympt, $schmpt, $path, $is_required) = @_; | ||
| 555 | $schmpt = _norm_schema($schmpt); | ||
| 556 | my $rpath = !$path ? "(object)" : $path; | ||
| 557 | my $r = ''; | ||
| 558 | if ($schmpt->{default}) { | ||
| 559 | my $val = _quote_var($schmpt->{default}); | ||
| 560 | $r = " $sympt = $val if not defined $sympt;\n"; | ||
| 561 | } | ||
| 562 | $r .= "if('ARRAY' eq ref($sympt)) {\n"; | ||
| 563 | $r .= $self->_validate_schemas_array($sympt, $schmpt, $path); | ||
| 564 | if (defined $schmpt->{minItems}) { | ||
| 565 | $r .= " push \@\$errors, '$path must contain not less than $schmpt->{minItems} items'"; | ||
| 566 | $r .= " if \@{$sympt} < $schmpt->{minItems};\n"; | ||
| 567 | } | ||
| 568 | if (defined $schmpt->{maxItems}) { | ||
| 569 | $r .= " push \@\$errors, '$path must contain not more than $schmpt->{maxItems} items'"; | ||
| 570 | $r .= " if \@{$sympt} > $schmpt->{maxItems};\n"; | ||
| 571 | } | ||
| 572 | if (defined $schmpt->{uniqueItems}) { | ||
| 573 | $r .= " { my %seen;\n"; | ||
| 574 | $r .= " for (\@{$sympt}) {\n"; | ||
| 575 | $r .= " if(\$seen{\$_}) { push \@\$errors, '$path must contain only unique items'; last }\n"; | ||
| 576 | $r .= " \$seen{\$_} = 1;\n"; | ||
| 577 | $r .= " };\n"; | ||
| 578 | $r .= " }\n"; | ||
| 579 | } | ||
| 580 | if ($schmpt->{items}) { | ||
| 581 | my $type = $schmpt->{items}{type} // _guess_schema_type($schmpt->{items}); | ||
| 582 | my $val_func = "_validate_$type"; | ||
| 583 | my $ivf = $self->$val_func("\$_[0]", $schmpt->{items}, "$path/[]", $is_required); | ||
| 584 | $r .= " { my \$tf = sub { $ivf };\n"; | ||
| 585 | $r .= " \$tf->(\$_, \"$rpath\") for (\@{$sympt});\n"; | ||
| 586 | $r .= " }\n"; | ||
| 587 | } | ||
| 588 | $r .= "}\n"; | ||
| 589 | if ($is_required) { | ||
| 590 | $path = "array" if $path eq ""; | ||
| 591 | $r .= "else {\n"; | ||
| 592 | $r .= " push \@\$errors, \"$path is required\";\n"; | ||
| 593 | $r .= "}\n"; | ||
| 594 | } | ||
| 595 | return $r; | ||
| 596 | } | ||
| 597 | |||
| 598 | 1; | ||
| 599 | |||
| 600 | __END__ |