Validator/Constraints/OAS30.pm

filter: Statementstmt 94.0% 110 / 117 Branchbran 77.0% 37 / 48 Conditioncond 85.0% 17 / 20 MC/DCmcdc 66.6% 8 / 12 Subroutinesub 84.2% 16 / 19 Podpod 0.0% 0 / 11 totaltotal 82.8% 188 / 227 SCAR 36.1
CC
35
Cov
89%
required
12.1
discriminator
7.3
type
5.0
CRAP 36.8
« Validator/Format.pm ↑ Summary Validator/Constraints/Draft6.pm »
1 package JSONSchema::Validator::Constraints::OAS30;
2  
3 # ABSTRACT: OpenAPI 3.0 specification constraints
4  
5 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 9 ▶use JSONSchema::Validator::JSONPointer 'json_pointer';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use JSONSchema::Validator::Error 'error';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 9 ▶use parent 'JSONSchema::Validator::Constraints::Draft4';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 sub type {
16 372 ▶ my ($self, $instance, $type, $schema, $instance_path, $schema_path, $data) = @_;
Subroutine type: called CC=5 SCAR=16.1
Pod undocumented
17  
18 372 ▶ if ($self->check_type($instance, 'null')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->check_type($instance, "null")) 2 370 372
19 return $self->nullable( $instance,
20 2 ▶ $schema->{nullable} // 0,
Condition100%2/2
$schema->{"nullable"} // 0
l!l
11
MC/DC100%1/1
$schema->{"nullable"} // 0
$schema->{"nullable"}
Truth table100%2/2
A
A= $schema->{"nullable"}
Aresult
11
00
21 $schema,
22 $instance_path,
23 $schema_path,
24 $data);
25 }
26  
27 370 my $result = 1;
28 370 ▶ $result = 0 unless $self->check_type($instance, $type);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_type($instance, $type) 10 360 370
29  
30 # # items must be present if type eq array
31 # if ($result && $type eq 'array') {
32 # $result = 0 unless exists $schema->{items};
33 # }
34  
35 370 ▶ return 1 if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 360 10 370
36  
37 10 push @{$data->{errors}}, error(
38 message => 'type mismatch',
39 instance_path => $instance_path,
40 schema_path => $schema_path
41 );
42 10 return 0;
43 }
44  
45 sub items {
46 15 ▶ my ($self, $instance, $items, $schema, $instance_path, $schema_path, $data) = @_;
Subroutine items: called CC=4 SCAR=13.9
Pod undocumented
47 15 ▶ return 1 unless $self->check_type($instance, 'array');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_type($instance, "array") 0 15 15
48  
49 # items is object and NOT array
50  
51 15 my $result = 1;
52 15 for my $i (0 .. $#{$instance}) {
53 28 my $item = $instance->[$i];
54 28 my $ipath = json_pointer->append($instance_path, $i);
55 28 my $r = $self->validator->_validate_schema($item, $items, $ipath, $schema_path, $data);
56 28 ▶ $result = 0 unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 6 22 28
57 }
58 15 return $result;
59 }
60  
61 sub nullable {
62 8 ▶ my ($self, $instance, $nullable, $schema, $instance_path, $schema_path, $data) = @_;
Subroutine nullable: called CC=4 SCAR=14.0
Pod undocumented
63 # A true value adds "null" to the allowed type specified by the type keyword, only if type is explicitly defined within the same Schema Object.
64 8 ▶ return 1 unless $schema->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless $schema->{"type"} 2 6 8
65 6 ▶ return 1 if $nullable;
Branch: 1 branch
BranchTrueFalseTotal
if $nullable 5 1 6
66 1 ▶ unless (defined $instance) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $instance) 1 0 1
67 1 push @{$data->{errors}}, error(
68 message => 'instance is nullable',
69 instance_path => $instance_path,
70 schema_path => $schema_path
71 );
72 1 return 0;
73 }
74 0 return 1;
75 }
76  
77 sub readOnly {
78 14 ▶ my ($self, $instance, $readOnly, $schema, $instance_path, $schema_path, $data) = @_;
Subroutine readOnly: called CC=3 SCAR=11.0
Pod undocumented
79 14 ▶ return 1 unless $readOnly;
Branch: 1 branch
BranchTrueFalseTotal
unless $readOnly 0 14 14
80 14 ▶ return 1 if $data->{direction} eq 'response';
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"direction"} eq "response" 12 2 14
81  
82 2 push @{$data->{errors}}, error(
83 message => 'instance is invalid in request because of readOnly property',
84 instance_path => $instance_path,
85 schema_path => $schema_path
86 );
87 2 return 0;
88 }
89  
90 sub writeOnly {
91 11 ▶ my ($self, $instance, $writeOnly, $schema, $instance_path, $schema_path, $data) = @_;
Subroutine writeOnly: called CC=3 SCAR=11.0
Pod undocumented
92 11 ▶ return 1 unless $writeOnly;
Branch: 1 branch
BranchTrueFalseTotal
unless $writeOnly 0 11 11
93 11 ▶ return 1 if $data->{direction} eq 'request';
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"direction"} eq "request" 9 2 11
94  
95 2 push @{$data->{errors}}, error(
96 message => "instance is invalid in response because of writeOnly property",
97 instance_path => $instance_path,
98 schema_path => $schema_path
99 );
100 2 return 0;
101 }
102  
103 sub required {
104 93 ▶ my ($self, $instance, $required, $schema, $instance_path, $schema_path, $data) = @_;
Subroutine required: called CC=12 SCAR=25.0
Pod undocumented
105 93 ▶ return 1 unless $self->check_type($instance, 'object');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_type($instance, "object") 0 93 93
106  
107 93 my $result = 1;
108 93 for my $idx (0 .. $#{$required}) {
109 184 my $prop = $required->[$idx];
110 184 ▶ next if exists $instance->{$prop};
Branch: 1 branch
BranchTrueFalseTotal
if exists $instance->{$prop} 158 26 184
111  
112 26 ▶ if ($schema->{properties} && $schema->{properties}{$prop}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"properties"} and $schema->{"properties"}{$prop}) 26 0 26
Condition33%1/3
$schema->{"properties"} and $schema->{"properties"}{$prop}
!ll&&!rl&&r
0026
MC/DC0%0/2
$schema->{"properties"} and $schema->{"properties"}{$prop}
$schema->{"properties"} $schema->{"properties"}{$prop}
Truth table33%1/3
A and B
A= $schema->{"properties"}B= $schema->{"properties"}{$prop}
ABresult
0X0
100
111
113 26 my $prop = $schema->{properties}{$prop};
114 26 ▶ my $read_only = $prop->{readOnly} // 0;
Condition100%2/2
$prop->{"readOnly"} // 0
l!l
1016
MC/DC100%1/1
$prop->{"readOnly"} // 0
$prop->{"readOnly"}
Truth table100%2/2
A
A= $prop->{"readOnly"}
Aresult
11
00
115 26 ▶ my $write_only = $prop->{writeOnly} // 0;
Condition100%2/2
$prop->{"writeOnly"} // 0
l!l
818
MC/DC100%1/1
$prop->{"writeOnly"} // 0
$prop->{"writeOnly"}
Truth table100%2/2
A
A= $prop->{"writeOnly"}
Aresult
11
00
116 26 my $direction = $data->{direction};
117  
118 26 ▶ next if $direction eq 'request' && $read_only;
Branch: 1 branch
BranchTrueFalseTotal
if $direction eq "request" and $read_only 8 18 26
Condition100%3/3
$direction eq "request" and $read_only
!ll&&!rl&&r
1448
MC/DC100%2/2
$direction eq "request" and $read_only
$direction eq "request" $read_only
Truth table100%3/3
A and B
A= $direction eq "request"B= $read_only
ABresult
0X0
100
111
119 18 ▶ next if $direction eq 'response' && $write_only;
Branch: 1 branch
BranchTrueFalseTotal
if $direction eq "response" and $write_only 6 12 18
Condition100%3/3
$direction eq "response" and $write_only
!ll&&!rl&&r
486
MC/DC100%2/2
$direction eq "response" and $write_only
$direction eq "response" $write_only
Truth table100%3/3
A and B
A= $direction eq "response"B= $write_only
ABresult
0X0
100
111
120 }
121  
122 12 push @{$data->{errors}}, error(
123 message => qq{instance does not have required property "${prop}"},
124 instance_path => $instance_path,
125 schema_path => json_pointer->append($schema_path, $idx)
126 );
127 12 $result = 0;
128 }
129 93 return $result;
130 }
131  
132 sub discriminator {
133 28 ▶ my ($self, $instance, $discriminator, $origin_schema, $instance_path, $schema_path, $data) = @_;
Subroutine discriminator: called CC=7 SCAR=19.9
Pod undocumented
134 28 ▶ return 1 unless $self->check_type($instance, 'object');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_type($instance, "object") 0 28 28
135  
136 28 my $path = $instance_path;
137  
138 28 my $property_name = $discriminator->{propertyName};
139 28 ▶ my $mapping = $discriminator->{mapping} // {};
Condition100%2/2
$discriminator->{"mapping"} // {}
l!l
424
MC/DC100%1/1
$discriminator->{"mapping"} // {}
$discriminator->{"mapping"}
Truth table100%2/2
A
A= $discriminator->{"mapping"}
Aresult
11
00
140  
141 28 my $type = $instance->{$property_name};
142 28 my $ref = $mapping->{$type};
143  
144 28 ▶ $ref = $self->__detect_discriminator_ref($ref || $type);
Condition66%2/3
$ref || $type
l!l&&r!l&&!r
4240
MC/DC0%0/2
$ref || $type
$ref $type
Truth table66%2/3
A || B
A= $refB= $type
ABresult
1X1
011
000
145  
146 # status == 1 needs to prevent recursion
147 28 $data->{discriminator}{$path} = 1;
148  
149 28 my $scope = $self->validator->scope;
150 28 $ref = URI->new($ref);
151 28 ▶ $ref = $ref->abs($scope) if $scope;
Branch: 1 branch
BranchTrueFalseTotal
if $scope 24 4 28
152  
153 28 my ($current_scope, $schema) = $self->validator->resolver->resolve($ref);
154  
155 28 ▶ croak "schema not resolved by ref $ref" unless $schema;
Branch: 1 branch
BranchTrueFalseTotal
unless $schema 0 28 28
156  
157 28 push @{$self->validator->scopes}, $current_scope;
158  
159 28 my $result = eval {
160 28 $self->validator->_validate_schema($instance, $schema, $instance_path, $schema_path, $data, apply_scope => 0);
161 };
162  
163 28 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 28 28
164 0 $result = 0;
165 0 push @{$data->{errors}}, error(
166 message => "exception: $@",
167 instance_path => $instance_path,
168 schema_path => $schema_path
169 );
170 }
171  
172 28 pop @{$self->validator->scopes};
173  
174 28 delete $data->{discriminator}{$path};
175  
176 28 return $result;
177 }
178  
179 sub deprecated {
180 6 ▶ my ($self, $instance, $deprecated, $schema, $instance_path, $schema_path, $data) = @_;
Subroutine deprecated: called CC=2 SCAR=7.0
Pod undocumented
181 6 ▶ return 1 unless $deprecated;
Branch: 1 branch
BranchTrueFalseTotal
unless $deprecated 0 6 6
182 6 push @{$data->{warnings}}, error(
183 message => 'instance is deprecated',
184 instance_path => $instance_path,
185 schema_path => $schema_path
186 );
187 6 return 1;
188 }
189  
190 # Additional properties defined by the JSON Schema specification that are not mentioned in OAS30 are strictly unsupported.
191 0 ▶sub dependencies { 1 }
Subroutine dependencies: not called CC=1 SCAR=6.9
Pod undocumented
192 0 ▶sub additionalItems { 1 }
Subroutine additionalItems: not called CC=1 SCAR=6.9
Pod undocumented
193 0 ▶sub patternProperties { 1 }
Subroutine patternProperties: not called CC=1 SCAR=6.9
Pod undocumented
194  
195 sub __detect_discriminator_ref {
196 28 ▶ my ($self, $ref) = @_;
Subroutine __detect_discriminator_ref: called CC=3 SCAR=11.0
197 # heuristic
198 28 ▶ return $ref if $ref =~ m|/|;
Branch: 1 branch
BranchTrueFalseTotal
if $ref =~ m[/] 2 26 28
199 26 ▶ return $ref if $ref =~ m/\.json$/;
Branch: 1 branch
BranchTrueFalseTotal
if $ref =~ /\.json$/ 0 26 26
200 26 return '#/components/schemas/' . $ref;
201 }
202  
203 9 1;
204  
205 __END__
« Validator/Format.pm ↑ Summary Validator/Constraints/Draft6.pm »