Validator/Schema/Draft201909.pm

filter: Statementstmt 97.1% 101 / 104 Branchbran 77.5% 31 / 40 Conditioncond 80.9% 34 / 42 MC/DCmcdc 64.0% 16 / 25 Subroutinesub 92.8% 13 / 14 Podpod n/a totaltotal 86.6% 195 / 225 CC 40 SCAR 23.0
CC
40
Cov
89%
_validate_type_array_contains
17.6
_state
9.5
_resolve_object
9.0
CRAP 9.9
« Validator/Schema/OpenAPIv3.pm ↑ Summary Validator/Schema/OpenAPIv2.pm »
1 package JSON::Validator::Schema::Draft201909;
2 13 ▶use Mojo::Base 'JSON::Validator::Schema';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 13 ▶use JSON::Validator::Schema::Draft4;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use JSON::Validator::Schema::Draft6;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use JSON::Validator::Schema::Draft7;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use JSON::Validator::URI qw(uri);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use JSON::Validator::Util qw(E is_bool is_type);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 13 has moniker => 'draft2019';
11 13 has specification => 'https://json-schema.org/draft/2019-09/schema';
12 16 ▶has _ref_keys => sub { [qw($ref $recursiveRef)] };
Subroutine __ANON__: called CC=1 SCAR=0.0
13  
14 2 ▶sub _build_formats {
Subroutine _build_formats: called CC=1 SCAR=0.0
15 2 my $formats = shift->JSON::Validator::Schema::Draft7::_build_formats;
16 2 $formats->{duration} = JSON::Validator::Formats->can('check_duration');
17 2 $formats->{uuid} = JSON::Validator::Formats->can('check_uuid');
18 2 return $formats;
19 }
20  
21 0 ▶sub _bundle_ref_path_expand { local $_ = $_[1]; s!^\$defs/!!; return '$defs', $_; }
Subroutine _bundle_ref_path_expand: not called CC=1 SCAR=6.9
22 59785 ▶sub _extract_ref_from_schema { $_[1]->{'$recursiveRef'} // $_[1]->{'$ref'} }
Subroutine _extract_ref_from_schema: called CC=2 SCAR=7.2
Condition66%2/3
$_[1]{"\$recursiveRef"} // $_[1]{"\$ref"}
l!l&&r!l&&!r
22597630
MC/DC0%0/2
$_[1]{"\$recursiveRef"} // $_[1]{"\$ref"}
$_[1]{"\$recursiveRef"} $_[1]{"\$ref"}
Truth table66%2/3
A // B
A= $_[1]{"\$recursiveRef"}B= $_[1]{"\$ref"}
ABresult
1X1
011
000
23  
24 207880 ▶sub _resolve_object {
Subroutine _resolve_object: called CC=9 SCAR=22.0
25 207880 my ($self, $state, $schema, $refs, $found) = @_;
26  
27 207880 ▶ if ($schema->{'$id'} and !ref $schema->{'$id'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"\$id"} and not ref $schema->{"\$id"}) 512 207368 207880
Condition100%3/3
$schema->{"\$id"} and not ref $schema->{"\$id"}
!ll&&!rl&&r
2073671512
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
28 512 my $id = uri $schema->{'$id'}, $state->{base_url};
29 512 $self->store->add($id => $schema);
30 512 $state = {%$state}; # make sure we don't mutate $state ref
31 512 $state->{base_url} = $id->clone->fragment(undef);
32 }
33 207880 ▶ if ($schema->{'$anchor'} && !ref $schema->{'$anchor'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"\$anchor"} and not ref $schema->{"\$anchor"}) 1 207879 207880
Condition100%3/3
$schema->{"\$anchor"} and not ref $schema->{"\$anchor"}
!ll&&!rl&&r
20787811
MC/DC100%2/2
$schema->{"\$anchor"} and not ref $schema->{"\$anchor"}
$schema->{"\$anchor"} not ref $schema->{"\$anchor"}
Truth table100%3/3
A and not(B)
A= $schema->{"\$anchor"}B= not ref $schema->{"\$anchor"}
ABresult
0X0
100
111
34 1 my $id = uri(uri()->new->fragment($schema->{'$anchor'}), $state->{base_url});
35 1 $self->store->add($id => $schema);
36 1 $state = {%$state, base_url => $id->fragment(undef)->to_string};
37 }
38  
39 207880 ▶ if ($found->{'$recursiveRef'} = $schema->{'$recursiveRef'} && !ref $schema->{'$recursiveRef'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found->{"\$recursiveRef"} = $schema->{"\$recursiveRef"} && !ref($schema->{"\$recursiveRef"})) 22 207858 207880
Condition100%3/3
$schema->{'$recursiveRef'} && !ref($schema->{'$recursiveRef'})
!ll&&!rl&&r
207857122
MC/DC100%2/2
$schema->{'$recursiveRef'} && !ref($schema->{'$recursiveRef'})
$schema->{'$recursiveRef'} !ref($schema->{'$recursiveRef'})
Truth table100%3/3
A && not(B)
A= $schema->{'$recursiveRef'}B= !ref($schema->{'$recursiveRef'})
ABresult
0X0
100
111
40 22 push @$refs, [$schema, $state];
41 }
42 207880 ▶ if ($found->{'$ref'} = $schema->{'$ref'} && !ref $schema->{'$ref'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found->{"\$ref"} = $schema->{"\$ref"} && !ref($schema->{"\$ref"})) 59763 148117 207880
Condition100%3/3
$schema->{'$ref'} && !ref($schema->{'$ref'})
!ll&&!rl&&r
14761050759763
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
43 59763 push @$refs, [$schema, $state];
44 }
45  
46 207880 return $state;
47 }
48  
49 47709 ▶sub _state {
Subroutine _state: called CC=9 SCAR=22.5
50 47709 my ($self, $curr, %override) = @_;
51 47709 my $schema = $override{schema};
52 47709 my (%alongside, %seen);
53  
54 47709 ▶ while (ref $schema eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $schema eq "HASH") 55169 0 55169
55 55169 ▶ last unless my $ref = $schema->{'$ref'} || $schema->{'$recursiveRef'};
Branch: 1 branch
BranchTrueFalseTotal
unless my $ref = $schema->{"\$ref"} || $schema->{"\$recursiveRef"} 47709 7460 55169
Condition100%3/3
$schema->{'$ref'} || $schema->{'$recursiveRef'}
l!l&&r!l&&!r
74362447709
MC/DC100%2/2
$schema->{'$ref'} || $schema->{'$recursiveRef'}
$schema->{'$ref'} $schema->{'$recursiveRef'}
Truth table100%3/3
A || B
A= $schema->{'$ref'}B= $schema->{'$recursiveRef'}
ABresult
1X1
011
000
56 7460 ▶ last if ref $ref;
Branch: 1 branch
BranchTrueFalseTotal
if ref $ref 0 7460 7460
57 7460 ▶ last if $seen{$schema}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$schema}++ 0 7460 7460
58  
59 7460 %alongside = (%alongside, %$schema);
60 $schema = $self->_refs->{$schema}{schema}
61 7460 ▶ // Carp::confess(qq(You have to call resolve() before validate() to lookup "$ref".));
Condition50%1/2
$self->_refs->{$schema}{"schema"} // Carp::confess(qq[You have to call resolve() before validate() to lookup "$ref".])
l!l
74600
MC/DC0%0/1
$self->_refs->{$schema}{"schema"} // Carp::confess(qq[You have to call resolve() before validate() to lookup "$ref".])
$self->_refs->{$schema}{"schema"}
Truth table50%1/2
A
A= $self->_refs->{$schema}{"schema"}
Aresult
11
00
62 }
63  
64 47709 ▶ return {%$curr, %override, schema => $schema} unless ref $schema eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $schema eq "HASH" 0 47709 47709
65  
66 47709 delete $alongside{$_} for qw($anchor $id $recursiveAnchor $recursiveRef $ref);
67 47709 return {%$curr, %override, schema => {%alongside, %$schema}};
68 }
69  
70 63 ▶sub _validate_type_array_contains {
Subroutine _validate_type_array_contains: called CC=16 SCAR=28.7
71 63 my ($self, $data, $state) = @_;
72 63 my ($path, $schema) = @$state{qw(path schema)};
73 63 ▶ return unless exists $schema->{contains};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $schema->{"contains"} 57 6 63
74 6 ▶ return if defined $schema->{minContains} and $schema->{minContains} == 0 and !$schema->{maxContains};
Branch: 1 branch
BranchTrueFalseTotal
if defined $schema->{"minContains"} and $schema->{"minContains"} == 0 and not $schema->{"maxContains"} 0 6 6
Condition50%3/6
defined $schema->{"minContains"} and $schema->{"minContains"} == 0 and not $schema->{"maxContains"}
!ll&&!rl&&r
600
defined $schema->{"minContains"} and $schema->{"minContains"} == 0
!ll&&!rl&&r
240
MC/DC0%0/3
defined $schema->{"minContains"} and $schema->{"minContains"} == 0 and not $schema->{"maxContains"}
defined $schema->{"minContains"} $schema->{"minContains"} == 0 not $schema->{"maxContains"}
Truth table50%2/4
A and B and not(C)
A= defined $schema->{"minContains"}B= $schema->{"minContains"} == 0C= not $schema->{"maxContains"}
ABCresult
0XX0
10X0
1100
1111
75 6 ▶ return if defined $schema->{minContains} and $schema->{minContains} == 0 and !@$data;
Branch: 1 branch
BranchTrueFalseTotal
if defined $schema->{"minContains"} and $schema->{"minContains"} == 0 and not @$data 0 6 6
Condition50%3/6
defined $schema->{"minContains"} and $schema->{"minContains"} == 0 and not @$data
!ll&&!rl&&r
600
defined $schema->{"minContains"} and $schema->{"minContains"} == 0
!ll&&!rl&&r
240
MC/DC0%0/3
defined $schema->{"minContains"} and $schema->{"minContains"} == 0 and not @$data
defined $schema->{"minContains"} $schema->{"minContains"} == 0 not @$data
Truth table50%2/4
A and B and not(C)
A= defined $schema->{"minContains"}B= $schema->{"minContains"} == 0C= not @$data
ABCresult
0XX0
10X0
1100
1111
76  
77 6 my ($n_valid, @e, @errors) = (0);
78 6 for my $i (0 .. @$data - 1) {
79 14 my @tmp = $self->_validate($data->[$i], $self->_state($state, path => [@$path, $i], schema => $schema->{contains}));
80 14 ▶ @tmp ? push @e, \@tmp : $n_valid++;
Branch: 1 branch
BranchTrueFalseTotal
@tmp ? : 3 11 14
81 }
82  
83 6 ▶ push @errors, map {@$_} @e if @e >= @$data;
Branch: 1 branch
BranchTrueFalseTotal
if @e >= @$data 1 5 6
84 push @errors, E $path, [array => 'maxContains', int @$data, $schema->{maxContains}]
85 6 ▶ if exists $schema->{maxContains} and $n_valid > $schema->{maxContains};
Branch: 1 branch
BranchTrueFalseTotal
if exists $schema->{"maxContains"} and $n_valid > $schema->{"maxContains"} 1 5 6
Condition100%3/3
exists $schema->{"maxContains"} and $n_valid > $schema->{"maxContains"}
!ll&&!rl&&r
231
MC/DC100%2/2
exists $schema->{"maxContains"} and $n_valid > $schema->{"maxContains"}
exists $schema->{"maxContains"} $n_valid > $schema->{"maxContains"}
Truth table100%3/3
A and B
A= exists $schema->{"maxContains"}B= $n_valid > $schema->{"maxContains"}
ABresult
0X0
100
111
86 push @errors, E $path, [array => 'minContains', int @$data, $schema->{minContains}]
87 6 ▶ if $schema->{minContains} and $n_valid < $schema->{minContains};
Branch: 1 branch
BranchTrueFalseTotal
if $schema->{"minContains"} and $n_valid < $schema->{"minContains"} 1 5 6
Condition100%3/3
$schema->{"minContains"} and $n_valid < $schema->{"minContains"}
!ll&&!rl&&r
231
MC/DC100%2/2
$schema->{"minContains"} and $n_valid < $schema->{"minContains"}
$schema->{"minContains"} $n_valid < $schema->{"minContains"}
Truth table100%3/3
A and B
A= $schema->{"minContains"}B= $n_valid < $schema->{"minContains"}
ABresult
0X0
100
111
88 6 ▶ push @errors, E $path, [array => 'contains'] if not @$data;
Branch: 1 branch
BranchTrueFalseTotal
unless @$data 0 6 6
89 6 return @errors;
90 }
91  
92 10675 ▶sub _validate_type_object_dependencies {
Subroutine _validate_type_object_dependencies: called CC=8 SCAR=20.8
93 10675 my ($self, $data, $state) = @_;
94 10675 ▶ my $dependencies = $state->{schema}{dependentSchemas} || {};
Condition100%2/2
$state->{'schema'}{'dependentSchemas'} || {}
l!l
50110174
MC/DC100%1/1
$state->{'schema'}{'dependentSchemas'} || {}
$state->{'schema'}{'dependentSchemas'}
Truth table100%2/2
A
A= $state->{'schema'}{'dependentSchemas'}
Aresult
11
00
95 10675 my @errors;
96  
97 10675 for my $k (keys %$dependencies) {
98 501 ▶ next if not exists $data->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $data->{$k} 0 501 501
99 501 ▶ if (ref $dependencies->{$k} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dependencies->{$k} eq 'ARRAY') { } 1 500 501
100 push @errors,
101 1 map { E [@{$state->{path}}, $_], [object => dependencies => $k] }
102 1 grep { !exists $data->{$_} } @{$dependencies->{$k}};
103 }
104 else {
105 500 push @errors, $self->_validate($data, $self->_state($state, schema => $dependencies->{$k}));
106 }
107 }
108  
109 10675 ▶ $dependencies = $state->{schema}{dependentRequired} || {};
Condition100%2/2
$state->{'schema'}{'dependentRequired'} || {}
l!l
110674
MC/DC100%1/1
$state->{'schema'}{'dependentRequired'} || {}
$state->{'schema'}{'dependentRequired'}
Truth table100%2/2
A
A= $state->{'schema'}{'dependentRequired'}
Aresult
11
00
110 10675 for my $k (keys %$dependencies) {
111 1 ▶ next if not exists $data->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $data->{$k} 0 1 1
112 push @errors,
113 1 map { E [@{$state->{path}}, $_], [object => dependencies => $k] }
114 1 grep { !exists $data->{$_} } @{$dependencies->{$k}};
115 }
116  
117 10675 return @errors;
118 }
119  
120 13 *_validate_number_max = \&JSON::Validator::Schema::Draft6::_validate_number_max;
121 13 *_validate_number_min = \&JSON::Validator::Schema::Draft6::_validate_number_min;
122 13 *_validate_type_array = \&JSON::Validator::Schema::Draft6::_validate_type_array;
123 13 *_validate_type_array_items = \&JSON::Validator::Schema::Draft4::_validate_type_array_items;
124 13 *_validate_type_array_min_max = \&JSON::Validator::Schema::Draft4::_validate_type_array_min_max;
125 13 *_validate_type_array_unique = \&JSON::Validator::Schema::Draft4::_validate_type_array_unique;
126 13 *_validate_type_object = \&JSON::Validator::Schema::Draft6::_validate_type_object;
127 13 *_validate_type_object_min_max = \&JSON::Validator::Schema::Draft4::_validate_type_object_min_max;
128 13 *_validate_type_object_names = \&JSON::Validator::Schema::Draft6::_validate_type_object_names;
129 13 *_validate_type_object_properties = \&JSON::Validator::Schema::Draft4::_validate_type_object_properties;
130  
131 13 1;
132  
133 =encoding utf8
134  
135 =head1 NAME
136  
137 JSON::Validator::Schema::Draft201909 - JSON-Schema Draft 2019-09
138  
139 =head1 SYNOPSIS
140  
141 See L<JSON::Validator::Schema/SYNOPSIS>.
142  
143 =head1 DESCRIPTION
144  
145 This class represents
146 L<https://json-schema.org/specification-links.html#2019-09-formerly-known-as-draft-8>.
147  
148 Support for parsing the draft is not yet complete. Look at
149 L<https://github.com/mojolicious/json-validator/blob/master/t/draft2019-09-acceptance.t>
150 for the most recent overview of what is not yet supported.
151  
152 Currently less than 1% of the official test suite gets skipped. Here is a list of known
153 limitations:
154  
155 =over 2
156  
157 =item * Float and integers are equal up to 64-bit representation limits
158  
159 This module is unable to say that the 64-bit number "9007199254740992.0" is the
160 same as "9007199254740992".
161  
162 =item * unevaluatedItems
163  
164 See L</unevaluatedProperties>
165  
166 =item * unevaluatedProperties
167  
168 L</unevaluatedItems> and L</unevaluatedProperties> needs to track what has been
169 validated or not using annotations. This is not yet supported.
170  
171 =item * $recursiveAnchor
172  
173 Basic support for C<$recursiveRef> is supported, but using it together with
174 C<$recursiveAnchor> is not.
175  
176 =back
177  
178 =head1 ATTRIBUTES
179  
180 =head2 specification
181  
182 my $str = $schema->specification;
183  
184 Defaults to "L<https://json-schema.org/draft/2019-09/schema>".
185  
186 =head1 SEE ALSO
187  
188 L<JSON::Validator::Schema>.
189  
190 =cut
« Validator/Schema/OpenAPIv3.pm ↑ Summary Validator/Schema/OpenAPIv2.pm »