lib/JSV/Compiler.pm

filter: Statementstmt 87.0% 384 / 441 Branchbran 72.0% 134 / 186 Conditioncond 64.4% 105 / 163 MC/DCmcdc 30.2% 26 / 86 Subroutinesub 100% 36 / 36 Podpod 100% 3 / 3 totaltotal 75.1% 688 / 915 CC 156 SCAR 30.8
CC
156
Cov
79%
_guess_schema_type
52.6
_validate_any_number
44.5
_validate_object
31.2
CRAP 21.7
« blib/lib/JSV/Compiler.pm ↑ Summary
1 package JSV::Compiler;
2 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use JSON::Pointer;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Storable 'dclone';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use Regexp::Common('RE_ALL', 'Email::Address', 'URI', 'time');
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use Scalar::Util qw(looks_like_number blessed weaken reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 8 our $VERSION = "0.05";
15  
16 sub new {
17 8 ▶ my ($class, %args) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
18 8 bless {
19 original_schema => {},
20 full_schema => {},
21 }, $class;
22 }
23  
24 sub load_schema {
25 26 ▶ my ($self, $file) = @_;
Subroutine load_schema: called CC=6 SCAR=30.9
Pod documented
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 sub _deep_walk {
43 26 ▶ my $visitor = shift;
Subroutine _deep_walk: called CC=1 SCAR=0.0
44 26 my $recurse;
45 ## no critic (Variables::RequireInitializationForLocalVars)
46 26 local $_;
47 $recurse = sub {
48 137 ▶ my ($cnode) = @_;
Subroutine __ANON__: called CC=11 SCAR=24.0
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
Condition66%4/6
$dtype and $dtype eq "HASH" || $dtype eq "ARRAY"
!ll&&!rl&&r
25450
$dtype eq "HASH" or $dtype eq "ARRAY"
l!l&&r!l&&!r
21024
MC/DC0%0/3
$dtype and $dtype eq "HASH" || $dtype eq "ARRAY"
$dtype $dtype eq "HASH" $dtype eq "ARRAY"
Truth table50%2/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
65983
$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 sub _resolve_references { ## no critic (Subroutines::ProhibitExcessComplexity)
76 26 ▶ my $self = $_[0];
Subroutine _resolve_references: called CC=5 SCAR=16.2
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 my $resolve = sub {
87 10 ▶ my ($ref) = @_;
Subroutine __ANON__: called CC=6 SCAR=17.9
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 sub {
101 253 ▶ my ($ctype, $cnode, $index) = @_;
Subroutine __ANON__: called CC=21 SCAR=30.9
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 sub compile {
132 27 ▶ my ($self, %opts) = @_;
Subroutine compile: called CC=8 SCAR=21.0
Pod documented
133 ## no critic (Variables::ProhibitLocalVars)
134 27 ▶ local $self->{coersion} = $opts{coersion} // 0;
Condition100%2/2
$opts{"coersion"} // 0
l!l
126
MC/DC100%1/1
$opts{"coersion"} // 0
$opts{"coersion"}
Truth table100%2/2
A
A= $opts{"coersion"}
Aresult
11
00
135 27 ▶ local $self->{to_json} = $opts{to_json} // 0;
Condition50%1/2
$opts{"to_json"} // 0
l!l
027
MC/DC0%0/1
$opts{"to_json"} // 0
$opts{"to_json"}
Truth table50%1/2
A
A= $opts{"to_json"}
Aresult
11
00
136 27 $self->{required_modules} = {};
137 27 ▶ my $input_sym = $opts{input_symbole} // '$_[0]';
Condition50%1/2
$opts{"input_symbole"} // "\$_[0]"
l!l
027
MC/DC0%0/1
$opts{"input_symbole"} // "\$_[0]"
$opts{"input_symbole"}
Truth table50%1/2
A
A= $opts{"input_symbole"}
Aresult
11
00
138 27 my $schema = _norm_schema($self->{full_schema});
139 27 ▶ my $type = $schema->{type} // _guess_schema_type($schema);
Condition66%2/3
$schema->{"type"} // _guess_schema_type($schema)
l!l&&r!l&&!r
7200
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
140 27 ▶ my $is_required = $opts{is_required} // $type eq 'object' || 0;
Condition80%4/5
($opts{'is_required'} // $type eq 'object') || 0
l!l
720
$opts{"is_required"} // $type eq "object"
l!l&&r!l&&!r
0720
MC/DC50%1/2
($opts{'is_required'} // $type eq 'object') || 0
$opts{"is_required"} $type eq "object"
Truth table66%2/3
A // B
A= $opts{"is_required"}B= $type eq "object"
ABresult
1X1
011
000
141 27 my $val_func = "_validate_$type";
142 27 my $val_expr = $self->$val_func($input_sym, $schema, "", $is_required);
143 return
144 wantarray
145 27 ▶ ? ($val_expr, map {$_ => [sort keys %{$self->{required_modules}{$_}}]} keys %{$self->{required_modules}})
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 26 1 27
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 217 ▶ my $shmpt = $_[0];
Subroutine _norm_schema: called CC=2 SCAR=6.9
153 return +{
154 217 ▶ type => _guess_schema_type($shmpt),
Branch: 1 branch
BranchTrueFalseTotal
if "HASH" ne ref $shmpt 26 191 217
155 const => $shmpt
156 } if 'HASH' ne ref $shmpt;
157 191 $shmpt;
158 }
159  
160 sub _guess_schema_type { ## no critic (Subroutines::ProhibitExcessComplexity)
161 68 ▶ my $shmpt = $_[0];
Subroutine _guess_schema_type: called CC=21 SCAR=39.6
162 68 ▶ if (my $class = blessed($shmpt)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $class = builtin::blessed($shmpt)) 26 42 68
163 26 ▶ if ($class =~ /bool/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class =~ /bool/i) { } 26 0 26
164 26 return 'boolean';
165 } else {
166 0 return 'object';
167 }
168 }
169 42 ▶ if ('HASH' ne ref $shmpt) {
Branch: 1 branch
BranchTrueFalseTotal
if ("HASH" ne ref $shmpt) 0 42 42
170 0 ▶ return 'number' if looks_like_number($shmpt);
Branch: 1 branch
BranchTrueFalseTotal
if &looks_like_number($shmpt) 0 0 0
171 0 return 'string';
172 }
173 42 ▶ return $shmpt->{type} if $shmpt->{type};
Branch: 1 branch
BranchTrueFalseTotal
if $shmpt->{"type"} 0 42 42
174 return 'object'
175 if defined $shmpt->{additionalProperties}
176 or $shmpt->{patternProperties}
177 or $shmpt->{properties}
178 or defined $shmpt->{minProperties}
179 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"} 0 36 36
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
180 return 'array'
181 if defined $shmpt->{additionalItems}
182 or defined $shmpt->{uniqueItems}
183 or $shmpt->{items}
184 or defined $shmpt->{minItems}
185 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
186 return 'number'
187 if defined $shmpt->{minimum}
188 or defined $shmpt->{maximum}
189 or exists $shmpt->{exclusiveMinimum}
190 or exists $shmpt->{exclusiveMaximum}
191 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"} 0 32 32
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
192 32 return 'string';
193 }
194  
195 sub _quote_var {
196 81 ▶ my $s = $_[0];
Subroutine _quote_var: called CC=2 SCAR=7.0
197 81 my $d = Data::Dumper->new([$s]);
198 81 $d->Terse(1);
199 81 my $qs = $d->Dump;
200 81 ▶ substr($qs, -1, 1, '') if substr($qs, -1, 1) eq "\n";
Branch: 1 branch
BranchTrueFalseTotal
if substr($qs, -1, 1) eq "\n" 81 0 81
201 81 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 8 );
214 #>>>
215  
216 ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
217 sub _validate_null {
218 1 ▶ my ($self, $sympt, $schmptm, $path) = @_;
Subroutine _validate_null: called CC=2 SCAR=7.0
219 1 my @sp = split /->/, $sympt;
220 1 my $el = pop @sp;
221 1 my $sh = join "->", @sp;
222 1 ▶ my $ec = $sh ? "|| ('HASH' eq ref($sh) && !exists ($sympt))" : '';
Branch: 1 branch
BranchTrueFalseTotal
$sh ? : 1 0 1
223 1 return "push \@\$errors, \"$path must be null\" if defined($sympt) $ec;\n";
224 }
225  
226 sub _validate_boolean {
227 30 ▶ my ($self, $sympt, $schmpt, $path, $is_required) = @_;
Subroutine _validate_boolean: called CC=7 SCAR=19.7
228 30 $schmpt = _norm_schema($schmpt);
229 30 my $r = '';
230 30 ▶ if (exists $schmpt->{default}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schmpt->{"default"}) 0 30 30
231 0 my $val = _quote_var($schmpt->{default});
232 0 $r = "$sympt = $val if not defined $sympt;\n";
233 }
234 30 $r .= "if(defined($sympt)) {\n";
235 30 $r .= $self->_validate_schemas_array($sympt, $schmpt, $path);
236 30 ▶ if (defined $schmpt->{const}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $schmpt->{"const"}) 26 4 30
237 26 $r .= " { no warnings 'uninitialized';\n";
238 26 ▶ my $not = $schmpt->{const} ? 'not' : "";
Branch: 1 branch
BranchTrueFalseTotal
$schmpt->{'const'} ? : 13 13 26
239 26 $r .= " push \@\$errors, \"$path must be \".($schmpt->{const}?'true':'false') if $not $sympt \n";
240 26 $r .= " }\n";
241 }
242 30 ▶ if ($self->{to_json}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'to_json'}) { } 0 30 30
elsif ($self->{'coersion'}) { } 1 29 30
243 0 $r .= " $sympt = (($sympt)? \\1: \\0);\n";
244 } elsif ($self->{coersion}) {
245 1 $r .= " $sympt = (($sympt)? 1: 0);\n";
246 }
247 30 $r .= "}\n";
248 30 ▶ if ($is_required) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_required) 26 4 30
249 26 $r .= "else {\n";
250 26 $r .= " push \@\$errors, \"$path is required\";\n";
251 26 $r .= "}\n";
252 }
253 30 return $r;
254 }
255  
256 sub _validate_string {
257 61 ▶ my ($self, $sympt, $schmpt, $path, $is_required) = @_;
Subroutine _validate_string: called CC=14 SCAR=27.0
258 61 $schmpt = _norm_schema($schmpt);
259 61 my $r = '';
260 61 ▶ if (defined $schmpt->{default}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $schmpt->{"default"}) 0 61 61
261 0 my $val = _quote_var($schmpt->{default});
262 0 $r = "$sympt = $val if not defined $sympt;\n";
263 }
264 61 $r .= "if(defined($sympt)) {\n";
265 61 $r .= $self->_validate_schemas_array($sympt, $schmpt, $path);
266 61 ▶ if (defined $schmpt->{maxLength}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $schmpt->{"maxLength"}) 2 59 61
267 2 $r .= " push \@\$errors, '$path length must be not greater than ";
268 2 $r .= "$schmpt->{maxLength}' if length($sympt) > $schmpt->{maxLength};\n";
269 }
270 61 ▶ if (defined $schmpt->{minLength}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $schmpt->{"minLength"}) 2 59 61
271 2 $r .= " push \@\$errors, '$path length must be not less than ";
272 2 $r .= "$schmpt->{minLength}' if length($sympt) < $schmpt->{minLength};\n";
273 }
274 61 ▶ if (defined $schmpt->{const}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $schmpt->{"const"}) 0 61 61
275 0 my $val = _quote_var($schmpt->{const});
276 0 $r .= " push \@\$errors, \"$path must be $schmpt->{const}\" if $sympt ne $val;\n";
277 }
278 61 ▶ if (defined $schmpt->{pattern}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $schmpt->{"pattern"}) 6 55 61
279 6 my $pattern = $schmpt->{pattern};
280 6 $pattern =~ s/\\Q(.*?)\\E/quotemeta($1)/eg;
281 6 $pattern =~ s/\\Q(.*)$/quotemeta($1)/eg;
282 6 $pattern =~ s/"/\\"/g;
283 6 $pattern =~ s|/|\\/|g;
284 6 $r .= " push \@\$errors, \"$path does not match pattern\" if $sympt !~ /$pattern/;\n";
285 }
286 61 ▶ 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 51 61
Condition66%4/6
$schmpt->{"enum"} and "ARRAY" eq ref $schmpt->{"enum"} and @{$schmpt->{"enum"};}
!ll&&!rl&&r
51010
$schmpt->{"enum"} and "ARRAY" eq ref $schmpt->{"enum"}
!ll&&!rl&&r
51010
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
287 10 my $can_list = join ", ", map {_quote_var($_)} @{$schmpt->{enum}};
288 10 $self->{required_modules}{'List::Util'}{none} = 1;
289 10 $r .= " push \@\$errors, \"$path must be on of $can_list\" if none {\$_ eq $sympt} ($can_list);\n";
290 }
291 61 ▶ if ($schmpt->{format} && $formats{$schmpt->{format}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schmpt->{"format"} and $formats{$schmpt->{"format"}}) 6 55 61
Condition66%2/3
$schmpt->{"format"} and $formats{$schmpt->{"format"}}
!ll&&!rl&&r
5506
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
292 6 $r .= " push \@\$errors, \"$path does not match format $schmpt->{format}\"";
293 6 $r .= " if $sympt !~ /^$formats{$schmpt->{format}}\$/;\n";
294 }
295 61 ▶ if ($self->{to_json} || $self->{coersion}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"to_json"} or $self->{"coersion"}) 1 60 61
Condition66%2/3
$self->{"to_json"} or $self->{"coersion"}
l!l&&r!l&&!r
0160
MC/DC50%1/2
$self->{"to_json"} or $self->{"coersion"}
$self->{"to_json"} $self->{"coersion"}
Truth table66%2/3
A or B
A= $self->{"to_json"}B= $self->{"coersion"}
ABresult
1X1
011
000
296 1 $r .= " $sympt = \"$sympt\";\n";
297 }
298 61 $r .= "}\n";
299 61 ▶ if ($is_required) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_required) 32 29 61
300 32 $r .= "else {\n";
301 32 $r .= " push \@\$errors, \"$path is required\";\n";
302 32 $r .= "}\n";
303 }
304 61 return $r;
305 }
306  
307 sub _validate_any_number { ## no critic (Subroutines::ProhibitManyArgs Subroutines::ProhibitExcessComplexity)
308 15 ▶ my ($self, $sympt, $schmpt, $path, $is_required, $re, $ntype) = @_;
Subroutine _validate_any_number: called CC=21 SCAR=38.0
309 15 $schmpt = _norm_schema($schmpt);
310 15 my $r = '';
311 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
312 15 ▶ if (defined $schmpt->{default}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $schmpt->{"default"}) 0 15 15
313 0 my $val = _quote_var($schmpt->{default});
314 0 $r = "$sympt = $val if not defined $sympt;\n";
315 }
316 15 $r .= "if(defined($sympt)) {\n";
317 15 $r .= $self->_validate_schemas_array($sympt, $schmpt, $path);
318 15 $r .= " {\n";
319 15 $r .= " if($sympt !~ /^$re\$/){ push \@\$errors, '$path does not look like $ntype number'; last }\n";
320 my ($minimum, $exclusiveMinimum, $maximum, $exclusiveMaximum) =
321 15 @{$schmpt}{qw(minimum exclusiveMinimum maximum exclusiveMaximum)};
322 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
323 0 $exclusiveMinimum = $minimum;
324 0 undef $minimum;
325 }
326 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
327 0 $exclusiveMaximum = $maximum;
328 0 undef $maximum;
329 }
330 15 ▶ if (defined $minimum) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $minimum) 5 10 15
331 5 $r .= " push \@\$errors, '$path must be not less than $minimum'";
332 5 $r .= " if $sympt < $minimum;\n";
333 }
334 15 ▶ if (defined $exclusiveMinimum) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $exclusiveMinimum) 1 14 15
335 1 $r .= " push \@\$errors, '$path must be greater than $exclusiveMinimum'";
336 1 $r .= " if $sympt <= $exclusiveMinimum;\n";
337 }
338 15 ▶ if (defined $maximum) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $maximum) 3 12 15
339 3 $r .= " push \@\$errors, '$path must be not greater than $maximum'";
340 3 $r .= " if $sympt > $maximum;\n";
341 }
342 15 ▶ if (defined $exclusiveMaximum) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $exclusiveMaximum) 0 15 15
343 0 $r .= " push \@\$errors, '$path must be less than $exclusiveMaximum'";
344 0 $r .= " if $sympt >= $exclusiveMaximum;\n";
345 }
346 15 ▶ if (defined $schmpt->{const}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $schmpt->{"const"}) 0 15 15
347 0 $r .= " push \@\$errors, '$path must be $schmpt->{const}' if $sympt != $schmpt->{const};\n";
348 }
349 15 ▶ if ($schmpt->{multipleOf}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schmpt->{"multipleOf"}) 0 15 15
350 0 $self->{required_modules}{'POSIX'}{floor} = 1;
351 0 $r .= " push \@\$errors, '$path must be multiple of $schmpt->{multipleOf}'";
352 0 $r .= " if $sympt / $schmpt->{multipleOf} != floor($sympt / $schmpt->{multipleOf});\n";
353 }
354 15 ▶ 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
355 0 my $can_list = join ", ", map {_quote_var($_)} @{$schmpt->{enum}};
356 0 $self->{required_modules}{'List::Util'}{none} = 1;
357 0 $r .= " push \@\$errors, '$path must be on of $can_list' if none {$_ == $sympt} ($can_list);\n";
358 }
359 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
360 0 $r .= " push \@\$errors, '$path does not match format $schmpt->{format}'";
361 0 $r .= " if $sympt !~ /^$formats{$schmpt->{format}}\$/;\n";
362 }
363 15 ▶ if ($self->{to_json} || $self->{coersion}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"to_json"} or $self->{"coersion"}) 1 14 15
Condition66%2/3
$self->{"to_json"} or $self->{"coersion"}
l!l&&r!l&&!r
0114
MC/DC50%1/2
$self->{"to_json"} or $self->{"coersion"}
$self->{"to_json"} $self->{"coersion"}
Truth table66%2/3
A or B
A= $self->{"to_json"}B= $self->{"coersion"}
ABresult
1X1
011
000
364 1 $r .= " $sympt += 0;\n";
365 }
366 15 $r .= "} }\n";
367 15 ▶ if ($is_required) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_required) 15 0 15
368 15 $r .= "else {\n";
369 15 $r .= " push \@\$errors, \"$path is required\";\n";
370 15 $r .= "}\n";
371 }
372 15 return $r;
373  
374 }
375  
376 sub _validate_number {
377 5 ▶ my ($self, $sympt, $schmpt, $path, $is_required) = @_;
Subroutine _validate_number: called CC=1 SCAR=0.0
378 5 return $self->_validate_any_number($sympt, $schmpt, $path, $is_required, $RE{num}{real});
379 }
380  
381 sub _validate_integer {
382 10 ▶ my ($self, $sympt, $schmpt, $path, $is_required) = @_;
Subroutine _validate_integer: called CC=1 SCAR=0.0
383 10 return $self->_validate_any_number($sympt, $schmpt, $path, $is_required, $RE{num}{int}, "integer");
384 }
385  
386 sub _make_schemas_array {
387 26 ▶ my ($self, $schemas, $rpath, $type) = @_;
Subroutine _make_schemas_array: called CC=5 SCAR=16.1
388 26 ▶ $schemas = [$schemas] if 'ARRAY' ne ref $schemas;
Branch: 1 branch
BranchTrueFalseTotal
if "ARRAY" ne ref $schemas 5 21 26
389 26 my @tfa;
390 26 for my $schm (@{$schemas}) {
391 57 my $subschm = _norm_schema($schm);
392 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
393 57 my $val_func = "_validate_$stype";
394 57 my $ivf = $self->$val_func("\$_[0]", $subschm, "$rpath", "required");
395 57 push @tfa, " sub {my \$errors = []; $ivf; \@\$errors == 0}\n";
396 }
397 26 return "(" . join(",\n", @tfa) . ")";
398 }
399  
400 sub _validate_all_of {
401 6 ▶ my ($self, $schmpt, $sympt, $rpath) = @_;
Subroutine _validate_all_of: called CC=1 SCAR=0.0
402 6 my $r = '';
403 6 $self->{required_modules}{'List::Util'}{notall} = 1;
404 6 $r .= " { my \@allOf = " . $self->_make_schemas_array($schmpt->{allOf}, $rpath, $schmpt->{type}) . ";\n";
405 6 $r .= " my \$stored_arg = ${sympt};\n";
406 6 $r .= " push \@\$errors, \"$rpath doesn't match all required schemas\" "
407 . "if notall { \$_->(\$stored_arg, \"$rpath\") } \@allOf;\n";
408 6 $r .= " }\n";
409 6 return $r;
410 }
411  
412 sub _validate_any_of {
413 5 ▶ my ($self, $schmpt, $sympt, $rpath) = @_;
Subroutine _validate_any_of: called CC=1 SCAR=0.0
414 5 my $r = '';
415 5 $self->{required_modules}{'List::Util'}{none} = 1;
416 5 $r .= " { my \@anyOf = " . $self->_make_schemas_array($schmpt->{anyOf}, $rpath, $schmpt->{type}) . ";\n";
417 5 $r .= " my \$stored_arg = ${sympt};\n";
418 5 $r .= " push \@\$errors, \"$rpath doesn't match any required schema\""
419 . " if none { \$_->(\$stored_arg, \"$rpath\") } \@anyOf;\n";
420 5 $r .= " }\n";
421 5 return $r;
422 }
423  
424 sub _validate_one_of {
425 10 ▶ my ($self, $schmpt, $sympt, $rpath) = @_;
Subroutine _validate_one_of: called CC=1 SCAR=0.0
426 10 my $r = '';
427 10 $r .= " { my \@oneOf = " . $self->_make_schemas_array($schmpt->{oneOf}, $rpath, $schmpt->{type}) . ";\n";
428 10 $r .= " my \$stored_arg = ${sympt};\n";
429 10 $r .= " my \$m = 0; for my \$t (\@oneOf) { ++\$m if \$t->(\$stored_arg, \"$rpath\"); last if \$m > 1; }\n";
430 10 $r .= " push \@\$errors, \"$rpath doesn't match exactly one required schema\" if \$m != 1;\n";
431 10 $r .= " }\n";
432 10 return $r;
433 }
434  
435 sub _validate_not_of {
436 5 ▶ my ($self, $schmpt, $sympt, $rpath) = @_;
Subroutine _validate_not_of: called CC=1 SCAR=0.0
437 5 my $r = '';
438 5 $self->{required_modules}{'List::Util'}{any} = 1;
439 5 $r .= " { my \@notOf = " . $self->_make_schemas_array($schmpt->{not}, $rpath, $schmpt->{type}) . ";\n";
440 5 $r .= " my \$stored_arg = ${sympt};\n";
441 5 $r .= " push \@\$errors, \"$rpath matches a schema when must not\" "
442 . " if any { \$_->(\$stored_arg, \"$rpath\") } \@notOf;\n";
443 5 $r .= " }\n";
444 5 return $r;
445 }
446  
447 sub _validate_schemas_array {
448 133 ▶ my ($self, $sympt, $schmpt, $path) = @_;
Subroutine _validate_schemas_array: called CC=6 SCAR=17.9
449 133 ▶ my $rpath = !$path ? "(object)" : $path;
Branch: 1 branch
BranchTrueFalseTotal
!$path ? : 27 106 133
450 133 my $r = '';
451 133 ▶ $r .= $self->_validate_any_of($schmpt, $sympt, $rpath) if defined $schmpt->{anyOf};
Branch: 1 branch
BranchTrueFalseTotal
if defined $schmpt->{"anyOf"} 5 128 133
452 133 ▶ $r .= $self->_validate_all_of($schmpt, $sympt, $rpath) if defined $schmpt->{allOf};
Branch: 1 branch
BranchTrueFalseTotal
if defined $schmpt->{"allOf"} 6 127 133
453 133 ▶ $r .= $self->_validate_one_of($schmpt, $sympt, $rpath) if defined $schmpt->{oneOf};
Branch: 1 branch
BranchTrueFalseTotal
if defined $schmpt->{"oneOf"} 10 123 133
454 133 ▶ $r .= $self->_validate_not_of($schmpt, $sympt, $rpath) if defined $schmpt->{not};
Branch: 1 branch
BranchTrueFalseTotal
if defined $schmpt->{"not"} 5 128 133
455 133 return $r;
456 }
457  
458 sub _validate_object { ## no critic (Subroutines::ProhibitExcessComplexity)
459 24 ▶ my ($self, $sympt, $schmpt, $path, $is_required) = @_;
Subroutine _validate_object: called CC=25 SCAR=34.4
460 24 $schmpt = _norm_schema($schmpt);
461 24 ▶ my $rpath = !$path ? "(object)" : $path;
Branch: 1 branch
BranchTrueFalseTotal
!$path ? : 7 17 24
462 24 ▶ my $ppref = $path ? "$path/" : "";
Branch: 1 branch
BranchTrueFalseTotal
$path ? : 17 7 24
463 24 my $r = '';
464 24 ▶ if ($schmpt->{default}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schmpt->{"default"}) 0 24 24
465 0 my $val = _quote_var($schmpt->{default});
466 0 $r = " $sympt = $val if not defined $sympt;\n";
467 }
468 24 $r .= "if('HASH' eq ref($sympt)) {\n";
469 24 $r .= $self->_validate_schemas_array($sympt, $schmpt, $path);
470 24 ▶ if ($schmpt->{properties} && 'HASH' eq ref $schmpt->{properties}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schmpt->{"properties"} and "HASH" eq ref $schmpt->{"properties"}) 22 2 24
Condition66%2/3
$schmpt->{"properties"} and "HASH" eq ref $schmpt->{"properties"}
!ll&&!rl&&r
2022
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
471 22 my %required;
472 22 ▶ if ($schmpt->{required} && 'ARRAY' eq ref $schmpt->{required}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schmpt->{"required"} and "ARRAY" eq ref $schmpt->{"required"}) 20 2 22
Condition66%2/3
$schmpt->{"required"} and "ARRAY" eq ref $schmpt->{"required"}
!ll&&!rl&&r
2020
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
473 20 %required = map {$_ => 1} @{$schmpt->{required}};
474 }
475 22 for my $k (keys %{$schmpt->{properties}}) {
476 45 my $type = 'string';
477 45 ▶ if ('HASH' eq ref $schmpt->{properties}{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if ("HASH" eq ref $schmpt->{"properties"}{$k}) 45 0 45
478 45 ▶ $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
33120
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
479 }
480 45 my $val_func = "_validate_$type";
481 45 my $qk = _quote_var($k);
482 45 $r .= $self->$val_func("${sympt}->{$qk}", $schmpt->{properties}{$k}, "$ppref$k", $required{$k});
483 }
484 }
485 24 ▶ if (defined $schmpt->{minProperties}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $schmpt->{"minProperties"}) 0 24 24
486 0 $schmpt->{minProperties} += 0;
487 0 $r .= " push \@\$errors, '$rpath must contain not less than $schmpt->{minProperties} properties'";
488 0 $r .= " if keys %{$sympt} < $schmpt->{minProperties};\n";
489 }
490 24 ▶ if (defined $schmpt->{maxProperties}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $schmpt->{"maxProperties"}) 0 24 24
491 0 $schmpt->{maxProperties} += 0;
492 0 $r .= " push \@\$errors, '$rpath must contain not more than $schmpt->{maxProperties} properties'";
493 0 $r .= " if keys %{$sympt} > $schmpt->{minProperties};\n";
494 }
495 24 my @pt;
496 24 ▶ if (defined $schmpt->{patternProperties}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $schmpt->{"patternProperties"}) 2 22 24
497 2 for my $pt (keys %{$schmpt->{patternProperties}}) {
498 2 my $type;
499 $type = $schmpt->{patternProperties}{$pt}{type}
500 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
501 2 my $val_func = "_validate_$type";
502 2 (my $upt = $pt) =~ s/"/\\"/g;
503 2 $upt =~ s/\\Q(.*?)\\E/quotemeta($1)/eg;
504 2 $upt =~ s/\\Q(.*)$/quotemeta($1)/eg;
505 2 $upt =~ s|/|\\/|g;
506 2 push @pt, $upt;
507 2 my $ivf = $self->$val_func("\$_[0]", $schmpt->{patternProperties}{$pt}, "\$_[1]", "required");
508 2 $r .= " { my \@props = grep {/$upt/} keys %{${sympt}};";
509  
510 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
511 2 my %apr = map {_quote_var($_) => undef} keys %{$schmpt->{properties}};
512 2 $r .= " my %defined_props = (" . join(", ", map {$_ => "undef"} keys %apr) . ");\n";
513 2 $r .= " \@props = grep {!exists \$defined_props{\$_} } \@props;\n";
514 }
515 2 $r .= " my \$tf = sub { $ivf };\n";
516 2 $r .= " for my \$prop (\@props) {\n";
517 2 $r .= " \$tf->(${sympt}->{\$prop}, \"$ppref\${prop}\");\n";
518 2 $r .= " };\n";
519 2 $r .= " }\n";
520 }
521 }
522 24 ▶ if (defined $schmpt->{additionalProperties}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $schmpt->{"additionalProperties"}) 10 14 24
523 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
524 10 my %apr;
525 10 $r .= " {\n";
526 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
527 10 %apr = map {_quote_var($_) => undef} keys %{$schmpt->{properties}};
528 10 $r .= " my %allowed_props = (" . join(", ", map {$_ => "undef"} keys %apr) . ");\n";
529 10 $r .= " my \@unallowed_props = grep {!exists \$allowed_props{\$_} } keys %{${sympt}};\n";
530 10 ▶ if (@pt) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pt) 2 8 10
531 $r .=
532 " \@unallowed_props = grep { "
533 2 . join("&&", map {"!/$_/"} @pt)
534 . " } \@unallowed_props;\n";
535 }
536 10 $r .= " push \@\$errors, \"$rpath contains not allowed properties: \@unallowed_props\" ";
537 10 $r .= " if \@unallowed_props;\n";
538 } else {
539 0 $r .= " push \@\$errors, \"$rpath can't contain properties\" if %{${sympt}};\n";
540 }
541 10 $r .= " }\n";
542 }
543 }
544 24 $r .= "}\n";
545 24 ▶ if ($is_required) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_required) 24 0 24
546 24 $r .= "else {\n";
547 24 $r .= " push \@\$errors, \"$rpath is required\";\n";
548 24 $r .= "}\n";
549 }
550 24 return $r;
551 }
552  
553 sub _validate_array {
554 3 ▶ my ($self, $sympt, $schmpt, $path, $is_required) = @_;
Subroutine _validate_array: called CC=10 SCAR=26.5
555 3 $schmpt = _norm_schema($schmpt);
556 3 ▶ my $rpath = !$path ? "(object)" : $path;
Branch: 1 branch
BranchTrueFalseTotal
!$path ? : 0 3 3
557 3 my $r = '';
558 3 ▶ if ($schmpt->{default}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schmpt->{"default"}) 0 3 3
559 0 my $val = _quote_var($schmpt->{default});
560 0 $r = " $sympt = $val if not defined $sympt;\n";
561 }
562 3 $r .= "if('ARRAY' eq ref($sympt)) {\n";
563 3 $r .= $self->_validate_schemas_array($sympt, $schmpt, $path);
564 3 ▶ if (defined $schmpt->{minItems}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $schmpt->{"minItems"}) 3 0 3
565 3 $r .= " push \@\$errors, '$path must contain not less than $schmpt->{minItems} items'";
566 3 $r .= " if \@{$sympt} < $schmpt->{minItems};\n";
567 }
568 3 ▶ if (defined $schmpt->{maxItems}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $schmpt->{"maxItems"}) 0 3 3
569 0 $r .= " push \@\$errors, '$path must contain not more than $schmpt->{maxItems} items'";
570 0 $r .= " if \@{$sympt} > $schmpt->{maxItems};\n";
571 }
572 3 ▶ if (defined $schmpt->{uniqueItems}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $schmpt->{"uniqueItems"}) 3 0 3
573 3 $r .= " { my %seen;\n";
574 3 $r .= " for (\@{$sympt}) {\n";
575 3 $r .= " if(\$seen{\$_}) { push \@\$errors, '$path must contain only unique items'; last }\n";
576 3 $r .= " \$seen{\$_} = 1;\n";
577 3 $r .= " };\n";
578 3 $r .= " }\n";
579 }
580 3 ▶ if ($schmpt->{items}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schmpt->{"items"}) 3 0 3
581 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
582 3 my $val_func = "_validate_$type";
583 3 my $ivf = $self->$val_func("\$_[0]", $schmpt->{items}, "$path/[]", $is_required);
584 3 $r .= " { my \$tf = sub { $ivf };\n";
585 3 $r .= " \$tf->(\$_, \"$rpath\") for (\@{$sympt});\n";
586 3 $r .= " }\n";
587 }
588 3 $r .= "}\n";
589 3 ▶ if ($is_required) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_required) 0 3 3
590 0 ▶ $path = "array" if $path eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $path eq "" 0 0 0
591 0 $r .= "else {\n";
592 0 $r .= " push \@\$errors, \"$path is required\";\n";
593 0 $r .= "}\n";
594 }
595 3 return $r;
596 }
597  
598 8 1;
599  
600 __END__
« blib/lib/JSV/Compiler.pm ↑ Summary