| 1 | package Valiemon::Attributes::Properties; | ||||||||||||||
| 2 | 12 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 12 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 12 | ▶ | use utf8; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 12 | ▶ | use parent qw(Valiemon::Attributes); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 12 | ▶ | use Carp qw(croak); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 12 | ▶ | use Clone qw(clone); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 12 | ▶ | use List::MoreUtils qw(all); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 12 | ▶ | use Valiemon; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | 184 | ▶ | sub attr_name { 'properties' } | ||||||||||||
|
Subroutine
attr_name: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 13 | |||||||||||||||
| 14 | sub is_valid { | ||||||||||||||
| 15 | 184 | ▶ | my ($class, $context, $schema, $data) = @_; | ||||||||||||
|
Subroutine
is_valid: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 16 | $context->in_attr($class, sub { | ||||||||||||||
| 17 | 184 | ▶ | return 1 unless ref $data eq 'HASH'; # ignore | ||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.0
Branch: 1 branch
| |||||||||||||||
| 18 | |||||||||||||||
| 19 | 178 | my $properties = $schema->{properties}; | |||||||||||||
| 20 | 178 | ▶ | unless (ref $properties eq 'HASH') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 21 | 0 | croak sprintf '`properties` must be an object at %s', $context->position | |||||||||||||
| 22 | } | ||||||||||||||
| 23 | |||||||||||||||
| 24 | 178 | my $is_valid = 1; | |||||||||||||
| 25 | 178 | for my $prop (keys %$properties) { | |||||||||||||
| 26 | 254 | ▶ | unless (exists $data->{$prop}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 27 | my $definition = $properties->{$prop}->{'$ref'} # resolve ref TODO refactor | ||||||||||||||
| 28 | ? $context->rv->resolve_ref($properties->{$prop}->{'$ref'}) | ||||||||||||||
| 29 | 75 | ▶ | : $properties->{$prop}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 30 | 75 | ▶ | if (my $default = $definition->{default}) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 31 | # fill in default | ||||||||||||||
| 32 | 6 | $data->{$prop} = clone($default); | |||||||||||||
| 33 | # in case default value applied, skip validation for default value | ||||||||||||||
| 34 | 6 | next; | |||||||||||||
| 35 | } elsif ($definition->{required}) { | ||||||||||||||
| 36 | # if required specified and default not exists, it's invalid | ||||||||||||||
| 37 | # TODO check definition. Is it valid existing "default" and "required" in same property??? | ||||||||||||||
| 38 | 5 | $is_valid=0; | |||||||||||||
| 39 | 5 | last; | |||||||||||||
| 40 | } else { | ||||||||||||||
| 41 | 64 | next; # skip on no-required empty | |||||||||||||
| 42 | } | ||||||||||||||
| 43 | } | ||||||||||||||
| 44 | |||||||||||||||
| 45 | 179 | my $sub_data = $data->{$prop}; | |||||||||||||
| 46 | 179 | my $sub_schema = $properties->{$prop}; | |||||||||||||
| 47 | my $res = $context->in($prop, sub { | ||||||||||||||
| 48 | 179 | ▶ | $context->sub_validator($sub_schema)->validate($sub_data, $context); | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 49 | 179 | }); | |||||||||||||
| 50 | |||||||||||||||
| 51 | 176 | ▶ | if (!$res) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 52 | 43 | $is_valid = 0; | |||||||||||||
| 53 | 43 | last; | |||||||||||||
| 54 | } | ||||||||||||||
| 55 | } | ||||||||||||||
| 56 | |||||||||||||||
| 57 | 175 | $is_valid; | |||||||||||||
| 58 | 184 | }); | |||||||||||||
| 59 | } | ||||||||||||||
| 60 | |||||||||||||||
| 61 | 12 | 1; | |||||||||||||