| 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 | 189 | ▶ | sub attr_name { 'properties' } | ||||||||||||||||||
|
Subroutine
attr_name: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 189 | ▶ | sub is_valid { | ||||||||||||||||||
|
Subroutine
is_valid: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 15 | 189 | my ($class, $context, $schema, $data) = @_; | |||||||||||||||||||
| 16 | 189 | ▶ | $context->in_attr($class, sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=10 SCAR=23.0
| |||||||||||||||||||||
| 17 | 189 | ▶ | return 1 unless ref $data eq 'HASH'; # ignore | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 183 | my $properties = $schema->{properties}; | |||||||||||||||||||
| 20 | 183 | ▶ | unless (ref $properties eq 'HASH') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 21 | 0 | croak sprintf '`properties` must be an object at %s', $context->position | |||||||||||||||||||
| 22 | } | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 183 | my $is_valid = 1; | |||||||||||||||||||
| 25 | 183 | for my $prop (keys %$properties) { | |||||||||||||||||||
| 26 | 261 | ▶ | 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 | 74 | ▶ | : $properties->{$prop}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 30 | 74 | ▶ | 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 ($context->prims->is_boolean($definition->{required}) && $definition->{required}) { | |||||||||||||||||||
|
Condition100%3/3
$context->prims->is_boolean($definition->{"required"}) and $definition->{"required"}
MC/DC100%2/2
$context->prims->is_boolean($definition->{"required"}) and $definition->{"required"}
$context->prims->is_boolean($definition->{"required"})
$definition->{"required"}
Truth table100%3/3
A and B
A= $context->prims->is_boolean($definition->{"required"})B= $definition->{"required"}
| |||||||||||||||||||||
| 36 | # if {required: true} specified and default not exists, it's invalid(draftv3). | ||||||||||||||||||||
| 37 | # if {required: [properties]} specified, just skip it(draftv4). | ||||||||||||||||||||
| 38 | # TODO check definition. Is it valid existing "default" and "required" in same property??? | ||||||||||||||||||||
| 39 | 5 | $is_valid=0; | |||||||||||||||||||
| 40 | 5 | last; | |||||||||||||||||||
| 41 | } else { | ||||||||||||||||||||
| 42 | 63 | next; # skip on no-required empty | |||||||||||||||||||
| 43 | } | ||||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 187 | my $sub_data = $data->{$prop}; | |||||||||||||||||||
| 47 | 187 | my $sub_schema = $properties->{$prop}; | |||||||||||||||||||
| 48 | 187 | ▶ | my $res = $context->in($prop, sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 49 | 187 | $context->sub_validator($sub_schema)->validate($sub_data, $context); | |||||||||||||||||||
| 50 | 187 | }); | |||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 185 | ▶ | if (!$res) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 53 | 43 | $is_valid = 0; | |||||||||||||||||||
| 54 | 43 | last; | |||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 181 | $is_valid; | |||||||||||||||||||
| 59 | 189 | }); | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 12 | 1; | |||||||||||||||||||