Validator/OAS30.pm

filter: Statementstmt 95.1% 235 / 247 Branchbran 78.0% 103 / 132 Conditioncond 66.1% 39 / 59 MC/DCmcdc 30.3% 10 / 33 Subroutinesub 100% 26 / 26 Podpod 83.3% 5 / 6 totaltotal 83.1% 418 / 503 SCAR 47.9
CC
96
Cov
86%
_validate_content
21.0
validate_response
17.9
_validate_type_params
16.4
CRAP 120.9
« Validator/Constraints/Draft4.pm ↑ Summary Validator/Format.pm »
1 package JSONSchema::Validator::OAS30;
2  
3 # ABSTRACT: Validator for OpenAPI Specification 3.0
4  
5 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 8 ▶use JSONSchema::Validator::JSONPointer;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use JSONSchema::Validator::Error 'error';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use JSONSchema::Validator::Constraints::OAS30;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use JSONSchema::Validator::URIResolver;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use JSONSchema::Validator::Util 'json_decode';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 8 ▶use parent 'JSONSchema::Validator::Draft4';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 8 ▶use constant SPECIFICATION => 'OAS30';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 8 ▶use constant ID => 'https://spec.openapis.org/oas/3.0/schema/2019-04-02';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 8 ▶use constant ID_FIELD => '';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 sub new {
22 39 ▶ my ($class, %params) = @_;
Subroutine new: called CC=3 SCAR=11.0
Pod documented
23  
24 39 $params{using_id_with_ref} = 0;
25 39 my $self = $class->create(%params);
26  
27 39 ▶ my $validate_deprecated = $params{validate_deprecated} // 1;
Condition100%2/2
$params{"validate_deprecated"} // 1
l!l
336
MC/DC100%1/1
$params{"validate_deprecated"} // 1
$params{"validate_deprecated"}
Truth table100%2/2
A
A= $params{"validate_deprecated"}
Aresult
11
00
28 39 $self->{validate_deprecated} = $validate_deprecated;
29  
30 39 ▶ my $constraints = JSONSchema::Validator::Constraints::OAS30->new(validator => $self, strict => $params{strict} // 0);
Condition50%1/2
$params{"strict"} // 0
l!l
039
MC/DC0%0/1
$params{"strict"} // 0
$params{"strict"}
Truth table50%1/2
A
A= $params{"strict"}
Aresult
11
00
31 39 $self->{constraints} = $constraints;
32  
33 39 return $self;
34 }
35  
36 9 ▶sub validate_deprecated { shift->{validate_deprecated} }
Subroutine validate_deprecated: called CC=1 SCAR=0.0
Pod documented
37  
38 sub validate_schema {
39 110 ▶ my ($self, $instance, %params) = @_;
Subroutine validate_schema: called CC=10 SCAR=23.7
Pod documented
40  
41 110 ▶ my $schema = $params{schema} // $self->schema;
Condition66%2/3
$params{"schema"} // $self->schema
l!l&&r!l&&!r
75350
MC/DC0%0/2
$params{"schema"} // $self->schema
$params{"schema"} $self->schema
Truth table66%2/3
A // B
A= $params{"schema"}B= $self->schema
ABresult
1X1
011
000
42 110 ▶ my $instance_path = $params{instance_path} // '/';
Condition50%1/2
$params{"instance_path"} // "/"
l!l
0110
MC/DC0%0/1
$params{"instance_path"} // "/"
$params{"instance_path"}
Truth table50%1/2
A
A= $params{"instance_path"}
Aresult
11
00
43 110 ▶ my $schema_path = $params{schema_path} // '/';
Condition50%1/2
$params{"schema_path"} // "/"
l!l
0110
MC/DC0%0/1
$params{"schema_path"} // "/"
$params{"schema_path"}
Truth table50%1/2
A
A= $params{"schema_path"}
Aresult
11
00
44 110 my $direction = $params{direction};
45 110 my $scope = $params{scope};
46  
47 110 ▶ croak 'param "direction" is required' unless $direction;
Branch: 1 branch
BranchTrueFalseTotal
unless $direction 0 110 110
48 110 ▶ croak '"direction" must have one of values: "request", "response"'
Branch: 1 branch
BranchTrueFalseTotal
if $direction ne "request" and $direction ne "response" 0 110 110
Condition66%2/3
$direction ne "request" and $direction ne "response"
!ll&&!rl&&r
86240
MC/DC0%0/2
$direction ne "request" and $direction ne "response"
$direction ne "request" $direction ne "response"
Truth table66%2/3
A and B
A= $direction ne "request"B= $direction ne "response"
ABresult
0X0
100
111
49 if $direction ne 'request' && $direction ne 'response';
50  
51 110 ▶ croak 'No schema specified' unless defined $schema;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $schema 0 110 110
52  
53 110 ▶ push @{$self->scopes}, $scope if $scope;
Branch: 1 branch
BranchTrueFalseTotal
if $scope 32 78 110
54  
55 110 my ($errors, $warnings) = ([], []);
56 110 my $result = $self->_validate_schema($instance, $schema, $instance_path, $schema_path, {
57 errors => $errors,
58 warnings => $warnings,
59 direction => $direction
60 }
61 );
62  
63 110 ▶ pop @{$self->scopes} if $scope;
Branch: 1 branch
BranchTrueFalseTotal
if $scope 32 78 110
64  
65 110 return $result, $errors, $warnings;
66 }
67  
68 sub _schema_keys {
69 499 ▶ my ($self, $schema, $instance_path, $data) = @_;
Subroutine _schema_keys: called CC=7 SCAR=19.5
70 # if ref exists other preperties MUST be ignored
71 499 ▶ return '$ref' if $schema->{'$ref'};
Branch: 1 branch
BranchTrueFalseTotal
if $schema->{"\$ref"} 62 437 499
72  
73 437 ▶ return ('deprecated') if $schema->{deprecated} && !$self->validate_deprecated;
Branch: 1 branch
BranchTrueFalseTotal
if $schema->{"deprecated"} and not $self->validate_deprecated 2 435 437
Condition100%3/3
$schema->{"deprecated"} and not $self->validate_deprecated
!ll&&!rl&&r
43142
MC/DC100%2/2
$schema->{"deprecated"} and not $self->validate_deprecated
$schema->{"deprecated"} not $self->validate_deprecated
Truth table100%3/3
A and not(B)
A= $schema->{"deprecated"}B= not $self->validate_deprecated
ABresult
0X0
100
111
74  
75 435 ▶ if (grep { $_ eq 'discriminator' } keys %$schema) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq "discriminator";} keys %$schema) 52 383 435
76 52 ▶ my $status = $data->{discriminator}{$instance_path} // 0;
Condition100%2/2
$data->{"discriminator"}{$instance_path} // 0
l!l
2428
MC/DC100%1/1
$data->{"discriminator"}{$instance_path} // 0
$data->{"discriminator"}{$instance_path}
Truth table100%2/2
A
A= $data->{"discriminator"}{$instance_path}
Aresult
11
00
77 52 ▶ return ('discriminator') unless $status;
Branch: 1 branch
BranchTrueFalseTotal
unless $status 28 24 52
78  
79 # status is 1
80 24 return grep { $_ ne 'discriminator' } keys %$schema;
81 }
82  
83 383 return keys %$schema;
84 }
85  
86 sub validate_request {
87 21 ▶ my ($self, %params) = @_;
Subroutine validate_request: called CC=10 SCAR=23.4
Pod documented
88  
89 21 ▶ my $method = lc($params{method} or croak 'param "method" is required');
Condition33%1/3
$params{'method'} || croak('param "method" is required')
l!l&&r!l&&!r
2100
MC/DC0%0/2
$params{'method'} || croak('param "method" is required')
$params{'method'} croak('param "method" is required')
Truth table33%1/3
A || B
A= $params{'method'}B= croak('param "method" is required')
ABresult
1X1
011
000
90 21 ▶ my $openapi_path = $params{openapi_path} or croak 'param "openapi_path" is required';
Branch: 1 branch
BranchTrueFalseTotal
my $openapi_path = $params{"openapi_path"} or croak("param \"openapi_path\" is required") 0 21 21
91  
92 21 my $get_user_param = $self->_wrap_params($params{parameters});
93  
94 21 ▶ my $user_body = $params{parameters}{body} // []; # [exists, content-type, value]
Condition50%1/2
$params{"parameters"}{"body"} // []
l!l
210
MC/DC0%0/1
$params{"parameters"}{"body"} // []
$params{"parameters"}{"body"}
Truth table50%1/2
A
A= $params{"parameters"}{"body"}
Aresult
11
00
95  
96 21 my $base_ptr = $self->json_pointer->xget('paths', $openapi_path);
97 21 ▶ return 1, [], [] unless $base_ptr;
Branch: 1 branch
BranchTrueFalseTotal
unless $base_ptr 0 21 21
98  
99 21 my $schema_params = {query => {}, header => {}, path => {}, cookie => {}};
100  
101 # Common Parameter Object
102 21 my $common_params_ptr = $base_ptr->xget('parameters');
103 21 $self->_fill_parameters($schema_params, $common_params_ptr);
104  
105 # Operation Object
106 21 my $operation_ptr = $base_ptr->xget($method);
107 21 ▶ return 1, [], [] unless $operation_ptr;
Branch: 1 branch
BranchTrueFalseTotal
unless $operation_ptr 0 21 21
108  
109 21 my ($result, $context) = (1, {errors => [], warnings => [], direction => 'request'});
110 21 ▶ if ($operation_ptr->xget('deprecated')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($operation_ptr->xget("deprecated")) 1 20 21
111 1 push @{$context->{warnings}}, error(message => "method $method of $openapi_path is deprecated");
112 1 ▶ return $result, $context->{errors}, $context->{warnings} unless $self->validate_deprecated;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->validate_deprecated 1 0 1
113 }
114  
115 # Parameter Object
116 20 my $params_ptr = $operation_ptr->xget('parameters');
117 20 $self->_fill_parameters($schema_params, $params_ptr);
118  
119 # validate path, query, header, cookie
120 20 my $r = $self->_validate_params($context, $schema_params, $get_user_param);
121 20 ▶ $result = 0 unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 7 13 20
122  
123 # validate body
124 20 my $body_ptr = $operation_ptr->xget('requestBody');
125 20 $r = $self->_validate_body($context, $user_body, $body_ptr);
126 20 ▶ $result = 0 unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 4 16 20
127  
128 20 return $result, $context->{errors}, $context->{warnings};
129 }
130  
131 sub validate_response {
132 21 ▶ my ($self, %params) = @_;
Subroutine validate_response: called CC=15 SCAR=28.8
Pod documented
133  
134 21 ▶ my $method = lc($params{method} or croak 'param "method" is required');
Condition33%1/3
$params{'method'} || croak('param "method" is required')
l!l&&r!l&&!r
2100
MC/DC0%0/2
$params{'method'} || croak('param "method" is required')
$params{'method'} croak('param "method" is required')
Truth table33%1/3
A || B
A= $params{'method'}B= croak('param "method" is required')
ABresult
1X1
011
000
135 21 ▶ my $openapi_path = $params{openapi_path} or croak 'param "openapi_path" is required';
Branch: 1 branch
BranchTrueFalseTotal
my $openapi_path = $params{"openapi_path"} or croak("param \"openapi_path\" is required") 0 21 21
136 21 ▶ my $http_status = $params{status} or croak 'param "status" is required';
Branch: 1 branch
BranchTrueFalseTotal
my $http_status = $params{"status"} or croak("param \"status\" is required") 0 21 21
137  
138 21 my $get_user_param = $self->_wrap_params($params{parameters});
139  
140 21 ▶ my $user_body = $params{parameters}{body} // []; # [exists, content-type, value]
Condition50%1/2
$params{"parameters"}{"body"} // []
l!l
210
MC/DC0%0/1
$params{"parameters"}{"body"} // []
$params{"parameters"}{"body"}
Truth table50%1/2
A
A= $params{"parameters"}{"body"}
Aresult
11
00
141  
142 21 my $base_ptr = $self->json_pointer->xget('paths', $openapi_path, $method);
143 21 ▶ return 1, [], [] unless $base_ptr;
Branch: 1 branch
BranchTrueFalseTotal
unless $base_ptr 0 21 21
144  
145 21 my ($result, $context) = (1, {errors => [], warnings => [], direction => 'response'});
146 21 ▶ if ($base_ptr->xget('deprecated')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base_ptr->xget("deprecated")) 1 20 21
147 1 push @{$context->{warnings}}, error(message => "method $method of $openapi_path is deprecated");
148 1 ▶ return $result, $context->{errors}, $context->{warnings} unless $self->validate_deprecated;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->validate_deprecated 1 0 1
149 }
150  
151 20 my $responses_ptr = $base_ptr->xget('responses');
152 20 ▶ return $result, $context->{errors}, $context->{warnings} unless $responses_ptr;
Branch: 1 branch
BranchTrueFalseTotal
unless $responses_ptr 0 20 20
153  
154 20 my $status_ptr = $responses_ptr->xget($http_status);
155 20 ▶ $status_ptr = $responses_ptr->xget('default') unless $status_ptr;
Branch: 1 branch
BranchTrueFalseTotal
unless $status_ptr 2 18 20
156  
157 20 ▶ unless ($status_ptr) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($status_ptr) 0 20 20
158 0 push @{$context->{errors}}, error(message => "unspecified response with status code $http_status");
159 0 return 0, $context->{errors}, $context->{warnings};
160 }
161  
162 20 my $schema_params = {header => {}};
163 20 $self->_fill_parameters($schema_params, $status_ptr->xget('headers'));
164  
165 # validate headers
166 20 my $r = $self->_validate_params($context, $schema_params, $get_user_param);
167 20 ▶ $result = 0 unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 0 20 20
168  
169 # validate body
170 20 my ($exists, $content_type, $data) = @$user_body;
171 20 ▶ return $result, $context->{errors}, $context->{warnings} unless $exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $exists 5 15 20
172  
173 15 ($r, my $errors, my $warnings) = $self->_validate_content($context, $status_ptr, $content_type, $data);
174 15 ▶ unless ($r) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r) 7 8 15
175 7 push @{$context->{errors}}, error(message => 'response body error', context => $errors);
176 7 $result = 0;
177 }
178 15 ▶ push @{$context->{warnings}}, error(message => 'response body warning', context => $warnings) if @$warnings;
Branch: 1 branch
BranchTrueFalseTotal
if @$warnings 0 15 15
179  
180 15 return $result, $context->{errors}, $context->{warnings};
181 }
182  
183 sub _validate_body {
184 20 ▶ my ($self, $ctx, $user_body, $body_ptr) = @_;
Subroutine _validate_body: called CC=8 SCAR=22.4
185  
186 # body in specification is omit
187 20 ▶ return 1 unless $body_ptr;
Branch: 1 branch
BranchTrueFalseTotal
unless $body_ptr 6 14 20
188  
189 # skip validation if user not specify body
190 14 ▶ return 1 unless $user_body && @$user_body;
Branch: 1 branch
BranchTrueFalseTotal
unless $user_body and @$user_body 0 14 14
Condition33%1/3
$user_body and @$user_body
!ll&&!rl&&r
0014
MC/DC0%0/2
$user_body and @$user_body
$user_body @$user_body
Truth table33%1/3
A and B
A= $user_bodyB= @$user_body
ABresult
0X0
100
111
191  
192 14 my ($exists, $content_type, $data) = @$user_body;
193  
194 14 ▶ unless ($exists) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($exists) 1 13 14
195 1 my $required = $body_ptr->xget('required');
196 1 ▶ if ($required) {
Branch: 1 branch
BranchTrueFalseTotal
if ($required) 0 1 1
197 0 push @{$ctx->{errors}}, error(message => q{body is required});
198 0 return 0;
199 }
200 1 return 1;
201 }
202  
203 13 my ($result, $errors, $warnings) = $self->_validate_content($ctx, $body_ptr, $content_type, $data);
204 13 ▶ push @{$ctx->{errors}}, error(message => 'request body error', context => $errors) if @$errors;
Branch: 1 branch
BranchTrueFalseTotal
if @$errors 4 9 13
205 13 ▶ push @{$ctx->{warnings}}, error(message => 'request body warning', context => $warnings) if @$warnings;
Branch: 1 branch
BranchTrueFalseTotal
if @$warnings 0 13 13
206 13 return $result;
207 }
208  
209 sub _validate_params {
210 40 ▶ my ($self, $ctx, $schema_params, $get_user_param) = @_;
Subroutine _validate_params: called CC=5 SCAR=16.1
211 40 my $result = 1;
212 40 for my $type (keys %$schema_params) {
213 100 ▶ next unless %{$schema_params->{$type}};
Branch: 1 branch
BranchTrueFalseTotal
unless %{$schema_params->{$type};} 55 45 100
214 # skip validation if user not specify getter for such params type
215 45 ▶ next unless $get_user_param->{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless $get_user_param->{$type} 6 39 45
216 39 my $r = $self->_validate_type_params($ctx, $type, $schema_params->{$type}, $get_user_param->{$type});
217 39 ▶ $result = 0 unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 7 32 39
218 }
219 40 return $result;
220 }
221  
222 sub _validate_type_params {
223 39 ▶ my ($self, $ctx, $type, $params, $get_user_param) = @_;
Subroutine _validate_type_params: called CC=16 SCAR=28.0
224  
225 39 my $result = 1;
226 39 for my $param (keys %$params) {
227 66 my $data_ptr = $params->{$param};
228  
229 66 my ($exists, $value) = $get_user_param->($param);
230 66 ▶ ($exists, $value) = $get_user_param->(lc $param) if !$exists && $type eq 'header';
Branch: 1 branch
BranchTrueFalseTotal
if not $exists and $type eq "header" 9 57 66
Condition100%3/3
not $exists and $type eq "header"
!ll&&!rl&&r
41169
MC/DC100%2/2
not $exists and $type eq "header"
not $exists $type eq "header"
Truth table100%3/3
not(A) and B
A= not $existsB= $type eq "header"
ABresult
0X0
100
111
231  
232 66 ▶ unless ($exists) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($exists) 17 49 66
233 17 ▶ if ($data_ptr->xget('required')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_ptr->xget("required")) 2 15 17
234 2 push @{$ctx->{errors}}, error(message => qq{$type param "$param" is required});
235 2 $result = 0;
236 }
237 17 next;
238 }
239  
240 49 ▶ if ($data_ptr->xget('deprecated')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_ptr->xget("deprecated")) 1 48 49
241 1 push @{$ctx->{warnings}}, error(message => qq{$type param "$param" is deprecated});
242 1 ▶ next unless $self->validate_deprecated;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->validate_deprecated 1 0 1
243 }
244  
245 48 ▶ next unless $data_ptr->xget('schema') || $data_ptr->xget('content');
Branch: 1 branch
BranchTrueFalseTotal
unless $data_ptr->xget("schema") or $data_ptr->xget("content") 0 48 48
Condition66%2/3
$data_ptr->xget("schema") or $data_ptr->xget("content")
l!l&&r!l&&!r
3990
MC/DC0%0/2
$data_ptr->xget("schema") or $data_ptr->xget("content")
$data_ptr->xget("schema") $data_ptr->xget("content")
Truth table66%2/3
A or B
A= $data_ptr->xget("schema")B= $data_ptr->xget("content")
ABresult
1X1
011
000
246  
247 48 ▶ $value = [$value] if ref $value ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value ne "ARRAY" 48 0 48
248  
249 48 for my $v (@$value) {
250 48 my ($r, $errors, $warnings);
251  
252 48 ▶ if ($data_ptr->xget('schema')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($data_ptr->xget('schema')) { } 39 9 48
elsif ($data_ptr->xget('content')) { } 9 0 9
253 39 my $schema_ptr = $data_ptr->xget('schema');
254 ($r, $errors, $warnings) = $self->validate_schema($v,
255 schema => $schema_ptr->value,
256 path => '/',
257 direction => $ctx->{direction},
258 39 scope => $schema_ptr->scope
259 );
260 } elsif ($data_ptr->xget('content')) {
261 9 ($r, $errors, $warnings) = $self->_validate_content($ctx, $data_ptr, undef, $v);
262 }
263  
264 48 ▶ unless ($r) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r) 5 43 48
265 5 push @{$ctx->{errors}}, error(message => qq{$type param "$param" has error}, context => $errors);
266 5 $result = 0;
267 }
268 48 ▶ push @{$ctx->{warnings}}, error(message => qq{$type param "$param" has warning}, context => $warnings) if @$warnings;
Branch: 1 branch
BranchTrueFalseTotal
if @$warnings 0 48 48
269 }
270 }
271  
272 39 return $result;
273 }
274  
275 # ptr - JSONSchema::Validator::JSONPointer
276 # content_type - string|null
277 # data - string|HASH|ARRAY
278 sub _validate_content {
279 37 ▶ my ($self, $ctx, $ptr, $content_type, $data) = @_;
Subroutine _validate_content: called CC=20 SCAR=30.5
280  
281 37 my $content_ptr = $ptr->xget('content');
282 # content in body is required but in params is optional
283 37 ▶ return 1, [], [] unless $content_ptr;
Branch: 1 branch
BranchTrueFalseTotal
unless $content_ptr 1 36 37
284  
285 36 my $ctype_ptr;
286 36 ▶ if ($content_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_type) { } 27 9 36
287 27 $ctype_ptr = $content_ptr->xget($content_type);
288 27 ▶ unless ($ctype_ptr) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ctype_ptr) 0 27 27
289 0 return 0, [error(message => qq{content with content-type $content_type is omit in schema})], [];
290 }
291 } else {
292 9 my $mtype_map = $content_ptr->value;
293 9 my @keys = $content_ptr->keys(raw => 1);
294 9 ▶ return 0, [error(message => qq{schema must has exactly one content_type})], [] unless scalar(@keys) == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @keys == 1 0 9 9
295  
296 9 $content_type = $keys[0];
297 9 $ctype_ptr = $content_ptr->xget($content_type);
298 }
299  
300 36 ▶ unless (ref $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $data) 9 27 36
301 9 ▶ if (index($content_type, 'application/json') != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($content_type, "application/json") != -1) 9 0 9
302 9 eval { $data = json_decode($data); };
303 }
304 # do we need to support other content-type?
305 }
306  
307 36 my $schema_ptr = $ctype_ptr->xget('schema');
308 36 my $schema_prop_ptr = $schema_ptr->xget('properties');
309  
310 36 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($schema_prop_ptr and $content_type and index($content_type, "application/x-www-form-urlencoded") != -1 || index($content_type, "multipart/") != -1 and ref $data eq "HASH") 5 31 36
Condition75%9/12
$schema_prop_ptr and $content_type and index($content_type, "application/x-www-form-urlencoded") != -1 || index($content_type, "multipart/") != -1 and ref $data eq "HASH"
!ll&&!rl&&r
3105
$schema_prop_ptr and $content_type and index($content_type, "application/x-www-form-urlencoded") != -1 || index($content_type, "multipart/") != -1
!ll&&!rl&&r
6255
$schema_prop_ptr and $content_type
!ll&&!rl&&r
6030
index($content_type, "application/x-www-form-urlencoded") != -1 or index($content_type, "multipart/") != -1
l!l&&r!l&&!r
5025
MC/DC40%2/5
$schema_prop_ptr and $content_type and index($content_type, "application/x-www-form-urlencoded") != -1 || index($content_type, "multipart/") != -1 and ref $data eq "HASH"
$schema_prop_ptr $content_type index($content_type, "application/x-www-form-urlencoded") != -1 index($content_type, "multipart/") != -1 ref $data eq "HASH"
Truth table42%3/7
A and B and C or D and E
A= $schema_prop_ptrB= $content_typeC= index($content_type, "application/x-www-form-urlencoded") != -1D= index($content_type, "multipart/") != -1E= ref $data eq "HASH"
ABCDEresult
0XXXX0
10XXX0
1100X0
111X00
110100
111X11
110111
311 $schema_prop_ptr &&
312 $content_type &&
313 (
314 index($content_type ,'application/x-www-form-urlencoded') != -1 ||
315 index($content_type, 'multipart/') != -1
316 ) &&
317 ref $data eq 'HASH'
318 ) {
319 5 for my $property_name ($schema_prop_ptr->keys(raw => 1)) {
320 12 my $property_ctype_ptr = $ctype_ptr->xget('encoding', $property_name, 'contentType');
321 12 ▶ my $property_ctype = $property_ctype_ptr ? $property_ctype_ptr->value : '';
Branch: 1 branch
BranchTrueFalseTotal
$property_ctype_ptr ? : 3 9 12
322 12 ▶ unless ($property_ctype) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($property_ctype) 9 3 12
323 9 my $prop_type_ptr = $schema_prop_ptr->xget($property_name, 'type');
324 9 ▶ $property_ctype = $prop_type_ptr && $prop_type_ptr->value eq 'object' ? 'application/json' : '';
Branch: 1 branch
BranchTrueFalseTotal
$prop_type_ptr && $prop_type_ptr->value eq 'object' ? : 2 7 9
Condition66%2/3
$prop_type_ptr and $prop_type_ptr->value eq "object"
!ll&&!rl&&r
072
MC/DC50%1/2
$prop_type_ptr and $prop_type_ptr->value eq "object"
$prop_type_ptr $prop_type_ptr->value eq "object"
Truth table66%2/3
A and B
A= $prop_type_ptrB= $prop_type_ptr->value eq "object"
ABresult
0X0
100
111
325 }
326  
327 12 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (index($property_ctype, "application/json") != -1 and exists $data->{$property_name} and not ref $data->{$property_name}) 5 7 12
Condition66%4/6
index($property_ctype, "application/json") != -1 and exists $data->{$property_name} and not ref $data->{$property_name}
!ll&&!rl&&r
705
index($property_ctype, "application/json") != -1 and exists $data->{$property_name}
!ll&&!rl&&r
705
MC/DC33%1/3
index($property_ctype, "application/json") != -1 and exists $data->{$property_name} and not ref $data->{$property_name}
index($property_ctype, "application/json") != -1 exists $data->{$property_name} not ref $data->{$property_name}
Truth table50%2/4
A and B and not(C)
A= index($property_ctype, "application/json") != -1B= exists $data->{$property_name}C= not ref $data->{$property_name}
ABCresult
0XX0
10X0
1100
1111
328 index($property_ctype, 'application/json') != -1 &&
329 exists $data->{$property_name} &&
330 !ref $data->{$property_name}
331 ) {
332 5 eval {
333 5 $data->{$property_name} = json_decode($data->{$property_name});
334 };
335 }
336 # do we need to support other content-type?
337 }
338 }
339  
340 return $self->validate_schema($data,
341 schema => $schema_ptr->value,
342 path => '/',
343 direction => $ctx->{direction},
344 36 scope => $schema_ptr->scope
345 );
346 }
347  
348 sub _fill_parameters {
349 61 ▶ my ($self, $hash, $ptr) = @_;
Subroutine _fill_parameters: called CC=6 SCAR=17.9
350 61 ▶ return unless ref $ptr->value;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ptr->value 24 37 61
351  
352 37 ▶ if (ref $ptr->value eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $ptr->value eq 'ARRAY') { } 25 12 37
elsif (ref $ptr->value eq 'HASH') { } 12 0 12
353 25 for my $p ($ptr->keys) {
354 61 my $param_ptr = $ptr->get($p);
355 61 my $param = $param_ptr->value;
356  
357 61 my ($name, $in) = @$param{qw/name in/};
358  
359 61 $hash->{$in}{$name} = $param_ptr;
360 }
361 } elsif (ref $ptr->value eq 'HASH') {
362 # currently used for headers in response
363 12 my $in = 'header';
364 12 for my $name ($ptr->keys(raw => 1)) {
365 12 my $param_ptr = $ptr->xget($name);
366 12 $hash->{$in}{$name} = $param_ptr;
367 }
368 }
369 }
370  
371 sub _wrap_params {
372 42 ▶ my ($self, $parameters) = @_;
Subroutine _wrap_params: called CC=6 SCAR=18.3
373  
374 42 my $get_user_param = {};
375 42 for my $type (qw/path query header cookie/) {
376 168 ▶ next unless $parameters->{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless $parameters->{$type} 99 69 168
377  
378 69 ▶ if (ref $parameters->{$type} eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $parameters->{$type} eq 'CODE') { } 0 69 69
elsif (ref $parameters->{$type} eq 'HASH') { } 69 0 69
379 0 $get_user_param->{$type} = $parameters->{$type};
380 } elsif (ref $parameters->{$type} eq 'HASH') {
381 69 my $data = $parameters->{$type};
382 69 ▶ $data = +{ map { lc $_ => $data->{$_} } keys %$data } if $type eq 'header';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "header" 27 42 69
383 $get_user_param->{$type} = sub {
384 75 ▶ my $param = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
385 75 return (exists($data->{$param}), $data->{$param});
386 }
387 69 } else {
388 0 croak qq{param "$type" must be hashref or coderef};
389 }
390 }
391  
392 42 return $get_user_param;
393 }
394  
395 sub json_pointer {
396 42 ▶ my $self = shift;
Subroutine json_pointer: called CC=1 SCAR=0.0
Pod undocumented
397 42 return JSONSchema::Validator::JSONPointer->new(
398 scope => $self->scope,
399 value => $self->schema,
400 validator => $self
401 );
402 }
403  
404 8 1;
405  
406 __END__
« Validator/Constraints/Draft4.pm ↑ Summary Validator/Format.pm »