Validator/Schema.pm

filter: Statementstmt 97.1% 509 / 524 Branchbran 91.5% 315 / 344 Conditioncond 75.8% 151 / 199 MC/DCmcdc 55.0% 65 / 118 Subroutinesub 91.3% 53 / 58 Podpod 100% 10 / 10 totaltotal 88.0% 1103 / 1253 CC 265 SCAR 27.0
CC
265
Cov
91%
_validate_type_object
39.1
_validate
24.0
_validate_type_array
23.0
CRAP 14.8
« Validator/Schema/Draft4.pm ↑ Summary Validator/Util.pm »
1 package JSON::Validator::Schema;
2 80 ▶use Mojo::Base 'JSON::Validator'; # TODO: Change this to "use Mojo::Base -base"
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 80 ▶use Carp qw(carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 80 ▶use JSON::Validator::Formats;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 80 ▶use JSON::Validator::URI qw(uri);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 80 ▶use JSON::Validator::Util qw(E data_checksum data_type is_bool is_num is_type prefix_errors schema_type str2data);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 80 ▶use List::Util qw(first uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 80 ▶use Mojo::JSON qw(false true);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 80 ▶use Mojo::JSON::Pointer;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 80 ▶use Mojo::Util qw(deprecated);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 80 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 has errors => sub {
15 19 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
16 19 ▶ my $specification = $self->specification || 'http://json-schema.org/draft-04/schema#';
Condition100%2/2
$self->specification || 'http://json-schema.org/draft-04/schema#'
l!l
172
MC/DC100%1/1
$self->specification || 'http://json-schema.org/draft-04/schema#'
$self->specification
Truth table100%2/2
A
A= $self->specification
Aresult
11
00
17 19 my $validator = $self->new(store => $self->store, _refs => {})->coerce('numbers,strings')->resolve($specification);
18 19 return [$self->_validate_id($self->id), $validator->validate($self->data)];
19 80 };
20  
21 169 ▶has formats => sub { shift->_build_formats };
Subroutine __ANON__: called CC=1 SCAR=0.0
22  
23 has id => sub {
24 614 ▶ my $data = shift->data;
Subroutine __ANON__: called CC=4 SCAR=13.9
25 614 ▶ return is_type($data, 'HASH') ? $data->{'$id'} || $data->{id} || '' : '';
Branch: 1 branch
BranchTrueFalseTotal
is_type($data, 'HASH') ? : 600 14 614
Condition100%5/5
$data->{"\$id"} or $data->{"id"} or ""
l!l
16584
$data->{"\$id"} or $data->{"id"}
l!l&&r!l&&!r
511584
MC/DC100%2/2
$data->{"\$id"} or $data->{"id"} or ""
$data->{"\$id"} $data->{"id"}
Truth table100%3/3
A or B
A= $data->{"\$id"}B= $data->{"id"}
ABresult
1X1
011
000
26 80 };
27  
28 has moniker => sub {
29 2 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=8.1
30 2 ▶ return "draft$1" if $self->specification =~ m!draft-(\d+)!;
Branch: 1 branch
BranchTrueFalseTotal
if $self->specification =~ /draft-(\d+)/u 2 0 2
31 0 return '';
32 80 };
33  
34 80 has recursive_data_protection => 1;
35  
36 has specification => sub {
37 2 ▶ my $data = shift->data;
Subroutine __ANON__: called CC=4 SCAR=16.9
38 2 ▶ is_type($data, 'HASH') ? $data->{'$schema'} || $data->{schema} || '' : '';
Branch: 1 branch
BranchTrueFalseTotal
is_type($data, 'HASH') ? : 2 0 2
Condition40%2/5
$data->{"\$schema"} or $data->{"schema"} or ""
l!l
02
$data->{"\$schema"} or $data->{"schema"}
l!l&&r!l&&!r
002
MC/DC0%0/2
$data->{"\$schema"} or $data->{"schema"} or ""
$data->{"\$schema"} $data->{"schema"}
Truth table33%1/3
A or B
A= $data->{"\$schema"}B= $data->{"schema"}
ABresult
1X1
011
000
39 80 };
40  
41 18 ▶has _ref_keys => sub { [qw($ref)] };
Subroutine __ANON__: called CC=1 SCAR=0.0
42 0 ▶has _refs => sub { +{} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
43  
44 sub bundle {
45 24 ▶ my ($self, $args) = @_;
Subroutine bundle: called CC=7 SCAR=19.7
Pod documented
46 24 ▶ my $data = $args->{schema} // $self->data;
Condition66%2/3
$args->{"schema"} // $self->data
l!l&&r!l&&!r
12120
MC/DC0%0/2
$args->{"schema"} // $self->data
$args->{"schema"} $self->data
Truth table66%2/3
A // B
A= $args->{"schema"}B= $self->data
ABresult
1X1
011
000
47  
48 # Avoid $ref on top level
49 24 ▶ while (ref $data eq 'HASH' and $data->{'$ref'} and !ref $data->{'$ref'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq "HASH" and $data->{"\$ref"} and not ref $data->{"\$ref"}) 5 24 29
Condition66%4/6
ref $data eq "HASH" and $data->{"\$ref"} and not ref $data->{"\$ref"}
!ll&&!rl&&r
2405
ref $data eq "HASH" and $data->{"\$ref"}
!ll&&!rl&&r
0245
MC/DC33%1/3
ref $data eq "HASH" and $data->{"\$ref"} and not ref $data->{"\$ref"}
ref $data eq "HASH" $data->{"\$ref"} not ref $data->{"\$ref"}
Truth table50%2/4
A and B and not(C)
A= ref $data eq "HASH"B= $data->{"\$ref"}C= not ref $data->{"\$ref"}
ABCresult
0XX0
10X0
1100
1111
50 5 $data = $self->_refs->{$data}{schema};
51 }
52  
53 # Check if we have a complex schema or not
54 24 ▶ return $self->new(%$self) unless ref $data eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 0 24 24
55  
56 24 my $bundle = $self->new(%$self, data => {}, _refs => {});
57 24 local $bundle->{seen_bundle_ref} = {};
58 24 local $bundle->{seen_schema} = {};
59 24 $bundle->_bundle_from({base_url => $self->id, root => $self->data, schema => $data});
60  
61 24 my $id = JSON::Validator::URI->new->from_data($bundle->data);
62 24 ▶ $bundle->id($bundle->store->exists($id) ? $id : $self->store->add($id, $bundle->data));
Branch: 1 branch
BranchTrueFalseTotal
$bundle->store->exists($id) ? : 5 19 24
63  
64 24 return $bundle;
65 }
66  
67 sub contains {
68 0 ▶ deprecated 'contains() is deprecated';
Subroutine contains: not called CC=1 SCAR=6.9
Pod documented
69 0 state $p = Mojo::JSON::Pointer->new;
70 0 return $p->data(shift->{data})->contains(@_);
71 }
72  
73 sub data {
74 4719 ▶ my $self = shift;
Subroutine data: called CC=3 SCAR=11.0
Pod documented
75 4719 ▶ return $self->{data} //= {} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 3887 832 4719
Condition50%1/2
$self->{"data"} //= {}
l!l
38870
MC/DC0%0/1
$self->{"data"} //= {}
$self->{"data"}
Truth table50%1/2
A
A= $self->{"data"}
Aresult
11
00
76 832 $self->{data} = shift;
77 832 delete $self->{errors};
78 832 return $self;
79 }
80  
81 sub get {
82 383 ▶ my ($self, $pointer, $cb) = @_;
Subroutine get: called CC=4 SCAR=15.6
Pod documented
83 383 my %state = (path => [], root => $self->data, schema => $self->data);
84 383 ▶ return $self->_get([@$pointer], \%state, $cb) if is_type $pointer, 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if is_type($pointer, "ARRAY") 321 62 383
85 62 ▶ return $self->_get([split '/', $pointer], \%state, $cb) if $pointer =~ s!^/!!;
Branch: 1 branch
BranchTrueFalseTotal
if $pointer =~ s[^/][]u 62 0 62
86 0 ▶ return length $pointer ? undef : $self->data;
Branch: 1 branch
BranchTrueFalseTotal
length $pointer ? : 0 0 0
87 }
88  
89 1 ▶sub is_invalid { !!@{shift->errors} }
Subroutine is_invalid: called CC=1 SCAR=0.0
Pod documented
90  
91 0 ▶sub load_and_validate_schema { Carp::confess('load_and_validate_schema(...) is unsupported.') }
Subroutine load_and_validate_schema: not called CC=1 SCAR=6.9
Pod documented
92  
93 sub new {
94 717 ▶ my $class = shift;
Subroutine new: called CC=4 SCAR=13.9
Pod documented
95 717 ▶ return $class->SUPER::new(@_) unless @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ % 2 70 647 717
96  
97 647 my $data = shift;
98 647 ▶ $data = str2data $data if !ref $data and $data =~ m!^\{|^---|\n!s;
Branch: 1 branch
BranchTrueFalseTotal
if not ref $data and $data =~ /^\{|^---|\n/su 21 626 647
Condition100%3/3
not ref $data and $data =~ /^\{|^---|\n/su
!ll&&!rl&&r
5656121
MC/DC100%2/2
not ref $data and $data =~ /^\{|^---|\n/su
not ref $data $data =~ /^\{|^---|\n/su
Truth table100%3/3
not(A) and B
A= not ref $dataB= $data =~ /^\{|^---|\n/su
ABresult
0X0
100
111
99 647 return $class->SUPER::new(@_)->resolve($data);
100 }
101  
102 sub resolve {
103 826 ▶ my $self = shift;
Subroutine resolve: called CC=19 SCAR=29.6
Pod documented
104 826 ▶ my $data = shift // $self->data; # $self->data will get deprecated
Condition66%2/3
shift() // $self->data
l!l&&r!l&&!r
816100
MC/DC0%0/2
shift() // $self->data
shift() $self->data
Truth table66%2/3
A // B
A= shift()B= $self->data
ABresult
1X1
011
000
105  
106 826 ▶ my $state
Branch: 2 branches
BranchTrueFalseTotal
!ref($data) ? : 90 736 826
builtin::blessed($data) && $data->can('to_abs') ? : 28 708 736
Condition100%3/3
builtin::blessed($data) and $data->can("to_abs")
!ll&&!rl&&r
6941428
MC/DC100%2/2
builtin::blessed($data) and $data->can("to_abs")
builtin::blessed($data) $data->can("to_abs")
Truth table100%3/3
A and B
A= builtin::blessed($data)B= $data->can("to_abs")
ABresult
0X0
100
111
107 = !ref $data ? $self->store->resolve($data)
108 : (blessed $data && $data->can('to_abs')) ? $self->store->resolve($data->to_abs)
109 : {root => $data, schema => $data};
110  
111 824 $self->_refs({});
112 824 $self->data($state->{schema});
113 824 ▶ $self->id($state->{id} || JSON::Validator::URI->new->from_data($state->{schema})) unless $self->id;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->id 634 190 824
Condition66%2/3
$state->{'id'} || 'JSON::Validator::URI'->new->from_data($state->{'schema'})
l!l&&r!l&&!r
765580
MC/DC0%0/2
$state->{'id'} || 'JSON::Validator::URI'->new->from_data($state->{'schema'})
$state->{'id'} 'JSON::Validator::URI'->new->from_data($state->{'schema'})
Truth table66%2/3
A || B
A= $state->{'id'}B= 'JSON::Validator::URI'->new->from_data($state->{'schema'})
ABresult
1X1
011
000
114 824 ▶ $state->{id} ||= $self->store->exists($self->id) || $self->store->add($self->id, $self->data);
Condition66%4/6
$state->{"id"} ||= $self->store->exists($self->id) || $self->store->add($self->id, $self->data)
l!l&&r!l&&!r
1167080
$self->store->exists($self->id) || $self->store->add($self->id, $self->data)
l!l&&r!l&&!r
4312770
MC/DC0%0/3
$state->{"id"} ||= $self->store->exists($self->id) || $self->store->add($self->id, $self->data)
$state->{"id"} $self->store->exists($self->id) $self->store->add($self->id, $self->data)
Truth table0%0/4
A ||= B || C
A= $state->{"id"}B= $self->store->exists($self->id)C= $self->store->add($self->id, $self->data)
ABCresult
1XX1
01X1
0011
0000
115 824 ▶ $state->{base_url} ||= $self->id;
Condition66%2/3
$state->{"base_url"} ||= $self->id
l!l&&r!l&&!r
1167080
MC/DC0%0/2
$state->{"base_url"} ||= $self->id
$state->{"base_url"} $self->id
Truth table66%2/3
A ||= B
A= $state->{"base_url"}B= $self->id
ABresult
1X1
011
000
116  
117 824 my (@topics, @refs, %seen) = ([$state, $state->{schema}]);
118  
119 # Search the whole document for id/$id/$ref/$recursiveRef/...
120 TOPIC:
121 966 ▶ while (@topics) {
Branch: 1 branch
BranchTrueFalseTotal
if (@topics) 19278 966 20244
122 19278 my ($state, $schema) = @{shift @topics};
123  
124 19278 ▶ if (is_type $schema, 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (is_type($schema, 'ARRAY')) { } 1628 17650 19278
elsif (is_type($schema, 'HASH')) { } 14438 3212 17650
125 1628 push @topics, map { [$state, $_] } @$schema;
126 }
127 elsif (is_type $schema, 'HASH') {
128 14438 ▶ next TOPIC if $seen{$schema}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$schema}++ 3112 11326 14438
129 11326 $state = $self->_resolve_object($state, $schema, \@refs, \my %found);
130 11326 ▶ ref $schema->{$_} and !$found{$_} and push @topics, [$state, $schema->{$_}] for keys %$schema;
Branch: 1 branch
BranchTrueFalseTotal
ref $schema->{$_} and not $found{$_} and push @topics, [$state, $schema->{$_}] 11972 10187 22159
Condition66%2/3
ref $schema->{$_} and not $found{$_}
!ll&&!rl&&r
10187011972
MC/DC50%1/2
ref $schema->{$_} and not $found{$_}
ref $schema->{$_} not $found{$_}
Truth table66%2/3
A and not(B)
A= ref $schema->{$_}B= not $found{$_}
ABresult
0X0
100
111
131 }
132 }
133  
134 # Need to resolve the $ref/$recursiveRef/... after id/$id/$anchor/... is found above
135 966 @topics = ();
136 966 ▶ while (my $r = shift @refs) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = shift @refs) 3276 964 4240
137 3276 my ($schema, $state) = @$r;
138 3276 my $resolved = $self->store->resolve($self->_extract_ref_from_schema($schema), $state);
139 3274 $self->_refs->{$schema} = $resolved;
140 3274 push @topics, [$resolved, $resolved->{schema}];
141 }
142  
143 # Traverse the newly discovered sub documents, if any
144 964 ▶ goto TOPIC if @topics;
Branch: 1 branch
BranchTrueFalseTotal
if @topics 142 822 964
145  
146 822 return $self;
147 }
148  
149 sub validate {
150 1064 ▶ my ($self, $data, $schema) = @_;
Subroutine validate: called CC=2 SCAR=6.9
Pod documented
151  
152 1064 local $self->{seen} = {};
153 1064 my %state = (base_url => $self->id, path => [], root => $self->data);
154 my @errors
155 1064 ▶ = sort { $a->path cmp $b->path } $self->_validate($_[1], $self->_state(\%state, schema => $schema // $self->data));
Condition100%3/3
$schema // $self->data
l!l&&r!l&&!r
2887697
MC/DC100%2/2
$schema // $self->data
$schema $self->data
Truth table100%3/3
A // B
A= $schemaB= $self->data
ABresult
1X1
011
000
156 1064 return @errors;
157 }
158  
159 sub schema {
160 0 ▶ deprecated 'schema() is deprecated';
Subroutine schema: not called CC=2 SCAR=17.9
Pod documented
161 0 ▶ return $_[0]->can('data') ? $_[0] : $_[0]->SUPER::schema;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->can('data') ? : 0 0 0
162 }
163  
164 sub _build_formats {
165 return {
166 'byte' => JSON::Validator::Formats->can('check_byte'),
167 'date' => JSON::Validator::Formats->can('check_date'),
168 'date-time' => JSON::Validator::Formats->can('check_date_time'),
169 'duration' => JSON::Validator::Formats->can('check_duration'),
170 'double' => JSON::Validator::Formats->can('check_double'),
171 'email' => JSON::Validator::Formats->can('check_email'),
172 'float' => JSON::Validator::Formats->can('check_float'),
173 'hostname' => JSON::Validator::Formats->can('check_hostname'),
174 'idn-email' => JSON::Validator::Formats->can('check_idn_email'),
175 'idn-hostname' => JSON::Validator::Formats->can('check_idn_hostname'),
176 'int32' => JSON::Validator::Formats->can('check_int32'),
177 'int64' => JSON::Validator::Formats->can('check_int64'),
178 'ipv4' => JSON::Validator::Formats->can('check_ipv4'),
179 'ipv6' => JSON::Validator::Formats->can('check_ipv6'),
180 'iri' => JSON::Validator::Formats->can('check_iri'),
181 'iri-reference' => JSON::Validator::Formats->can('check_iri_reference'),
182 'json-pointer' => JSON::Validator::Formats->can('check_json_pointer'),
183 'regex' => JSON::Validator::Formats->can('check_regex'),
184 'relative-json-pointer' => JSON::Validator::Formats->can('check_relative_json_pointer'),
185 0 ▶ 'string' => sub { undef },
Subroutine __ANON__: not called CC=1 SCAR=6.9
186 139 ▶ 'time' => JSON::Validator::Formats->can('check_time'),
Subroutine _build_formats: called CC=1 SCAR=0.0
187 'uri' => JSON::Validator::Formats->can('check_uri'),
188 'uri-reference' => JSON::Validator::Formats->can('check_uri_reference'),
189 'uri-reference' => JSON::Validator::Formats->can('check_uri_reference'),
190 'uri-template' => JSON::Validator::Formats->can('check_uri_template'),
191 'uuid' => JSON::Validator::Formats->can('check_uuid'),
192 };
193 }
194  
195 # $self inside _bundle() is the target bundle schema object and not the source
196 sub _bundle_from {
197 24 ▶ my ($self, $root_state) = @_;
Subroutine _bundle_from: called CC=18 SCAR=30.5
198  
199 24 my @topics = ([$root_state, $root_state->{schema}, $self->data, []]);
200 24 ▶ while (my $topic = shift @topics) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $topic = shift @topics) 201 24 225
201 201 my ($state, $source, $target, $path) = @$topic;
202 201 ▶ next if $state->{seen_schema}{$target}++; # Avoid recursion
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"seen_schema"}{$target}++ 11 190 201
203  
204 190 ▶ if (ref $source eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $source eq 'HASH') { } 186 4 190
elsif (ref $source eq 'ARRAY') { } 4 0 4
205 186 for my $k (keys %$source) {
206 296 ▶ if ($k eq '$ref') {
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq '$ref') { } 48 248 296
207 48 local @$state{qw(root_state schema_path)} = ($root_state, $path);
208 48 push @topics, $self->_bundle_ref($state, $source, $target);
209 }
210 else {
211 248 my $type = ref $source->{$k};
212 248 ▶ $target->{$k} //= $type eq 'HASH' ? {} : $type eq 'ARRAY' ? [] : $source->{$k};
Branch: 2 branches
BranchTrueFalseTotal
$type eq 'HASH' ? : 123 124 247
$type eq 'ARRAY' ? : 4 120 124
Condition66%2/3
$target->{$k} //= $type eq "HASH" ? {} : ($type eq "ARRAY" ? [] : $source->{$k})
l!l&&r!l&&!r
12470
MC/DC0%0/2
$target->{$k} //= $type eq "HASH" ? {} : ($type eq "ARRAY" ? [] : $source->{$k})
$target->{$k} $type eq "HASH" ? {} : ($type eq "ARRAY" ? [] : $source->{$k})
Truth table66%2/3
A //= B
A= $target->{$k}B= $type eq "HASH" ? {} : ($type eq "ARRAY" ? [] : $source->{$k})
ABresult
1X1
011
000
213 248 ▶ push @topics, [$state, $source->{$k}, $target->{$k}, [@$path, $k]] if $type eq 'HASH' or $type eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "HASH" or $type eq "ARRAY" 128 120 248
Condition100%3/3
$type eq "HASH" or $type eq "ARRAY"
l!l&&r!l&&!r
1244120
MC/DC100%2/2
$type eq "HASH" or $type eq "ARRAY"
$type eq "HASH" $type eq "ARRAY"
Truth table100%3/3
A or B
A= $type eq "HASH"B= $type eq "ARRAY"
ABresult
1X1
011
000
214 }
215 }
216 }
217 elsif (ref $source eq 'ARRAY') {
218 4 for my $i (0 .. @$source - 1) {
219 1 my $type = ref $source->[$i];
220 1 ▶ $target->[$i] //= $type eq 'HASH' ? {} : $type eq 'ARRAY' ? [] : $source->[$i];
Branch: 2 branches
BranchTrueFalseTotal
$type eq 'HASH' ? : 1 0 1
$type eq 'ARRAY' ? : 0 0 0
Condition33%1/3
$target->[$i] //= $type eq "HASH" ? {} : ($type eq "ARRAY" ? [] : $source->[$i])
l!l&&r!l&&!r
010
MC/DC0%0/2
$target->[$i] //= $type eq "HASH" ? {} : ($type eq "ARRAY" ? [] : $source->[$i])
$target->[$i] $type eq "HASH" ? {} : ($type eq "ARRAY" ? [] : $source->[$i])
Truth table33%1/3
A //= B
A= $target->[$i]B= $type eq "HASH" ? {} : ($type eq "ARRAY" ? [] : $source->[$i])
ABresult
1X1
011
000
221 1 ▶ push @topics, [$state, $source->[$i], $target->[$i], [@$path, $i]] if $type eq 'HASH' or $type eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "HASH" or $type eq "ARRAY" 1 0 1
Condition33%1/3
$type eq "HASH" or $type eq "ARRAY"
l!l&&r!l&&!r
100
MC/DC0%0/2
$type eq "HASH" or $type eq "ARRAY"
$type eq "HASH" $type eq "ARRAY"
Truth table33%1/3
A or B
A= $type eq "HASH"B= $type eq "ARRAY"
ABresult
1X1
011
000
222 }
223 }
224 else {
225 0 warn "This should not happen! _bundle_from << $source => $target";
226 }
227 }
228 }
229  
230 sub _bundle_ref {
231 47 ▶ my ($self, $state, $source, $target) = @_;
Subroutine _bundle_ref: called CC=9 SCAR=22.7
232  
233 my $uri
234 = $state->{base_url} eq $state->{root_state}{base_url}
235 ? uri($source->{'$ref'})
236 47 ▶ : uri($source->{'$ref'}, $state->{base_url});
Branch: 1 branch
BranchTrueFalseTotal
$state->{'base_url'} eq $state->{'root_state'}{'base_url'} ? : 40 7 47
237  
238 47 my $source_state = $self->store->resolve($source->{'$ref'}, $state);
239 47 my $prefix = join '-', map { s!^/+!!; $_ } grep { length $_ } pop @{$uri->path}, $uri->fragment;
240 47 my ($i, $suffix, @path) = (-1);
241  
242 47 ▶ while (++$i <= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$i <= 3) 47 0 47
243 47 ▶ $suffix ||= data_checksum $source_state->{schema} if $i;
Branch: 1 branch
BranchTrueFalseTotal
if $i 0 47 47
Condition0%0/3
$suffix ||= data_checksum($source_state->{"schema"})
l!l&&r!l&&!r
000
MC/DC0%0/2
$suffix ||= data_checksum($source_state->{"schema"})
$suffix data_checksum($source_state->{"schema"})
Truth table0%0/3
A ||= B
A= $suffixB= data_checksum($source_state->{"schema"})
ABresult
1X1
011
000
244 47 ▶ @path = $self->_bundle_ref_path_expand($source_state, $i ? join '-', $prefix, substr $suffix, 0, $i * 4 : $prefix);
Branch: 1 branch
BranchTrueFalseTotal
$i ? : 0 47 47
245 47 $path[-1] =~ s!^\W+!!;
246 47 $path[-1] =~ s![^\w-]!_!g; # Make a pretty path
247 47 ▶ last unless $self->{seen_bundle_ref}{@path};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"seen_bundle_ref"}{@path} 47 0 47
248 }
249  
250 47 my $def_target = $self->data;
251 47 $target->{'$ref'} = join '/', '#', @path;
252 47 ▶ $def_target = $def_target->{$_} //= {} for @path;
Condition100%2/2
$def_target->{$_} //= {}
l!l
4252
MC/DC100%1/1
$def_target->{$_} //= {}
$def_target->{$_}
Truth table100%2/2
A
A= $def_target->{$_}
Aresult
11
00
253 47 $self->_refs->{$target} = $source_state;
254  
255 47 return [$source_state, $source_state->{schema}, $def_target, $state->{schema_path}];
256 }
257  
258 45 ▶sub _bundle_ref_path_expand { local $_ = $_[2]; s!^definitions/!!; return 'definitions', $_; }
Subroutine _bundle_ref_path_expand: called CC=1 SCAR=0.0
259 1401 ▶sub _extract_ref_from_schema { $_[1]->{'$ref'} }
Subroutine _extract_ref_from_schema: called CC=1 SCAR=0.0
260  
261 sub _get {
262 529 ▶ my ($self, $pointer, $state, $cb) = @_;
Subroutine _get: called CC=11 SCAR=24.4
263  
264 529 my $path = $state->{path};
265 529 my $schema = $state->{schema};
266 my $follow_ref = sub {
267 1573 ▶ return if $pointer->[0] and $pointer->[0] eq '$ref';
Subroutine __ANON__: called CC=9 SCAR=22.2
Branch: 1 branch
BranchTrueFalseTotal
if $pointer->[0] and $pointer->[0] eq "\$ref" 2 1571 1573
Condition100%3/3
$pointer->[0] and $pointer->[0] eq "\$ref"
!ll&&!rl&&r
36712042
MC/DC100%2/2
$pointer->[0] and $pointer->[0] eq "\$ref"
$pointer->[0] $pointer->[0] eq "\$ref"
Truth table100%3/3
A and B
A= $pointer->[0]B= $pointer->[0] eq "\$ref"
ABresult
0X0
100
111
268  
269 1571 my $ref_keys = $self->_ref_keys; # ($ref, $recursiveRef ...)
270 1571 my $schema_lookup = $schema;
271 1571 ▶ while (ref $schema eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $schema eq "HASH") 1530 75 1605
272 1530 ▶ last unless my $ref_key = first { $schema->{$_} && !ref $schema->{$_} } @$ref_keys;
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 2 branches
BranchTrueFalseTotal
unless my $ref_key = &first(sub { $schema->{$_} and not ref $schema->{$_}; } , @$ref_keys) 1496 34 1530
$schema->{$_} and not ref $schema->{$_} 34 2460 2494
273  
274 34 ▶ $state = $self->_refs->{$schema_lookup}
Condition33%1/3
$self->_refs->{$schema_lookup} // Carp::confess(qq[resolve() must be called before validate() to lookup "$schema_lookup->{$ref_key}".])
l!l&&r!l&&!r
3400
MC/DC0%0/2
$self->_refs->{$schema_lookup} // Carp::confess(qq[resolve() must be called before validate() to lookup "$schema_lookup->{$ref_key}".])
$self->_refs->{$schema_lookup} Carp::confess(qq[resolve() must be called before validate() to lookup "$schema_lookup->{$ref_key}".])
Truth table33%1/3
A // B
A= $self->_refs->{$schema_lookup}B= Carp::confess(qq[resolve() must be called before validate() to lookup "$schema_lookup->{$ref_key}".])
ABresult
1X1
011
000
275 // Carp::confess(qq(resolve() must be called before validate() to lookup "$schema_lookup->{$ref_key}".));
276 34 ▶ if (is_type $state->{schema}, 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (is_type($state->{'schema'}, 'HASH')) { } 34 0 34
277 34 $schema_lookup = $state->{schema};
278 34 $schema = {%{$state->{schema}}, %$schema};
279 34 ▶ $state->{schema}{'$ref'} ? ($schema->{'$ref'} = $state->{schema}{'$ref'}) : delete $schema->{'$ref'};
Branch: 1 branch
BranchTrueFalseTotal
$state->{'schema'}{'$ref'} ? : 4 30 34
280 $state->{schema}{'$recursiveRef'}
281 ? ($schema->{'$recursiveRef'} = $state->{schema}{'$recursiveRef'})
282 34 ▶ : delete $schema->{'$recursiveRef'};
Branch: 1 branch
BranchTrueFalseTotal
$state->{'schema'}{'$recursiveRef'} ? : 0 34 34
283 }
284 else {
285 0 $schema = $schema_lookup = $state->{schema};
286 }
287  
288 34 $state = {%$state, path => $path, schema => $schema};
289 }
290 529 };
291  
292 529 $follow_ref->();
293  
294 529 ▶ while (@$pointer) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$pointer) 1233 340 1573
295 1233 my $p = shift @$pointer;
296  
297 1233 ▶ unless (defined $p) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $p) 7 1226 1233
298 7 my $i = 0;
299 return Mojo::Collection->new(
300 14 ▶ map { $self->_get([@$pointer], {%$state, path => [@$path, $_->[1]], schema => $_->[0]}, $cb) }
Branch: 1 branch
BranchTrueFalseTotal
ref $schema eq 'ARRAY' ? : 0 7 7
301 0 ▶ ref $schema eq 'ARRAY' ? (map { [$_, $i++] } @$schema)
Branch: 1 branch
BranchTrueFalseTotal
ref $schema eq 'HASH' ? : 7 0 7
302 7 : ref $schema eq 'HASH' ? (map { [$schema->{$_}, $_] } sort keys %$schema)
303 : ([$schema, '']));
304 }
305  
306 1226 $p =~ s!~1!/!g;
307 1226 $p =~ s/~0/~/g;
308 1226 push @$path, $p;
309  
310 1226 ▶ if (ref $schema eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $schema eq 'HASH') { } 1206 20 1226
elsif (ref $schema eq 'ARRAY') { } 20 0 20
311 1206 ▶ return undef unless exists $schema->{$p};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $schema->{$p} 182 1024 1206
312 1024 $schema = $schema->{$p};
313 }
314 elsif (ref $schema eq 'ARRAY') {
315 20 ▶ return undef unless $p =~ /^\d+$/ and @$schema > $p;
Branch: 1 branch
BranchTrueFalseTotal
unless $p =~ /^\d+$/u and @$schema > $p 0 20 20
Condition33%1/3
$p =~ /^\d+$/u and @$schema > $p
!ll&&!rl&&r
0020
MC/DC0%0/2
$p =~ /^\d+$/u and @$schema > $p
$p =~ /^\d+$/u @$schema > $p
Truth table33%1/3
A and B
A= $p =~ /^\d+$/uB= @$schema > $p
ABresult
0X0
100
111
316 20 $schema = $schema->[$p];
317 }
318 else {
319 0 return undef;
320 }
321  
322 1044 $follow_ref->();
323 }
324  
325 340 ▶ return $cb->($schema, E($path)->path) if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 3 337 340
326 337 return $schema;
327 }
328  
329 sub _register_ref {
330 3 ▶ my ($self, $ref, %state) = @_;
Subroutine _register_ref: called CC=5 SCAR=19.1
331 3 ▶ $state{base_url} //= $self->id;
Condition33%1/3
$state{"base_url"} //= $self->id
l!l&&r!l&&!r
030
MC/DC0%0/2
$state{"base_url"} //= $self->id
$state{"base_url"} $self->id
Truth table33%1/3
A //= B
A= $state{"base_url"}B= $self->id
ABresult
1X1
011
000
332 3 ▶ $state{id} //= $self->id;
Condition33%1/3
$state{"id"} //= $self->id
l!l&&r!l&&!r
030
MC/DC0%0/2
$state{"id"} //= $self->id
$state{"id"} $self->id
Truth table33%1/3
A //= B
A= $state{"id"}B= $self->id
ABresult
1X1
011
000
333 3 ▶ $state{root} //= $self->data;
Condition33%1/3
$state{"root"} //= $self->data
l!l&&r!l&&!r
030
MC/DC0%0/2
$state{"root"} //= $self->data
$state{"root"} $self->data
Truth table33%1/3
A //= B
A= $state{"root"}B= $self->data
ABresult
1X1
011
000
334 3 ▶ $state{source} //= '_register_ref';
Condition50%1/2
$state{"source"} //= "_register_ref"
l!l
03
MC/DC0%0/1
$state{"source"} //= "_register_ref"
$state{"source"}
Truth table50%1/2
A
A= $state{"source"}
Aresult
11
00
335 3 $self->_refs->{$ref} = \%state;
336 }
337  
338 sub _resolve_object {
339 6569 ▶ my ($self, $state, $schema, $refs, $found) = @_;
Subroutine _resolve_object: called CC=5 SCAR=16.1
340  
341 6569 ▶ if ($schema->{id} and !ref $schema->{id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"id"} and not ref $schema->{"id"}) 29 6540 6569
Condition100%3/3
$schema->{"id"} and not ref $schema->{"id"}
!ll&&!rl&&r
6531929
MC/DC100%2/2
$schema->{"id"} and not ref $schema->{"id"}
$schema->{"id"} not ref $schema->{"id"}
Truth table100%3/3
A and not(B)
A= $schema->{"id"}B= not ref $schema->{"id"}
ABresult
0X0
100
111
342 29 my $id = uri $schema->{id}, $state->{base_url};
343 29 $self->store->add($id => $schema);
344 29 $state = {%$state, base_url => $id->fragment(undef), id => $id};
345 }
346  
347 6569 ▶ if ($found->{'$ref'} = $schema->{'$ref'} && !ref $schema->{'$ref'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found->{"\$ref"} = $schema->{"\$ref"} && !ref($schema->{"\$ref"})) 2100 4469 6569
Condition100%3/3
$schema->{'$ref'} && !ref($schema->{'$ref'})
!ll&&!rl&&r
4445242100
MC/DC100%2/2
$schema->{'$ref'} && !ref($schema->{'$ref'})
$schema->{'$ref'} !ref($schema->{'$ref'})
Truth table100%3/3
A && not(B)
A= $schema->{'$ref'}B= !ref($schema->{'$ref'})
ABresult
0X0
100
111
348 2100 push @$refs, [$schema, $state];
349 }
350  
351 6569 return $state;
352 }
353  
354 sub _state {
355 3714 ▶ my ($self, $curr, %override) = @_;
Subroutine _state: called CC=6 SCAR=18.4
356 3714 my $schema = $override{schema};
357 3714 my %seen;
358 3714 ▶ while (ref $schema eq 'HASH' and $schema->{'$ref'} and !ref $schema->{'$ref'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $schema eq "HASH" and $schema->{"\$ref"} and not ref $schema->{"\$ref"}) 729 3714 4443
Condition83%5/6
ref $schema eq "HASH" and $schema->{"\$ref"} and not ref $schema->{"\$ref"}
!ll&&!rl&&r
37140729
ref $schema eq "HASH" and $schema->{"\$ref"}
!ll&&!rl&&r
753639729
MC/DC66%2/3
ref $schema eq "HASH" and $schema->{"\$ref"} and not ref $schema->{"\$ref"}
ref $schema eq "HASH" $schema->{"\$ref"} not ref $schema->{"\$ref"}
Truth table75%3/4
A and B and not(C)
A= ref $schema eq "HASH"B= $schema->{"\$ref"}C= not ref $schema->{"\$ref"}
ABCresult
0XX0
10X0
1100
1111
359 729 ▶ last if $seen{$schema}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$schema}++ 0 729 729
360 $schema = $self->_refs->{$schema}{schema}
361 729 ▶ // Carp::confess(qq(resolve() must be called before validate() to lookup "$schema->{'$ref'}".));
Condition33%1/3
$self->_refs->{$schema}{"schema"} // Carp::confess(qq[resolve() must be called before validate() to lookup "$schema->{"\$ref"}".])
l!l&&r!l&&!r
72900
MC/DC0%0/2
$self->_refs->{$schema}{"schema"} // Carp::confess(qq[resolve() must be called before validate() to lookup "$schema->{"\$ref"}".])
$self->_refs->{$schema}{"schema"} Carp::confess(qq[resolve() must be called before validate() to lookup "$schema->{"\$ref"}".])
Truth table33%1/3
A // B
A= $self->_refs->{$schema}{"schema"}B= Carp::confess(qq[resolve() must be called before validate() to lookup "$schema->{"\$ref"}".])
ABresult
1X1
011
000
362 }
363  
364 3714 return {%$curr, %override, schema => $schema};
365 }
366  
367 sub _validate {
368 5989 ▶ my ($self, $data, $state) = @_;
Subroutine _validate: called CC=24 SCAR=31.8
369 5989 my $schema = $state->{schema};
370 5989 ▶ return $schema ? () : E $state->{path}, [not => 'not'] if is_bool $schema;
Branch: 2 branches
BranchTrueFalseTotal
if is_bool($schema) 92 5897 5989
$schema ? : 62 30 92
371  
372 5897 my @errors;
373 5897 ▶ if ($self->recursive_data_protection and 2 == grep { ref $_ && !is_bool($_) } $data, $schema) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->recursive_data_protection and 2 == grep({ref $_ and not is_bool($_);} $data, $schema)) 2981 2916 5897
ref $_ and not is_bool($_) 9008 2760 11768
Condition100%3/3
$self->recursive_data_protection and 2 == grep({ref $_ and not is_bool($_);} $data, $schema)
!ll&&!rl&&r
1329032981
MC/DC100%2/2
$self->recursive_data_protection and 2 == grep({ref $_ and not is_bool($_);} $data, $schema)
$self->recursive_data_protection 2 == grep({ref $_ and not is_bool($_);} $data, $schema)
Truth table100%3/3
A and B
A= $self->recursive_data_protectionB= 2 == grep({ref $_ and not is_bool($_);} $data, $schema)
ABresult
0X0
100
111
374 2981 my $seen_addr = "$schema:$data";
375 2981 ▶ return @{$self->{seen}{$seen_addr}} if $self->{seen}{$seen_addr}; # Avoid recursion
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"seen"}{$seen_addr} 2 2979 2981
376 2979 $self->{seen}{$seen_addr} = \@errors;
377 }
378  
379 5895 ▶ local $_[1] = $data->TO_JSON if blessed $data and $data->can('TO_JSON');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($data) and $data->can("TO_JSON") 4 5891 5895
Condition100%3/3
builtin::blessed($data) and $data->can("TO_JSON")
!ll&&!rl&&r
57441474
MC/DC100%2/2
builtin::blessed($data) and $data->can("TO_JSON")
builtin::blessed($data) $data->can("TO_JSON")
Truth table100%3/3
A and B
A= builtin::blessed($data)B= $data->can("TO_JSON")
ABresult
0X0
100
111
380  
381 5895 ▶ if ($schema->{not}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"not"}) 133 5762 5895
382 133 local $self->{seen} = {};
383 133 my @e = $self->_validate($_[1], $self->_state($state, schema => $schema->{not}));
384 133 ▶ push @errors, E $state->{path}, [not => 'not'] unless @e;
Branch: 1 branch
BranchTrueFalseTotal
unless @e 8 125 133
385 }
386 5895 ▶ if (my $rules = $schema->{allOf}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rules = $schema->{"allOf"}) 128 5767 5895
387 128 push @errors, $self->_validate_all_of($_[1], {%$state, schema => $rules});
388 }
389 5895 ▶ if (my $rules = $schema->{anyOf}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rules = $schema->{"anyOf"}) 188 5707 5895
390 188 push @errors, $self->_validate_any_of($_[1], {%$state, schema => $rules});
391 }
392 5895 ▶ if (my $rules = $schema->{oneOf}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rules = $schema->{"oneOf"}) 255 5640 5895
393 255 push @errors, $self->_validate_one_of($_[1], {%$state, schema => $rules});
394 }
395 5895 ▶ if (exists $schema->{if}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"if"}) 7 5888 5895
396 7 local $self->{seen} = {};
397 my $rules = !$schema->{if}
398 7 ▶ || $self->_validate($_[1], $self->_state($state, schema => $schema->{if})) ? $schema->{else} : $schema->{then};
Branch: 1 branch
BranchTrueFalseTotal
!$schema->{'if'} || $self->_validate($_[1], $self->_state($state, 'schema', $schema->{'if'})) ? : 3 4 7
Condition66%2/3
not $schema->{"if"} or $self->_validate($_[1], $self->_state($state, "schema", $schema->{"if"}))
l!l&&r!l&&!r
034
MC/DC50%1/2
not $schema->{"if"} or $self->_validate($_[1], $self->_state($state, "schema", $schema->{"if"}))
not $schema->{"if"} $self->_validate($_[1], $self->_state($state, "schema", $schema->{"if"}))
Truth table66%2/3
not(A) or B
A= not $schema->{"if"}B= $self->_validate($_[1], $self->_state($state, "schema", $schema->{"if"}))
ABresult
1X1
011
000
399 7 ▶ push @errors, $self->_validate($_[1], $self->_state($state, schema => $rules // {}));
Condition50%1/2
$rules // {}
l!l
70
MC/DC0%0/1
$rules // {}
$rules
Truth table50%1/2
A
A= $rules
Aresult
11
00
400 }
401  
402 5895 ▶ my $type = $schema->{type} || schema_type $schema, $_[1];
Condition100%3/3
$schema->{'type'} || schema_type($schema, $_[1])
l!l&&r!l&&!r
35754431877
MC/DC100%2/2
$schema->{'type'} || schema_type($schema, $_[1])
$schema->{'type'} schema_type($schema, $_[1])
Truth table100%3/3
A || B
A= $schema->{'type'}B= schema_type($schema, $_[1])
ABresult
1X1
011
000
403 5895 ▶ if (ref $type eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $type eq 'ARRAY') { } 265 5630 5895
elsif ($type) { } 3753 1877 5630
404 265 push @errors, $self->_validate_any_of_types($_[1], {%$state, schema => [map { +{%$schema, type => $_} } @$type]});
405 }
406 elsif ($type) {
407 3753 my $method = sprintf '_validate_type_%s', $type;
408 3753 push @errors, $self->$method($_[1], $state);
409 }
410  
411 5895 ▶ return @errors if @errors;
Branch: 1 branch
BranchTrueFalseTotal
if @errors 1282 4613 5895
412  
413 4613 ▶ if (exists $schema->{const}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"const"}) 34 4579 4613
414 34 push @errors, $self->_validate_type_const($_[1], $state);
415 }
416 4613 ▶ if ($schema->{enum}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"enum"}) 325 4288 4613
417 325 push @errors, $self->_validate_type_enum($_[1], $state);
418 }
419  
420 4613 return @errors;
421 }
422  
423 sub _validate_all_of {
424 128 ▶ my ($self, $data, $state) = @_;
Subroutine _validate_all_of: called CC=7 SCAR=19.5
425 128 my (@errors, @errors_with_prefix);
426  
427 128 my $i = 0;
428 128 for my $rule (@{$state->{schema}}) {
429 239 local $self->{seen} = {};
430 239 ▶ next unless my @e = $self->_validate($_[1], $self->_state($state, schema => $rule));
Branch: 1 branch
BranchTrueFalseTotal
unless my(@e) = $self->_validate($_[1], $self->_state($state, "schema", $rule)) 210 29 239
431 29 push @errors, @e;
432 29 push @errors_with_prefix, [$i, @e];
433 }
434 continue {
435 239 $i++;
436 }
437  
438 128 ▶ return if not @errors;
Branch: 1 branch
BranchTrueFalseTotal
unless @errors 106 22 128
439  
440 22 my $e = E $state->{path};
441 return prefix_errors(allOf => @errors_with_prefix)
442 if @errors == 1
443 22 ▶ or (grep { $_->details->[1] ne 'type' or $_->path ne $e->path } @errors);
Branch: 2 branches
BranchTrueFalseTotal
if @errors == 1 or grep {$_->details->[1] ne "type" or $_->path ne $e->path;} @errors 21 1 22
$_->details->[1] ne "type" or $_->path ne $e->path 7 8 15
Condition100%3/3
@errors == 1 or grep {$_->details->[1] ne "type" or $_->path ne $e->path;} @errors
l!l&&r!l&&!r
1561
MC/DC100%2/2
@errors == 1 or grep {$_->details->[1] ne "type" or $_->path ne $e->path;} @errors
@errors == 1 grep {$_->details->[1] ne "type" or $_->path ne $e->path;} @errors
Truth table100%3/3
A or B
A= @errors == 1B= grep {$_->details->[1] ne "type" or $_->path ne $e->path;} @errors
ABresult
1X1
011
000
444  
445 # combine all 'type' errors at the base path together
446 1 my @details = map $_->details, @errors;
447 1 my $want_types = join '/', uniq map $_->[0], @details;
448 1 return $e->details([allOf => type => $want_types, $details[-1][2]]);
449 }
450  
451 sub _validate_any_of_types {
452 265 ▶ my ($self, $data, $state) = @_;
Subroutine _validate_any_of_types: called CC=5 SCAR=16.1
453 265 my @errors;
454  
455 265 for my $rule (@{$state->{schema}}) {
456 297 local $self->{seen} = {};
457 297 ▶ return unless my @e = $self->_validate($_[1], $self->_state($state, schema => $rule));
Branch: 1 branch
BranchTrueFalseTotal
unless my(@e) = $self->_validate($_[1], $self->_state($state, "schema", $rule)) 247 50 297
458 50 push @errors, @e;
459 }
460  
461 # favor a non-type error from one of the rules
462 18 my $e = E $state->{path};
463 18 ▶ if (my @e = grep { $_->details->[1] ne 'type' or $_->path ne $e->path } @errors) {
Branch: 2 branches
BranchTrueFalseTotal
if (my(@e) = grep({$_->details->[1] ne "type" or $_->path ne $e->path;} @errors)) 4 14 18
$_->details->[1] ne "type" or $_->path ne $e->path 32 3 35
464 4 return @e;
465 }
466  
467 # the type didn't match any of the rules: combine the errors together
468 14 my @details = map $_->details, @errors;
469 14 my $want_types = join '/', uniq map $_->[0], @details;
470 14 return $e->details([$want_types => 'type', $details[-1][2]]);
471 }
472  
473 sub _validate_any_of {
474 188 ▶ my ($self, $data, $state) = @_;
Subroutine _validate_any_of: called CC=6 SCAR=17.9
475 188 my (@errors, @errors_with_prefix);
476  
477 188 my $i = 0;
478 188 for my $rule (@{$state->{schema}}) {
479 219 local $self->{seen} = {};
480 219 ▶ return unless my @e = $self->_validate($_[1], $self->_state($state, schema => $rule));
Branch: 1 branch
BranchTrueFalseTotal
unless my(@e) = $self->_validate($_[1], $self->_state($state, "schema", $rule)) 174 45 219
481 45 push @errors, @e;
482 45 push @errors_with_prefix, [$i, @e];
483 }
484 continue {
485 45 $i++;
486 }
487  
488 14 my $e = E $state->{path};
489 return prefix_errors(anyOf => @errors_with_prefix)
490 if @errors == 1
491 14 ▶ or (grep { $_->details->[1] ne 'type' or $_->path ne $e->path } @errors);
Branch: 2 branches
BranchTrueFalseTotal
if @errors == 1 or grep {$_->details->[1] ne "type" or $_->path ne $e->path;} @errors 13 1 14
$_->details->[1] ne "type" or $_->path ne $e->path 10 17 27
Condition100%3/3
@errors == 1 or grep {$_->details->[1] ne "type" or $_->path ne $e->path;} @errors
l!l&&r!l&&!r
1121
MC/DC100%2/2
@errors == 1 or grep {$_->details->[1] ne "type" or $_->path ne $e->path;} @errors
@errors == 1 grep {$_->details->[1] ne "type" or $_->path ne $e->path;} @errors
Truth table100%3/3
A or B
A= @errors == 1B= grep {$_->details->[1] ne "type" or $_->path ne $e->path;} @errors
ABresult
1X1
011
000
492  
493 # combine all 'type' errors at the base path together
494 1 my @details = map $_->details, @errors;
495 1 my $want_types = join '/', uniq map $_->[0], @details;
496 1 return $e->details([anyOf => type => $want_types, $details[-1][2]]);
497 }
498  
499 sub _validate_id {
500 19 ▶ my ($self, $id) = @_;
Subroutine _validate_id: called CC=6 SCAR=18.2
501 19 ▶ return unless length $id;
Branch: 1 branch
BranchTrueFalseTotal
unless length $id 0 19 19
502 19 ▶ return E '/id', 'Fragment not allowed.' if $id =~ /\#./;
Branch: 1 branch
BranchTrueFalseTotal
if $id =~ /\#./u 1 18 19
503 18 ▶ return E '/id', 'Relative URL not allowed.' unless $id =~ /^\w+:/ or -e $id or $id =~ m!^/!;
Branch: 1 branch
BranchTrueFalseTotal
unless $id =~ /^\w+:/u or -e $id or $id =~ m[^/]u 2 16 18
Condition66%4/6
$id =~ /^\w+:/u or -e $id or $id =~ m[^/]u
l!l&&r!l&&!r
1602
$id =~ /^\w+:/u or -e $id
l!l&&r!l&&!r
1602
MC/DC33%1/3
$id =~ /^\w+:/u or -e $id or $id =~ m[^/]u
$id =~ /^\w+:/u -e $id $id =~ m[^/]u
Truth table50%2/4
A or B or C
A= $id =~ /^\w+:/uB= -e $idC= $id =~ m[^/]u
ABCresult
1XX1
01X1
0011
0000
504 16 return;
505 }
506  
507 sub _validate_one_of {
508 255 ▶ my ($self, $data, $state) = @_;
Subroutine _validate_one_of: called CC=10 SCAR=23.0
509 255 my ($path, $schema) = @$state{qw(path schema)};
510 255 my (@errors, @errors_with_prefix);
511  
512 255 my ($i, @passed) = (0);
513 255 for my $rule (@{$state->{schema}}) {
514 544 local $self->{seen} = {};
515 544 my @e = $self->_validate($_[1], $self->_state($state, schema => $rule));
516 544 ▶ push @passed, $i and next unless @e;
Branch: 1 branch
BranchTrueFalseTotal
unless @e 242 302 544
Condition60%3/5
@e || push(@passed, $i) && (next)
l!l&&r!l&&!r
3022420
push @passed, $i and next
!ll
0242
MC/DC0%0/2
@e || push(@passed, $i) && (next)
@e push @passed, $i
Truth table66%2/3
A || B
A= @eB= push @passed, $i
ABresult
1X1
011
000
517 302 push @errors_with_prefix, [$i, @e];
518 302 push @errors, @e;
519 }
520 continue {
521 544 $i++;
522 }
523  
524 255 ▶ return if @passed == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @passed == 1 232 23 255
525 23 ▶ return E $path, [oneOf => 'all_rules_match'] unless @errors;
Branch: 1 branch
BranchTrueFalseTotal
unless @errors 3 20 23
526 20 ▶ return E $path, [oneOf => 'n_rules_match', join(', ', @passed)] if @passed;
Branch: 1 branch
BranchTrueFalseTotal
if @passed 2 18 20
527  
528 18 my $e = E $state->{path};
529 return prefix_errors(oneOf => @errors_with_prefix)
530 if @errors == 1
531 18 ▶ or (grep { $_->details->[1] ne 'type' or $_->path ne $e->path } @errors);
Branch: 2 branches
BranchTrueFalseTotal
if @errors == 1 or grep {$_->details->[1] ne "type" or $_->path ne $e->path;} @errors 17 1 18
$_->details->[1] ne "type" or $_->path ne $e->path 11 39 50
Condition100%3/3
@errors == 1 or grep {$_->details->[1] ne "type" or $_->path ne $e->path;} @errors
l!l&&r!l&&!r
1161
MC/DC100%2/2
@errors == 1 or grep {$_->details->[1] ne "type" or $_->path ne $e->path;} @errors
@errors == 1 grep {$_->details->[1] ne "type" or $_->path ne $e->path;} @errors
Truth table100%3/3
A or B
A= @errors == 1B= grep {$_->details->[1] ne "type" or $_->path ne $e->path;} @errors
ABresult
1X1
011
000
532  
533 # the type didn't match any of the rules: combine the errors together
534 1 my @details = map $_->details, @errors;
535 1 my $want_types = join '/', uniq map $_->[0], @details;
536 1 return $e->details([oneOf => type => $want_types, $details[-1][2]]);
537 }
538  
539 sub _validate_number_max {
540 152 ▶ my ($self, $value, $state, $expected) = @_;
Subroutine _validate_number_max: called CC=8 SCAR=20.8
541 152 my ($path, $schema) = @$state{qw(path schema)};
542 152 my @errors;
543  
544 152 ▶ my $cmp_with = $schema->{exclusiveMaximum} // '';
Condition100%2/2
$schema->{"exclusiveMaximum"} // ""
l!l
4148
MC/DC100%1/1
$schema->{"exclusiveMaximum"} // ""
$schema->{"exclusiveMaximum"}
Truth table100%2/2
A
A= $schema->{"exclusiveMaximum"}
Aresult
11
00
545 152 ▶ if (is_bool $cmp_with) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_bool($cmp_with)) { } 2 150 152
elsif (is_num($cmp_with)) { } 2 148 150
546 2 ▶ push @errors, E $path, [$expected => ex_maximum => $value, $schema->{maximum}] unless $value < $schema->{maximum};
Branch: 1 branch
BranchTrueFalseTotal
unless $value < $schema->{"maximum"} 1 1 2
547 }
548 elsif (is_num $cmp_with) {
549 2 ▶ push @errors, E $path, [$expected => ex_maximum => $value, $cmp_with] unless $value < $cmp_with;
Branch: 1 branch
BranchTrueFalseTotal
unless $value < $cmp_with 1 1 2
550 }
551  
552 152 ▶ if (exists $schema->{maximum}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"maximum"}) 20 132 152
553 20 my $cmp_with = $schema->{maximum};
554 20 ▶ push @errors, E $path, [$expected => maximum => $value, $cmp_with] unless $value <= $cmp_with;
Branch: 1 branch
BranchTrueFalseTotal
unless $value <= $cmp_with 2 18 20
555 }
556  
557 152 return @errors;
558 }
559  
560 sub _validate_number_min {
561 152 ▶ my ($self, $value, $state, $expected) = @_;
Subroutine _validate_number_min: called CC=8 SCAR=20.8
562 152 my ($path, $schema) = @$state{qw(path schema)};
563 152 my @errors;
564  
565 152 ▶ my $cmp_with = $schema->{exclusiveMinimum} // '';
Condition100%2/2
$schema->{"exclusiveMinimum"} // ""
l!l
4148
MC/DC100%1/1
$schema->{"exclusiveMinimum"} // ""
$schema->{"exclusiveMinimum"}
Truth table100%2/2
A
A= $schema->{"exclusiveMinimum"}
Aresult
11
00
566 152 ▶ if (is_bool $cmp_with) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_bool($cmp_with)) { } 2 150 152
elsif (is_num($cmp_with)) { } 2 148 150
567 2 ▶ push @errors, E $path, [$expected => ex_minimum => $value, $schema->{minimum}] unless $value > $schema->{minimum};
Branch: 1 branch
BranchTrueFalseTotal
unless $value > $schema->{"minimum"} 1 1 2
568 }
569 elsif (is_num $cmp_with) {
570 2 ▶ push @errors, E $path, [$expected => ex_minimum => $value, $cmp_with] unless $value > $cmp_with;
Branch: 1 branch
BranchTrueFalseTotal
unless $value > $cmp_with 1 1 2
571 }
572  
573 152 ▶ if (exists $schema->{minimum}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"minimum"}) 31 121 152
574 31 my $cmp_with = $schema->{minimum};
575 31 ▶ push @errors, E $path, [$expected => minimum => $value, $cmp_with] unless $value >= $cmp_with;
Branch: 1 branch
BranchTrueFalseTotal
unless $value >= $cmp_with 7 24 31
576 }
577  
578 152 return @errors;
579 }
580  
581 sub _validate_type_enum {
582 434 ▶ my ($self, $data, $state) = @_;
Subroutine _validate_type_enum: called CC=5 SCAR=16.1
583 434 my $enum = $state->{schema}{enum};
584 434 my $m = data_checksum $data;
585  
586 434 for my $i (@$enum) {
587 1274 ▶ return if $m eq data_checksum $i;
Branch: 1 branch
BranchTrueFalseTotal
if $m eq data_checksum($i) 365 909 1274
588 }
589  
590 69 ▶ $enum = join ', ', map { (!defined or ref) ? Mojo::JSON::encode_json($_) : $_ } @$enum;
Branch: 1 branch
BranchTrueFalseTotal
!defined($_) || ref $_ ? : 10 85 95
Condition66%2/3
not defined $_ or ref $_
l!l&&r!l&&!r
01085
MC/DC50%1/2
not defined $_ or ref $_
not defined $_ ref $_
Truth table66%2/3
not(A) or B
A= not defined $_B= ref $_
ABresult
1X1
011
000
591 69 return E $state->{path}, [enum => enum => $enum];
592 }
593  
594 sub _validate_type_const {
595 58 ▶ my ($self, $data, $state) = @_;
Subroutine _validate_type_const: called CC=2 SCAR=6.9
596 58 my $const = $state->{schema}{const};
597  
598 58 ▶ return if data_checksum($data) eq data_checksum($const);
Branch: 1 branch
BranchTrueFalseTotal
if data_checksum($data) eq data_checksum($const) 35 23 58
599 23 return E $state->{path}, [const => const => Mojo::JSON::encode_json($const)];
600 }
601  
602 sub _validate_format {
603 303 ▶ my ($self, $value, $state) = @_;
Subroutine _validate_format: called CC=3 SCAR=11.0
604 303 my $format = $state->{schema}{format};
605 303 my $code = $self->formats->{$format};
606 303 ▶ return do { warn "Format rule for '$format' is missing"; return } unless $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $code 1 302 303
607 302 ▶ return unless my $err = $code->($value);
Branch: 1 branch
BranchTrueFalseTotal
unless my $err = &$code($value) 235 67 302
608 67 return E $state->{path}, [format => $format, $err];
609 }
610  
611 ▶sub _validate_type_any { }
Subroutine _validate_type_any: called CC=1 SCAR=0.0
612  
613 sub _validate_type_array {
614 163 ▶ my ($self, $data, $state) = @_;
Subroutine _validate_type_array: called CC=23 SCAR=31.4
615 163 my ($path, $schema) = @$state{qw(path schema)};
616 163 my @errors;
617  
618 163 ▶ if (ref $data ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data ne "ARRAY") 71 92 163
619 71 return E $path, [array => type => data_type $data];
620 }
621 92 ▶ if (defined $schema->{minItems} and $schema->{minItems} > @$data) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $schema->{"minItems"} and $schema->{"minItems"} > @$data) 1 91 92
Condition100%3/3
defined $schema->{"minItems"} and $schema->{"minItems"} > @$data
!ll&&!rl&&r
8831
MC/DC100%2/2
defined $schema->{"minItems"} and $schema->{"minItems"} > @$data
defined $schema->{"minItems"} $schema->{"minItems"} > @$data
Truth table100%3/3
A and B
A= defined $schema->{"minItems"}B= $schema->{"minItems"} > @$data
ABresult
0X0
100
111
622 1 push @errors, E $path, [array => minItems => int(@$data), $schema->{minItems}];
623 }
624 92 ▶ if (defined $schema->{maxItems} and $schema->{maxItems} < @$data) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $schema->{"maxItems"} and $schema->{"maxItems"} < @$data) 2 90 92
Condition100%3/3
defined $schema->{"maxItems"} and $schema->{"maxItems"} < @$data
!ll&&!rl&&r
8552
MC/DC100%2/2
defined $schema->{"maxItems"} and $schema->{"maxItems"} < @$data
defined $schema->{"maxItems"} $schema->{"maxItems"} < @$data
Truth table100%3/3
A and B
A= defined $schema->{"maxItems"}B= $schema->{"maxItems"} < @$data
ABresult
0X0
100
111
625 2 push @errors, E $path, [array => maxItems => int(@$data), $schema->{maxItems}];
626 }
627 92 ▶ if ($schema->{uniqueItems}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"uniqueItems"}) 10 82 92
628 10 my %uniq;
629 10 for (@$data) {
630 25 ▶ next if !$uniq{data_checksum($_)}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $uniq{data_checksum($_)}++ 20 5 25
631 5 push @errors, E $path, [array => 'uniqueItems'];
632 5 last;
633 }
634 }
635  
636 92 ▶ if (exists $schema->{contains}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"contains"}) 8 84 92
637 8 my ($contains, @e) = ($self->_state($state, schema => $schema->{contains}));
638 8 for my $i (0 .. @$data - 1) {
639 13 my @tmp = $self->_validate($data->[$i], {%$contains, path => [@$path, $i]});
640 13 ▶ push @e, \@tmp if @tmp;
Branch: 1 branch
BranchTrueFalseTotal
if @tmp 10 3 13
641 }
642 8 ▶ push @errors, map {@$_} @e if @e >= @$data;
Branch: 1 branch
BranchTrueFalseTotal
if @e >= @$data 5 3 8
643 8 ▶ push @errors, E $path, [array => 'contains'] if not @$data;
Branch: 1 branch
BranchTrueFalseTotal
unless @$data 1 7 8
644 }
645  
646 92 ▶ if (ref $schema->{items} eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $schema->{'items'} eq 'ARRAY') { } 13 79 92
elsif (exists $schema->{'items'}) { } 62 17 79
647 13 ▶ my $additional_items = $schema->{additionalItems} // {};
Condition100%2/2
$schema->{"additionalItems"} // {}
l!l
58
MC/DC100%1/1
$schema->{"additionalItems"} // {}
$schema->{"additionalItems"}
Truth table100%2/2
A
A= $schema->{"additionalItems"}
Aresult
11
00
648 13 my @rules = @{$schema->{items}};
649  
650 13 ▶ if ($additional_items) {
Branch: 1 branch
BranchTrueFalseTotal
if ($additional_items) 8 5 13
651 8 ▶ push @rules, $additional_items while @rules < @$data;
Branch: 1 branch
BranchTrueFalseTotal
@rules < @$data and do { push @rules, $additional_items } 2 8 10
652 }
653  
654 13 ▶ if (@rules >= @$data) {
Branch: 2 branches
BranchTrueFalseTotal
if (@rules >= @$data) { } 8 5 13
elsif (not $additional_items) { } 5 0 5
655 8 for my $i (0 .. @$data - 1) {
656 24 push @errors, $self->_validate($data->[$i], $self->_state($state, path => [@$path, $i], schema => $rules[$i]));
657 }
658 }
659 elsif (!$additional_items) {
660 5 push @errors, E $path, [array => additionalItems => int(@$data), int(@rules)];
661 }
662 }
663 elsif (exists $schema->{items}) {
664 62 my $items = $self->_state($state, schema => $schema->{items});
665 62 for my $i (0 .. @$data - 1) {
666 130 push @errors, $self->_validate($data->[$i], {%$items, path => [@$path, $i]});
667 }
668 }
669  
670 92 return @errors;
671 }
672  
673 sub _validate_type_boolean {
674 123 ▶ my ($self, $value, $state) = @_;
Subroutine _validate_type_boolean: called CC=6 SCAR=17.9
675  
676 # String that looks like a boolean
677 123 ▶ if (defined $value and $self->{coerce}{booleans}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and $self->{"coerce"}{"booleans"}) 41 82 123
Condition100%3/3
defined $value and $self->{"coerce"}{"booleans"}
!ll&&!rl&&r
18141
MC/DC100%2/2
defined $value and $self->{"coerce"}{"booleans"}
defined $value $self->{"coerce"}{"booleans"}
Truth table100%3/3
A and B
A= defined $valueB= $self->{"coerce"}{"booleans"}
ABresult
0X0
100
111
678 41 ▶ $_[1] = false if $value =~ m!^(0|false|)$!;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^(0|false|)$/u 10 31 41
679 41 ▶ $_[1] = true if $value =~ m!^(1|true)$!;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^(1|true)$/u 25 16 41
680 }
681  
682 123 ▶ return if is_bool $_[1];
Branch: 1 branch
BranchTrueFalseTotal
if is_bool($_[1]) 88 35 123
683 35 return E $state->{path}, [boolean => type => data_type $value];
684 }
685  
686 sub _validate_type_integer {
687 218 ▶ my ($self, $value, $state) = @_;
Subroutine _validate_type_integer: called CC=3 SCAR=11.0
688 218 my @errors = $self->_validate_type_number($_[1], $state, 'integer');
689  
690 218 ▶ return @errors if @errors;
Branch: 1 branch
BranchTrueFalseTotal
if @errors 68 150 218
691 150 ▶ return if $value =~ /^-?\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^-?\d+$/u 149 1 150
692 1 return E $state->{path}, [integer => type => data_type $value];
693 }
694  
695 sub _validate_type_null {
696 6 ▶ my ($self, $value, $state) = @_;
Subroutine _validate_type_null: called CC=2 SCAR=6.9
697  
698 6 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 3 3 6
699 3 return E $state->{path}, [null => type => data_type $value];
700 }
701  
702 sub _validate_type_number {
703 393 ▶ my ($self, $value, $state, $expected) = @_;
Subroutine _validate_type_number: called CC=10 SCAR=23.0
704 393 my @errors;
705  
706 393 ▶ $expected ||= 'number';
Condition100%2/2
$expected ||= "number"
l!l
218175
MC/DC100%1/1
$expected ||= "number"
$expected
Truth table100%2/2
A
A= $expected
Aresult
11
00
707  
708 393 ▶ if (!defined $value or ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value or ref $value) 3 390 393
Condition100%3/3
not defined $value or ref $value
l!l&&r!l&&!r
12390
MC/DC100%2/2
not defined $value or ref $value
not defined $value ref $value
Truth table100%3/3
not(A) or B
A= not defined $valueB= ref $value
ABresult
1X1
011
000
709 3 return E $state->{path}, [$expected => type => data_type $value];
710 }
711 390 ▶ unless (is_num $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_num($value)) 186 204 390
712 return E $state->{path}, [$expected => type => data_type $value]
713 186 ▶ if !$self->{coerce}{numbers} or $value !~ /^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"coerce"}{"numbers"} and $value =~ /^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$/u 79 107 186
Condition100%3/3
$self->{"coerce"}{"numbers"} and $value =~ /^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$/u
!ll&&!rl&&r
736107
MC/DC100%2/2
$self->{"coerce"}{"numbers"} and $value =~ /^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$/u
$self->{"coerce"}{"numbers"} $value =~ /^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$/u
Truth table100%3/3
A and B
A= $self->{"coerce"}{"numbers"}B= $value =~ /^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$/u
ABresult
0X0
100
111
714 107 $_[1] = 0 + $value; # coerce input value
715 }
716  
717 311 ▶ push @errors, $self->_validate_format($value, $state) if $state->{schema}{format};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"schema"}{"format"} 16 295 311
718 311 push @errors, $self->_validate_number_max($value, $state, $expected);
719 311 push @errors, $self->_validate_number_min($value, $state, $expected);
720  
721 311 my $d = $state->{schema}{multipleOf};
722 311 ▶ push @errors, E $state->{path}, [$expected => multipleOf => $d] if $d and ($value / $d) =~ /\.[^0]+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $d and ($value / $d) =~ /\.[^0]+$/u 7 304 311
Condition100%3/3
$d and ($value / $d) =~ /\.[^0]+$/u
!ll&&!rl&&r
29687
MC/DC100%2/2
$d and ($value / $d) =~ /\.[^0]+$/u
$d ($value / $d) =~ /\.[^0]+$/u
Truth table100%3/3
A and B
A= $dB= ($value / $d) =~ /\.[^0]+$/u
ABresult
0X0
100
111
723  
724 311 return @errors;
725 }
726  
727 sub _validate_type_object {
728 486 ▶ my ($self, $data, $state) = @_;
Subroutine _validate_type_object: called CC=39 SCAR=36.7
729 486 my ($path, $schema) = @$state{qw(path schema)};
730  
731 486 ▶ return E $path, [object => type => data_type $data] unless ref $data eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 46 440 486
732  
733 440 my @errors;
734 440 my @dkeys = keys %$data;
735 440 ▶ if (defined $schema->{maxProperties} and $schema->{maxProperties} < @dkeys) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $schema->{"maxProperties"} and $schema->{"maxProperties"} < @dkeys) 1 439 440
Condition100%3/3
defined $schema->{"maxProperties"} and $schema->{"maxProperties"} < @dkeys
!ll&&!rl&&r
43721
MC/DC100%2/2
defined $schema->{"maxProperties"} and $schema->{"maxProperties"} < @dkeys
defined $schema->{"maxProperties"} $schema->{"maxProperties"} < @dkeys
Truth table100%3/3
A and B
A= defined $schema->{"maxProperties"}B= $schema->{"maxProperties"} < @dkeys
ABresult
0X0
100
111
736 1 push @errors, E $path, [object => maxProperties => int(@dkeys), $schema->{maxProperties}];
737 }
738 440 ▶ if (defined $schema->{minProperties} and $schema->{minProperties} > @dkeys) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $schema->{"minProperties"} and $schema->{"minProperties"} > @dkeys) 2 438 440
Condition100%3/3
defined $schema->{"minProperties"} and $schema->{"minProperties"} > @dkeys
!ll&&!rl&&r
43622
MC/DC100%2/2
defined $schema->{"minProperties"} and $schema->{"minProperties"} > @dkeys
defined $schema->{"minProperties"} $schema->{"minProperties"} > @dkeys
Truth table100%3/3
A and B
A= defined $schema->{"minProperties"}B= $schema->{"minProperties"} > @dkeys
ABresult
0X0
100
111
739 2 push @errors, E $path, [object => minProperties => int(@dkeys), $schema->{minProperties}];
740 }
741 440 ▶ if (exists $schema->{propertyNames}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"propertyNames"}) 3 437 440
742 3 my $property_names = $self->_state($state, schema => $schema->{propertyNames});
743 3 for my $name (keys %$data) {
744 5 ▶ next unless my @e = $self->_validate($name, $property_names);
Branch: 1 branch
BranchTrueFalseTotal
unless my(@e) = $self->_validate($name, $property_names) 3 2 5
745 2 push @errors, prefix_errors propertyName => map [$name, $_], @e;
746 }
747 }
748  
749 440 my %rules;
750 440 ▶ for my $k (keys %{$schema->{properties} || {}}) {
Branch: 1 branch
BranchTrueFalseTotal
$schema->{"properties"} or {} 59 381 440
751 640 my $r = $schema->{properties}{$k};
752 640 push @{$rules{$k}}, $r;
753 640 ▶ if ($self->{coerce}{defaults} and ref $r eq 'HASH' and exists $r->{default} and !exists $data->{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"coerce"}{"defaults"} and ref $r eq "HASH" and exists $r->{"default"} and not exists $data->{$k}) 1 639 640
Condition66%6/9
$self->{"coerce"}{"defaults"} and ref $r eq "HASH" and exists $r->{"default"} and not exists $data->{$k}
!ll&&!rl&&r
63901
$self->{"coerce"}{"defaults"} and ref $r eq "HASH" and exists $r->{"default"}
!ll&&!rl&&r
63901
$self->{"coerce"}{"defaults"} and ref $r eq "HASH"
!ll&&!rl&&r
63901
MC/DC25%1/4
$self->{"coerce"}{"defaults"} and ref $r eq "HASH" and exists $r->{"default"} and not exists $data->{$k}
$self->{"coerce"}{"defaults"} ref $r eq "HASH" exists $r->{"default"} not exists $data->{$k}
Truth table40%2/5
A and B and C and not(D)
A= $self->{"coerce"}{"defaults"}B= ref $r eq "HASH"C= exists $r->{"default"}D= not exists $data->{$k}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
754 1 $data->{$k} = $r->{default};
755 }
756 }
757  
758 440 ▶ for my $p (keys %{$schema->{patternProperties} || {}}) {
Branch: 1 branch
BranchTrueFalseTotal
$schema->{"patternProperties"} or {} 428 12 440
759 18 my $r = $schema->{patternProperties}{$p};
760 18 push @{$rules{$_}}, $r for grep { $_ =~ /$p/ } @dkeys;
761 }
762  
763 440 ▶ my $additional = exists $schema->{additionalProperties} ? $schema->{additionalProperties} : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $schema->{'additionalProperties'} ? : 10 430 440
764 440 ▶ if ($additional) {
Branch: 2 branches
BranchTrueFalseTotal
if ($additional) { } 437 3 440
elsif (my(@k) = grep({not $rules{$_};} @dkeys)) { } 3 0 3
765 437 ▶ $additional = {} unless is_type $additional, 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless is_type($additional, "HASH") 1 436 437
766 437 ▶ $rules{$_} ||= [$additional] for @dkeys;
Condition100%2/2
$rules{$_} ||= [$additional]
l!l
527155
MC/DC100%1/1
$rules{$_} ||= [$additional]
$rules{$_}
Truth table100%2/2
A
A= $rules{$_}
Aresult
11
00
767 }
768 9 elsif (my @k = grep { !$rules{$_} } @dkeys) {
769 3 local $" = ', ';
770 3 return E $path, [object => additionalProperties => join ', ', sort @k];
771 }
772  
773 437 ▶ for my $k (uniq @{$schema->{required} || []}) {
Branch: 1 branch
BranchTrueFalseTotal
$schema->{"required"} or [] 347 90 437
774 108 ▶ next if exists $data->{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{$k} 77 31 108
775 31 push @errors, E [@$path, $k], [object => 'required'];
776 31 delete $rules{$k};
777 }
778  
779 437 ▶ my $dependencies = $schema->{dependencies} || {};
Condition100%2/2
$schema->{'dependencies'} || {}
l!l
9428
MC/DC100%1/1
$schema->{'dependencies'} || {}
$schema->{'dependencies'}
Truth table100%2/2
A
A= $schema->{'dependencies'}
Aresult
11
00
780 437 for my $k (keys %$dependencies) {
781 11 ▶ next if not exists $data->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $data->{$k} 8 3 11
782 3 ▶ if (ref $dependencies->{$k} eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $dependencies->{$k} eq 'ARRAY') { } 2 1 3
elsif (ref $dependencies->{$k} eq 'HASH') { } 1 0 1
783 push @errors,
784 2 map { E [@$path, $_], [object => dependencies => $k] } grep { !exists $data->{$_} } @{$dependencies->{$k}};
785 }
786 elsif (ref $dependencies->{$k} eq 'HASH') {
787 1 push @errors, $self->_validate_type_object($data, $self->_state($state, schema => $schema->{dependencies}{$k}));
788 }
789 }
790  
791 437 for my $k (keys %rules) {
792 780 for my $r (@{$rules{$k}}) {
793 780 ▶ next unless exists $data->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $data->{$k} 97 683 780
794 683 my $s2 = $self->_state($state, path => [@$path, $k], schema => $r);
795 683 my @e = $self->_validate($data->{$k}, $s2);
796 683 push @errors, @e;
797 683 ▶ next if @e or !is_type $r, 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if @e or not is_type($r, "HASH") 296 387 683
Condition100%3/3
@e or not is_type($r, "HASH")
l!l&&r!l&&!r
2942387
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
798 387 ▶ push @errors, $self->_validate_type_enum($data->{$k}, $s2) if $r->{enum};
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"enum"} 22 365 387
799 387 ▶ push @errors, $self->_validate_type_const($data->{$k}, $s2) if $r->{const};
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"const"} 3 384 387
800 }
801 }
802  
803 437 return @errors;
804 }
805  
806 sub _validate_type_string {
807 1013 ▶ my ($self, $value, $state) = @_;
Subroutine _validate_type_string: called CC=16 SCAR=27.8
808 1013 my ($path, $schema) = @$state{qw(path schema)};
809 1013 my @errors;
810  
811 1013 ▶ if (!$schema->{type} and !defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema->{"type"} or defined $value) 0 1013 1013
Condition66%2/3
$schema->{"type"} or defined $value
l!l&&r!l&&!r
969440
MC/DC0%0/2
$schema->{"type"} or defined $value
$schema->{"type"} defined $value
Truth table66%2/3
A or B
A= $schema->{"type"}B= defined $value
ABresult
1X1
011
000
812 0 return;
813 }
814 1013 ▶ if (!defined $value or ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value or ref $value) 46 967 1013
Condition100%3/3
not defined $value or ref $value
l!l&&r!l&&!r
244967
MC/DC100%2/2
not defined $value or ref $value
not defined $value ref $value
Truth table100%3/3
not(A) or B
A= not defined $valueB= ref $value
ABresult
1X1
011
000
815 46 return E $path, [string => type => data_type $value];
816 }
817 967 ▶ if (B::svref_2object(\$value)->FLAGS & (B::SVp_IOK | B::SVp_NOK) and 0 + $value eq $value and $value * 0 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (B::svref_2object(\$value)->FLAGS & 12288 and 0 + $value eq $value and $value * 0 == 0) 27 940 967
Condition66%4/6
B::svref_2object(\$value)->FLAGS & 12288 and 0 + $value eq $value and $value * 0 == 0
!ll&&!rl&&r
940027
B::svref_2object(\$value)->FLAGS & 12288 and 0 + $value eq $value
!ll&&!rl&&r
940027
MC/DC33%1/3
B::svref_2object(\$value)->FLAGS & 12288 and 0 + $value eq $value and $value * 0 == 0
B::svref_2object(\$value)->FLAGS & 12288 0 + $value eq $value $value * 0 == 0
Truth table50%2/4
A and B and C
A= B::svref_2object(\$value)->FLAGS & 12288B= 0 + $value eq $valueC= $value * 0 == 0
ABCresult
0XX0
10X0
1100
1111
818 27 ▶ return E $path, [string => type => data_type $value] unless $self->{coerce}{strings};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"coerce"}{"strings"} 24 3 27
819 3 $_[1] = "$value"; # coerce input value
820 }
821 943 ▶ if ($schema->{format}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"format"}) 287 656 943
822 287 push @errors, $self->_validate_format($value, $state);
823 }
824 943 ▶ if (defined $schema->{maxLength}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $schema->{"maxLength"}) 45 898 943
825 45 ▶ if (length($value) > $schema->{maxLength}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $value > $schema->{"maxLength"}) 16 29 45
826 16 push @errors, E $path, [string => maxLength => length($value), $schema->{maxLength}];
827 }
828 }
829 943 ▶ if (defined $schema->{minLength}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $schema->{"minLength"}) 37 906 943
830 37 ▶ if (length($value) < $schema->{minLength}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $value < $schema->{"minLength"}) 3 34 37
831 3 push @errors, E $path, [string => minLength => length($value), $schema->{minLength}];
832 }
833 }
834 943 ▶ if (defined $schema->{pattern}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $schema->{"pattern"}) 43 900 943
835 43 my $p = $schema->{pattern};
836 43 ▶ push @errors, E $path, [string => pattern => $p] unless $value =~ /$p/;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /$p/u 13 30 43
837 }
838  
839 943 return @errors;
840 }
841  
842 80 1;
843  
844 =encoding utf8
845  
846 =head1 NAME
847  
848 JSON::Validator::Schema - Base class for JSON::Validator schemas
849  
850 =head1 SYNOPSIS
851  
852 =head2 Basics
853  
854 # Create a new schema from a file on disk
855 # It is also possible to create the object from JSON::Validator::Schema,
856 # but you most likely want to use one of the subclasses.
857 my $schema = JSON::Validator::Schema::Draft7->new('file:///cool/beans.yaml');
858  
859 # Validate the schema
860 die $schema->errors->[0] if $schema->is_invalid;
861  
862 # Validate data
863 my @errors = $schema->validate({some => 'data'});
864 die $errors[0] if @errors;
865  
866 =head2 Shared store
867  
868 my $store = JSON::Validator::Store->new;
869 my $schema = JSON::Validator::Schema::Draft7->new(store => $store);
870  
871 # Will not fetch the file from web, if the $store has already retrieved
872 # the schema
873 $schema->resolve('https://api.example.com/cool/beans.json');
874  
875 =head2 Make a new validation class
876  
877 package JSON::Validator::Schema::SomeSchema;
878 use Mojo::Base 'JSON::Validator::Schema';
879 has specification => 'https://api.example.com/my/spec.json#';
880 1;
881  
882 =head1 DESCRIPTION
883  
884 L<JSON::Validator::Schema> is the base class for
885 L<JSON::Validator::Schema::Draft4>,
886 L<JSON::Validator::Schema::Draft6>,
887 L<JSON::Validator::Schema::Draft7>,
888 L<JSON::Validator::Schema::Draft201909>,
889 L<JSON::Validator::Schema::OpenAPIv2> and
890 L<JSON::Validator::Schema::OpenAPIv3>.
891  
892 Any of the classes above can be used instead of L<JSON::Validator> if you know
893 which draft/version you are working with up front.
894  
895 =head2 Validation
896  
897 L<JSON::Validator::Schema> can both validate user input and the schema itself.
898  
899 =over 2
900  
901 =item *
902  
903 A L<JSON::Validator::Schema> represents a set of validation rules stored in
904 L</data>. The rules stored in the L</data> attribute will be used when calling
905 the L</validate> method.
906  
907 =item *
908  
909 The input to C<validate()> could be some data from a web request or some other
910 user input. C<validate()> returns a list of L<JSON::Validator::Error> objects,
911 if the user input (input to C<validate()>) contains invalid data.
912  
913 =item *
914  
915 The L</errors> and L</is_invalid> attributes has nothing to do with user input,
916 meaning it is I<not> relevant for L</validate>. These two accessors are used to
917 check if the rules/schema stored in L</data> is correct. The validation is
918 performed against the L</specification>. This is pretty much the same as:
919  
920 my $jv = JSON::Validator->new;
921 my $draft7 = $jv->schema('http://json-schema.org/draft-07/schema#')->schema;
922 my $schema = $jv->schema({name => {type => 'string'}})->schema;
923 my @errors = $draft7->validate($schema->data);
924  
925 =back
926  
927 =head1 ATTRIBUTES
928  
929 =head2 errors
930  
931 $array_ref = $schema->errors;
932  
933 Holds the errors after checking L</data> against L</specification>.
934 C<$array_ref> containing no elements means L</data> is valid. Each element in
935 the array-ref is a L<JSON::Validator::Error> object.
936  
937 This attribute is I<not> changed by L</validate>. It only reflects if the
938 C<$schema> is valid.
939  
940 =head2 formats
941  
942 $hash_ref = $schema->formats;
943 $schema = $schema->formats(\%hash);
944  
945 Holds a hash-ref, where the keys are supported JSON type "formats", and
946 the associated values hold code blocks which can validate the given format.
947 A code block should return C<undef> on success and an error string on error:
948  
949 sub { return defined $_[0] && $_[0] eq "42" ? undef : "Not the answer." };
950  
951 See L<JSON::Validator::Formats> for a list of supported formats.
952  
953 =head2 recursive_data_protection
954  
955 The value of this attribute will be copied into the created L</schema>. See
956 L<JSON::Validator::Schema/recursive_data_protection> for more details.
957  
958 =head2 id
959  
960 $str = $schema->id;
961 $schema = $schema->id($str);
962  
963 Holds the ID for this schema. Usually extracted from C<"$id"> or C<"id"> in
964 L</data>.
965  
966 =head2 moniker
967  
968 $str = $schema->moniker;
969 $schema = $schema->moniker("some_name");
970  
971 Used to get/set the moniker for the given schema. Will be "draft04" if
972 L</specification> points to a JSON Schema draft URL, and fallback to
973 empty string if unable to guess a moniker name.
974  
975 This attribute will (probably) detect more monikers from a given
976 L</specification> or C</id> in the future.
977  
978 =head2 recursive_data_protection
979  
980 $schema = $schema->recursive_data_protection($bool);
981 $bool = $schema->recursive_data_protection;
982  
983 Recursive data protection is active by default, however it can be deactivated
984 by assigning a false value to the L</recursive_data_protection> attribute.
985  
986 Recursive data protection can have a noticeable impact on memory usage when
987 validating large data structures. If you are encountering issues with memory
988 and you can guarantee that you do not have any loops in your data structure
989 then deactivating the recursive data protection may help.
990  
991 This attribute is EXPERIMENTAL and may change in a future release.
992  
993 B<Disclaimer: Use at your own risk, if you have any doubt then don't use it>
994  
995 =head2 specification
996  
997 $str = $schema->specification;
998 $schema = $schema->specification($str);
999  
1000 The URL to the specification used when checking for L</errors>. Usually
1001 extracted from C<"$schema"> or C<"schema"> in L</data>.
1002  
1003 =head2 store
1004  
1005 $store = $schema->store;
1006  
1007 Holds a L<JSON::Validator::Store> object that caches the retrieved schemas.
1008 This object can be shared amongst different schema objects to prevent
1009 a schema from having to be downloaded again.
1010  
1011 =head1 METHODS
1012  
1013 =head2 bundle
1014  
1015 $bundled = $schema->bundle;
1016  
1017 C<$bundled> is a new L<JSON::Validator::Schema> object where none of the "$ref"
1018 will point to external resources. This can be useful, if you want to have a
1019 bunch of files locally, but hand over a single file to a client.
1020  
1021 Mojo::File->new("client.json")
1022 ->spurt(Mojo::JSON::to_json($schema->bundle->data));
1023  
1024 =head2 coerce
1025  
1026 $schema = $schema->coerce('bool,def,num,str');
1027 $schema = $schema->coerce('booleans,defaults,numbers,strings');
1028 $hash_ref = $schema->coerce;
1029  
1030 Set the given type to coerce. Before enabling coercion this module is very
1031 strict when it comes to validating types. Example: The string C<"1"> is not the
1032 same as the number C<1>, unless you have "numbers" coercion enabled.
1033  
1034 =over 2
1035  
1036 =item * booleans
1037  
1038 Will convert what could be interpreted as a boolean (that is, an actual
1039 numeric C<1> or C<0>, and the strings "true" and "false") to a
1040 L<JSON::PP::Boolean> object. Note that "foo" is not considered a true value and
1041 will fail the validation.
1042  
1043 =item * defaults
1044  
1045 Will copy the default value defined in the schema, into the input structure,
1046 if the input value is non-existing.
1047  
1048 Note that support for "default" is currently EXPERIMENTAL, and enabling this
1049 might be changed in future versions.
1050  
1051 =item * numbers
1052  
1053 Will convert strings that look like numbers, into true numbers. This works for
1054 both the "integer" and "number" types.
1055  
1056 =item * strings
1057  
1058 Will convert a number into a string. This works for the "string" type.
1059  
1060 =back
1061  
1062 =head2 contains
1063  
1064 This method will be removed in a future release.
1065  
1066 =head2 data
1067  
1068 my $hash_ref = $schema->data;
1069 my $schema = $schema->data($bool);
1070 my $schema = $schema->data($hash_ref);
1071  
1072 Will set a structure representing the schema. In most cases you want to
1073 use L</resolve> instead of L</data>.
1074  
1075 =head2 get
1076  
1077 my $data = $schema->get([@json_pointer]);
1078 my $data = $schema->get($json_pointer);
1079 my $data = $schema->get($json_pointer, sub { my ($data, $json_pointer) = @_; });
1080  
1081 This method will extract data from L</data>, using a C<$json_pointer> -
1082 L<RFC 6901|http://tools.ietf.org/html/rfc6901>. It can however be used in a more
1083 complex way by passing in an array-ref: The array-ref can contain C<undef()>
1084 values, will result in extracting any element on that point, regardless of
1085 value. In that case a L<Mojo::Collection> will be returned.
1086  
1087 A callback can also be given. This callback will be called each time the
1088 C<$json_pointer> matches some data, and will pass in the C<$json_pointer> at
1089 that place.
1090  
1091 In addition if this method "sees" a JSON-Schema C<$ref> on the way, the "$ref"
1092 will be followed into any given sub-schema.
1093  
1094 =head2 is_invalid
1095  
1096 my $bool = $schema->is_invalid;
1097  
1098 Returns true if the schema in L</data> is invalid. Internally this method calls
1099 L</errors> which will validate L</data> against L</specification>.
1100  
1101 =head2 load_and_validate_schema
1102  
1103 This method is unsupported. Use L</is_invalid> or L</errors> instead.
1104  
1105 =head2 new
1106  
1107 my $schema = JSON::Validator::Schema->new($data);
1108 my $schema = JSON::Validator::Schema->new($data, %attributes);
1109 my $schema = JSON::Validator::Schema->new(%attributes);
1110  
1111 Construct a new L<JSON::Validator::Schema> object. Passing C<$data> as the
1112 first argument will cause L</resolve> to be called, meaning the constructor
1113 might throw an exception if the schema could not be successfully resolved.
1114  
1115 =head2 resolve
1116  
1117 $schema = $schema->resolve($uri);
1118 $schema = $schema->resolve($data);
1119  
1120 Used to resolve L<$uri> or L<$data> and store the resolved schema in L</data>.
1121 If C<$data> or C<$uri> contain any "$ref", then these schemas will be
1122 downloaded and resolved as well.
1123  
1124 If L</data> does not contain an "id" or "$id", then L</id> will be assigned a
1125 autogenerated "urn". This "urn" might be changed in future releases, but should
1126 always be the same for the same L</data>.
1127  
1128 =head2 schema
1129  
1130 This method will be removed in a future release.
1131  
1132 =head2 validate
1133  
1134 @errors = $schema->validate($any);
1135  
1136 Will validate C<$any> against the schema defined in L</data>. Each element in
1137 C<@errors> is an L<JSON::Validator::Error> object.
1138  
1139 =head1 SEE ALSO
1140  
1141 L<JSON::Validator>.
1142  
1143 =cut
« Validator/Schema/Draft4.pm ↑ Summary Validator/Util.pm »