Validator/Constraints/Draft4.pm

filter: Statementstmt 91.0% 335 / 368 Branchbran 72.9% 140 / 192 Conditioncond 54.5% 18 / 33 MC/DCmcdc 33.3% 7 / 21 Subroutinesub 95.2% 40 / 42 Podpod 0.0% 0 / 32 totaltotal 78.4% 540 / 688 CC 124 SCAR 24.4
CC
124
Cov
83%
exclusiveMaximum
42.0
exclusiveMinimum
42.0
enum
38.8
CRAP 11.5
« Validator/Util.pm ↑ Summary Validator/OAS30.pm »
1 package JSONSchema::Validator::Constraints::Draft4;
2  
3 # ABSTRACT: JSON Schema Draft4 specification constraints
4  
5 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 15 ▶use JSONSchema::Validator::Error 'error';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 ▶use JSONSchema::Validator::JSONPointer 'json_pointer';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 15 ▶use JSONSchema::Validator::Util qw(serialize unbool round is_type detect_type);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 15 ▶use JSONSchema::Validator::Format qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
15 validate_date_time validate_date validate_time
16 validate_email validate_hostname
17 validate_idn_email
18 validate_ipv4 validate_ipv6
19 validate_uuid
20 validate_byte
21 validate_int32 validate_int64
22 validate_float validate_double
23 validate_regex
24 validate_json_pointer validate_relative_json_pointer
25 validate_uri validate_uri_reference
26 validate_iri validate_iri_reference
27 validate_uri_template
28 15 );
29  
30 15 ▶use constant FORMAT_VALIDATIONS => {
Subroutine BEGIN: called CC=1 SCAR=0.0
31 'date-time' => ['string', \&validate_date_time],
32 'date' => ['string', \&validate_date],
33 'time' => ['string', \&validate_time],
34 'email' => ['string', \&validate_email],
35 'idn-email' => ['string', \&validate_idn_email],
36 'hostname' => ['string', \&validate_hostname],
37 'ipv4' => ['string', \&validate_ipv4],
38 'ipv6' => ['string', \&validate_ipv6],
39 'uuid' => ['string', \&validate_uuid],
40 'byte' => ['string', \&validate_byte],
41 'int32' => ['integer', \&validate_int32],
42 'int64' => ['integer', \&validate_int64],
43 'float' => ['number', \&validate_float],
44 'double' => ['number', \&validate_double],
45 'regex' => ['string', \&validate_regex],
46 'json-pointer' => ['string', \&validate_json_pointer],
47 'relative-json-pointer' => ['string', \&validate_relative_json_pointer],
48 'uri' => ['string', \&validate_uri],
49 'uri-reference' => ['string', \&validate_uri_reference],
50 'iri' => ['string', \&validate_iri],
51 'iri-reference' => ['string', \&validate_iri_reference],
52 'uri-template' => ['string', \&validate_uri_template]
53 15 };
54  
55 321 ▶sub new {
Subroutine new: called CC=3 SCAR=11.2
Pod undocumented
56 321 my ($class, %params) = @_;
57  
58 321 ▶ my $validator = $params{validator} or croak 'validator is required';
Branch: 1 branch
BranchTrueFalseTotal
my $validator = $params{"validator"} or croak("validator is required") 321 0 321
59 321 ▶ my $strict = $params{strict} // 1;
Condition50%1/2
$params{"strict"} // 1
l!l
3210
MC/DC0%0/1
$params{"strict"} // 1
$params{"strict"}
Truth table50%1/2
A
A= $params{"strict"}
Aresult
11
00
60  
61 321 weaken($validator);
62  
63 321 my $self = {
64 validator => $validator,
65 errors => [],
66 strict => $strict
67 };
68  
69 321 bless $self, $class;
70  
71 321 return $self;
72 }
73  
74 3481 ▶sub validator { shift->{validator} }
Subroutine validator: called CC=1 SCAR=0.0
Pod undocumented
75 3053 ▶sub strict { shift->{strict} }
Subroutine strict: called CC=1 SCAR=0.0
Pod undocumented
76  
77 # params: $self, $value, $type, $strict
78 4365 ▶sub check_type {
Subroutine check_type: called CC=2 SCAR=7.2
Pod undocumented
79 4365 ▶ return is_type($_[1], $_[2], $_[3] // $_[0]->strict);
Condition66%2/3
$_[3] // $_[0]->strict
l!l&&r!l&&!r
178825770
MC/DC0%0/2
$_[3] // $_[0]->strict
$_[3] $_[0]->strict
Truth table66%2/3
A // B
A= $_[3]B= $_[0]->strict
ABresult
1X1
011
000
80 }
81  
82 835 ▶sub type {
Subroutine type: called CC=3 SCAR=11.0
Pod undocumented
83 835 my ($self, $instance, $types, $schema, $instance_path, $schema_path, $data) = @_;
84 835 ▶ my @types = ref $types ? @$types : ($types);
Branch: 1 branch
BranchTrueFalseTotal
CORE::ref $types ? : 62 773 835
85  
86 835 ▶ return 1 if grep { $self->check_type($instance, $_) } @types;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$self->check_type($instance, $_);} @types 700 135 835
87  
88 135 my $actual_type = detect_type($instance);
89 135 push @{$data->{errors}}, error(
90 message => "type mismatch (expecting any of (@types), found: $actual_type)",
91 instance_path => $instance_path,
92 schema_path => $schema_path
93 );
94 135 return 0;
95 }
96  
97 25 ▶sub minimum {
Subroutine minimum: called CC=3 SCAR=11.0
Pod undocumented
98 25 my ($self, $instance, $minimum, $schema, $instance_path, $schema_path, $data) = @_;
99 25 ▶ return 1 unless $self->check_type($instance, 'number');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_type($instance, "number") 1 24 25
100 24 ▶ return 1 if $instance >= $minimum;
Branch: 1 branch
BranchTrueFalseTotal
if $instance >= $minimum 17 7 24
101 7 push @{$data->{errors}}, error(
102 message => "${instance} is less than ${minimum}",
103 instance_path => $instance_path,
104 schema_path => $schema_path
105 );
106 7 return 0;
107 }
108  
109 13 ▶sub maximum {
Subroutine maximum: called CC=3 SCAR=11.0
Pod undocumented
110 13 my ($self, $instance, $maximum, $schema, $instance_path, $schema_path, $data) = @_;
111 13 ▶ return 1 unless $self->check_type($instance, 'number');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_type($instance, "number") 0 13 13
112 13 ▶ return 1 if $instance <= $maximum;
Branch: 1 branch
BranchTrueFalseTotal
if $instance <= $maximum 10 3 13
113 3 push @{$data->{errors}}, error(
114 message => "${instance} is greater than ${maximum}",
115 instance_path => $instance_path,
116 schema_path => $schema_path
117 );
118 3 return 0;
119 }
120  
121 0 ▶sub exclusiveMaximum {
Subroutine exclusiveMaximum: not called CC=6 SCAR=37.4
Pod undocumented
122 0 my ($self, $instance, $exclusiveMaximum, $schema, $instance_path, $schema_path, $data) = @_;
123 0 ▶ return 1 unless $self->check_type($instance, 'number');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_type($instance, "number") 0 0 0
124 0 ▶ return 1 unless exists $schema->{maximum};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $schema->{"maximum"} 0 0 0
125  
126 0 my $maximum = $schema->{maximum};
127  
128 0 my $res = $self->maximum($instance, $maximum, $schema, $instance_path, $schema_path, $data);
129 0 ▶ return 0 unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 0 0
130  
131 0 ▶ return 1 unless $exclusiveMaximum;
Branch: 1 branch
BranchTrueFalseTotal
unless $exclusiveMaximum 0 0 0
132 0 ▶ return 1 if $instance != $maximum;
Branch: 1 branch
BranchTrueFalseTotal
if $instance != $maximum 0 0 0
133  
134 0 push @{$data->{errors}}, error(
135 message => "${instance} is equal to ${maximum}",
136 instance_path => $instance_path,
137 schema_path => $schema_path
138 );
139 0 return 0;
140 }
141  
142 0 ▶sub exclusiveMinimum {
Subroutine exclusiveMinimum: not called CC=6 SCAR=37.4
Pod undocumented
143 0 my ($self, $instance, $exclusiveMinimum, $schema, $instance_path, $schema_path, $data) = @_;
144 0 ▶ return 1 unless $self->check_type($instance, 'number');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_type($instance, "number") 0 0 0
145 0 ▶ return 1 unless exists $schema->{minimum};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $schema->{"minimum"} 0 0 0
146  
147 0 my $minimum = $schema->{minimum};
148  
149 0 my $res = $self->minimum($instance, $minimum, $schema, $instance_path, $schema_path, $data);
150 0 ▶ return 0 unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 0 0
151  
152 0 ▶ return 1 unless $exclusiveMinimum;
Branch: 1 branch
BranchTrueFalseTotal
unless $exclusiveMinimum 0 0 0
153 0 ▶ return 1 if $instance != $minimum;
Branch: 1 branch
BranchTrueFalseTotal
if $instance != $minimum 0 0 0
154  
155 0 push @{$data->{errors}}, error(
156 message => "${instance} is equal to ${minimum}",
157 instance_path => $instance_path,
158 schema_path => $schema_path
159 );
160 0 return 0;
161 }
162  
163 16 ▶sub minItems {
Subroutine minItems: called CC=3 SCAR=11.0
Pod undocumented
164 16 my ($self, $instance, $min, $schema, $instance_path, $schema_path, $data) = @_;
165 16 ▶ return 1 unless $self->check_type($instance, 'array');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_type($instance, "array") 0 16 16
166 16 ▶ return 1 if scalar(@$instance) >= $min;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$instance >= $min 15 1 16
167 1 push @{$data->{errors}}, error(
168 message => "minItems (>= ${min}) constraint violated",
169 instance_path => $instance_path,
170 schema_path => $schema_path
171 );
172 1 return 0;
173 }
174  
175 4 ▶sub maxItems {
Subroutine maxItems: called CC=3 SCAR=11.0
Pod undocumented
176 4 my ($self, $instance, $max, $schema, $instance_path, $schema_path, $data) = @_;
177 4 ▶ return 1 unless $self->check_type($instance, 'array');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_type($instance, "array") 1 3 4
178 3 ▶ return 1 if scalar(@$instance) <= $max;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$instance <= $max 2 1 3
179 1 push @{$data->{errors}}, error(
180 message => "maxItems (<= ${max}) constraint violated",
181 instance_path => $instance_path,
182 schema_path => $schema_path
183 );
184 1 return 0;
185 }
186  
187 3 ▶sub minLength {
Subroutine minLength: called CC=3 SCAR=11.0
Pod undocumented
188 3 my ($self, $instance, $min, $schema, $instance_path, $schema_path, $data) = @_;
189 3 ▶ return 1 unless $self->check_type($instance, 'string');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_type($instance, "string") 0 3 3
190 3 ▶ return 1 if length $instance >= $min;
Branch: 1 branch
BranchTrueFalseTotal
if length $instance >= $min 2 1 3
191 1 push @{$data->{errors}}, error(
192 message => "minLength (>= ${min}) constraint violated",
193 instance_path => $instance_path,
194 schema_path => $schema_path
195 );
196 1 return 0;
197 }
198  
199 4 ▶sub maxLength {
Subroutine maxLength: called CC=3 SCAR=11.0
Pod undocumented
200 4 my ($self, $instance, $max, $schema, $instance_path, $schema_path, $data) = @_;
201 4 ▶ return 1 unless $self->check_type($instance, 'string');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_type($instance, "string") 1 3 4
202 3 ▶ return 1 if length $instance <= $max;
Branch: 1 branch
BranchTrueFalseTotal
if length $instance <= $max 2 1 3
203 1 push @{$data->{errors}}, error(
204 message => "maxLength (<= ${max}) constraint violated",
205 instance_path => $instance_path,
206 schema_path => $schema_path
207 );
208 1 return 0;
209 }
210  
211 37 ▶sub dependencies {
Subroutine dependencies: called CC=8 SCAR=21.2
Pod undocumented
212 37 my ($self, $instance, $dependencies, $schema, $instance_path, $schema_path, $data) = @_;
213  
214 # ignore non-object
215 37 ▶ return 1 unless $self->check_type($instance, 'object');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_type($instance, "object") 3 34 37
216  
217 34 my $result = 1;
218  
219 34 for my $prop (keys %$dependencies) {
220 64 ▶ next unless exists $instance->{$prop};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $instance->{$prop} 62 2 64
221 2 my $dep = $dependencies->{$prop};
222 2 my $spath = json_pointer->append($schema_path, $prop);
223  
224 # need strict check beacase of schema check
225 2 ▶ if ($self->check_type($dep, 'array', 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->check_type($dep, 'array', 1)) { } 2 0 2
226 2 for my $idx (0 .. $#{$dep}) {
227 2 my $p = $dep->[$idx];
228 2 ▶ next if exists $instance->{$p};
Branch: 1 branch
BranchTrueFalseTotal
if exists $instance->{$p} 1 1 2
229  
230 1 push @{$data->{errors}}, error(
231 message => "dependencies constraint violated: property $p is ommited",
232 instance_path => $instance_path,
233 schema_path => json_pointer->append($spath, $idx)
234 );
235 1 $result = 0;
236 }
237 } else {
238 # $dep is object or boolean (starting draft 6 boolean is valid schema)
239 0 my $r = $self->validator->_validate_schema($instance, $dep, $instance_path, $spath, $data);
240 0 ▶ $result = 0 unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 0 0 0
241 }
242 }
243  
244 34 return $result;
245 }
246  
247 10 ▶sub additionalItems {
Subroutine additionalItems: called CC=9 SCAR=22.1
Pod undocumented
248 10 my ($self, $instance, $additionalItems, $schema, $instance_path, $schema_path, $data) = @_;
249 10 ▶ return 1 unless $self->check_type($instance, 'array');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_type($instance, "array") 0 10 10
250 # need strict check beacase of schema check
251 10 ▶ return 1 if $self->check_type($schema->{items} // {}, 'object', 1);
Branch: 1 branch
BranchTrueFalseTotal
if $self->check_type($schema->{"items"} // {}, "object", 1) 0 10 10
Condition50%1/2
$schema->{"items"} // {}
l!l
100
MC/DC0%0/1
$schema->{"items"} // {}
$schema->{"items"}
Truth table50%1/2
A
A= $schema->{"items"}
Aresult
11
00
252  
253 10 my $len_items = scalar @{$schema->{items}};
254  
255 # need strict check beacase of schema check
256 10 ▶ if ($self->check_type($additionalItems, 'boolean', 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->check_type($additionalItems, "boolean", 1)) 8 2 10
257 8 ▶ return 1 if $additionalItems;
Branch: 1 branch
BranchTrueFalseTotal
if $additionalItems 0 8 8
258 8 ▶ if (scalar @$instance > $len_items) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$instance > $len_items) 2 6 8
259 2 push @{$data->{errors}}, error(
260 message => 'additionalItems constraint violated',
261 instance_path => $instance_path,
262 schema_path => $schema_path
263 );
264 2 return 0;
265 }
266  
267 6 return 1;
268 }
269  
270 # additionalItems is object
271  
272 2 my $result = 1;
273 2 my @items_last_part = @$instance[$len_items .. $#{$instance}];
274  
275 2 for my $index (0 .. $#items_last_part) {
276 6 my $item = $items_last_part[$index];
277  
278 6 my $ipath = json_pointer->append($instance_path, $len_items + $index);
279 6 my $r = $self->validator->_validate_schema($item, $additionalItems, $ipath, $schema_path, $data);
280 6 ▶ $result = 0 unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 1 5 6
281 }
282  
283 2 return $result;
284 }
285  
286 143 ▶sub additionalProperties {
Subroutine additionalProperties: called CC=13 SCAR=25.7
Pod undocumented
287 143 my ($self, $instance, $addProps, $schema, $instance_path, $schema_path, $data) = @_;
288 143 ▶ return 1 unless $self->check_type($instance, 'object');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_type($instance, "object") 6 137 143
289  
290 137 ▶ my $patterns = join '|', keys %{$schema->{patternProperties} // {}};
Condition100%2/2
$schema->{"patternProperties"} // {}
l!l
7958
MC/DC100%1/1
$schema->{"patternProperties"} // {}
$schema->{"patternProperties"}
Truth table100%2/2
A
A= $schema->{"patternProperties"}
Aresult
11
00
291  
292 137 my @extra_props;
293 137 for my $p (keys %$instance) {
294 235 ▶ next if $schema->{properties} && exists $schema->{properties}{$p};
Branch: 1 branch
BranchTrueFalseTotal
if $schema->{"properties"} and exists $schema->{"properties"}{$p} 136 99 235
Condition100%3/3
$schema->{"properties"} and exists $schema->{"properties"}{$p}
!ll&&!rl&&r
7128136
MC/DC100%2/2
$schema->{"properties"} and exists $schema->{"properties"}{$p}
$schema->{"properties"} exists $schema->{"properties"}{$p}
Truth table100%3/3
A and B
A= $schema->{"properties"}B= exists $schema->{"properties"}{$p}
ABresult
0X0
100
111
295 99 ▶ next if $patterns && $p =~ m/$patterns/u;
Branch: 1 branch
BranchTrueFalseTotal
if $patterns and $p =~ /$patterns/u 12 87 99
Condition100%3/3
$patterns and $p =~ /$patterns/u
!ll&&!rl&&r
79812
MC/DC100%2/2
$patterns and $p =~ /$patterns/u
$patterns $p =~ /$patterns/u
Truth table100%3/3
A and B
A= $patternsB= $p =~ /$patterns/u
ABresult
0X0
100
111
296 87 push @extra_props, $p;
297 }
298  
299 137 ▶ return 1 unless @extra_props;
Branch: 1 branch
BranchTrueFalseTotal
unless @extra_props 80 57 137
300  
301 # need strict check beacase of schema check
302 57 ▶ if ($self->check_type($addProps, 'object', 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->check_type($addProps, "object", 1)) 39 18 57
303 39 my $result = 1;
304 39 for my $p (@extra_props) {
305 69 my $ipath = json_pointer->append($instance_path, $p);
306 69 my $r = $self->validator->_validate_schema($instance->{$p}, $addProps, $ipath, $schema_path, $data);
307 69 ▶ $result = 0 unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 1 68 69
308 }
309 39 return $result;
310 }
311  
312 # addProps is boolean
313  
314 18 ▶ return 1 if $addProps;
Branch: 1 branch
BranchTrueFalseTotal
if $addProps 0 18 18
315  
316 18 push @{$data->{errors}}, error(
317 message => 'additionalProperties constraint violated; properties: ' . join(', ', @extra_props),
318 instance_path => $instance_path,
319 schema_path => $schema_path
320 );
321 18 return 0;
322 }
323  
324 69 ▶sub allOf {
Subroutine allOf: called CC=3 SCAR=11.0
Pod undocumented
325 69 my ($self, $instance, $allOf, $schema, $instance_path, $schema_path, $data) = @_;
326  
327 69 my $result = 1;
328 69 for my $idx (0 .. $#{$allOf}) {
329 160 my $subschema = $allOf->[$idx];
330 160 my $spath = json_pointer->append($schema_path, $idx);
331 160 my $r = $self->validator->_validate_schema($instance, $subschema, $instance_path, $spath, $data);
332 160 ▶ $result = 0 unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 22 138 160
333 }
334  
335 69 return $result;
336 }
337  
338 68 ▶sub anyOf {
Subroutine anyOf: called CC=5 SCAR=16.1
Pod undocumented
339 68 my ($self, $instance, $anyOf, $schema, $instance_path, $schema_path, $data) = @_;
340  
341 68 my $errors = $data->{errors};
342 68 my $local_errors = [];
343  
344 68 my $result = 0;
345 68 for my $idx (0 .. $#$anyOf) {
346 70 $data->{errors} = [];
347 70 my $spath = json_pointer->append($schema_path, $idx);
348 70 $result = $self->validator->_validate_schema($instance, $anyOf->[$idx], $instance_path, $spath, $data);
349 70 ▶ unless ($result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 3 67 70
350 3 push @{$local_errors}, error(
351 message => qq'${idx} part of "anyOf" has errors',
352 context => $data->{errors},
353 3 instance_path => $instance_path,
354 schema_path => $spath
355 );
356 }
357 70 ▶ last if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 67 3 70
358 }
359 68 $data->{errors} = $errors;
360 68 ▶ return 1 if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 67 1 68
361  
362 1 push @{$data->{errors}}, error(
363 message => 'instance does not satisfy any schema of "anyOf"',
364 context => $local_errors,
365 instance_path => $instance_path,
366 schema_path => $schema_path
367 );
368 1 return 0;
369 }
370  
371 64 ▶sub oneOf {
Subroutine oneOf: called CC=6 SCAR=17.9
Pod undocumented
372 64 my ($self, $instance, $oneOf, $schema, $instance_path, $schema_path, $data) = @_;
373  
374 64 my $errors = $data->{errors};
375 64 my ($local_errors, $valid_schemas) = ([], []);
376  
377 64 my $num = 0;
378 64 for my $idx (0 .. $#$oneOf) {
379 136 $data->{errors} = [];
380 136 my $spath = json_pointer->append($schema_path, $idx);
381 136 my $r = $self->validator->_validate_schema($instance, $oneOf->[$idx], $instance_path, $spath, $data);
382 136 ▶ if ($r) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r) { } 64 72 136
383 64 push @{$valid_schemas}, $spath;
384 } else {
385 72 push @{$local_errors}, error(
386 message => qq'${idx} part of "oneOf" has errors',
387 context => $data->{errors},
388 72 instance_path => $instance_path,
389 schema_path => $spath
390 );
391 }
392 136 ▶ ++$num if $r;
Branch: 1 branch
BranchTrueFalseTotal
if $r 64 72 136
393 }
394 64 $data->{errors} = $errors;
395 64 ▶ return 1 if $num == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $num == 1 62 2 64
396  
397 2 ▶ if ($num > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num > 1) { } 1 1 2
398 1 push @{$data->{errors}}, error(
399 message => 'instance is valid under more than one schema of "oneOf": ' . join(' ', @$valid_schemas),
400 instance_path => $instance_path,
401 schema_path => $schema_path
402 );
403 } else {
404 1 push @{$data->{errors}}, error(
405 message => 'instance is not valid under any of given schemas of "oneOf"',
406 context => $local_errors,
407 instance_path => $instance_path,
408 schema_path => $schema_path
409 );
410 }
411  
412 2 return 0;
413 }
414  
415 184 ▶sub enum {
Subroutine enum: called CC=16 SCAR=36.6
Pod undocumented
416 184 my ($self, $instance, $enum, $schema, $instance_path, $schema_path, $data) = @_;
417  
418 184 my $result = 0;
419 184 for my $e (@$enum) {
420 # schema must have strict check
421 606 ▶ if ($self->check_type($e, 'boolean', 1)) {
Branch: 5 branches
BranchTrueFalseTotal
if ($self->check_type($e, 'boolean', 1)) { } 4 602 606
elsif ($self->check_type($e, 'object', 1) or $self->check_type($e, 'array', 1)) { } 0 602 602
elsif ($self->check_type($e, 'number', 1)) { } 4 598 602
elsif (defined $e and defined $instance) { } 598 0 598
elsif (CORE::not defined $e || defined $instance) { } 0 0 0
422 4 ▶ $result = $self->check_type($instance, 'boolean')
Branch: 1 branch
BranchTrueFalseTotal
$self->check_type($instance, 'boolean') ? : 4 0 4
423 ? unbool($instance) eq unbool($e)
424 : 0
425 ▶ } elsif ($self->check_type($e, 'object', 1) || $self->check_type($e, 'array', 1)) {
Condition33%1/3
$self->check_type($e, "object", 1) or $self->check_type($e, "array", 1)
l!l&&r!l&&!r
00602
MC/DC0%0/2
$self->check_type($e, "object", 1) or $self->check_type($e, "array", 1)
$self->check_type($e, "object", 1) $self->check_type($e, "array", 1)
Truth table33%1/3
A or B
A= $self->check_type($e, "object", 1)B= $self->check_type($e, "array", 1)
ABresult
1X1
011
000
426 0 ▶ $result = $self->check_type($instance, 'object') ||
Branch: 1 branch
BranchTrueFalseTotal
$self->check_type($instance, 'object') || $self->check_type($instance, 'array') ? : 0 0 0
Condition0%0/3
$self->check_type($instance, "object") or $self->check_type($instance, "array")
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->check_type($instance, "object") or $self->check_type($instance, "array")
$self->check_type($instance, "object") $self->check_type($instance, "array")
Truth table0%0/3
A or B
A= $self->check_type($instance, "object")B= $self->check_type($instance, "array")
ABresult
1X1
011
000
427 $self->check_type($instance, 'array')
428 ? serialize($instance) eq serialize($e)
429 : 0;
430 } elsif ($self->check_type($e, 'number', 1)) {
431 4 ▶ $result = $self->check_type($instance, 'number')
Branch: 1 branch
BranchTrueFalseTotal
$self->check_type($instance, 'number') ? : 4 0 4
432 ? $e == $instance
433 : 0;
434 ▶ } elsif (defined $e && defined $instance) {
Condition33%1/3
defined $e and defined $instance
!ll&&!rl&&r
00598
MC/DC0%0/2
defined $e and defined $instance
defined $e defined $instance
Truth table33%1/3
A and B
A= defined $eB= defined $instance
ABresult
0X0
100
111
435 598 $result = $e eq $instance;
436 ▶ } elsif (!defined $e && !defined $instance) {
Condition0%0/3
defined $e || defined $instance
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $e || defined $instance
defined $e defined $instance
Truth table0%0/3
A || B
A= defined $eB= defined $instance
ABresult
1X1
011
000
437 0 $result = 1;
438 } else {
439 0 $result = 0;
440 }
441 606 ▶ last if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 165 441 606
442 }
443  
444 184 ▶ return 1 if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 165 19 184
445  
446 19 push @{$data->{errors}}, error(
447 message => "instance is not of enums",
448 instance_path => $instance_path,
449 schema_path => $schema_path
450 );
451 19 return 0;
452 }
453  
454 49 ▶sub items {
Subroutine items: called CC=8 SCAR=20.8
Pod undocumented
455 49 my ($self, $instance, $items, $schema, $instance_path, $schema_path, $data) = @_;
456 49 ▶ return 1 unless $self->check_type($instance, 'array');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_type($instance, "array") 2 47 49
457  
458 47 my $result = 1;
459 47 ▶ if ($self->check_type($items, 'array', 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->check_type($items, 'array', 1)) { } 10 37 47
460 10 ▶ my $min = $#{$items} > $#{$instance} ? $#{$instance} : $#{$items};
Branch: 1 branch
BranchTrueFalseTotal
$#{$items;} > $#{$instance;} ? : 2 8 10
461 10 for my $i (0 .. $min) {
462 17 my $item = $instance->[$i];
463 17 my $subschema = $items->[$i];
464 17 my $spath = json_pointer->append($schema_path, $i);
465 17 my $ipath = json_pointer->append($instance_path, $i);
466 17 my $r = $self->validator->_validate_schema($item, $subschema, $ipath, $spath, $data);
467 17 ▶ $result = 0 unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 0 17 17
468 }
469 } else {
470 # items is object
471 37 for my $i (0 .. $#{$instance}) {
472 71 my $item = $instance->[$i];
473 71 my $ipath = json_pointer->append($instance_path, $i);
474 71 my $r = $self->validator->_validate_schema($item, $items, $ipath, $schema_path, $data);
475 71 ▶ $result = 0 unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 9 62 71
476 }
477 }
478 47 return $result;
479 }
480  
481 54 ▶sub format {
Subroutine format: called CC=4 SCAR=15.0
Pod undocumented
482 54 my ($self, $instance, $format, $schema, $instance_path, $schema_path, $data) = @_;
483 54 ▶ return 1 unless exists FORMAT_VALIDATIONS->{$format};
Branch: 1 branch
BranchTrueFalseTotal
unless exists {"byte" => ["string",sub { package JSONSchema::Validator::Format; use warnings; use strict; return 0 if length($_[0]) % 4 != 0; return 1 if $_[0] =~ /$BASE64_PATTERN/; return 0;}],"date" => ["string",sub { package JSONSchema::Validator::Format; use warnings; use strict; my(@dt) = $_[0] =~ /$DATE_PATTERN_FULL/; return _validate_date(@dt);}],"date-time" => ["string",sub { package JSONSchema::Validator::Format; use warnings; use strict; my(@dt) = $_[0] =~ /$DATETIME_PATTERN/; my($Y, $m, $d, $H, $M, $S, $sign, $HH, $MM) = @dt; my $r = _validate_date($Y, $m, $d); return 0 unless $r; $r = _validate_time($H, $M, $S, $sign, $HH, $MM); return 0 unless $r; return 1;}],"double" => ["number",sub { package JSONSchema::Validator::Format; use warnings; use strict; return validate_float($_[0]);}],"email" => ["string",sub { package JSONSchema::Validator::Format; use warnings; use strict; return $_[0] =~ /$EMAIL_PATTERN/ ? 1 : 0;}],"float" => ["number",sub { package JSONSchema::Validator::Format; use warnings; use strict; return 0 if $_[0] =~ /\A\s+|\s+\z/; return 0 unless looks_like_number($_[0]); return 1;}],"hostname" => ["string",sub { package JSONSchema::Validator::Format; use warnings; use strict; my $hostname = shift(); return 0 if length $hostname > 255; $hostname =~ s/\.\z//; return 0 unless $hostname =~ /$HOSTNAME_PATTERN/; my @labels = split(/\./, $hostname, -1); my(@filtered) = grep({length $_ <= 63;} @labels); return 0 unless scalar @labels == scalar @filtered; return 1;}],"idn-email" => ["string",sub { package JSONSchema::Validator::Format; use warnings; use strict; return $_[0] =~ /$IDN_EIMAIL_PATTERN/ ? 1 : 0;}],"int32" => ["integer",sub { package JSONSchema::Validator::Format; use warnings; use strict; return _validate_int_32_64($_[0], '214748364');}],"int64" => ["integer",sub { package JSONSchema::Validator::Format; use warnings; use strict; return _validate_int_32_64($_[0], '922337203685477580');}],"ipv4" => ["string",sub { package JSONSchema::Validator::Format; use warnings; use strict; return $_[0] =~ /$IPV4_FINAL_PATTERN/ ? 1 : 0;}],"ipv6" => ["string",sub { package JSONSchema::Validator::Format; use warnings; use strict; return $_[0] =~ /$IPV6_FINAL_PATTERN/ ? 1 : 0;}],"iri" => ["string",sub { package JSONSchema::Validator::Format; use warnings; use strict; return $_[0] =~ /$IRI_PATTERN/ ? 1 : 0;}],"iri-reference" => ["string",sub { package JSONSchema::Validator::Format; use warnings; use strict; return $_[0] =~ /$IRI_REFERENCE_PATTERN/ ? 1 : 0;}],"json-pointer" => ["string",sub { package JSONSchema::Validator::Format; use warnings; use strict; return 1 if $_[0] eq ''; return 0 unless index($_[0], '/') == 0; return 0 if $_[0] =~ /~(?:[^01]|\z)/; return 1;}],"regex" => ["string",sub { package JSONSchema::Validator::Format; use warnings; use strict; return eval { do { qr/$_[0]/ } } ? 1 : 0;}],"relative-json-pointer" => ["string",sub { package JSONSchema::Validator::Format; use warnings; use strict; my($integer, $pointer) = $_[0] =~ /\A(0|[1-9][0-9]*)(.*)\z/s; return 0 unless defined $integer; return 1 if $pointer eq '#'; return validate_json_pointer($pointer);}],"time" => ["string",sub { package JSONSchema::Validator::Format; use warnings; use strict; my(@dt) = $_[0] =~ /$TIME_PATTERN_FULL/; return _validate_time(@dt);}],"uri" => ["string",sub { package JSONSchema::Validator::Format; use warnings; use strict; return $_[0] =~ /$URI_PATTERN/ ? 1 : 0;}],"uri-reference" => ["string",sub { package JSONSchema::Validator::Format; use warnings; use strict; return $_[0] =~ /$URI_REFERENCE_PATTERN/ ? 1 : 0;}],"uri-template" => ["string",sub { package JSONSchema::Validator::Format; use warnings; use strict; return $_[0] =~ /$URI_TEMPLATE_PATTERN/ ? 1 : 0;}],"uuid" => ["string",sub { package JSONSchema::Validator::Format; use warnings; use strict; return $_[0] =~ /$UUID_PATTERN/ ? 1 : 0;}]}->{$format} 0 54 54
484  
485 54 my ($type, $checker) = @{FORMAT_VALIDATIONS->{$format}};
486 54 ▶ return 1 unless $self->check_type($instance, $type);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_type($instance, $type) 5 49 54
487  
488 49 my $result = $checker->($instance);
489 49 ▶ return 1 if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 49 0 49
490  
491 0 push @{$data->{errors}}, error(
492 message => "instance is not $format",
493 instance_path => $instance_path,
494 schema_path => $schema_path
495 );
496 0 return 0;
497 }
498  
499 2 ▶sub maxProperties {
Subroutine maxProperties: called CC=3 SCAR=11.0
Pod undocumented
500 2 my ($self, $instance, $maxProperties, $schema, $instance_path, $schema_path, $data) = @_;
501 2 ▶ return 1 unless $self->check_type($instance, 'object');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_type($instance, "object") 0 2 2
502 2 ▶ return 1 if scalar(keys %$instance) <= $maxProperties;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %$instance <= $maxProperties 1 1 2
503  
504 1 push @{$data->{errors}}, error(
505 message => "instance has more than $maxProperties properties",
506 instance_path => $instance_path,
507 schema_path => $schema_path
508 );
509 1 return 0;
510 }
511  
512 7 ▶sub minProperties {
Subroutine minProperties: called CC=3 SCAR=11.0
Pod undocumented
513 7 my ($self, $instance, $minProperties, $schema, $instance_path, $schema_path, $data) = @_;
514 7 ▶ return 1 unless $self->check_type($instance, 'object');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_type($instance, "object") 0 7 7
515 7 ▶ return 1 if scalar(keys %$instance) >= $minProperties;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %$instance >= $minProperties 6 1 7
516  
517 1 push @{$data->{errors}}, error(
518 message => "instance has less than $minProperties properties",
519 instance_path => $instance_path,
520 schema_path => $schema_path
521 );
522 1 return 0;
523 }
524  
525 5 ▶sub multipleOf {
Subroutine multipleOf: called CC=5 SCAR=16.2
Pod undocumented
526 5 my ($self, $instance, $multipleOf, $schema, $instance_path, $schema_path, $data) = @_;
527 5 ▶ return 1 unless $self->check_type($instance, 'number');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_type($instance, "number") 0 5 5
528  
529 5 my $result = 1;
530  
531 5 my $div = $instance / $multipleOf;
532 5 ▶ $result = 0 if $div == 'Inf' || int($div) != $div;
Branch: 1 branch
BranchTrueFalseTotal
if $div == "Inf" or int $div != $div 2 3 5
Condition66%2/3
$div == "Inf" or int $div != $div
l!l&&r!l&&!r
023
MC/DC50%1/2
$div == "Inf" or int $div != $div
$div == "Inf" int $div != $div
Truth table66%2/3
A or B
A= $div == "Inf"B= int $div != $div
ABresult
1X1
011
000
533  
534 5 ▶ return 1 if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 3 2 5
535  
536 2 push @{$data->{errors}}, error(
537 message => "instance is not multiple of $multipleOf",
538 instance_path => $instance_path,
539 schema_path => $schema_path
540 );
541 2 return 0;
542 }
543  
544 62 ▶sub not {
Subroutine not: called CC=2 SCAR=6.9
Pod undocumented
545 62 my ($self, $instance, $not, $schema, $instance_path, $schema_path, $data) = @_;
546  
547 62 my $errors = $data->{errors};
548 62 $data->{errors} = [];
549  
550 # not is schema
551 62 my $result = $self->validator->_validate_schema($instance, $not, $instance_path, $schema_path, $data);
552 62 $data->{errors} = $errors;
553 62 ▶ return 1 unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 61 1 62
554  
555 1 push @{$data->{errors}}, error(
556 message => 'instance satisfies the schema defined in \"not\" keyword',
557 instance_path => $instance_path,
558 schema_path => $schema_path
559 );
560 1 return 0;
561 }
562  
563 15 ▶sub pattern {
Subroutine pattern: called CC=3 SCAR=11.0
Pod undocumented
564 15 my ($self, $instance, $pattern, $schema, $instance_path, $schema_path, $data) = @_;
565 15 ▶ return 1 unless $self->check_type($instance, 'string');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_type($instance, "string") 0 15 15
566 15 ▶ return 1 if $instance =~ m/$pattern/u;
Branch: 1 branch
BranchTrueFalseTotal
if $instance =~ /$pattern/u 11 4 15
567  
568 4 push @{$data->{errors}}, error(
569 message => "instance does not match $pattern",
570 instance_path => $instance_path,
571 schema_path => $schema_path
572 );
573 4 return 0;
574 }
575  
576 135 ▶sub patternProperties {
Subroutine patternProperties: called CC=6 SCAR=17.9
Pod undocumented
577 135 my ($self, $instance, $patternProperties, $schema, $instance_path, $schema_path, $data) = @_;
578 135 ▶ return 1 unless $self->check_type($instance, 'object');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_type($instance, "object") 0 135 135
579  
580 135 my $result = 1;
581 135 for my $pattern (keys %$patternProperties) {
582 147 my $subschema = $patternProperties->{$pattern};
583 147 my $spath = json_pointer->append($schema_path, $pattern);
584 147 for my $k (keys %$instance) {
585 260 my $v = $instance->{$k};
586 260 ▶ if ($k =~ m/$pattern/u) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k =~ /$pattern/u) 35 225 260
587 35 my $ipath = json_pointer->append($instance_path, $k);
588 35 my $r = $self->validator->_validate_schema($v, $subschema, $ipath, $spath, $data);
589 35 ▶ $result = 0 unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 3 32 35
590 }
591 }
592 }
593 135 return $result;
594 }
595  
596 415 ▶sub properties {
Subroutine properties: called CC=5 SCAR=16.1
Pod undocumented
597 415 my ($self, $instance, $properties, $schema, $instance_path, $schema_path, $data) = @_;
598 415 ▶ return 1 unless $self->check_type($instance, 'object');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_type($instance, "object") 1 414 415
599  
600 414 my $result = 1;
601 414 for my $prop (keys %$properties) {
602 5327 ▶ next unless exists $instance->{$prop};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $instance->{$prop} 4632 695 5327
603  
604 695 my $subschema = $properties->{$prop};
605 695 my $spath = json_pointer->append($schema_path, $prop);
606 695 my $ipath = json_pointer->append($instance_path, $prop);
607 695 my $r = $self->validator->_validate_schema($instance->{$prop}, $subschema, $ipath, $spath, $data);
608 695 ▶ $result = 0 unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 73 622 695
609 }
610 414 return $result;
611 }
612  
613 181 ▶sub required {
Subroutine required: called CC=4 SCAR=13.9
Pod undocumented
614 181 my ($self, $instance, $required, $schema, $instance_path, $schema_path, $data) = @_;
615 181 ▶ return 1 unless $self->check_type($instance, 'object');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_type($instance, "object") 0 181 181
616  
617 181 my $result = 1;
618 181 for my $idx (0 .. $#{$required}) {
619 240 my $prop = $required->[$idx];
620 240 ▶ next if exists $instance->{$prop};
Branch: 1 branch
BranchTrueFalseTotal
if exists $instance->{$prop} 109 131 240
621 131 push @{$data->{errors}}, error(
622 message => qq{instance does not have required property "$prop"},
623 instance_path => $instance_path,
624 schema_path => json_pointer->append($schema_path, $idx)
625 );
626 131 $result = 0;
627 }
628 181 return $result;
629 }
630  
631 # doesn't work for string that looks like number with the same number in array
632 24 ▶sub uniqueItems {
Subroutine uniqueItems: called CC=8 SCAR=20.9
Pod undocumented
633 24 my ($self, $instance, $uniqueItems, $schema, $instance_path, $schema_path, $data) = @_;
634 24 ▶ return 1 unless $self->check_type($instance, 'array');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_type($instance, "array") 0 24 24
635 # uniqueItems is boolean
636 24 ▶ return 1 unless $uniqueItems;
Branch: 1 branch
BranchTrueFalseTotal
unless $uniqueItems 0 24 24
637  
638 my %hash = map {
639 24 my $type = detect_type($_, $self->strict);
640  
641 45 my $value;
642 45 ▶ if ($type eq 'null') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'null') { } 1 44 45
elsif ($type eq 'object' or $type eq 'array') { } 4 40 44
elsif ($type eq 'boolean') { } 10 30 40
643 1 $value = ''
644 ▶ } elsif ($type eq 'object' || $type eq 'array') {
Condition66%2/3
$type eq "object" or $type eq "array"
l!l&&r!l&&!r
4040
MC/DC50%1/2
$type eq "object" or $type eq "array"
$type eq "object" $type eq "array"
Truth table66%2/3
A or B
A= $type eq "object"B= $type eq "array"
ABresult
1X1
011
000
645 4 $value = serialize($_);
646 } elsif ($type eq 'boolean') {
647 10 $value = "$_";
648 } else {
649 # integer/number/string
650 30 $value = $_;
651 }
652  
653 45 my $key = "${type}#${value}";
654 45 $key => 1;
655 } @$instance;
656 24 ▶ return 1 if scalar(keys %hash) == scalar @$instance;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %hash == scalar @$instance 22 2 24
657 2 push @{$data->{errors}}, error(
658 message => "instance has non-unique elements",
659 instance_path => $instance_path,
660 schema_path => $schema_path
661 );
662 2 return 0;
663 }
664  
665 388 ▶sub ref {
Subroutine ref: called CC=4 SCAR=14.3
Pod undocumented
666 388 my ($self, $instance, $ref, $origin_schema, $instance_path, $schema_path, $data) = @_;
667  
668 388 my $scope = $self->validator->scope;
669 388 $ref = URI->new($ref);
670 388 ▶ $ref = $ref->abs($scope) if $scope;
Branch: 1 branch
BranchTrueFalseTotal
if $scope 339 49 388
671  
672 388 my ($current_scope, $schema) = $self->validator->resolver->resolve($ref);
673  
674 388 ▶ croak "schema not resolved by ref $ref" unless $schema;
Branch: 1 branch
BranchTrueFalseTotal
unless $schema 0 388 388
675  
676 388 push @{$self->validator->scopes}, $current_scope;
677  
678 388 my $result = eval {
679 388 $self->validator->_validate_schema($instance, $schema, $instance_path, $schema_path, $data, apply_scope => 0);
680 };
681  
682 388 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 388 388
683 0 $result = 0;
684 0 push @{$data->{errors}}, error(
685 message => "exception: $@",
686 instance_path => $instance_path,
687 schema_path => $schema_path
688 );
689 }
690  
691 388 pop @{$self->validator->scopes};
692  
693 388 return $result;
694 }
695  
696 15 1;
697  
698 __END__
« Validator/Util.pm ↑ Summary Validator/OAS30.pm »