Validator/Schema/Draft201909.pm

filter: Statementstmt 96.1% 100 / 104 Branchbran 75.0% 30 / 40 Conditioncond 79.0% 34 / 43 MC/DCmcdc 61.5% 16 / 26 Subroutinesub 92.8% 13 / 14 Podpod n/a totaltotal 85.0% 193 / 227 CC 40 SCAR 23.0
CC
40
Cov
88%
_validate_type_array_contains
17.6
_state
9.8
_resolve_object
9.0
CRAP 10.0
« Validator/Schema/OpenAPIv3.pm ↑ Summary Validator/Schema/OpenAPIv2.pm »
1 package JSON::Validator::Schema::Draft201909;
2 11 ▶use Mojo::Base 'JSON::Validator::Schema';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 11 ▶use JSON::Validator::Schema::Draft4;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use JSON::Validator::Schema::Draft6;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use JSON::Validator::Schema::Draft7;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use JSON::Validator::URI qw(uri);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use JSON::Validator::Util qw(E is_bool is_type);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 11 has moniker => 'draft2019';
11 11 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 sub _build_formats {
15 2 ▶ my $formats = shift->JSON::Validator::Schema::Draft7::_build_formats;
Subroutine _build_formats: called CC=1 SCAR=0.0
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 676 ▶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
226540
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 sub _resolve_object {
25 3375 ▶ my ($self, $state, $schema, $refs, $found) = @_;
Subroutine _resolve_object: called CC=9 SCAR=22.0
26  
27 3375 ▶ if ($schema->{'$id'} and !ref $schema->{'$id'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"\$id"} and not ref $schema->{"\$id"}) 12 3363 3375
Condition100%3/3
$schema->{"\$id"} and not ref $schema->{"\$id"}
!ll&&!rl&&r
3362112
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 12 my $id = uri $schema->{'$id'}, $state->{base_url};
29 12 $self->store->add($id => $schema);
30 12 $state = {%$state}; # make sure we don't mutate $state ref
31 12 $state->{base_url} = $id->clone->fragment(undef);
32 }
33 3375 ▶ if ($schema->{'$anchor'} && !ref $schema->{'$anchor'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"\$anchor"} and not ref $schema->{"\$anchor"}) 1 3374 3375
Condition100%3/3
$schema->{"\$anchor"} and not ref $schema->{"\$anchor"}
!ll&&!rl&&r
337311
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 3375 ▶ if ($found->{'$recursiveRef'} = $schema->{'$recursiveRef'} && !ref $schema->{'$recursiveRef'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found->{"\$recursiveRef"} = $schema->{"\$recursiveRef"} && !ref($schema->{"\$recursiveRef"})) 22 3353 3375
Condition100%3/3
$schema->{'$recursiveRef'} && !ref($schema->{'$recursiveRef'})
!ll&&!rl&&r
3352122
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 3375 ▶ if ($found->{'$ref'} = $schema->{'$ref'} && !ref $schema->{'$ref'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found->{"\$ref"} = $schema->{"\$ref"} && !ref($schema->{"\$ref"})) 654 2721 3375
Condition100%3/3
$schema->{'$ref'} && !ref($schema->{'$ref'})
!ll&&!rl&&r
27156654
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 654 push @$refs, [$schema, $state];
44 }
45  
46 3375 return $state;
47 }
48  
49 sub _state {
50 2201 ▶ my ($self, $curr, %override) = @_;
Subroutine _state: called CC=9 SCAR=22.8
51 2201 my $schema = $override{schema};
52 2201 my (%alongside, %seen);
53  
54 2201 ▶ while (ref $schema eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $schema eq "HASH") 2659 0 2659
55 2659 ▶ last unless my $ref = $schema->{'$ref'} || $schema->{'$recursiveRef'};
Branch: 1 branch
BranchTrueFalseTotal
unless my $ref = $schema->{"\$ref"} || $schema->{"\$recursiveRef"} 2201 458 2659
Condition100%3/3
$schema->{'$ref'} || $schema->{'$recursiveRef'}
l!l&&r!l&&!r
434242201
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 458 ▶ last if ref $ref;
Branch: 1 branch
BranchTrueFalseTotal
if ref $ref 0 458 458
57 458 ▶ last if $seen{$schema}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$schema}++ 0 458 458
58  
59 458 %alongside = (%alongside, %$schema);
60 $schema = $self->_refs->{$schema}{schema}
61 458 ▶ // Carp::confess(qq(You have to call resolve() before validate() to lookup "$ref".));
Condition33%1/3
$self->_refs->{$schema}{"schema"} // Carp::confess(qq[You have to call resolve() before validate() to lookup "$ref".])
l!l&&r!l&&!r
45800
MC/DC0%0/2
$self->_refs->{$schema}{"schema"} // Carp::confess(qq[You have to call resolve() before validate() to lookup "$ref".])
$self->_refs->{$schema}{"schema"} Carp::confess(qq[You have to call resolve() before validate() to lookup "$ref".])
Truth table33%1/3
A // B
A= $self->_refs->{$schema}{"schema"}B= Carp::confess(qq[You have to call resolve() before validate() to lookup "$ref".])
ABresult
1X1
011
000
62 }
63  
64 2201 ▶ return {%$curr, %override, schema => $schema} unless ref $schema eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $schema eq "HASH" 0 2201 2201
65  
66 2201 delete $alongside{$_} for qw($anchor $id $recursiveAnchor $recursiveRef $ref);
67 2201 return {%$curr, %override, schema => {%alongside, %$schema}};
68 }
69  
70 sub _validate_type_array_contains {
71 63 ▶ my ($self, $data, $state) = @_;
Subroutine _validate_type_array_contains: called CC=16 SCAR=28.7
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 sub _validate_type_object_dependencies {
93 671 ▶ my ($self, $data, $state) = @_;
Subroutine _validate_type_object_dependencies: called CC=8 SCAR=21.0
94 671 ▶ my $dependencies = $state->{schema}{dependentSchemas} || {};
Condition100%2/2
$state->{'schema'}{'dependentSchemas'} || {}
l!l
1670
MC/DC100%1/1
$state->{'schema'}{'dependentSchemas'} || {}
$state->{'schema'}{'dependentSchemas'}
Truth table100%2/2
A
A= $state->{'schema'}{'dependentSchemas'}
Aresult
11
00
95 671 my @errors;
96  
97 671 for my $k (keys %$dependencies) {
98 1 ▶ next if not exists $data->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $data->{$k} 0 1 1
99 1 ▶ if (ref $dependencies->{$k} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dependencies->{$k} eq 'ARRAY') { } 1 0 1
100 push @errors,
101 1 map { E [@{$state->{path}}, $_], [object => dependencies => $k] }
102 1 grep { !exists $data->{$_} } @{$dependencies->{$k}};
103 }
104 else {
105 0 push @errors, $self->_validate($data, $self->_state($state, schema => $dependencies->{$k}));
106 }
107 }
108  
109 671 ▶ $dependencies = $state->{schema}{dependentRequired} || {};
Condition100%2/2
$state->{'schema'}{'dependentRequired'} || {}
l!l
1670
MC/DC100%1/1
$state->{'schema'}{'dependentRequired'} || {}
$state->{'schema'}{'dependentRequired'}
Truth table100%2/2
A
A= $state->{'schema'}{'dependentRequired'}
Aresult
11
00
110 671 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 671 return @errors;
118 }
119  
120 11 *_validate_number_max = \&JSON::Validator::Schema::Draft6::_validate_number_max;
121 11 *_validate_number_min = \&JSON::Validator::Schema::Draft6::_validate_number_min;
122 11 *_validate_type_array = \&JSON::Validator::Schema::Draft6::_validate_type_array;
123 11 *_validate_type_array_items = \&JSON::Validator::Schema::Draft4::_validate_type_array_items;
124 11 *_validate_type_array_min_max = \&JSON::Validator::Schema::Draft4::_validate_type_array_min_max;
125 11 *_validate_type_array_unique = \&JSON::Validator::Schema::Draft4::_validate_type_array_unique;
126 11 *_validate_type_object = \&JSON::Validator::Schema::Draft6::_validate_type_object;
127 11 *_validate_type_object_min_max = \&JSON::Validator::Schema::Draft4::_validate_type_object_min_max;
128 11 *_validate_type_object_names = \&JSON::Validator::Schema::Draft6::_validate_type_object_names;
129 11 *_validate_type_object_properties = \&JSON::Validator::Schema::Draft4::_validate_type_object_properties;
130  
131 11 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 »