lib/JSV/Compiler.pm

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