Valiemon/Attributes/Properties.pm

filter: Statementstmt 98.0% 49 / 50 Branchbran 92.8% 13 / 14 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 2 totaltotal 95.1% 79 / 83 CC 10 SCAR 16.7
CC
10
Cov
97%
__ANON__
10.0
is_valid
1.0
BEGIN
1.0
CRAP 5.3
« Valiemon/Primitives.pm ↑ Summary Valiemon.pm »
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
BranchTrueFalseTotal
unless ref $data eq "HASH" 6 183 189
18  
19 183 my $properties = $schema->{properties};
20 183 ▶ unless (ref $properties eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $properties eq "HASH") 0 183 183
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
BranchTrueFalseTotal
unless (exists $data->{$prop}) 74 187 261
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
BranchTrueFalseTotal
$properties->{$prop}{'$ref'} ? : 1 73 74
30 74 ▶ if (my $default = $definition->{default}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $default = $definition->{'default'}) { } 6 68 74
elsif ($context->prims->is_boolean($definition->{'required'}) and $definition->{'required'}) { } 5 63 68
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"}
!ll&&!rl&&r
6215
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"}
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless ($res) 43 142 185
53 43 $is_valid = 0;
54 43 last;
55 }
56 }
57  
58 181 $is_valid;
59 189 });
60 }
61  
62 12 1;
« Valiemon/Primitives.pm ↑ Summary Valiemon.pm »