Validator/Constraints/OAS30.pm

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