blib/lib/JSON/SchemaValidator.pm

filter: Statementstmt 30.9% 142 / 459 Branchbran 17.5% 61 / 348 Conditioncond 26.6% 16 / 60 MC/DCmcdc 18.4% 7 / 38 Subroutinesub 65.7% 25 / 38 Podpod 0.0% 0 / 3 totaltotal 26.5% 251 / 946 CC 219 SCAR 57.8
CC
219
Cov
25%
_validate_object
1482.0
_validate_array
272.1
_resolve_refs
267.8
CRAP 322.6
↑ Summary blib/lib/JSON/SchemaValidator/Pointer.pm »
1 package JSON::SchemaValidator;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 our $VERSION = '1.03';
7  
8 3 ▶use B ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Storable ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 require Carp;
11 3 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 ▶use JSON::SchemaValidator::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use JSON::SchemaValidator::Pointer qw(pointer);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 3 my $DATETIME_RE = qr/
17 ^
18 [0-9]{4}\-[0-9]{2}\-[0-9]{2}T[0-9]{2}
19 :
20 [0-9]{2}:[0-9]{2}
21 (?:\.[0-9]{1,6})?
22 (?:
23 Z
24 |
25 [-+][0-9]{2}:[0-9]{2}
26 )
27 $
28 /ix;
29  
30 3 my $HOSTNAME_RE = qr/
31 (?:
32 (?:[a-z0-9]|[a-z0-9][a-z0-9\-]*[a-z0-9])\.
33 )*
34 (?:[a-z0-9]|[a-z0-9][a-z0-9\-]*[a-z0-9])
35 /ix;
36  
37 3 my $EMAIL_RE = qr/
38 [a-z0-9\._\%\+!\$\&\*=\^\|\~#%\{\}\/\-]+
39 \@
40 $HOSTNAME_RE
41 /ix;
42  
43 sub new {
44 1 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=5.7
Pod undocumented
45 1 my (%params) = @_;
46  
47 1 my $self = {};
48 1 bless $self, $class;
49  
50 $self->{formats} = {
51 hostname => sub {
52 0 ▶ my ($hostname) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
53  
54 0 ▶ return 0 if length $hostname > 255;
Branch: 1 branch
BranchTrueFalseTotal
if length $hostname > 255 0 0 0
55  
56 0 ▶ return 0 unless $hostname =~ qr/^$HOSTNAME_RE$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $hostname =~ qr/^$HOSTNAME_RE$/ 0 0 0
57  
58 0 ▶ return 0 if grep { length > 63 } split /\./, $hostname;
Branch: 1 branch
BranchTrueFalseTotal
if grep {length $_ > 63;} split(/\./, $hostname, 0) 0 0 0
59  
60 0 return 1;
61 },
62 email => sub {
63 0 ▶ my ($email) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
64  
65 0 ▶ return 0 unless $email =~ m/^$EMAIL_RE$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $email =~ /^$EMAIL_RE$/ 0 0 0
66  
67 0 my ($username, $hostname) = split /@/, $email;
68  
69 0 ▶ return 0 if length $hostname > 255;
Branch: 1 branch
BranchTrueFalseTotal
if length $hostname > 255 0 0 0
70 0 ▶ return 0 if grep { length > 63 } split /\./, $hostname;
Branch: 1 branch
BranchTrueFalseTotal
if grep {length $_ > 63;} split(/\./, $hostname, 0) 0 0 0
71  
72 0 return 1;
73 },
74 ipv4 => sub {
75 0 ▶ my ($ipv4) = @_;
Subroutine __ANON__: not called CC=8 SCAR=42.8
76  
77 0 my @parts = split m/\./, $ipv4;
78  
79 0 ▶ return unless @parts > 0 && @parts < 5;
Branch: 1 branch
BranchTrueFalseTotal
unless @parts > 0 and @parts < 5 0 0 0
Condition0%0/3
@parts > 0 and @parts < 5
!ll&&!rl&&r
000
MC/DC0%0/2
@parts > 0 and @parts < 5
@parts > 0 @parts < 5
Truth table0%0/3
A and B
A= @parts > 0B= @parts < 5
ABresult
0X0
100
111
80  
81 0 for my $part (@parts) {
82 0 ▶ return unless $part =~ m/^[0-9]+$/ && $part >= 0 && $part < 256;
Branch: 1 branch
BranchTrueFalseTotal
unless $part =~ /^[0-9]+$/ and $part >= 0 and $part < 256 0 0 0
Condition0%0/6
$part =~ /^[0-9]+$/ and $part >= 0 and $part < 256
!ll&&!rl&&r
000
$part =~ /^[0-9]+$/ and $part >= 0
!ll&&!rl&&r
000
MC/DC0%0/3
$part =~ /^[0-9]+$/ and $part >= 0 and $part < 256
$part =~ /^[0-9]+$/ $part >= 0 $part < 256
Truth table0%0/4
A and B and C
A= $part =~ /^[0-9]+$/B= $part >= 0C= $part < 256
ABCresult
0XX0
10X0
1100
1111
83 }
84  
85 0 ▶ return unless $parts[-1] > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $parts[-1] > 0 0 0 0
86  
87 0 return 1;
88 },
89 ipv6 => sub {
90 0 ▶ my ($ipv6) = @_;
Subroutine __ANON__: not called CC=6 SCAR=37.4
91  
92 0 my @parts = split m/\:/, $ipv6;
93  
94 0 ▶ return unless @parts > 0 && @parts < 9;
Branch: 1 branch
BranchTrueFalseTotal
unless @parts > 0 and @parts < 9 0 0 0
Condition0%0/3
@parts > 0 and @parts < 9
!ll&&!rl&&r
000
MC/DC0%0/2
@parts > 0 and @parts < 9
@parts > 0 @parts < 9
Truth table0%0/3
A and B
A= @parts > 0B= @parts < 9
ABresult
0X0
100
111
95  
96 0 for my $part (@parts) {
97 0 ▶ next if $part eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $part eq "" 0 0 0
98  
99 0 ▶ return unless $part =~ m/^[0-9a-f]{1,4}$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $part =~ /^[0-9a-f]{1,4}$/i 0 0 0
100 }
101  
102 0 return 1;
103 },
104 'date-time' => sub {
105 0 ▶ my ($date_time) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
106  
107 0 ▶ return unless $date_time =~ $DATETIME_RE;
Branch: 1 branch
BranchTrueFalseTotal
unless $date_time =~ /$DATETIME_RE/ 0 0 0
108  
109 0 $date_time =~ s{\.[0-9]*}{};
110 0 $date_time =~ s{Z$}{+00:00}i;
111 0 $date_time =~ s{:([0-9]+)$}{$1}i;
112  
113 0 ▶ return unless eval { Time::Piece->strptime(uc($date_time), '%Y-%m-%dT%T%z') };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { "Time::Piece"->strptime(uc $date_time, "%Y-%m-%dT%T%z") } } 0 0 0
114  
115 0 return 1;
116 }
117 1 };
118 1 $self->{fetcher} = $params{fetcher};
119  
120 1 return $self;
121 }
122  
123 0 ▶sub formats { shift->{formats} }
Subroutine formats: not called CC=1 SCAR=6.9
Pod undocumented
124  
125 sub validate {
126 1 ▶ my $self = shift;
Subroutine validate: called CC=1 SCAR=0.0
Pod undocumented
127 1 my ($json, $schema) = @_;
128  
129 1 $schema = Storable::dclone($schema);
130  
131 1 my $context = {
132 root => $schema,
133 ids => {},
134 pointer => '#',
135 };
136  
137 1 $self->_collect_ids($context, $schema);
138  
139 1 my $result = $self->_validate($context, $json, $schema);
140  
141 1 return $result;
142 }
143  
144 sub _collect_ids {
145 2 ▶ my $self = shift;
Subroutine _collect_ids: called CC=20 SCAR=54.5
146 2 my ($context, $schema) = @_;
147  
148 2 ▶ if (_is_object($schema)) {
Branch: 2 branches
BranchTrueFalseTotal
if (_is_object($schema)) { } 1 1 2
elsif (_is_array($schema)) { } 0 1 1
149 1 my $new_context = {%$context};
150  
151 1 ▶ if ($schema->{id} && _is_string($schema->{id})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"id"} and _is_string($schema->{"id"})) 0 1 1
Condition33%1/3
$schema->{"id"} and _is_string($schema->{"id"})
!ll&&!rl&&r
100
MC/DC0%0/2
$schema->{"id"} and _is_string($schema->{"id"})
$schema->{"id"} _is_string($schema->{"id"})
Truth table33%1/3
A and B
A= $schema->{"id"}B= _is_string($schema->{"id"})
ABresult
0X0
100
111
152 0 my $base_url = $context->{base_url};
153 0 my $path = $context->{path};
154  
155 0 my $id = $schema->{id};
156  
157 0 ▶ if ($id =~ m/^http/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id =~ /^http/) { } 0 0 0
158 0 ($base_url) = $id =~ m/^([^#]+)/;
159 0 $path = undef;
160  
161 0 ▶ if ($base_url !~ m{/$}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($base_url =~ m[/$]) 0 0 0
162 0 ($path) = $base_url =~ m{([^\/]+)$};
163 0 $base_url =~ s{[^\/]+$}{};
164 }
165  
166 0 $base_url =~ s{/$}{};
167 }
168 else {
169 0 ▶ if ($id !~ m/^#/) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $id =~ /^#/) { } 0 0 0
elsif ($path) { } 0 0 0
170 0 ▶ if ($id =~ m{/$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id =~ m[/$]) { } 0 0 0
171 0 $base_url .= "/$id";
172 0 $base_url =~ s{/$}{};
173 0 $path = undef;
174  
175 0 $id = "$base_url/";
176 }
177 else {
178 0 $path = $id;
179  
180 0 ▶ if ($base_url) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base_url) 0 0 0
181 0 $id = "$base_url/$id";
182 }
183 }
184 }
185 elsif ($path) {
186 0 $id = "$path$id";
187  
188 0 ▶ if ($base_url) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base_url) 0 0 0
189 0 $id = "$base_url/$id";
190 }
191 }
192 }
193  
194 0 $context->{ids}->{$id} = $schema;
195  
196 0 $new_context->{base_url} = $base_url;
197 0 $new_context->{path} = $path;
198 }
199  
200 1 ▶ if ($schema->{'$ref'} && _is_string($schema->{'$ref'})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"\$ref"} and _is_string($schema->{"\$ref"})) 0 1 1
Condition33%1/3
$schema->{"\$ref"} and _is_string($schema->{"\$ref"})
!ll&&!rl&&r
100
MC/DC0%0/2
$schema->{"\$ref"} and _is_string($schema->{"\$ref"})
$schema->{"\$ref"} _is_string($schema->{"\$ref"})
Truth table33%1/3
A and B
A= $schema->{"\$ref"}B= _is_string($schema->{"\$ref"})
ABresult
0X0
100
111
201 0 my $ref = $schema->{'$ref'};
202  
203 0 ▶ if ($ref !~ m/^http/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ref =~ /^http/) 0 0 0
204 0 ▶ if ($ref =~ m/^#/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref =~ /^#/) 0 0 0
205 0 ▶ if (my $path = $new_context->{path}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $path = $new_context->{"path"}) 0 0 0
206 0 $ref = "$path$ref";
207 }
208 }
209  
210 0 ▶ if (my $base_url = $new_context->{base_url}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $base_url = $new_context->{"base_url"}) 0 0 0
211 0 $ref = "$base_url/$ref";
212 }
213  
214 0 $schema->{'$ref'} = $ref;
215 }
216 }
217  
218 1 foreach my $key (keys %$schema) {
219 1 $self->_collect_ids($new_context, $schema->{$key});
220 }
221 }
222 elsif (_is_array($schema)) {
223 0 foreach my $el (@$schema) {
224 0 $self->_collect_ids($context, $el);
225 }
226 }
227 }
228  
229 sub _resolve_refs {
230 1 ▶ my $self = shift;
Subroutine _resolve_refs: called CC=19 SCAR=55.9
231 1 my ($context, $schema) = @_;
232  
233 1 ▶ if (_is_object($schema)) {
Branch: 2 branches
BranchTrueFalseTotal
if (_is_object($schema)) { } 1 0 1
elsif (_is_array($schema)) { } 0 0 0
234 1 ▶ if ($schema->{'$ref'} && _is_string($schema->{'$ref'})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"\$ref"} and _is_string($schema->{"\$ref"})) 0 1 1
Condition33%1/3
$schema->{"\$ref"} and _is_string($schema->{"\$ref"})
!ll&&!rl&&r
100
MC/DC0%0/2
$schema->{"\$ref"} and _is_string($schema->{"\$ref"})
$schema->{"\$ref"} _is_string($schema->{"\$ref"})
Truth table33%1/3
A and B
A= $schema->{"\$ref"}B= _is_string($schema->{"\$ref"})
ABresult
0X0
100
111
235 0 my $ref = delete $schema->{'$ref'};
236  
237 0 my $subschema;
238 0 ▶ if (exists $context->{ids}->{$ref}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $context->{'ids'}{$ref}) { } 0 0 0
239 0 $subschema = $context->{ids}->{$ref};
240 }
241 else {
242 0 ▶ if ($ref !~ m/^http/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ref =~ /^http/) 0 0 0
243 0 ▶ if ($ref =~ m/^#/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref =~ /^#/) 0 0 0
244 0 ▶ if ($context->{path}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context->{"path"}) 0 0 0
245 0 $ref = "$context->{path}/$ref";
246 }
247 }
248  
249 0 ▶ if ($context->{base_url}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context->{"base_url"}) 0 0 0
250 0 $ref = "$context->{base_url}/$ref";
251 }
252 }
253  
254 0 ▶ if (exists $context->{ids}->{$ref}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $context->{'ids'}{$ref}) { } 0 0 0
elsif ($ref =~ /^http/) { } 0 0 0
255 0 $subschema = $context->{ids}->{$ref};
256 }
257 elsif ($ref =~ m/^http/) {
258 0 $subschema = $self->_resolve_remote_ref($context, $ref);
259 }
260 else {
261 0 $subschema = pointer($context->{root}, $ref);
262 }
263 }
264  
265 0 ▶ if ($subschema) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subschema) 0 0 0
266 0 for my $key (keys %$schema) {
267 0 ▶ next if $key eq 'definitions';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "definitions" 0 0 0
268  
269 0 delete $schema->{$key};
270 }
271  
272 0 foreach my $key (keys %$subschema) {
273 0 ▶ next if $key eq 'id';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "id" 0 0 0
274  
275 0 $schema->{$key} = $subschema->{$key};
276 }
277  
278 0 ▶ if ($schema->{'$ref'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"\$ref"}) 0 0 0
279 0 $self->_resolve_refs($context, $schema);
280 }
281 }
282 }
283 }
284 elsif (_is_array($schema)) {
285 0 foreach my $el (@$schema) {
286 0 $self->_resolve_refs($context, $el);
287 }
288 }
289 }
290  
291 sub _validate {
292 1 ▶ my $self = shift;
Subroutine _validate: called CC=14 SCAR=39.7
293 1 my ($context, $json, $schema) = @_;
294  
295 1 my $pointer = $context->{pointer};
296  
297 1 my $result = $self->_build_result;
298  
299 1 $self->_resolve_refs($context, $schema);
300  
301 1 ▶ if (_is_object($schema)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_object($schema)) 1 0 1
302 1 ▶ if (my $types = $schema->{type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $types = $schema->{"type"}) 1 0 1
303 1 my $subresult = $self->_validate_type($context, $json, $types);
304 1 $result->merge($subresult);
305 }
306  
307 1 ▶ if (my $enum = $schema->{enum}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $enum = $schema->{"enum"}) 0 1 1
308 0 my $subresult = $self->_validate_enum($context, $json, $enum);
309 0 $result->merge($subresult);
310 }
311  
312 1 ▶ if (exists $schema->{const}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"const"}) 0 1 1
313 0 my $subresult = $self->_validate_const($context, $json, $schema->{const});
314 0 $result->merge($subresult);
315 }
316 }
317  
318 1 ▶ if (_is_object($json)) {
Branch: 4 branches
BranchTrueFalseTotal
if (_is_object($json)) { } 0 1 1
elsif (_is_array($json)) { } 1 0 1
elsif (_is_number($json)) { } 0 0 0
elsif (_is_string($json)) { } 0 0 0
319 0 my $subresult = $self->_validate_object($context, $json, $schema);
320 0 $result->merge($subresult);
321 }
322 elsif (_is_array($json)) {
323 1 my $subresult = $self->_validate_array($context, $json, $schema);
324 1 $result->merge($subresult);
325 }
326 elsif (_is_number($json)) {
327 0 my $subresult = $self->_validate_number($context, $json, $schema);
328 0 $result->merge($subresult);
329 }
330 elsif (_is_string($json)) {
331 0 my $subresult = $self->_validate_string($context, $json, $schema);
332 0 $result->merge($subresult);
333 }
334  
335 1 ▶ if (my $subschema_type = _subschema($schema)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $subschema_type = _subschema($schema)) 0 1 1
336 0 $self->_resolve_refs($context, $schema->{$subschema_type});
337  
338 0 my $subresult = $self->_validate_subschemas($context, $json, $subschema_type, $schema->{$subschema_type});
339 0 $result->merge($subresult);
340 }
341  
342 1 ▶ if (_is_string($json)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_string($json)) 0 1 1
343 0 ▶ if (my $format = $schema->{format}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $format = $schema->{"format"}) 0 0 0
344 0 ▶ if (my $cb = $self->{formats}->{$format}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $self->{"formats"}{$format}) 0 0 0
345 0 ▶ if (!$cb->($json)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&$cb($json)) 0 0 0
346 0 $result->add_error(
347 uri => $pointer,
348 message => 'Must be of format ' . $format,
349 attribute => 'format',
350 details => [$format]
351 );
352 }
353 }
354 }
355 }
356  
357 1 return $result;
358 }
359  
360 sub _validate_type {
361 1 ▶ my $self = shift;
Subroutine _validate_type: called CC=8 SCAR=24.1
362 1 my ($context, $json, $types) = @_;
363  
364 1 my $result = $self->_build_result;
365  
366 1 ▶ $types = [$types] unless ref $types eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $types eq "ARRAY" 1 0 1
367  
368 1 my @results;
369 1 foreach my $type (@$types) {
370 1 ▶ if (_is_object($type)) {
Branch: 2 branches
BranchTrueFalseTotal
if (_is_object($type)) { } 0 1 1
elsif (not _is_type($json, $type)) { } 1 0 1
371 0 my $subresult = $self->_validate($context, $json, $type);
372 0 push @results, $subresult;
373 }
374 elsif (!_is_type($json, $type)) {
375 push @results,
376 $self->_build_result->add_error(
377 uri => $context->{pointer},
378 1 message => 'Must be of type ' . $type,
379 attribute => 'type',
380 details => [$type]
381 );
382 }
383 else {
384 0 push @results, $self->_build_result;
385 }
386 }
387  
388 1 ▶ if (@results && !grep { $_->is_success } @results) {
Branch: 1 branch
BranchTrueFalseTotal
if (@results and not grep({$_->is_success;} @results)) 1 0 1
Condition33%1/3
@results and not grep({$_->is_success;} @results)
!ll&&!rl&&r
001
MC/DC0%0/2
@results and not grep({$_->is_success;} @results)
@results not grep({$_->is_success;} @results)
Truth table33%1/3
A and not(B)
A= @resultsB= not grep({$_->is_success;} @results)
ABresult
0X0
100
111
389 1 ▶ if (@results == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@results == 1) { } 1 0 1
390 1 $result->merge($results[0]);
391 }
392 else {
393 $result->add_error(
394 uri => $context->{pointer},
395 0 message => "Must be one of",
396 attribute => 'type',
397 );
398 }
399 }
400  
401 1 return $result;
402 }
403  
404 sub _validate_subschemas {
405 0 ▶ my $self = shift;
Subroutine _validate_subschemas: not called CC=11 SCAR=48.8
406 0 my ($context, $json, $type, $subschemas) = @_;
407  
408 0 my $result = $self->_build_result;
409  
410 0 ▶ $subschemas = [$subschemas] unless ref $subschemas eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $subschemas eq "ARRAY" 0 0 0
411  
412 0 my @subresults;
413 0 foreach my $subschema (@$subschemas) {
414 0 my $subresult = $self->_validate($context, $json, $subschema);
415  
416 0 push @subresults, $subresult;
417 }
418  
419 0 my @valid = grep { $_->is_success } @subresults;
420  
421 0 ▶ if ($type eq 'allOf') {
Branch: 4 branches
BranchTrueFalseTotal
if ($type eq 'allOf') { } 0 0 0
elsif ($type eq 'anyOf') { } 0 0 0
elsif ($type eq 'oneOf') { } 0 0 0
elsif ($type eq 'not') { } 0 0 0
422 0 ▶ if (@valid != @subresults) {
Branch: 1 branch
BranchTrueFalseTotal
if (@valid != @subresults) 0 0 0
423 $result->add_error(
424 uri => $context->{pointer},
425 0 message => "Must be all of",
426 attribute => 'allOf',
427 );
428 }
429 }
430 elsif ($type eq 'anyOf') {
431 0 ▶ if (!@valid) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@valid) 0 0 0
432 $result->add_error(
433 uri => $context->{pointer},
434 0 message => "Must be any of",
435 attribute => 'anyOf',
436 );
437 }
438 }
439 elsif ($type eq 'oneOf') {
440 0 ▶ if (@valid != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@valid != 1) 0 0 0
441 $result->add_error(
442 uri => $context->{pointer},
443 0 message => "Must be one of",
444 attribute => 'oneOf',
445 );
446 }
447 }
448 elsif ($type eq 'not') {
449 0 ▶ if (@valid) {
Branch: 1 branch
BranchTrueFalseTotal
if (@valid) 0 0 0
450 $result->add_error(
451 uri => $context->{pointer},
452 0 message => "Must not be of",
453 attribute => 'not',
454 );
455 }
456 }
457  
458 0 return $result;
459 }
460  
461 sub _validate_object {
462 0 ▶ my $self = shift;
Subroutine _validate_object: not called CC=38 SCAR=73.0
463 0 my ($context, $json, $schema) = @_;
464  
465 0 $schema = Storable::dclone($schema);
466  
467 0 my $result = $self->_build_result(root => $context->{pointer});
468  
469 0 ▶ my @required = exists $schema->{required} ? @{$schema->{required}} : ();
Branch: 1 branch
BranchTrueFalseTotal
exists $schema->{'required'} ? : 0 0 0
470  
471 0 ▶ if (exists $schema->{properties}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"properties"}) 0 0 0
472 0 foreach my $key (keys %{$schema->{properties}}) {
473  
474 # Required only if a boolean, otherwise it's a list of required properties
475 0 ▶ if (exists $schema->{properties}->{$key}->{required}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"properties"}{$key}{"required"} and _is_boolean($schema->{"properties"}{$key}{"required"})) 0 0 0
Condition0%0/3
exists $schema->{"properties"}{$key}{"required"} and _is_boolean($schema->{"properties"}{$key}{"required"})
!ll&&!rl&&r
000
MC/DC0%0/2
exists $schema->{"properties"}{$key}{"required"} and _is_boolean($schema->{"properties"}{$key}{"required"})
exists $schema->{"properties"}{$key}{"required"} _is_boolean($schema->{"properties"}{$key}{"required"})
Truth table0%0/3
A and B
A= exists $schema->{"properties"}{$key}{"required"}B= _is_boolean($schema->{"properties"}{$key}{"required"})
ABresult
0X0
100
111
476 && _is_boolean($schema->{properties}->{$key}->{required}))
477 {
478 0 push @required, $key;
479 }
480 }
481 }
482  
483 0 ▶ if (exists $schema->{dependencies}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"dependencies"}) 0 0 0
484 0 foreach my $dependency (keys %{$schema->{dependencies}}) {
485 0 ▶ next unless exists $json->{$dependency};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $json->{$dependency} 0 0 0
486  
487 0 ▶ if (_is_array($schema->{dependencies}->{$dependency})) {
Branch: 2 branches
BranchTrueFalseTotal
if (_is_array($schema->{'dependencies'}{$dependency})) { } 0 0 0
elsif (_is_object($schema->{'dependencies'}{$dependency})) { } 0 0 0
488 0 push @required, @{$schema->{dependencies}->{$dependency}};
489 }
490 elsif (_is_object($schema->{dependencies}->{$dependency})) {
491 0 my $dependency_schema = $schema->{dependencies}->{$dependency};
492  
493 0 foreach my $key (keys %$dependency_schema) {
494 0 ▶ if ($key eq 'required') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq 'required') { } 0 0 0
495 0 push @required, @{$dependency_schema->{$key}};
496 }
497 else {
498 0 $schema->{$key} = $dependency_schema->{$key};
499 }
500 }
501 }
502 }
503 }
504  
505 0 ▶ if (defined(my $min_properties = $schema->{minProperties})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $min_properties = $schema->{"minProperties"})) 0 0 0
506 0 ▶ if (keys %$json < $min_properties) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$json < $min_properties) 0 0 0
507 $result->add_error(
508 uri => $context->{pointer},
509 0 message => "Must have minimum " . $min_properties . ' property(ies)',
510 attribute => 'minProperties',
511 details => [$min_properties]
512 );
513 }
514 }
515  
516 0 ▶ if (defined(my $max_properties = $schema->{maxProperties})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $max_properties = $schema->{"maxProperties"})) 0 0 0
517 0 ▶ if (keys %$json > $max_properties) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$json > $max_properties) 0 0 0
518 $result->add_error(
519 uri => $context->{pointer},
520 0 message => "Must have maximum " . $max_properties . ' property(ies)',
521 attribute => 'maxProperties',
522 details => [$max_properties]
523 );
524 }
525 }
526  
527 0 ▶ if (@required) {
Branch: 1 branch
BranchTrueFalseTotal
if (@required) 0 0 0
528 0 foreach my $name (@required) {
529 0 ▶ if (!exists $json->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $json->{$name}) 0 0 0
530 0 $result->add_error(
531 uri => "$context->{pointer}/$name",
532 message => 'Required',
533 attribute => 'required',
534 details => ['(true)']
535 );
536 }
537 }
538 }
539  
540 0 my @additional_properties = grep { !exists $schema->{properties}->{$_} } keys %$json;
541  
542 0 ▶ if (exists $schema->{additionalProperties}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"additionalProperties"}) 0 0 0
543 0 ▶ if (_is_boolean($schema->{additionalProperties}) && !$schema->{additionalProperties}) {
Branch: 2 branches
BranchTrueFalseTotal
if (_is_boolean($schema->{'additionalProperties'}) and not $schema->{'additionalProperties'}) { } 0 0 0
elsif (_is_object($schema->{'additionalProperties'})) { } 0 0 0
Condition0%0/3
_is_boolean($schema->{"additionalProperties"}) and not $schema->{"additionalProperties"}
!ll&&!rl&&r
000
MC/DC0%0/2
_is_boolean($schema->{"additionalProperties"}) and not $schema->{"additionalProperties"}
_is_boolean($schema->{"additionalProperties"}) not $schema->{"additionalProperties"}
Truth table0%0/3
A and not(B)
A= _is_boolean($schema->{"additionalProperties"})B= not $schema->{"additionalProperties"}
ABresult
0X0
100
111
544 0 PROPERTY: foreach my $additional_property (@additional_properties) {
545 0 ▶ if (my $pattern_properties = $schema->{patternProperties}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pattern_properties = $schema->{"patternProperties"}) 0 0 0
546 0 foreach my $pattern_property (keys %$pattern_properties) {
547 0 ▶ next PROPERTY if $additional_property =~ m/$pattern_property/;
Branch: 1 branch
BranchTrueFalseTotal
if $additional_property =~ /$pattern_property/ 0 0 0
548 }
549 }
550  
551 $result->add_error(
552 0 uri => "$context->{pointer}/$additional_property",
553 message => 'Unknown property',
554 );
555 }
556 }
557 elsif (_is_object($schema->{additionalProperties})) {
558 0 ADDITIONAL_PROPERTY: foreach my $additional_property (@additional_properties) {
559  
560 # patternProperties overwrite additionalProperties
561 0 ▶ if (my $pattern_properties = $schema->{patternProperties}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pattern_properties = $schema->{"patternProperties"}) 0 0 0
562 0 foreach my $pattern_property (keys %$pattern_properties) {
563 0 ▶ next ADDITIONAL_PROPERTY if $additional_property =~ m/$pattern_property/;
Branch: 1 branch
BranchTrueFalseTotal
if $additional_property =~ /$pattern_property/ 0 0 0
564 }
565 }
566  
567 my $subresult = $self->_validate(
568 {%$context, pointer => "$context->{pointer}/$additional_property"},
569 $json->{$additional_property},
570 $schema->{additionalProperties}
571 0 );
572 0 $result->merge($subresult);
573 }
574 }
575 }
576  
577 0 ▶ if (my $properties = $schema->{properties}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $properties = $schema->{"properties"}) 0 0 0
578 0 foreach my $name (keys %$properties) {
579 0 ▶ if (exists $json->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $json->{$name}) 0 0 0
580 my $subresult = $self->_validate({%$context, pointer => "$context->{pointer}/$name"},
581 0 $json->{$name}, $properties->{$name});
582 0 $result->merge($subresult);
583 }
584 }
585 }
586  
587 0 ▶ if (_is_object($schema->{patternProperties})) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_object($schema->{"patternProperties"})) 0 0 0
588 0 foreach my $pattern_property (keys %{$schema->{patternProperties}}) {
589 0 my @matched_properties = grep { m/$pattern_property/ } keys %$json;
590  
591 0 foreach my $property (@matched_properties) {
592 my $subresult = $self->_validate({%$context, pointer => "$context->{pointer}/$property"},
593 0 $json->{$property}, $schema->{patternProperties}->{$pattern_property});
594 0 $result->merge($subresult);
595 }
596 }
597 }
598  
599 0 return $result;
600 }
601  
602 sub _validate_array {
603 1 ▶ my $self = shift;
Subroutine _validate_array: called CC=22 SCAR=56.1
604 1 my ($context, $json, $schema) = @_;
605  
606 1 my $result = $self->_build_result(root => $context->{pointer});
607  
608 1 ▶ if (defined(my $min_items = $schema->{minItems})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $min_items = $schema->{"minItems"})) 0 1 1
609 0 ▶ if (@$json < $min_items) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$json < $min_items) 0 0 0
610 $result->add_error(
611 uri => $context->{pointer},
612 0 message => "Must have minimum " . $min_items . ' item(s)',
613 attribute => 'minItems',
614 details => [$min_items],
615 );
616 }
617 }
618  
619 1 ▶ if (defined(my $max_items = $schema->{maxItems})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $max_items = $schema->{"maxItems"})) 0 1 1
620 0 ▶ if (@$json > $max_items) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$json > $max_items) 0 0 0
621 $result->add_error(
622 uri => $context->{pointer},
623 0 message => "Must have maximum " . $max_items . ' item(s)',
624 attribute => 'maxItems',
625 details => [$max_items],
626 );
627 }
628 }
629  
630 1 ▶ if (_is_array($schema->{items})) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_array($schema->{"items"})) 0 1 1
631 0 my $exp_length = @{$schema->{items}};
632 0 my $got_length = @$json;
633  
634 0 ▶ for (my $i = 0; $i < @{$schema->{items}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$schema->{"items"};}) 0 0 0
635 0 ▶ last if @$json < $i + 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$json < $i + 1 0 0 0
636  
637 my $subresult =
638 0 $self->_validate({%$context, pointer => "$context->{pointer}\[$i]"}, $json->[$i], $schema->{items}->[$i]);
639 0 $result->merge($subresult);
640 }
641  
642 0 ▶ if ($got_length > $exp_length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($got_length > $exp_length) 0 0 0
643 0 ▶ if (_is_boolean($schema->{additionalItems})) {
Branch: 2 branches
BranchTrueFalseTotal
if (_is_boolean($schema->{'additionalItems'})) { } 0 0 0
elsif (_is_object($schema->{'additionalItems'})) { } 0 0 0
644 0 ▶ if (!$schema->{additionalItems}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema->{"additionalItems"}) 0 0 0
645 $result->add_error(
646 uri => $context->{pointer},
647 0 message => "Must have exactly " . @{$schema->{items}} . ' item(s)',
648 attribute => 'additionalItems',
649 0 details => [scalar @{$schema->{items}}]
650  
651 );
652 }
653 }
654 elsif (_is_object($schema->{additionalItems})) {
655 0 for ($exp_length .. $got_length - 1) {
656 my $subresult = $self->_validate({%$context, pointer => "$context->{pointer}\[$_]"},
657 0 $json->[$_], $schema->{additionalItems});
658 0 $result->merge($subresult);
659 }
660 }
661 }
662 }
663  
664 1 ▶ if (_is_object($schema->{items})) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_object($schema->{"items"})) 0 1 1
665 0 ▶ for (my $i = 0; $i < @$json; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$json) 0 0 0
666 my $subresult =
667 0 $self->_validate({%$context, pointer => "$context->{pointer}/$i"}, $json->[$i], $schema->{items});
668 0 $result->merge($subresult);
669 }
670 }
671  
672 1 ▶ if ($schema->{uniqueItems}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"uniqueItems"}) 0 1 1
673 0 my $seen = {};
674 0 foreach my $el (@$json) {
675 0 ▶ my $hash = ref $el ? JSON::encode_json($el) : defined $el ? $el : 'null';
Branch: 2 branches
BranchTrueFalseTotal
ref $el ? : 0 0 0
defined $el ? : 0 0 0
676  
677 0 ▶ if (exists $seen->{$hash}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $seen->{$hash}) 0 0 0
678 $result->add_error(
679 uri => $context->{pointer},
680 0 message => "Must have unique items",
681 attribute => 'uniqueItems',
682 details => ['(true)']
683 );
684 0 last;
685 }
686 0 $seen->{$hash}++;
687 }
688 }
689  
690 1 ▶ if ($schema->{contains}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"contains"}) 0 1 1
691 0 ▶ if (!@$json) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$json) 0 0 0
692 $result->add_error(
693 uri => $context->{pointer},
694 0 message => "Must not be empty",
695 attribute => 'contains'
696 );
697 }
698 }
699  
700 1 return $result;
701 }
702  
703 sub _validate_string {
704 0 ▶ my $self = shift;
Subroutine _validate_string: not called CC=7 SCAR=40.3
705 0 my ($context, $json, $schema) = @_;
706  
707 0 my $result = $self->_build_result(pointer => $context->{pointer});
708  
709 0 ▶ if (defined(my $max_length = $schema->{maxLength})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $max_length = $schema->{"maxLength"})) 0 0 0
710 0 ▶ if (length($json) > $max_length) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $json > $max_length) 0 0 0
711 $result->add_error(
712 uri => $context->{pointer},
713 0 message => "Must have the maximum length of $max_length",
714 attribute => 'maxLength',
715 details => [$max_length]
716 );
717 }
718 }
719  
720 0 ▶ if (defined(my $min_length = $schema->{minLength})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $min_length = $schema->{"minLength"})) 0 0 0
721 0 ▶ if (length($json) < $min_length) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $json < $min_length) 0 0 0
722 $result->add_error(
723 uri => $context->{pointer},
724 0 message => "Must have the minimum length of $min_length",
725 attribute => 'minLength',
726 details => [$min_length]
727 );
728 }
729 }
730  
731 0 ▶ if (my $pattern = $schema->{pattern}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pattern = $schema->{"pattern"}) 0 0 0
732 0 ▶ if ($json !~ m/$pattern/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($json =~ /$pattern/) 0 0 0
733 $result->add_error(
734 uri => $context->{pointer},
735 0 message => "Must match pattern $pattern",
736 attribute => 'pattern',
737 details => ["$pattern"]
738 );
739 }
740 }
741  
742 0 return $result;
743 }
744  
745 sub _validate_number {
746 0 ▶ my $self = shift;
Subroutine _validate_number: not called CC=15 SCAR=54.8
747 0 my ($context, $json, $schema) = @_;
748  
749 0 my $result = $self->_build_result(pointer => $context->{pointer});
750  
751 0 ▶ if (defined(my $minimum = $schema->{minimum})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $minimum = $schema->{"minimum"})) 0 0 0
752 0 ▶ if ($schema->{exclusiveMinimum}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{'exclusiveMinimum'}) { } 0 0 0
753 0 ▶ if ($json <= $minimum) {
Branch: 1 branch
BranchTrueFalseTotal
if ($json <= $minimum) 0 0 0
754 $result->add_error(
755 uri => $context->{pointer},
756 0 message => "Must be greater than or equals to $minimum",
757 attribute => 'minimum',
758 details => [$minimum]
759 );
760 }
761 }
762 else {
763 0 ▶ if ($json < $minimum) {
Branch: 1 branch
BranchTrueFalseTotal
if ($json < $minimum) 0 0 0
764 $result->add_error(
765 uri => $context->{pointer},
766 0 message => "Must be greater than $minimum",
767 attribute => 'minimum',
768 details => [$minimum]
769 );
770 }
771 }
772 }
773  
774 0 ▶ if (_is_number($schema->{exclusiveMaximum})) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_number($schema->{"exclusiveMaximum"})) 0 0 0
775 0 my $maximum = $schema->{exclusiveMaximum};
776  
777 0 ▶ if ($json >= $maximum) {
Branch: 1 branch
BranchTrueFalseTotal
if ($json >= $maximum) 0 0 0
778 $result->add_error(
779 uri => $context->{pointer},
780 0 message => "Must be less than or equals to $maximum",
781 attribute => 'maximum',
782 details => [$maximum]
783 );
784 }
785 }
786  
787 0 ▶ if (defined(my $maximum = $schema->{maximum})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $maximum = $schema->{"maximum"})) 0 0 0
788 0 ▶ if ($schema->{exclusiveMaximum}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{'exclusiveMaximum'}) { } 0 0 0
789 0 ▶ if ($json >= $maximum) {
Branch: 1 branch
BranchTrueFalseTotal
if ($json >= $maximum) 0 0 0
790 $result->add_error(
791 uri => $context->{pointer},
792 0 message => "Must be less than or equals to $maximum",
793 attribute => 'maximum',
794 details => [$maximum]
795 );
796 }
797 }
798 else {
799 0 ▶ if ($json > $maximum) {
Branch: 1 branch
BranchTrueFalseTotal
if ($json > $maximum) 0 0 0
800 $result->add_error(
801 uri => $context->{pointer},
802 0 message => "Must be less than $maximum",
803 attribute => 'maximum',
804 details => [$maximum]
805 );
806 }
807 }
808 }
809  
810 0 ▶ if (defined(my $divisibleBy = $schema->{divisibleBy})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $divisibleBy = $schema->{"divisibleBy"})) 0 0 0
811 0 ▶ if (sprintf('%0.6f', $json) ne sprintf('%0.6f', int($json / $divisibleBy) * $divisibleBy)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sprintf("%0.6f", $json) ne sprintf("%0.6f", int($json / $divisibleBy) * $divisibleBy)) 0 0 0
812 $result->add_error(
813 uri => $context->{pointer},
814 0 message => "Must be divisible by $divisibleBy",
815 attribute => 'divisibleBy',
816 details => [$divisibleBy]
817 );
818 }
819 }
820  
821 0 ▶ if (defined(my $multipleOf = $schema->{multipleOf})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $multipleOf = $schema->{"multipleOf"})) 0 0 0
822 0 ▶ if (sprintf('%0.6f', $json) ne sprintf('%0.6f', int($json / $multipleOf) * $multipleOf)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sprintf("%0.6f", $json) ne sprintf("%0.6f", int($json / $multipleOf) * $multipleOf)) 0 0 0
823 $result->add_error(
824 uri => $context->{pointer},
825 0 message => "Must be multiple of by $multipleOf",
826 attribute => 'multipleOf',
827 details => [$multipleOf]
828 );
829 }
830 }
831  
832 0 return $result;
833 }
834  
835 sub _validate_enum {
836 0 ▶ my $self = shift;
Subroutine _validate_enum: not called CC=6 SCAR=37.4
837 0 my ($context, $json, $enum) = @_;
838  
839 0 my $result = $self->_build_result(pointer => $context->{pointer});
840  
841 0 my $set = {};
842 0 foreach my $el (@$enum) {
843 0 ▶ my $hash = ref $el ? JSON::encode_json($el) : $el;
Branch: 1 branch
BranchTrueFalseTotal
ref $el ? : 0 0 0
844 0 $set->{$hash} = 1;
845 }
846  
847 0 ▶ my $hash = ref $json ? JSON::encode_json($json) : defined $json ? $json : 'null';
Branch: 2 branches
BranchTrueFalseTotal
ref $json ? : 0 0 0
defined $json ? : 0 0 0
848  
849 0 ▶ if (!exists $set->{$hash}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $set->{$hash}) 0 0 0
850 $result->add_error(
851 uri => $context->{pointer},
852 0 message => "Must be one of",
853 attribute => 'enum',
854 details => [@$enum]
855 );
856 }
857  
858 0 return $result;
859 }
860  
861 sub _validate_const {
862 0 ▶ my $self = shift;
Subroutine _validate_const: not called CC=13 SCAR=52.0
863 0 my ($context, $json, $const) = @_;
864  
865 0 my $result = $self->_build_result();
866  
867 0 my $exp_type = _type($const);
868  
869 0 ▶ if (_is_type($json, $exp_type) || ($exp_type eq 'integer' && _type($json) eq 'number')) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_type($json, $exp_type) or $exp_type eq 'integer' and _type($json) eq 'number') { } 0 0 0
Condition0%0/6
_is_type($json, $exp_type) or $exp_type eq "integer" and _type($json) eq "number"
l!l&&r!l&&!r
000
$exp_type eq "integer" and _type($json) eq "number"
!ll&&!rl&&r
000
MC/DC0%0/3
_is_type($json, $exp_type) or $exp_type eq "integer" and _type($json) eq "number"
_is_type($json, $exp_type) $exp_type eq "integer" _type($json) eq "number"
Truth table0%0/4
A or B and C
A= _is_type($json, $exp_type)B= $exp_type eq "integer"C= _type($json) eq "number"
ABCresult
1XX1
0111
00X0
0100
870 0 ▶ if (_is_object($json) || _is_array($json)) {
Branch: 4 branches
BranchTrueFalseTotal
if (_is_object($json) or _is_array($json)) { } 0 0 0
elsif (_is_number($json)) { } 0 0 0
elsif (_is_string($json)) { } 0 0 0
elsif (_is_boolean($json)) { } 0 0 0
Condition0%0/3
_is_object($json) or _is_array($json)
l!l&&r!l&&!r
000
MC/DC0%0/2
_is_object($json) or _is_array($json)
_is_object($json) _is_array($json)
Truth table0%0/3
A or B
A= _is_object($json)B= _is_array($json)
ABresult
1X1
011
000
871 0 ▶ if (JSON->new->utf8->canonical->encode($json) ne JSON->new->utf8->canonical->encode($const)) {
Branch: 1 branch
BranchTrueFalseTotal
if ("JSON"->new->utf8->canonical->encode($json) ne "JSON"->new->utf8->canonical->encode($const)) 0 0 0
872 $result->add_error(
873 uri => $context->{pointer},
874 0 message => "Must be equal to const",
875 attribute => 'const',
876 );
877 }
878 }
879 elsif (_is_number($json)) {
880 0 ▶ if (sprintf('%0.6f', $const) ne sprintf('%0.6f', $json)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sprintf("%0.6f", $const) ne sprintf("%0.6f", $json)) 0 0 0
881 $result->add_error(
882 uri => $context->{pointer},
883 0 message => "Must be of equal to $const",
884 attribute => 'const',
885 details => [$const]
886 );
887 }
888 }
889 elsif (_is_string($json)) {
890 0 ▶ if ($json ne $const) {
Branch: 1 branch
BranchTrueFalseTotal
if ($json ne $const) 0 0 0
891 $result->add_error(
892 uri => $context->{pointer},
893 0 message => "Must be of equal to $const",
894 attribute => 'const',
895 details => [$const]
896 );
897 }
898 }
899 elsif (_is_boolean($json)) {
900 0 ▶ if ($const != $json) {
Branch: 1 branch
BranchTrueFalseTotal
if ($const != $json) 0 0 0
901 $result->add_error(
902 uri => $context->{pointer},
903 0 message => "Must be of equal to $const",
904 attribute => 'const',
905 details => [$const]
906 );
907 }
908 }
909 }
910 else {
911 $result->add_error(
912 uri => $context->{pointer},
913 0 message => "Must be of type $exp_type",
914 attribute => 'const',
915 details => [$exp_type]
916 );
917 }
918  
919 0 return $result;
920 }
921  
922 sub _is_object {
923 23 ▶ my ($value) = @_;
Subroutine _is_object: called CC=2 SCAR=6.9
924  
925 23 ▶ return defined $value && ref $value eq 'HASH';
Condition100%3/3
defined $value && ref $value eq 'HASH'
!ll&&!rl&&r
1175
MC/DC100%2/2
defined $value && ref $value eq 'HASH'
defined $value ref $value eq 'HASH'
Truth table100%3/3
A && B
A= defined $valueB= ref $value eq 'HASH'
ABresult
0X0
100
111
926 }
927  
928 sub _is_array {
929 17 ▶ my ($value) = @_;
Subroutine _is_array: called CC=2 SCAR=6.9
930  
931 17 ▶ return defined $value && ref $value eq 'ARRAY';
Condition100%3/3
defined $value && ref $value eq 'ARRAY'
!ll&&!rl&&r
1124
MC/DC100%2/2
defined $value && ref $value eq 'ARRAY'
defined $value ref $value eq 'ARRAY'
Truth table100%3/3
A && B
A= defined $valueB= ref $value eq 'ARRAY'
ABresult
0X0
100
111
932 }
933  
934 sub _is_boolean {
935 13 ▶ my ($value) = @_;
Subroutine _is_boolean: called CC=2 SCAR=7.1
936  
937 13 ▶ return defined $value && JSON::is_bool($value);
Condition66%2/3
defined $value && &JSON::is_bool($value)
!ll&&!rl&&r
0112
MC/DC50%1/2
defined $value && &JSON::is_bool($value)
defined $value &JSON::is_bool($value)
Truth table66%2/3
A && B
A= defined $valueB= &JSON::is_bool($value)
ABresult
0X0
100
111
938 }
939  
940 sub _is_number {
941 11 ▶ my ($value) = @_;
Subroutine _is_number: called CC=6 SCAR=18.5
942  
943 11 ▶ return 0 unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 11 11
944 11 ▶ return 0 if ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 0 11 11
945 11 ▶ return 0 if JSON::is_bool($value);
Branch: 1 branch
BranchTrueFalseTotal
if &JSON::is_bool($value) 0 11 11
946  
947 11 my $b_obj = B::svref_2object(\$value);
948 11 my $flags = $b_obj->FLAGS;
949 11 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if $flags & 12288 and not $flags & 16384 7 4 11
Condition66%2/3
$flags & 12288 and not $flags & 16384
!ll&&!rl&&r
407
MC/DC50%1/2
$flags & 12288 and not $flags & 16384
$flags & 12288 not $flags & 16384
Truth table66%2/3
A and not(B)
A= $flags & 12288B= not $flags & 16384
ABresult
0X0
100
111
950 if $flags & (B::SVp_IOK() | B::SVp_NOK())
951 && !($flags & B::SVp_POK());
952  
953 4 return 0;
954 }
955  
956 sub _is_integer {
957 10 ▶ my ($value) = @_;
Subroutine _is_integer: called CC=6 SCAR=18.5
958  
959 10 ▶ return 0 unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 10 10
960 10 ▶ return 0 if ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 0 10 10
961 10 ▶ return 0 if JSON::is_bool($value);
Branch: 1 branch
BranchTrueFalseTotal
if &JSON::is_bool($value) 0 10 10
962  
963 10 my $b_obj = B::svref_2object(\$value);
964 10 my $flags = $b_obj->FLAGS;
965 10 ▶ return 1 if ($flags & B::SVp_IOK()) && !($flags & B::SVp_POK());
Branch: 1 branch
BranchTrueFalseTotal
if $flags & 4096 and not $flags & 16384 4 6 10
Condition66%2/3
$flags & 4096 and not $flags & 16384
!ll&&!rl&&r
604
MC/DC50%1/2
$flags & 4096 and not $flags & 16384
$flags & 4096 not $flags & 16384
Truth table66%2/3
A and not(B)
A= $flags & 4096B= not $flags & 16384
ABresult
0X0
100
111
966  
967 6 return 0;
968 }
969  
970 sub _is_string {
971 3 ▶ my ($value) = @_;
Subroutine _is_string: called CC=5 SCAR=16.6
972  
973 3 ▶ return 0 unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 3 3
974 3 ▶ return 0 if ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 1 2 3
975 2 ▶ return 0 if _is_boolean($value);
Branch: 1 branch
BranchTrueFalseTotal
if _is_boolean($value) 0 2 2
976 2 ▶ return 0 if _is_number($value);
Branch: 1 branch
BranchTrueFalseTotal
if _is_number($value) 0 2 2
977  
978 2 return 1;
979 }
980  
981 sub _is_null {
982 19 ▶ my ($value) = @_;
Subroutine _is_null: called CC=2 SCAR=6.9
983  
984 19 ▶ return defined $value ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 16 3 19
985 }
986  
987 sub _is_type {
988 10 ▶ my ($value, $type) = @_;
Subroutine _is_type: called CC=4 SCAR=14.5
989  
990 10 my $real_type = _type($value);
991  
992 10 ▶ if ($type eq 'number') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "number") 3 7 10
993 3 ▶ return 1 if $real_type eq 'integer';
Branch: 1 branch
BranchTrueFalseTotal
if $real_type eq "integer" 1 2 3
994 }
995  
996 9 return $real_type eq $type;
997  
998 0 ▶ return _type($value) eq $type ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
_type($value) eq $type ? : 0 0 0
999 }
1000  
1001 sub _type {
1002 18 ▶ my ($value) = @_;
Subroutine _type: called CC=8 SCAR=20.8
1003  
1004 18 ▶ return 'null' if _is_null($value);
Branch: 1 branch
BranchTrueFalseTotal
if _is_null($value) 2 16 18
1005 16 ▶ return 'object' if _is_object($value);
Branch: 1 branch
BranchTrueFalseTotal
if _is_object($value) 2 14 16
1006 14 ▶ return 'array' if _is_array($value);
Branch: 1 branch
BranchTrueFalseTotal
if _is_array($value) 3 11 14
1007 11 ▶ return 'boolean' if _is_boolean($value);
Branch: 1 branch
BranchTrueFalseTotal
if _is_boolean($value) 2 9 11
1008 9 ▶ return 'integer' if _is_integer($value);
Branch: 1 branch
BranchTrueFalseTotal
if _is_integer($value) 3 6 9
1009 6 ▶ return 'number' if _is_number($value);
Branch: 1 branch
BranchTrueFalseTotal
if _is_number($value) 4 2 6
1010 2 ▶ return 'string' if _is_string($value);
Branch: 1 branch
BranchTrueFalseTotal
if _is_string($value) 2 0 2
1011  
1012 0 Carp::croak("Unknown type");
1013 }
1014  
1015 sub _subschema {
1016 1 ▶ my ($schema) = @_;
Subroutine _subschema: called CC=3 SCAR=11.1
1017  
1018 1 for (qw/allOf anyOf oneOf not/) {
1019 4 ▶ return $_ if $schema->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $schema->{$_} 0 4 4
1020 }
1021  
1022 1 return;
1023 }
1024  
1025 sub _resolve_remote_ref {
1026 0 ▶ my $self = shift;
Subroutine _resolve_remote_ref: not called CC=7 SCAR=40.3
1027 0 my ($context, $ref) = @_;
1028  
1029 0 my ($url, $pointer) = $ref =~ m/^([^#]+)(#.*)?$/;
1030  
1031 0 my $schema;
1032  
1033 0 ▶ if (exists $context->{ids}->{$url}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $context->{'ids'}{$url}) { } 0 0 0
elsif ($context->{'remote_cache'}{$url}) { } 0 0 0
1034 0 $schema = $context->{ids}->{$url};
1035 }
1036 elsif ($context->{remote_cache}->{$url}) {
1037 0 $schema = $context->{remote_cache}->{$url};
1038 }
1039 else {
1040 0 $schema = eval { $self->{fetcher}->($url) };
1041 0 $context->{remote_cache}->{$url} = $schema;
1042  
1043 0 ▶ if ($schema) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema) 0 0 0
1044 0 ▶ $schema->{id} //= $url;
Condition0%0/3
$schema->{"id"} //= $url
l!l&&r!l&&!r
000
MC/DC0%0/2
$schema->{"id"} //= $url
$schema->{"id"} $url
Truth table0%0/3
A //= B
A= $schema->{"id"}B= $url
ABresult
1X1
011
000
1045  
1046 0 $self->_collect_ids($context, $schema);
1047 }
1048 }
1049  
1050 0 ▶ if ($schema && $pointer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema and $pointer) 0 0 0
Condition0%0/3
$schema and $pointer
!ll&&!rl&&r
000
MC/DC0%0/2
$schema and $pointer
$schema $pointer
Truth table0%0/3
A and B
A= $schemaB= $pointer
ABresult
0X0
100
111
1051 0 $schema = pointer($schema, $pointer);
1052 }
1053  
1054 0 return $schema;
1055 }
1056  
1057 sub _build_result {
1058 4 ▶ my $self = shift;
Subroutine _build_result: called CC=1 SCAR=0.0
1059  
1060 4 return JSON::SchemaValidator::Result->new;
1061 }
1062  
1063 3 1;
1064 __END__
↑ Summary blib/lib/JSON/SchemaValidator/Pointer.pm »