Validator/Constraints/Draft4.pm

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