Validator/OAS30.pm

filter: Statementstmt 95.1% 237 / 249 Branchbran 79.2% 103 / 130 Conditioncond 68.4% 39 / 57 MC/DCmcdc 32.2% 10 / 31 Subroutinesub 100% 26 / 26 Podpod 83.3% 5 / 6 totaltotal 84.1% 420 / 499 CC 94 SCAR 24.2
CC
94
Cov
87%
_validate_content
21.0
validate_response
17.0
_validate_type_params
14.3
CRAP 11.2
« Validator/Constraints/Draft4.pm ↑ Summary Validator/URIResolver.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 39 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
22 39 my ($class, %params) = @_;
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 121 ▶sub validate_schema {
Subroutine validate_schema: called CC=10 SCAR=23.7
Pod documented
39 121 my ($self, $instance, %params) = @_;
40  
41 121 ▶ my $schema = $params{schema} // $self->schema;
Condition66%2/3
$params{"schema"} // $self->schema
l!l&&r!l&&!r
86350
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 121 ▶ my $instance_path = $params{instance_path} // '/';
Condition50%1/2
$params{"instance_path"} // "/"
l!l
0121
MC/DC0%0/1
$params{"instance_path"} // "/"
$params{"instance_path"}
Truth table50%1/2
A
A= $params{"instance_path"}
Aresult
11
00
43 121 ▶ my $schema_path = $params{schema_path} // '/';
Condition50%1/2
$params{"schema_path"} // "/"
l!l
0121
MC/DC0%0/1
$params{"schema_path"} // "/"
$params{"schema_path"}
Truth table50%1/2
A
A= $params{"schema_path"}
Aresult
11
00
44 121 my $direction = $params{direction};
45 121 my $scope = $params{scope};
46  
47 121 ▶ croak 'param "direction" is required' unless $direction;
Branch: 1 branch
BranchTrueFalseTotal
unless $direction 0 121 121
48 121 ▶ croak '"direction" must have one of values: "request", "response"'
Branch: 1 branch
BranchTrueFalseTotal
if $direction ne "request" and $direction ne "response" 0 121 121
Condition66%2/3
$direction ne "request" and $direction ne "response"
!ll&&!rl&&r
90310
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 121 ▶ croak 'No schema specified' unless defined $schema;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $schema 0 121 121
52  
53 121 ▶ push @{$self->scopes}, $scope if $scope;
Branch: 1 branch
BranchTrueFalseTotal
if $scope 34 87 121
54  
55 121 my ($errors, $warnings) = ([], []);
56 121 my $result = $self->_validate_schema($instance, $schema, $instance_path, $schema_path, {
57 errors => $errors,
58 warnings => $warnings,
59 direction => $direction
60 }
61 );
62  
63 121 ▶ pop @{$self->scopes} if $scope;
Branch: 1 branch
BranchTrueFalseTotal
if $scope 34 87 121
64  
65 121 return $result, $errors, $warnings;
66 }
67  
68 524 ▶sub _schema_keys {
Subroutine _schema_keys: called CC=7 SCAR=19.5
69 524 my ($self, $schema, $instance_path, $data) = @_;
70 # if ref exists other preperties MUST be ignored
71 524 ▶ return '$ref' if $schema->{'$ref'};
Branch: 1 branch
BranchTrueFalseTotal
if $schema->{"\$ref"} 62 462 524
72  
73 462 ▶ return ('deprecated') if $schema->{deprecated} && !$self->validate_deprecated;
Branch: 1 branch
BranchTrueFalseTotal
if $schema->{"deprecated"} and not $self->validate_deprecated 2 460 462
Condition100%3/3
$schema->{"deprecated"} and not $self->validate_deprecated
!ll&&!rl&&r
45642
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 460 ▶ if (grep { $_ eq 'discriminator' } keys %$schema) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq "discriminator";} keys %$schema) 52 408 460
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 408 return keys %$schema;
84 }
85  
86 22 ▶sub validate_request {
Subroutine validate_request: called CC=10 SCAR=23.3
Pod documented
87 22 my ($self, %params) = @_;
88  
89 22 ▶ my $method = lc($params{method} or croak 'param "method" is required');
Condition50%1/2
$params{'method'} || croak('param "method" is required')
l!l
220
MC/DC0%0/1
$params{'method'} || croak('param "method" is required')
$params{'method'}
Truth table50%1/2
A
A= $params{'method'}
Aresult
11
00
90 22 ▶ 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") 22 0 22
91  
92 22 my $get_user_param = $self->_wrap_params($params{parameters});
93  
94 22 ▶ my $user_body = $params{parameters}{body} // []; # [exists, content-type, value]
Condition50%1/2
$params{"parameters"}{"body"} // []
l!l
220
MC/DC0%0/1
$params{"parameters"}{"body"} // []
$params{"parameters"}{"body"}
Truth table50%1/2
A
A= $params{"parameters"}{"body"}
Aresult
11
00
95  
96 22 my $base_ptr = $self->json_pointer->xget('paths', $openapi_path);
97 22 ▶ return 1, [], [] unless $base_ptr;
Branch: 1 branch
BranchTrueFalseTotal
unless $base_ptr 0 22 22
98  
99 22 my $schema_params = {query => {}, header => {}, path => {}, cookie => {}};
100  
101 # Common Parameter Object
102 22 my $common_params_ptr = $base_ptr->xget('parameters');
103 22 $self->_fill_parameters($schema_params, $common_params_ptr);
104  
105 # Operation Object
106 22 my $operation_ptr = $base_ptr->xget($method);
107 22 ▶ return 1, [], [] unless $operation_ptr;
Branch: 1 branch
BranchTrueFalseTotal
unless $operation_ptr 0 22 22
108  
109 22 my ($result, $context) = (1, {errors => [], warnings => [], direction => 'request'});
110 22 ▶ if ($operation_ptr->xget('deprecated')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($operation_ptr->xget("deprecated")) 1 21 22
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 21 my $params_ptr = $operation_ptr->xget('parameters');
117 21 $self->_fill_parameters($schema_params, $params_ptr);
118  
119 # validate path, query, header, cookie
120 21 my $r = $self->_validate_params($context, $schema_params, $get_user_param);
121 21 ▶ $result = 0 unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 8 13 21
122  
123 # validate body
124 21 my $body_ptr = $operation_ptr->xget('requestBody');
125 21 $r = $self->_validate_body($context, $user_body, $body_ptr);
126 21 ▶ $result = 0 unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 4 17 21
127  
128 21 return $result, $context->{errors}, $context->{warnings};
129 }
130  
131 22 ▶sub validate_response {
Subroutine validate_response: called CC=15 SCAR=28.3
Pod documented
132 22 my ($self, %params) = @_;
133  
134 22 ▶ my $method = lc($params{method} or croak 'param "method" is required');
Condition50%1/2
$params{'method'} || croak('param "method" is required')
l!l
220
MC/DC0%0/1
$params{'method'} || croak('param "method" is required')
$params{'method'}
Truth table50%1/2
A
A= $params{'method'}
Aresult
11
00
135 22 ▶ 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") 22 0 22
136 22 ▶ 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") 22 0 22
137  
138 22 my $get_user_param = $self->_wrap_params($params{parameters});
139  
140 22 ▶ my $user_body = $params{parameters}{body} // []; # [exists, content-type, value]
Condition50%1/2
$params{"parameters"}{"body"} // []
l!l
220
MC/DC0%0/1
$params{"parameters"}{"body"} // []
$params{"parameters"}{"body"}
Truth table50%1/2
A
A= $params{"parameters"}{"body"}
Aresult
11
00
141  
142 22 my $base_ptr = $self->json_pointer->xget('paths', $openapi_path, $method);
143 22 ▶ return 1, [], [] unless $base_ptr;
Branch: 1 branch
BranchTrueFalseTotal
unless $base_ptr 0 22 22
144  
145 22 my ($result, $context) = (1, {errors => [], warnings => [], direction => 'response'});
146 22 ▶ if ($base_ptr->xget('deprecated')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base_ptr->xget("deprecated")) 1 21 22
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 21 my $responses_ptr = $base_ptr->xget('responses');
152 21 ▶ return $result, $context->{errors}, $context->{warnings} unless $responses_ptr;
Branch: 1 branch
BranchTrueFalseTotal
unless $responses_ptr 0 21 21
153  
154 21 my $status_ptr = $responses_ptr->xget($http_status);
155 21 ▶ $status_ptr = $responses_ptr->xget('default') unless $status_ptr;
Branch: 1 branch
BranchTrueFalseTotal
unless $status_ptr 2 19 21
156  
157 21 ▶ unless ($status_ptr) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($status_ptr) 0 21 21
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 21 my $schema_params = {header => {}};
163 21 $self->_fill_parameters($schema_params, $status_ptr->xget('headers'));
164  
165 # validate headers
166 21 my $r = $self->_validate_params($context, $schema_params, $get_user_param);
167 21 ▶ $result = 0 unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 1 20 21
168  
169 # validate body
170 21 my ($exists, $content_type, $data) = @$user_body;
171 21 ▶ return $result, $context->{errors}, $context->{warnings} unless $exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $exists 5 16 21
172  
173 16 ($r, my $errors, my $warnings) = $self->_validate_content($context, $status_ptr, $content_type, $data);
174 16 ▶ unless ($r) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r) 7 9 16
175 7 push @{$context->{errors}}, error(message => 'response body error', context => $errors);
176 7 $result = 0;
177 }
178 0 ▶ push @{$context->{warnings}}, error(message => 'response body warning', context => $warnings) if @$warnings;
Branch: 1 branch
BranchTrueFalseTotal
if @$warnings 0 16 16
179  
180 16 return $result, $context->{errors}, $context->{warnings};
181 }
182  
183 21 ▶sub _validate_body {
Subroutine _validate_body: called CC=8 SCAR=22.4
184 21 my ($self, $ctx, $user_body, $body_ptr) = @_;
185  
186 # body in specification is omit
187 21 ▶ return 1 unless $body_ptr;
Branch: 1 branch
BranchTrueFalseTotal
unless $body_ptr 6 15 21
188  
189 # skip validation if user not specify body
190 15 ▶ return 1 unless $user_body && @$user_body;
Branch: 1 branch
BranchTrueFalseTotal
unless $user_body and @$user_body 0 15 15
Condition33%1/3
$user_body and @$user_body
!ll&&!rl&&r
0015
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 15 my ($exists, $content_type, $data) = @$user_body;
193  
194 15 ▶ unless ($exists) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($exists) 2 13 15
195 2 my $required = $body_ptr->xget('required');
196 2 ▶ if ($required) {
Branch: 1 branch
BranchTrueFalseTotal
if ($required) 0 2 2
197 0 push @{$ctx->{errors}}, error(message => q{body is required});
198 0 return 0;
199 }
200 2 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 0 ▶ 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 42 ▶sub _validate_params {
Subroutine _validate_params: called CC=5 SCAR=16.1
210 42 my ($self, $ctx, $schema_params, $get_user_param) = @_;
211 42 my $result = 1;
212 42 for my $type (keys %$schema_params) {
213 105 ▶ next unless %{$schema_params->{$type}};
Branch: 1 branch
BranchTrueFalseTotal
unless %{$schema_params->{$type};} 56 49 105
214 # skip validation if user not specify getter for such params type
215 49 ▶ if (not exists $get_user_param->{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $get_user_param->{$type}) 2 47 49
216 2 push @{$ctx->{errors}}, error(message => qq{schema specifies '$type' parameters, but '$type' parameter missing in instance data});
217 2 $result = 0;
218 2 next;
219 }
220 47 my $r = $self->_validate_type_params($ctx, $type, $schema_params->{$type}, $get_user_param->{$type});
221 47 ▶ $result = 0 unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 7 40 47
222 }
223 42 return $result;
224 }
225  
226 47 ▶sub _validate_type_params {
Subroutine _validate_type_params: called CC=14 SCAR=26.6
227 47 my ($self, $ctx, $type, $params, $get_user_param) = @_;
228  
229 47 my $result = 1;
230 47 for my $param (keys %$params) {
231 77 my $data_ptr = $params->{$param};
232  
233 77 my ($exists, $value) = $get_user_param->($param);
234 77 ▶ ($exists, $value) = $get_user_param->(lc $param) if !$exists && $type eq 'header';
Branch: 1 branch
BranchTrueFalseTotal
if not $exists and $type eq "header" 15 62 77
Condition100%3/3
not $exists and $type eq "header"
!ll&&!rl&&r
451715
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
235  
236 77 ▶ unless ($exists) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($exists) 18 59 77
237 18 ▶ if ($data_ptr->xget('required')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_ptr->xget("required")) 2 16 18
238 2 push @{$ctx->{errors}}, error(message => qq{$type param "$param" is required});
239 2 $result = 0;
240 }
241 18 next;
242 }
243  
244 59 ▶ if ($data_ptr->xget('deprecated')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_ptr->xget("deprecated")) 1 58 59
245 1 push @{$ctx->{warnings}}, error(message => qq{$type param "$param" is deprecated});
246 1 ▶ next unless $self->validate_deprecated;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->validate_deprecated 1 0 1
247 }
248  
249 58 ▶ 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 58 58
Condition66%2/3
$data_ptr->xget("schema") or $data_ptr->xget("content")
l!l&&r!l&&!r
42160
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
250  
251 58 my ($r, $errors, $warnings);
252  
253 58 ▶ if ($data_ptr->xget('schema')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($data_ptr->xget('schema')) { } 42 16 58
elsif ($data_ptr->xget('content')) { } 16 0 16
254 42 my $schema_ptr = $data_ptr->xget('schema');
255 ($r, $errors, $warnings) = $self->validate_schema($value,
256 schema => $schema_ptr->value,
257 path => '/',
258 direction => $ctx->{direction},
259 42 scope => $schema_ptr->scope
260 );
261 } elsif ($data_ptr->xget('content')) {
262 16 ($r, $errors, $warnings) = $self->_validate_content($ctx, $data_ptr, undef, $value);
263 }
264  
265 58 ▶ unless ($r) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r) 5 53 58
266 5 push @{$ctx->{errors}}, error(message => qq{$type param "$param" has error}, context => $errors);
267 5 $result = 0;
268 }
269 0 ▶ push @{$ctx->{warnings}}, error(message => qq{$type param "$param" has warning}, context => $warnings) if @$warnings;
Branch: 1 branch
BranchTrueFalseTotal
if @$warnings 0 58 58
270 }
271  
272 47 return $result;
273 }
274  
275 # ptr - JSONSchema::Validator::JSONPointer
276 # content_type - string|null
277 # data - string|HASH|ARRAY
278 45 ▶sub _validate_content {
Subroutine _validate_content: called CC=20 SCAR=30.5
279 45 my ($self, $ctx, $ptr, $content_type, $data) = @_;
280  
281 45 my $content_ptr = $ptr->xget('content');
282 # content in body is required but in params is optional
283 45 ▶ return 1, [], [] unless $content_ptr;
Branch: 1 branch
BranchTrueFalseTotal
unless $content_ptr 1 44 45
284  
285 44 my $ctype_ptr;
286 44 ▶ if ($content_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_type) { } 28 16 44
287 28 $ctype_ptr = $content_ptr->xget($content_type);
288 28 ▶ unless ($ctype_ptr) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ctype_ptr) 0 28 28
289 0 return 0, [error(message => qq{content with content-type $content_type is not in schema})], [];
290 }
291 } else {
292 16 my $mtype_map = $content_ptr->value;
293 16 my @keys = $content_ptr->keys(raw => 1);
294 16 ▶ return 0, [error(message => qq{content type not specified; schema must have exactly one content_type})], [] unless scalar(@keys) == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @keys == 1 0 16 16
295  
296 16 $content_type = $keys[0];
297 16 $ctype_ptr = $content_ptr->xget($content_type);
298 }
299  
300 44 ▶ unless (ref $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $data) 10 34 44
301 10 ▶ if (index($content_type, 'application/json') != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($content_type, "application/json") != -1) 10 0 10
302 10 eval { $data = json_decode($data); };
303 }
304 # do we need to support other content-type?
305 }
306  
307 44 my $schema_ptr = $ctype_ptr->xget('schema');
308 44 my $schema_prop_ptr = $schema_ptr->xget('properties');
309  
310 44 ▶ 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 39 44
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
3905
$schema_prop_ptr and $content_type and index($content_type, "application/x-www-form-urlencoded") != -1 || index($content_type, "multipart/") != -1
!ll&&!rl&&r
12275
$schema_prop_ptr and $content_type
!ll&&!rl&&r
12032
index($content_type, "application/x-www-form-urlencoded") != -1 or index($content_type, "multipart/") != -1
l!l&&r!l&&!r
5027
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 44 scope => $schema_ptr->scope
345 );
346 }
347  
348 64 ▶sub _fill_parameters {
Subroutine _fill_parameters: called CC=6 SCAR=17.9
349 64 my ($self, $hash, $ptr) = @_;
350 64 ▶ return unless ref $ptr->value;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ptr->value 24 40 64
351  
352 40 ▶ if (ref $ptr->value eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $ptr->value eq 'ARRAY') { } 27 13 40
elsif (ref $ptr->value eq 'HASH') { } 13 0 13
353 27 for my $p ($ptr->keys) {
354 67 my $param_ptr = $ptr->get($p);
355 67 my $param = $param_ptr->value;
356  
357 67 my ($name, $in) = @$param{qw/name in/};
358  
359 67 $hash->{$in}{$name} = $param_ptr;
360 }
361 } elsif (ref $ptr->value eq 'HASH') {
362 # currently used for headers in response
363 13 my $in = 'header';
364 13 for my $name ($ptr->keys(raw => 1)) {
365 13 my $param_ptr = $ptr->xget($name);
366 13 $hash->{$in}{$name} = $param_ptr;
367 }
368 }
369 }
370  
371 44 ▶sub _wrap_params {
Subroutine _wrap_params: called CC=6 SCAR=18.3
372 44 my ($self, $parameters) = @_;
373  
374 44 my $get_user_param = {};
375 44 for my $type (qw/path query header cookie/) {
376 176 ▶ next unless $parameters->{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless $parameters->{$type} 99 77 176
377  
378 77 ▶ if (ref $parameters->{$type} eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $parameters->{$type} eq 'CODE') { } 0 77 77
elsif (ref $parameters->{$type} eq 'HASH') { } 77 0 77
379 0 $get_user_param->{$type} = $parameters->{$type};
380 } elsif (ref $parameters->{$type} eq 'HASH') {
381 77 my $data = $parameters->{$type};
382 77 ▶ $data = +{ map { lc $_ => $data->{$_} } keys %$data } if $type eq 'header';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "header" 33 44 77
383 92 ▶ $get_user_param->{$type} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
384 92 my $param = shift;
385 92 return (exists($data->{$param}), $data->{$param});
386 }
387 77 } else {
388 0 croak qq{param "$type" must be hashref or coderef};
389 }
390 }
391  
392 44 return $get_user_param;
393 }
394  
395 44 ▶sub json_pointer {
Subroutine json_pointer: called CC=1 SCAR=0.0
Pod undocumented
396 44 my $self = shift;
397 44 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/URIResolver.pm »