Validator/Schema/Draft4.pm

filter: Statementstmt 85.4% 106 / 124 Branchbran 76.9% 60 / 78 Conditioncond 87.1% 34 / 39 MC/DCmcdc 77.2% 17 / 22 Subroutinesub 100% 15 / 15 Podpod n/a totaltotal 83.4% 232 / 278 CC 67 SCAR 29.5
CC
67
Cov
83%
_validate_type_array_items
39.6
_validate_type_object_properties
31.2
_validate_type_object_dependencies
8.6
CRAP 19.1
« Validator.pm ↑ Summary Validator/Schema.pm »
1 package JSON::Validator::Schema::Draft4;
2 38 ▶use Mojo::Base 'JSON::Validator::Schema';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 38 ▶use JSON::Validator::Util qw(E data_checksum data_type is_type);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 38 ▶use List::Util 'uniq';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 31 ▶has id => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
8 31 my $data = shift->data;
9 31 ▶ return is_type($data, 'HASH') ? $data->{id} || '' : '';
Branch: 1 branch
BranchTrueFalseTotal
is_type($data, 'HASH') ? : 31 0 31
Condition100%2/2
$data->{"id"} or ""
l!l
1021
MC/DC100%1/1
$data->{"id"} or ""
$data->{"id"}
Truth table100%2/2
A
A= $data->{"id"}
Aresult
11
00
10 38 };
11  
12 38 has specification => 'http://json-schema.org/draft-04/schema#';
13  
14 3 ▶sub _build_formats {
Subroutine _build_formats: called CC=1 SCAR=0.0
15 return {
16 3 'date-time' => JSON::Validator::Formats->can('check_date_time'),
17 'email' => JSON::Validator::Formats->can('check_email'),
18 'hostname' => JSON::Validator::Formats->can('check_hostname'),
19 'ipv4' => JSON::Validator::Formats->can('check_ipv4'),
20 'ipv6' => JSON::Validator::Formats->can('check_ipv6'),
21 'regex' => JSON::Validator::Formats->can('check_regex'),
22 'uri' => JSON::Validator::Formats->can('check_uri'),
23 };
24 }
25  
26 47 ▶sub _validate_number_max {
Subroutine _validate_number_max: called CC=5 SCAR=16.1
27 47 my ($self, $value, $state, $expected) = @_;
28 47 ▶ return unless defined(my $cmp_with = $state->{schema}{maximum});
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $cmp_with = $state->{"schema"}{"maximum"}) 42 5 47
29  
30 5 ▶ my $key = $state->{schema}{exclusiveMaximum} ? 'ex_maximum' : 'maximum';
Branch: 1 branch
BranchTrueFalseTotal
$state->{'schema'}{'exclusiveMaximum'} ? : 2 3 5
31 5 ▶ return if $key eq 'maximum' ? $value <= $cmp_with : $value < $cmp_with;
Branch: 2 branches
BranchTrueFalseTotal
if $key eq "maximum" ? $value <= $cmp_with : $value < $cmp_with 2 3 5
$key eq 'maximum' ? : 3 2 5
32 3 return E $state->{path}, [$expected => $key => $value, $cmp_with];
33 }
34  
35 47 ▶sub _validate_number_min {
Subroutine _validate_number_min: called CC=5 SCAR=16.3
36 47 my ($self, $value, $state, $expected) = @_;
37 47 ▶ return unless defined(my $cmp_with = $state->{schema}{minimum});
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $cmp_with = $state->{"schema"}{"minimum"}) 43 4 47
38  
39 4 ▶ my $key = $state->{schema}{exclusiveMinimum} ? 'ex_minimum' : 'minimum';
Branch: 1 branch
BranchTrueFalseTotal
$state->{'schema'}{'exclusiveMinimum'} ? : 0 4 4
40 4 ▶ return if $key eq 'minimum' ? $value >= $cmp_with : $value > $cmp_with;
Branch: 2 branches
BranchTrueFalseTotal
if $key eq "minimum" ? $value >= $cmp_with : $value > $cmp_with 3 1 4
$key eq 'minimum' ? : 4 0 4
41 1 return E $state->{path}, [$expected => $key => $value, $cmp_with];
42 }
43  
44 158 ▶sub _validate_type_array {
Subroutine _validate_type_array: called CC=2 SCAR=6.9
45 158 my ($self, $data, $state) = @_;
46 158 ▶ return E $state->{path}, [array => type => data_type $data] if ref $data ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $data ne "ARRAY" 1 157 158
47  
48 return (
49 157 $self->_validate_type_array_min_max($_[1], $state),
50 $self->_validate_type_array_unique($_[1], $state),
51 $self->_validate_type_array_items($_[1], $state),
52 );
53 }
54  
55 240 ▶sub _validate_type_array_items {
Subroutine _validate_type_array_items: called CC=10 SCAR=36.8
56 240 my ($self, $data, $state) = @_;
57 240 my ($path, $schema) = @$state{qw(path schema)};
58 240 my @errors;
59  
60 240 ▶ if (ref $schema->{items} eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $schema->{'items'} eq 'ARRAY') { } 0 240 240
elsif (exists $schema->{'items'}) { } 211 29 240
61 0 ▶ my $additional_items = $schema->{additionalItems} // {};
Condition0%0/2
$schema->{"additionalItems"} // {}
l!l
00
MC/DC0%0/1
$schema->{"additionalItems"} // {}
$schema->{"additionalItems"}
Truth table0%0/2
A
A= $schema->{"additionalItems"}
Aresult
11
00
62 0 my @rules = @{$schema->{items}};
63  
64 0 ▶ if ($additional_items) {
Branch: 1 branch
BranchTrueFalseTotal
if ($additional_items) 0 0 0
65 0 ▶ push @rules, $additional_items while @rules < @$data;
Branch: 1 branch
BranchTrueFalseTotal
@rules < @$data and do { push @rules, $additional_items } 0 0 0
66 }
67  
68 0 ▶ if (@rules >= @$data) {
Branch: 2 branches
BranchTrueFalseTotal
if (@rules >= @$data) { } 0 0 0
elsif (not $additional_items) { } 0 0 0
69 0 for my $i (0 .. @$data - 1) {
70 0 push @errors, $self->_validate($data->[$i], $self->_state($state, path => [@$path, $i], schema => $rules[$i]));
71 }
72 }
73 elsif (!$additional_items) {
74 0 push @errors, E $path, [array => additionalItems => int(@$data), int(@rules)];
75 }
76 }
77 elsif (exists $schema->{items}) {
78 211 for my $i (0 .. @$data - 1) {
79 push @errors,
80 182 $self->_validate($data->[$i], $self->_state($state, path => [@$path, $i], schema => $schema->{items}));
81 }
82 }
83  
84 240 return @errors;
85 }
86  
87 240 ▶sub _validate_type_array_min_max {
Subroutine _validate_type_array_min_max: called CC=5 SCAR=16.1
88 240 my ($self, $data, $state) = @_;
89 240 my @errors;
90  
91 240 ▶ if (defined $state->{schema}{minItems} and $state->{schema}{minItems} > @$data) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $state->{"schema"}{"minItems"} and $state->{"schema"}{"minItems"} > @$data) 9 231 240
Condition100%3/3
defined $state->{"schema"}{"minItems"} and $state->{"schema"}{"minItems"} > @$data
!ll&&!rl&&r
681639
MC/DC100%2/2
defined $state->{"schema"}{"minItems"} and $state->{"schema"}{"minItems"} > @$data
defined $state->{"schema"}{"minItems"} $state->{"schema"}{"minItems"} > @$data
Truth table100%3/3
A and B
A= defined $state->{"schema"}{"minItems"}B= $state->{"schema"}{"minItems"} > @$data
ABresult
0X0
100
111
92 9 push @errors, E $state->{path}, [array => minItems => int(@$data), $state->{schema}{minItems}];
93 }
94 240 ▶ if (defined $state->{schema}{maxItems} and $state->{schema}{maxItems} < @$data) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $state->{"schema"}{"maxItems"} and $state->{"schema"}{"maxItems"} < @$data) 4 236 240
Condition100%3/3
defined $state->{"schema"}{"maxItems"} and $state->{"schema"}{"maxItems"} < @$data
!ll&&!rl&&r
22884
MC/DC100%2/2
defined $state->{"schema"}{"maxItems"} and $state->{"schema"}{"maxItems"} < @$data
defined $state->{"schema"}{"maxItems"} $state->{"schema"}{"maxItems"} < @$data
Truth table100%3/3
A and B
A= defined $state->{"schema"}{"maxItems"}B= $state->{"schema"}{"maxItems"} < @$data
ABresult
0X0
100
111
95 4 push @errors, E $state->{path}, [array => maxItems => int(@$data), $state->{schema}{maxItems}];
96 }
97  
98 240 return @errors;
99 }
100  
101 240 ▶sub _validate_type_array_unique {
Subroutine _validate_type_array_unique: called CC=4 SCAR=14.5
102 240 my ($self, $data, $state) = @_;
103 240 ▶ return unless $state->{schema}{uniqueItems};
Branch: 1 branch
BranchTrueFalseTotal
unless $state->{"schema"}{"uniqueItems"} 201 39 240
104  
105 39 my (@errors, %uniq);
106 39 for (@$data) {
107 56 ▶ next if !$uniq{data_checksum($_)}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $uniq{data_checksum($_)}++ 56 0 56
108 0 push @errors, E $state->{path}, [array => 'uniqueItems'];
109 0 last;
110 }
111  
112 39 return @errors;
113 }
114  
115 298 ▶sub _validate_type_object {
Subroutine _validate_type_object: called CC=2 SCAR=6.9
116 298 my ($self, $data, $state) = @_;
117 298 ▶ return E $state->{path}, [object => type => data_type $data] if ref $data ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $data ne "HASH" 3 295 298
118  
119 return (
120 295 $self->_validate_type_object_min_max($_[1], $state),
121 $self->_validate_type_object_dependencies($_[1], $state),
122 $self->_validate_type_object_properties($_[1], $state),
123 );
124 }
125  
126 11263 ▶sub _validate_type_object_min_max {
Subroutine _validate_type_object_min_max: called CC=5 SCAR=16.1
127 11263 my ($self, $data, $state) = @_;
128  
129 11263 my @errors;
130 11263 my @dkeys = keys %$data;
131 11263 ▶ if (defined $state->{schema}{maxProperties} and $state->{schema}{maxProperties} < @dkeys) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $state->{"schema"}{"maxProperties"} and $state->{"schema"}{"maxProperties"} < @dkeys) 4 11259 11263
Condition100%3/3
defined $state->{"schema"}{"maxProperties"} and $state->{"schema"}{"maxProperties"} < @dkeys
!ll&&!rl&&r
11247124
MC/DC100%2/2
defined $state->{"schema"}{"maxProperties"} and $state->{"schema"}{"maxProperties"} < @dkeys
defined $state->{"schema"}{"maxProperties"} $state->{"schema"}{"maxProperties"} < @dkeys
Truth table100%3/3
A and B
A= defined $state->{"schema"}{"maxProperties"}B= $state->{"schema"}{"maxProperties"} < @dkeys
ABresult
0X0
100
111
132 4 push @errors, E $state->{path}, [object => maxProperties => int(@dkeys), $state->{schema}{maxProperties}];
133 }
134 11263 ▶ if (defined $state->{schema}{minProperties} and $state->{schema}{minProperties} > @dkeys) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $state->{"schema"}{"minProperties"} and $state->{"schema"}{"minProperties"} > @dkeys) 8 11255 11263
Condition100%3/3
defined $state->{"schema"}{"minProperties"} and $state->{"schema"}{"minProperties"} > @dkeys
!ll&&!rl&&r
11230258
MC/DC100%2/2
defined $state->{"schema"}{"minProperties"} and $state->{"schema"}{"minProperties"} > @dkeys
defined $state->{"schema"}{"minProperties"} $state->{"schema"}{"minProperties"} > @dkeys
Truth table100%3/3
A and B
A= defined $state->{"schema"}{"minProperties"}B= $state->{"schema"}{"minProperties"} > @dkeys
ABresult
0X0
100
111
135 8 push @errors, E $state->{path}, [object => minProperties => int(@dkeys), $state->{schema}{minProperties}];
136 }
137  
138 11263 return @errors;
139 }
140  
141 588 ▶sub _validate_type_object_dependencies {
Subroutine _validate_type_object_dependencies: called CC=5 SCAR=21.6
142 588 my ($self, $data, $state) = @_;
143 588 ▶ my $dependencies = $state->{schema}{dependencies} || {};
Condition100%2/2
$state->{'schema'}{'dependencies'} || {}
l!l
15573
MC/DC100%1/1
$state->{'schema'}{'dependencies'} || {}
$state->{'schema'}{'dependencies'}
Truth table100%2/2
A
A= $state->{'schema'}{'dependencies'}
Aresult
11
00
144 588 my @errors;
145  
146 588 for my $k (keys %$dependencies) {
147 30 ▶ next if not exists $data->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $data->{$k} 30 0 30
148 0 ▶ if (ref $dependencies->{$k} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dependencies->{$k} eq 'ARRAY') { } 0 0 0
149 push @errors,
150 0 map { E [@{$state->{path}}, $_], [object => dependencies => $k] }
151 0 grep { !exists $data->{$_} } @{$dependencies->{$k}};
152 }
153 else {
154 0 push @errors, $self->_validate($data, $self->_state($state, schema => $dependencies->{$k}));
155 }
156 }
157  
158 588 return @errors;
159 }
160  
161 11263 ▶sub _validate_type_object_properties {
Subroutine _validate_type_object_properties: called CC=31 SCAR=34.4
162 11263 my ($self, $data, $state) = @_;
163 11263 my ($path, $schema, @errors, %rules) = @$state{qw(path schema)};
164 11263 my $defaults = $self->{coerce}{defaults};
165 11263 my @dkeys = keys %$data;
166  
167 11263 ▶ if (my $properties = $schema->{properties}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $properties = $schema->{"properties"}) 7294 3969 11263
168 7294 ▶ if ($defaults) {
Branch: 1 branch
BranchTrueFalseTotal
if ($defaults) { } 2 7292 7294
169 2 push @{$rules{$_}}, $properties->{$_} for keys %$properties;
170 }
171 else {
172 7292 ▶ defined $properties->{$_} && push @{$rules{$_}}, $properties->{$_} for @dkeys;
Branch: 1 branch
BranchTrueFalseTotal
defined $properties->{$_} and push @{$rules{$_};}, $properties->{$_} 9161 15928 25089
173 }
174 }
175  
176 11263 ▶ for my $p (keys %{$schema->{patternProperties} || {}}) {
Condition100%2/2
$schema->{'patternProperties'} || {}
l!l
25268737
MC/DC100%1/1
$schema->{'patternProperties'} || {}
$schema->{'patternProperties'}
Truth table100%2/2
A
A= $schema->{'patternProperties'}
Aresult
11
00
177 2561 my $r = $schema->{patternProperties}{$p};
178 2561 push @{$rules{$_}}, $r for grep /$p/, @dkeys;
179 }
180  
181 11263 ▶ my $additional = exists $schema->{additionalProperties} ? $schema->{additionalProperties} : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $schema->{'additionalProperties'} ? : 1546 9717 11263
182 11263 ▶ if ($additional) {
Branch: 2 branches
BranchTrueFalseTotal
if ($additional) { } 10830 433 11263
elsif (my(@k) = grep({not $rules{$_};} @dkeys)) { } 137 296 433
183 10830 ▶ $additional = {} unless is_type $additional, 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless is_type($additional, "HASH") 1 10829 10830
184 10830 ▶ $rules{$_} ||= [$additional] for @dkeys;
Condition100%2/2
$rules{$_} ||= [$additional]
l!l
857126651
MC/DC100%1/1
$rules{$_} ||= [$additional]
$rules{$_}
Truth table100%2/2
A
A= $rules{$_}
Aresult
11
00
185 }
186 946 elsif (my @k = grep { !$rules{$_} } @dkeys) {
187 137 local $" = ', ';
188 137 return E $path, [object => additionalProperties => join ', ', sort @k];
189 }
190  
191 11126 ▶ for my $k (uniq @{$schema->{required} || []}) {
Condition100%2/2
$schema->{'required'} || []
l!l
74863640
MC/DC100%1/1
$schema->{'required'} || []
$schema->{'required'}
Truth table100%2/2
A
A= $schema->{'required'}
Aresult
11
00
192 8589 ▶ next if exists $data->{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{$k} 6300 2289 8589
193 2289 push @errors, E [@$path, $k], [object => 'required'];
194 2289 delete $rules{$k};
195 }
196  
197 11126 for my $k (keys %rules) {
198 35900 for my $r (@{$rules{$k}}) {
199 35900 ▶ next if !exists $data->{$k} and !$defaults;
Branch: 1 branch
BranchTrueFalseTotal
unless exists $data->{$k} or $defaults 0 35900 35900
Condition66%2/3
exists $data->{$k} or $defaults
l!l&&r!l&&!r
3589820
MC/DC0%0/2
exists $data->{$k} or $defaults
exists $data->{$k} $defaults
Truth table66%2/3
A or B
A= exists $data->{$k}B= $defaults
ABresult
1X1
011
000
200 35900 my $s2 = $self->_state($state, path => [@$path, $k], schema => $r);
201 35900 ▶ if ($defaults and ref $s2->{schema} eq 'HASH' and exists $s2->{schema}{default} and !exists $data->{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($defaults and ref $s2->{"schema"} eq "HASH" and exists $s2->{"schema"}{"default"} and not exists $data->{$k}) 2 35898 35900
Condition77%7/9
$defaults and ref $s2->{"schema"} eq "HASH" and exists $s2->{"schema"}{"default"} and not exists $data->{$k}
!ll&&!rl&&r
3589622
$defaults and ref $s2->{"schema"} eq "HASH" and exists $s2->{"schema"}{"default"}
!ll&&!rl&&r
3589604
$defaults and ref $s2->{"schema"} eq "HASH"
!ll&&!rl&&r
3589604
MC/DC50%2/4
$defaults and ref $s2->{"schema"} eq "HASH" and exists $s2->{"schema"}{"default"} and not exists $data->{$k}
$defaults ref $s2->{"schema"} eq "HASH" exists $s2->{"schema"}{"default"} not exists $data->{$k}
Truth table60%3/5
A and B and C and not(D)
A= $defaultsB= ref $s2->{"schema"} eq "HASH"C= exists $s2->{"schema"}{"default"}D= not exists $data->{$k}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
202 2 $data->{$k} = $s2->{schema}{default};
203 }
204  
205 35900 ▶ next if !exists $data->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $data->{$k} 0 35900 35900
206  
207 35900 my @e = $self->_validate($data->{$k}, $s2);
208 35900 push @errors, @e;
209 35900 ▶ next if @e or !is_type $r, 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if @e or not is_type($r, "HASH") 1576 34324 35900
Condition100%3/3
@e or not is_type($r, "HASH")
l!l&&r!l&&!r
15591734324
MC/DC100%2/2
@e or not is_type($r, "HASH")
@e not is_type($r, "HASH")
Truth table100%3/3
A or not(B)
A= @eB= not is_type($r, "HASH")
ABresult
1X1
011
000
210 34324 ▶ push @errors, $self->_validate_type_enum($data->{$k}, $s2) if $r->{enum};
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"enum"} 588 33736 34324
211 34324 ▶ push @errors, $self->_validate_type_const($data->{$k}, $s2) if $r->{const};
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"const"} 1000 33324 34324
212 }
213 }
214  
215 11126 return @errors;
216 }
217  
218 38 1;
219  
220 =encoding utf8
221  
222 =head1 NAME
223  
224 JSON::Validator::Schema::Draft4 - JSON-Schema Draft 4
225  
226 =head1 SYNOPSIS
227  
228 See L<JSON::Validator::Schema/SYNOPSIS>.
229  
230 =head1 DESCRIPTION
231  
232 This class represents
233 L<https://json-schema.org/specification-links.html#draft-4>.
234  
235 =head1 ATTRIBUTES
236  
237 =head2 specification
238  
239 my $str = $schema->specification;
240 my $schema = $schema->specification($str);
241  
242 Defaults to "L<http://json-schema.org/draft-04/schema#>".
243  
244 =head1 SEE ALSO
245  
246 L<JSON::Validator::Schema>.
247  
248 =cut
« Validator.pm ↑ Summary Validator/Schema.pm »