Validator/Schema/OpenAPIv2.pm

filter: Statementstmt 97.9% 244 / 249 Branchbran 88.8% 128 / 144 Conditioncond 69.0% 78 / 113 MC/DCmcdc 42.4% 28 / 66 Subroutinesub 91.1% 31 / 34 Podpod 100% 9 / 9 totaltotal 84.2% 518 / 615 CC 131 SCAR 22.2
CC
131
Cov
89%
_find_all_nodes
15.1
base_url
14.2
routes
13.8
CRAP 9.2
« Validator/Schema/Draft201909.pm ↑ Summary Validator/URI.pm »
1 package JSON::Validator::Schema::OpenAPIv2;
2 20 ▶use Mojo::Base 'JSON::Validator::Schema::Draft4';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 20 ▶use JSON::Validator::Util qw(E data_type negotiate_content_type schema_type);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 20 ▶use Mojo::Collection;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 20 my $X_RE = qr{^x-};
8  
9 # Some of the keywords are OpenAPIv3 keywords
10 20 my %SKIP_KEYWORDS_IN_PATH = map { ($_, 1) } qw(description parameters servers summary);
11  
12 511 ▶has errors => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
13 511 my $self = shift;
14 511 my $validator = $self->new(store => $self->store, _refs => {})->coerce('numbers,strings');
15 511 return [$validator->resolve($self->specification)->validate($self->resolve->data)];
16 20 };
17  
18 20 has moniker => 'openapiv2';
19 20 has specification => 'http://swagger.io/v2/schema.json';
20  
21 1 ▶sub add_default_response {
Subroutine add_default_response: called CC=6 SCAR=18.6
Pod documented
22 1 my ($self, $params) = ($_[0], shift->_params_for_add_default_response(@_));
23  
24 1 ▶ my $definitions = $self->data->{definitions} ||= {};
Condition50%1/2
$self->data->{"definitions"} ||= {}
l!l
10
MC/DC0%0/1
$self->data->{"definitions"} ||= {}
$self->data->{"definitions"}
Truth table50%1/2
A
A= $self->data->{"definitions"}
Aresult
11
00
25 1 ▶ $definitions->{$params->{name}} ||= $params->{schema};
Condition33%1/3
$$definitions{$params->{"name"}} ||= $params->{"schema"}
l!l&&r!l&&!r
010
MC/DC0%0/2
$$definitions{$params->{"name"}} ||= $params->{"schema"}
$$definitions{$params->{"name"}} $params->{"schema"}
Truth table33%1/3
A ||= B
A= $$definitions{$params->{"name"}}B= $params->{"schema"}
ABresult
1X1
011
000
26 1 my $ref = {'$ref' => sprintf '%s#/definitions/%s', $self->id, $params->{name}};
27 1 $self->_register_ref($ref, schema => $definitions->{$params->{name}});
28  
29 1 for my $route ($self->routes->each) {
30 3 my $op = $self->get([paths => @$route{qw(path method)}]);
31 3 for my $status (@{$params->{status}}) {
32 15 ▶ $op->{responses}{$status} ||= {description => $params->{description}, schema => $ref};
Condition50%1/2
$op->{"responses"}{$status} ||= {"description", $params->{"description"}, "schema", $ref}
l!l
015
MC/DC0%0/1
$op->{"responses"}{$status} ||= {"description", $params->{"description"}, "schema", $ref}
$op->{"responses"}{$status}
Truth table50%1/2
A
A= $op->{"responses"}{$status}
Aresult
11
00
33 }
34 }
35  
36 1 return $self;
37 }
38  
39 10 ▶sub base_url {
Subroutine base_url: called CC=13 SCAR=26.5
Pod documented
40 10 my ($self, $url) = @_;
41 10 my $spec = $self->data;
42  
43 # Get
44 10 ▶ unless ($url) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url) 7 3 10
45 7 $url = Mojo::URL->new;
46 7 ▶ my @host_port = split ':', ($spec->{host} // '');
Condition50%1/2
$spec->{"host"} // ""
l!l
70
MC/DC0%0/1
$spec->{"host"} // ""
$spec->{"host"}
Truth table50%1/2
A
A= $spec->{"host"}
Aresult
11
00
47 7 ▶ $url->host($host_port[0]) if $host_port[0];
Branch: 1 branch
BranchTrueFalseTotal
if $host_port[0] 7 0 7
48 7 ▶ $url->port($host_port[1]) if $host_port[1];
Branch: 1 branch
BranchTrueFalseTotal
if $host_port[1] 3 4 7
49 7 ▶ $url->path($spec->{basePath} || '/');
Condition50%1/2
$spec->{'basePath'} || '/'
l!l
70
MC/DC0%0/1
$spec->{'basePath'} || '/'
$spec->{'basePath'}
Truth table50%1/2
A
A= $spec->{'basePath'}
Aresult
11
00
50 7 ▶ $url->scheme($spec->{schemes} && $spec->{schemes}[0] || undef);
Condition80%4/5
$spec->{'schemes'} && $spec->{'schemes'}[0] || undef
l!l
52
$spec->{"schemes"} and $spec->{"schemes"}[0]
!ll&&!rl&&r
205
MC/DC50%1/2
$spec->{'schemes'} && $spec->{'schemes'}[0] || undef
$spec->{"schemes"} $spec->{"schemes"}[0]
Truth table66%2/3
A and B
A= $spec->{"schemes"}B= $spec->{"schemes"}[0]
ABresult
111
0X0
100
51 7 ▶ $url->host('localhost') if $url->scheme and !$url->host;
Branch: 1 branch
BranchTrueFalseTotal
if $url->scheme and not $url->host 0 7 7
Condition66%2/3
$url->scheme and not $url->host
!ll&&!rl&&r
250
MC/DC0%0/2
$url->scheme and not $url->host
$url->scheme not $url->host
Truth table66%2/3
A and not(B)
A= $url->schemeB= not $url->host
ABresult
0X0
100
111
52 7 return $url;
53 }
54  
55 # Set
56 3 $url = Mojo::URL->new($url)->to_abs($self->base_url);
57 3 ▶ $spec->{host} = $url->host_port if $url->host_port;
Branch: 1 branch
BranchTrueFalseTotal
if $url->host_port 3 0 3
58 3 ▶ $spec->{schemes}[0] = $url->scheme if $url->scheme;
Branch: 1 branch
BranchTrueFalseTotal
if $url->scheme 3 0 3
59 3 ▶ $spec->{basePath} = $url->path->to_string || '/';
Condition100%2/2
$url->path->to_string || '/'
l!l
21
MC/DC100%1/1
$url->path->to_string || '/'
$url->path->to_string
Truth table100%2/2
A
A= $url->path->to_string
Aresult
11
00
60 3 return $self;
61 }
62  
63 1564 ▶sub coerce {
Subroutine coerce: called CC=3 SCAR=11.0
Pod documented
64 1564 my $self = shift;
65 1564 ▶ return $self->SUPER::coerce(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 513 1051 1564
66 1051 ▶ $self->{coerce} ||= {booleans => 1, numbers => 1, strings => 1};
Condition100%2/2
$self->{"coerce"} ||= {"booleans", 1, "numbers", 1, "strings", 1}
l!l
51046
MC/DC100%1/1
$self->{"coerce"} ||= {"booleans", 1, "numbers", 1, "strings", 1}
$self->{"coerce"}
Truth table100%2/2
A
A= $self->{"coerce"}
Aresult
11
00
67 1051 return $self->{coerce};
68 }
69  
70 21 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
71 21 my $self = shift->SUPER::new(@_);
72 21 $self->coerce; # make sure this attribute is built
73 21 $self;
74 }
75  
76 49 ▶sub parameters_for_request {
Subroutine parameters_for_request: called CC=7 SCAR=19.6
Pod documented
77 49 my $self = shift;
78 49 my ($method, $path) = (lc $_[0][0], $_[0][1]);
79  
80 49 my $cache_key = "parameters_for_request:$method:$path";
81 49 ▶ return $self->{cache}{$cache_key} if $self->{cache}{$cache_key};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cache"}{$cache_key} 38 11 49
82 11 ▶ return undef unless $self->get([paths => $path, $method]);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->get(["paths", $path, $method]) 1 10 11
83  
84 10 my @accepts = map {@$_} $self->_find_all_nodes([paths => $path, $method], 'consumes');
85 10 my @parameters;
86 10 for my $param (map {@$_} $self->_find_all_nodes([paths => $path, $method], 'parameters')) {
87 29 push @parameters, {%$param};
88 29 ▶ $parameters[-1]{type} ||= schema_type($param->{schema} || $param);
Condition50%3/6
$parameters[-1]{"type"} ||= schema_type($param->{"schema"} || $param)
l!l&&r!l&&!r
2603
$param->{'schema'} || $param
l!l&&r!l&&!r
300
MC/DC50%1/2
$parameters[-1]{"type"} ||= schema_type($param->{"schema"} || $param)
$parameters[-1]{"type"} schema_type($param->{"schema"} || $param)
MC/DC0%0/2
$param->{'schema'} || $param
$param->{'schema'} $param
Truth table66%2/3
A ||= B
A= $parameters[-1]{"type"}B= schema_type($param->{"schema"} || $param)
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $param->{'schema'}B= $param
ABresult
1X1
011
000
89 29 ▶ $parameters[-1]{accepts} = \@accepts if $param->{in} eq 'body';
Branch: 1 branch
BranchTrueFalseTotal
if $param->{"in"} eq "body" 3 26 29
90 }
91  
92 10 return $self->{cache}{$cache_key} = \@parameters;
93 }
94  
95 17 ▶sub parameters_for_response {
Subroutine parameters_for_response: called CC=9 SCAR=22.0
Pod documented
96 17 my $self = shift;
97 17 ▶ my ($method, $path, $status) = (lc $_[0][0], $_[0][1], $_[0][2] || 200);
Condition100%2/2
$_[0][2] || 200
l!l
314
MC/DC100%1/1
$_[0][2] || 200
$_[0][2]
Truth table100%2/2
A
A= $_[0][2]
Aresult
11
00
98  
99 17 ▶ $status ||= 200;
Condition50%1/2
$status ||= 200
l!l
170
MC/DC0%0/1
$status ||= 200
$status
Truth table50%1/2
A
A= $status
Aresult
11
00
100 17 my $cache_key = "parameters_for_response:$method:$path:$status";
101 17 ▶ return $self->{cache}{$cache_key} if $self->{cache}{$cache_key};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cache"}{$cache_key} 10 7 17
102  
103 7 my $responses = $self->get([paths => $path, $method, 'responses']);
104 7 ▶ my $response = $responses->{$status} || $responses->{default};
Condition100%3/3
$responses->{$status} || $responses->{'default'}
l!l&&r!l&&!r
421
MC/DC100%2/2
$responses->{$status} || $responses->{'default'}
$responses->{$status} $responses->{'default'}
Truth table100%3/3
A || B
A= $responses->{$status}B= $responses->{'default'}
ABresult
1X1
011
000
105 7 ▶ return undef unless $response;
Branch: 1 branch
BranchTrueFalseTotal
unless $response 1 6 7
106  
107 6 my @parameters;
108 6 ▶ if (my $headers = $response->{headers}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $headers = $response->{"headers"}) 2 4 6
109 2 push @parameters, map { +{%{$headers->{$_}}, in => 'header', name => $_} } sort keys %$headers;
110 }
111  
112 6 my @accepts = $self->_find_all_nodes([paths => $path, $method], 'produces');
113 6 ▶ if (exists $response->{schema}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $response->{"schema"}) 5 1 6
114 5 ▶ push @parameters, {%$response, in => 'body', name => 'body', accepts => pop @accepts || []};
Condition100%2/2
pop @accepts || []
l!l
32
MC/DC100%1/1
pop @accepts || []
pop @accepts
Truth table100%2/2
A
A= pop @accepts
Aresult
11
00
115 }
116  
117 6 return $self->{cache}{$cache_key} = \@parameters;
118 }
119  
120 7 ▶sub routes {
Subroutine routes: called CC=13 SCAR=26.2
Pod documented
121 7 my $self = shift;
122  
123 7 my @paths_with_weight;
124 7 ▶ for my $path (keys %{$self->get('/paths') || {}}) {
Condition50%1/2
$self->get('/paths') || {}
l!l
70
MC/DC0%0/1
$self->get('/paths') || {}
$self->get('/paths')
Truth table50%1/2
A
A= $self->get('/paths')
Aresult
11
00
125 24 ▶ next if $path =~ $X_RE;
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ /$X_RE/u 0 24 24
126 24 my $weight = '1';
127 24 ▶ $weight .= /^\{/ ? 0 : 1 for split '/', $path;
Branch: 1 branch
BranchTrueFalseTotal
/^\{/u ? : 24 52 76
128 24 push @paths_with_weight, [int $weight, $path];
129 }
130  
131 7 my @operations;
132 7 ▶ for (sort { $b->[0] <=> $a->[0] || $a->[1] cmp $b->[1] } @paths_with_weight) {
Condition66%2/3
$JSON::Validator::Schema::OpenAPIv2::b->[0] <=> $JSON::Validator::Schema::OpenAPIv2::a->[0] || $JSON::Validator::Schema::OpenAPIv2::a->[1] cmp $JSON::Validator::Schema::OpenAPIv2::b->[1]
l!l&&r!l&&!r
3280
MC/DC0%0/2
$JSON::Validator::Schema::OpenAPIv2::b->[0] <=> $JSON::Validator::Schema::OpenAPIv2::a->[0] || $JSON::Validator::Schema::OpenAPIv2::a->[1] cmp $JSON::Validator::Schema::OpenAPIv2::b->[1]
$JSON::Validator::Schema::OpenAPIv2::b->[0] <=> $JSON::Validator::Schema::OpenAPIv2::a->[0] $JSON::Validator::Schema::OpenAPIv2::a->[1] cmp $JSON::Validator::Schema::OpenAPIv2::b->[1]
Truth table66%2/3
A || B
A= $JSON::Validator::Schema::OpenAPIv2::b->[0] <=> $JSON::Validator::Schema::OpenAPIv2::a->[0]B= $JSON::Validator::Schema::OpenAPIv2::a->[1] cmp $JSON::Validator::Schema::OpenAPIv2::b->[1]
ABresult
1X1
011
000
133 24 my $path = $_->[1];
134 24 ▶ next unless my $path_item = $self->get([paths => $path]);
Branch: 1 branch
BranchTrueFalseTotal
unless my $path_item = $self->get(["paths", $path]) 0 24 24
135 24 ▶ $path_item = $self->get($path_item->{'$ref'} =~ s!^#!!r) if $path_item->{'$ref'};
Branch: 1 branch
BranchTrueFalseTotal
if $path_item->{"\$ref"} 0 24 24
136  
137 24 for my $method (sort keys %$path_item) {
138 30 ▶ next if $method =~ $X_RE or $SKIP_KEYWORDS_IN_PATH{$method};
Branch: 1 branch
BranchTrueFalseTotal
if $method =~ /$X_RE/u or $SKIP_KEYWORDS_IN_PATH{$method} 2 28 30
Condition66%2/3
$method =~ /$X_RE/u or $SKIP_KEYWORDS_IN_PATH{$method}
l!l&&r!l&&!r
0228
MC/DC50%1/2
$method =~ /$X_RE/u or $SKIP_KEYWORDS_IN_PATH{$method}
$method =~ /$X_RE/u $SKIP_KEYWORDS_IN_PATH{$method}
Truth table66%2/3
A or B
A= $method =~ /$X_RE/uB= $SKIP_KEYWORDS_IN_PATH{$method}
ABresult
1X1
011
000
139 28 push @operations, {method => $method, operation_id => $path_item->{$method}{operationId}, path => $path};
140 }
141 }
142  
143 7 return Mojo::Collection->new(@operations);
144 }
145  
146 138 ▶sub validate_request {
Subroutine validate_request: called CC=3 SCAR=11.0
Pod documented
147 138 my ($self, $method_path, $req) = @_;
148 138 my $parameters = $self->parameters_for_request($method_path);
149  
150 138 my %get;
151 138 for my $in (qw(body cookie formData header path query)) {
152 320 ▶ $get{$in} = ref $req->{$in} eq 'CODE' ? $req->{$in} : sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
153 320 my ($name, $param) = @_;
154 320 ▶ return {exists => exists $req->{$in}, value => $req->{$in}} unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 2 318 320
155 318 return {exists => exists $req->{$in}{$name}, value => $req->{$in}{$name}};
156 828 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
ref $req->{$in} eq 'CODE' ? : 98 730 828
157 }
158  
159 138 return $self->_validate_request_or_response(request => $parameters, \%get);
160 }
161  
162 34 ▶sub validate_response {
Subroutine validate_response: called CC=3 SCAR=11.0
Pod documented
163 34 my ($self, $method_path_status, $res) = @_;
164 34 my $parameters = $self->parameters_for_response($method_path_status);
165  
166 34 my %get;
167 34 for my $in (qw(body cookie header)) {
168 21 ▶ $get{$in} = ref $res->{$in} eq 'CODE' ? $res->{$in} : sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
169 21 my ($name, $param) = @_;
170 21 return {exists => exists $res->{$in}{$name}, value => $res->{$in}{$name}};
171 102 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
ref $res->{$in} eq 'CODE' ? : 32 70 102
172 }
173  
174 34 return $self->_validate_request_or_response(response => $parameters, \%get);
175 }
176  
177 4 ▶sub _build_formats {
Subroutine _build_formats: called CC=1 SCAR=1.2
178 4 my $self = shift;
179  
180 return {
181 0 ▶ 'binary' => sub {undef},
Subroutine __ANON__: not called CC=1 SCAR=6.9
182 'byte' => JSON::Validator::Formats->can('check_byte'),
183 'date' => JSON::Validator::Formats->can('check_date'),
184 'date-time' => JSON::Validator::Formats->can('check_date_time'),
185 'double' => JSON::Validator::Formats->can('check_double'),
186 'email' => JSON::Validator::Formats->can('check_email'),
187 'float' => JSON::Validator::Formats->can('check_float'),
188 'hostname' => JSON::Validator::Formats->can('check_hostname'),
189 'int32' => JSON::Validator::Formats->can('check_int32'),
190 'int64' => JSON::Validator::Formats->can('check_int64'),
191 'ipv4' => JSON::Validator::Formats->can('check_ipv4'),
192 'ipv6' => JSON::Validator::Formats->can('check_ipv6'),
193 0 ▶ 'password' => sub {undef},
Subroutine __ANON__: not called CC=1 SCAR=6.9
194 4 'regex' => JSON::Validator::Formats->can('check_regex'),
195 'uri' => JSON::Validator::Formats->can('check_uri'),
196 };
197 }
198  
199 4 ▶sub _bundle_ref {
Subroutine _bundle_ref: called CC=4 SCAR=13.9
200 4 my ($self, $state, $source, $target) = @_;
201  
202 # Recurse into the inner $ref to avoid invalid schema
203 4 my $source_state = $self->store->resolve($source->{'$ref'}, $state);
204 4 ▶ return $self->_bundle_ref($source_state, $source_state->{schema}, $target) if $source_state->{schema}{'$ref'};
Branch: 1 branch
BranchTrueFalseTotal
if $source_state->{"schema"}{"\$ref"} 1 3 4
205  
206 # Replace "paths" inline
207 3 ▶ if (@{$state->{schema_path}} == 2 and $state->{schema_path}[0] eq 'paths') {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$state->{"schema_path"};} == 2 and $state->{"schema_path"}[0] eq "paths") 1 2 3
Condition66%2/3
@{$state->{"schema_path"};} == 2 and $state->{"schema_path"}[0] eq "paths"
!ll&&!rl&&r
201
MC/DC50%1/2
@{$state->{"schema_path"};} == 2 and $state->{"schema_path"}[0] eq "paths"
@{$state->{"schema_path"};} == 2 $state->{"schema_path"}[0] eq "paths"
Truth table66%2/3
A and B
A= @{$state->{"schema_path"};} == 2B= $state->{"schema_path"}[0] eq "paths"
ABresult
0X0
100
111
208 1 delete $state->{seen_schema}{$target}; # path definitions should not recurse
209 1 return [$source_state, $source_state->{schema}, $target, $state->{schema_path}];
210 }
211  
212 2 return $self->SUPER::_bundle_ref($state, $source, $target);
213 }
214  
215 2 ▶sub _bundle_ref_path_expand {
Subroutine _bundle_ref_path_expand: called CC=5 SCAR=17.5
216 2 my ($self, $state, $ref) = @_;
217 2 ▶ return (parameters => $ref) if $state->{schema}{in};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"schema"}{"in"} 1 1 2
218 1 ▶ return ($1 => $2) if $ref =~ m!\b(definitions|parameters|responses)\b\.*(?:json|yaml)?(.+)$!;
Branch: 1 branch
BranchTrueFalseTotal
if $ref =~ /\b(definitions|parameters|responses)\b\.*(?:json|yaml)?(.+)$/u 0 1 1
219 1 ▶ return ($1, $2 ? ($2) : ()) if $ref =~ m!\b(x-[^/]+)/?\b(.*)!;
Branch: 2 branches
BranchTrueFalseTotal
if $ref =~ m[\b(x-[^/]+)/?\b(.*)]u 1 0 1
$2 ? : 1 0 1
220 0 return (definitions => $ref);
221 }
222  
223 219 ▶sub _coerce_arrays {
Subroutine _coerce_arrays: called CC=8 SCAR=20.8
224 219 my ($self, $val, $param) = @_;
225 219 my $data_type = data_type $val->{value};
226 219 ▶ my $schema = $param->{schema} || $param; # $param->{schema} is for OpenAPIv3
Condition66%2/3
$param->{'schema'} || $param
l!l&&r!l&&!r
541650
MC/DC0%0/2
$param->{'schema'} || $param
$param->{'schema'} $param
Truth table66%2/3
A || B
A= $param->{'schema'}B= $param
ABresult
1X1
011
000
227 219 ▶ $schema = $self->get($schema->{'$ref'} =~ s!^#!!r) if $schema->{'$ref'};
Branch: 1 branch
BranchTrueFalseTotal
if $schema->{"\$ref"} 1 218 219
228 219 my $schema_type = schema_type $schema;
229 219 ▶ return $val->{value} = [$val->{value}] if $schema_type eq 'array' and $data_type ne 'array';
Branch: 1 branch
BranchTrueFalseTotal
if $schema_type eq "array" and $data_type ne "array" 1 218 219
Condition100%3/3
$schema_type eq "array" and $data_type ne "array"
!ll&&!rl&&r
731451
MC/DC100%2/2
$schema_type eq "array" and $data_type ne "array"
$schema_type eq "array" $data_type ne "array"
Truth table100%3/3
A and B
A= $schema_type eq "array"B= $data_type ne "array"
ABresult
0X0
100
111
230 218 ▶ return $val->{value} = @{$val->{value}} ? $val->{value}[-1] : undef
Branch: 2 branches
BranchTrueFalseTotal
if $schema_type ne "array" and $data_type eq "array" 14 204 218
@{$val->{'value'};} ? : 14 0 14
Condition100%3/3
$schema_type ne "array" and $data_type eq "array"
!ll&&!rl&&r
1455914
MC/DC100%2/2
$schema_type ne "array" and $data_type eq "array"
$schema_type ne "array" $data_type eq "array"
Truth table100%3/3
A and B
A= $schema_type ne "array"B= $data_type eq "array"
ABresult
0X0
100
111
231 if $schema_type ne 'array' and $data_type eq 'array';
232 }
233  
234 319 ▶sub _coerce_default_value {
Subroutine _coerce_default_value: called CC=4 SCAR=13.9
235 319 my ($self, $val, $param) = @_;
236  
237 319 ▶ if ($param->{schema} and exists $param->{schema}{default}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($param->{'schema'} and exists $param->{'schema'}{'default'}) { } 2 317 319
elsif (exists $param->{'default'}) { } 82 235 317
Condition100%3/3
$param->{"schema"} and exists $param->{"schema"}{"default"}
!ll&&!rl&&r
252652
MC/DC100%2/2
$param->{"schema"} and exists $param->{"schema"}{"default"}
$param->{"schema"} exists $param->{"schema"}{"default"}
Truth table100%3/3
A and B
A= $param->{"schema"}B= exists $param->{"schema"}{"default"}
ABresult
0X0
100
111
238 2 @$val{qw(exists value)} = (1, $param->{schema}{default});
239 }
240 elsif (exists $param->{default}) {
241 82 @$val{qw(exists value)} = (1, $param->{default});
242 }
243 }
244  
245 304 ▶sub _coerce_parameter_format {
Subroutine _coerce_parameter_format: called CC=12 SCAR=24.9
246 304 my ($self, $val, $param) = @_;
247 304 ▶ return unless $val->{exists};
Branch: 1 branch
BranchTrueFalseTotal
unless $val->{"exists"} 232 72 304
248 72 ▶ return unless my $format = $param->{collectionFormat} || ($param->{type} eq 'array' ? 'csv' : '');
Branch: 2 branches
BranchTrueFalseTotal
unless my $format = $param->{"collectionFormat"} || ($param->{"type"} eq "array" ? "csv" : "") 32 40 72
$param->{'type'} eq 'array' ? : 7 32 39
Condition100%3/3
$param->{'collectionFormat'} || ($param->{'type'} eq 'array' ? 'csv' : '')
l!l&&r!l&&!r
33732
MC/DC100%2/2
$param->{'collectionFormat'} || ($param->{'type'} eq 'array' ? 'csv' : '')
$param->{'collectionFormat'} ($param->{'type'} eq 'array' ? 'csv' : '')
Truth table100%3/3
A || B
A= $param->{'collectionFormat'}B= ($param->{'type'} eq 'array' ? 'csv' : '')
ABresult
1X1
011
000
249 40 ▶ return $val->{value} = ref $val->{value} eq 'ARRAY' ? $val->{value} : [$val->{value}] if $format eq 'multi';
Branch: 2 branches
BranchTrueFalseTotal
if $format eq "multi" 16 24 40
ref $val->{'value'} eq 'ARRAY' ? : 0 16 16
250  
251 24 ▶ $val->{value} = $val->{value}[0] // '' if ref $val->{value} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $val->{"value"} eq "ARRAY" 2 22 24
Condition50%1/2
$val->{"value"}[0] // ""
l!l
20
MC/DC0%0/1
$val->{"value"}[0] // ""
$val->{"value"}[0]
Truth table50%1/2
A
A= $val->{"value"}[0]
Aresult
11
00
252 24 ▶ return $val->{value} = [split /\|/, $val->{value}] if $format eq 'pipes';
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "pipes" 4 20 24
253 20 ▶ return $val->{value} = [split /[ ]/, $val->{value}] if $format eq 'ssv';
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "ssv" 4 16 20
254 16 ▶ return $val->{value} = [split /\t/, $val->{value}] if $format eq 'tsv';
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "tsv" 4 12 16
255 12 return $val->{value} = [split /,/, $val->{value}];
256 }
257  
258 3 ▶sub _default_response_schema {
Subroutine _default_response_schema: called CC=1 SCAR=0.0
259 return {
260 3 type => 'object',
261 required => ['errors'],
262 properties => {
263 errors => {
264 type => 'array',
265 items => {
266 type => 'object',
267 required => ['message'],
268 properties => {message => {type => 'string'}, path => {type => 'string'}},
269 },
270 },
271 },
272 };
273 }
274  
275 1199 ▶sub _extract_ref_from_schema { $_[1]->{'$ref'} =~ /^\w+$/ ? "#/definitions/$_[1]->{'$ref'}" : $_[1]->{'$ref'} }
Subroutine _extract_ref_from_schema: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1]{'$ref'} =~ /^\w+$/u ? : 1 1198 1199
276  
277 52 ▶sub _find_all_nodes {
Subroutine _find_all_nodes: called CC=11 SCAR=27.1
278 52 my ($self, $pointer, $leaf) = @_;
279 52 my @found;
280 52 ▶ push @found, $self->data->{$leaf} if ref $self->data->{$leaf} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->data->{$leaf} eq "ARRAY" 7 45 52
281  
282 52 my @path;
283 52 for my $p (@$pointer) {
284 156 push @path, $p;
285 156 my $node = $self->get([@path]);
286 156 ▶ push @found, $node->{$leaf} if ref $node->{$leaf} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $node->{$leaf} eq "ARRAY" 28 128 156
287 }
288  
289 # Resolve all $ref before returning the data
290 52 for my $items (@found) {
291 35 for my $schema (@$items) {
292 62 my %seen;
293 62 ▶ while (ref $schema eq 'HASH' and $schema->{'$ref'} and !ref $schema->{'$ref'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $schema eq "HASH" and $schema->{"\$ref"} and not ref $schema->{"\$ref"}) 0 62 62
Condition50%3/6
ref $schema eq "HASH" and $schema->{"\$ref"} and not ref $schema->{"\$ref"}
!ll&&!rl&&r
6200
ref $schema eq "HASH" and $schema->{"\$ref"}
!ll&&!rl&&r
7550
MC/DC0%0/3
ref $schema eq "HASH" and $schema->{"\$ref"} and not ref $schema->{"\$ref"}
ref $schema eq "HASH" $schema->{"\$ref"} not ref $schema->{"\$ref"}
Truth table50%2/4
A and B and not(C)
A= ref $schema eq "HASH"B= $schema->{"\$ref"}C= not ref $schema->{"\$ref"}
ABCresult
0XX0
10X0
1100
1111
294 0 ▶ last if $seen{$schema}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$schema}++ 0 0 0
295 $schema = $self->_refs->{$schema}{schema}
296 0 ▶ // Carp::confess(qq(resolve() must be called before validate() to lookup "$schema->{'$ref'}".));
Condition0%0/2
$self->_refs->{$schema}{"schema"} // Carp::confess(qq[resolve() must be called before validate() to lookup "$schema->{"\$ref"}".])
l!l
00
MC/DC0%0/1
$self->_refs->{$schema}{"schema"} // Carp::confess(qq[resolve() must be called before validate() to lookup "$schema->{"\$ref"}".])
$self->_refs->{$schema}{"schema"}
Truth table0%0/2
A
A= $self->_refs->{$schema}{"schema"}
Aresult
11
00
297 }
298 }
299 }
300  
301 52 return @found;
302 }
303  
304 344 ▶sub _get_parameter_value {
Subroutine _get_parameter_value: called CC=1 SCAR=0.0
305 344 my ($self, $param, $get) = @_;
306 344 my $val = $get->{$param->{in}}->($param->{name}, $param);
307 344 @$val{qw(in name)} = (@$param{qw(in name)});
308 344 return $val;
309 }
310  
311 3 ▶sub _params_for_add_default_response {
Subroutine _params_for_add_default_response: called CC=6 SCAR=21.8
312 3 my $self = shift;
313 3 ▶ my $params = shift || {};
Condition50%1/2
shift() || {}
l!l
03
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
314  
315 return {
316 description => $params->{description} || 'Default response.',
317 name => $params->{name} || 'DefaultResponse',
318 schema => $params->{schema} || _default_response_schema(),
319 3 ▶ status => $params->{status} || [400, 401, 404, 500, 501],
Condition44%4/9
$params->{'description'} || 'Default response.'
l!l
03
$params->{'name'} || 'DefaultResponse'
l!l
03
$params->{'schema'} || _default_response_schema()
l!l&&r!l&&!r
030
$params->{'status'} || [400, 401, 404, 500, 501]
l!l
03
MC/DC0%0/1
$params->{'description'} || 'Default response.'
$params->{'description'}
MC/DC0%0/1
$params->{'name'} || 'DefaultResponse'
$params->{'name'}
MC/DC0%0/2
$params->{'schema'} || _default_response_schema()
$params->{'schema'} _default_response_schema()
MC/DC0%0/1
$params->{'status'} || [400, 401, 404, 500, 501]
$params->{'status'}
Truth table50%1/2
A
A= $params->{'description'}
Aresult
11
00
Truth table50%1/2
A
A= $params->{'name'}
Aresult
11
00
Truth table33%1/3
A || B
A= $params->{'schema'}B= _default_response_schema()
ABresult
1X1
011
000
Truth table50%1/2
A
A= $params->{'status'}
Aresult
11
00
320 };
321 }
322  
323 45 ▶sub _prefix_error_path {
Subroutine _prefix_error_path: called CC=2 SCAR=6.9
324 45 ▶ return join '', "/$_[0]", $_[1] =~ /\w/ ? ($_[1]) : ();
Branch: 1 branch
BranchTrueFalseTotal
$_[1] =~ /\w/u ? : 27 18 45
325 }
326  
327 24 ▶sub _validate_body {
Subroutine _validate_body: called CC=13 SCAR=25.7
328 24 my ($self, $direction, $val, $param) = @_;
329  
330 24 ▶ if ($val->{accept}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val->{"accept"}) 2 22 24
331 2 $val->{content_type} = negotiate_content_type($param->{accepts}, $val->{accept});
332 2 ▶ $val->{valid} = $val->{content_type} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$val->{'content_type'} ? : 1 1 2
333 2 ▶ return E "/header/Accept", [join(', ', @{$param->{accepts}}), type => $val->{accept}] unless $val->{valid};
Branch: 1 branch
BranchTrueFalseTotal
unless $val->{"valid"} 1 1 2
334 }
335 23 ▶ if (@{$param->{accepts}} and $val->{content_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$param->{"accepts"};} and $val->{"content_type"}) 3 20 23
Condition100%3/3
@{$param->{"accepts"};} and $val->{"content_type"}
!ll&&!rl&&r
1643
MC/DC100%2/2
@{$param->{"accepts"};} and $val->{"content_type"}
@{$param->{"accepts"};} $val->{"content_type"}
Truth table100%3/3
A and B
A= @{$param->{"accepts"};}B= $val->{"content_type"}
ABresult
0X0
100
111
336 3 my $negotiated = negotiate_content_type($param->{accepts}, $val->{content_type});
337 3 ▶ $val->{valid} = $negotiated ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$negotiated ? : 2 1 3
338 3 ▶ return E "/$param->{name}", [join(', ', @{$param->{accepts}}) => type => $val->{content_type}] unless $negotiated;
Branch: 1 branch
BranchTrueFalseTotal
unless $negotiated 1 2 3
339 }
340 22 ▶ if ($param->{required} and !$val->{exists}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{"required"} and not $val->{"exists"}) 1 21 22
Condition100%3/3
$param->{"required"} and not $val->{"exists"}
!ll&&!rl&&r
1921
MC/DC100%2/2
$param->{"required"} and not $val->{"exists"}
$param->{"required"} not $val->{"exists"}
Truth table100%3/3
A and not(B)
A= $param->{"required"}B= not $val->{"exists"}
ABresult
0X0
100
111
341 1 $val->{valid} = 0;
342 1 return E "/$param->{name}", [qw(object required)];
343 }
344 21 ▶ if ($val->{exists}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val->{"exists"}) 19 2 21
345 19 local $self->{"validate_$direction"} = 1;
346 19 ▶ $val->{content_type} //= $param->{accepts}[0];
Condition66%2/3
$val->{"content_type"} //= $param->{"accepts"}[0]
l!l&&r!l&&!r
0316
MC/DC50%1/2
$val->{"content_type"} //= $param->{"accepts"}[0]
$val->{"content_type"} $param->{"accepts"}[0]
Truth table66%2/3
A //= B
A= $val->{"content_type"}B= $param->{"accepts"}[0]
ABresult
1X1
011
000
347 8 my @errors = map { $_->path(_prefix_error_path($param->{name}, $_->path)); $_ }
348 19 $self->validate($val->{value}, $param->{schema});
349 19 ▶ $val->{valid} = @errors ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
@errors ? : 8 11 19
350 19 return @errors;
351 }
352  
353 2 return;
354 }
355  
356 0 ▶sub _validate_id { }
Subroutine _validate_id: not called CC=1 SCAR=0.0
357  
358 172 ▶sub _validate_request_or_response {
Subroutine _validate_request_or_response: called CC=10 SCAR=23.0
359 172 my ($self, $direction, $parameters, $get) = @_;
360  
361 172 my @errors;
362 172 for my $param (@$parameters) {
363 526 my $val = $self->_get_parameter_value($param, $get);
364 526 ▶ $self->_coerce_parameter_format($val, $param) if $direction eq 'request' and $param->{in} ne 'body';
Branch: 1 branch
BranchTrueFalseTotal
if $direction eq "request" and $param->{"in"} ne "body" 414 112 526
Condition100%3/3
$direction eq "request" and $param->{"in"} ne "body"
!ll&&!rl&&r
5359414
MC/DC100%2/2
$direction eq "request" and $param->{"in"} ne "body"
$direction eq "request" $param->{"in"} ne "body"
Truth table100%3/3
A and B
A= $direction eq "request"B= $param->{"in"} ne "body"
ABresult
0X0
100
111
365 526 ▶ $self->_coerce_default_value($val, $param) unless $val->{exists};
Branch: 1 branch
BranchTrueFalseTotal
unless $val->{"exists"} 319 207 526
366  
367 526 ▶ if ($param->{in} eq 'body') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{"in"} eq "body") 92 434 526
368 92 push @errors, $self->_validate_body($direction, $val, $param);
369 92 next;
370 }
371  
372 434 ▶ if ($val->{exists}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($val->{'exists'}) { } 219 215 434
elsif ($param->{'required'}) { } 2 213 215
373 219 $self->_coerce_arrays($val, $param);
374 219 local $self->{"validate_$direction"} = 1;
375 15 ▶ my @e = map { $_->path(_prefix_error_path($param->{name}, $_->path)); $_ }
Condition66%2/3
$param->{'schema'} || $param
l!l&&r!l&&!r
541650
MC/DC0%0/2
$param->{'schema'} || $param
$param->{'schema'} $param
Truth table66%2/3
A || B
A= $param->{'schema'}B= $param
ABresult
1X1
011
000
376 219 $self->validate($val->{value}, $param->{schema} || $param);
377 219 push @errors, @e;
378 219 ▶ $val->{valid} = @e ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
@e ? : 15 204 219
379 }
380 elsif ($param->{required}) {
381 2 push @errors, E "/$param->{name}", [qw(object required)];
382 2 $val->{valid} = 0;
383 }
384 }
385  
386 172 return @errors;
387 }
388  
389 3 ▶sub _validate_type_file {
Subroutine _validate_type_file: called CC=4 SCAR=14.1
390 3 my ($self, $data, $state) = @_;
391 3 ▶ return unless $state->{schema}{required} and (not defined $data or not length $data);
Branch: 1 branch
BranchTrueFalseTotal
unless $state->{"schema"}{"required"} and not defined $data && length $data 1 2 3
Condition66%4/6
$state->{"schema"}{"required"} and not defined $data && length $data
!ll&&!rl&&r
012
defined $data && length $data
!ll&&!rl&&r
021
MC/DC33%1/3
$state->{"schema"}{"required"} and not defined $data && length $data
$state->{"schema"}{"required"} defined $data length $data
Truth table50%2/4
A and not(B && C)
A= $state->{"schema"}{"required"}B= defined $dataC= length $data
ABCresult
0XX0
1110
10X1
1101
392 2 return E $state->{path}, 'Missing property.';
393 }
394  
395 274 ▶sub _validate_type_object {
Subroutine _validate_type_object: called CC=12 SCAR=24.9
396 274 my ($self, $data, $state) = @_;
397 274 my ($path, $schema) = @$state{qw(path schema)};
398 274 ▶ return E $path, [object => type => data_type $data] if ref $data ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $data ne "HASH" 0 274 274
399 274 ▶ return shift->SUPER::_validate_type_object(@_) unless $self->{validate_request};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"validate_request"} 256 18 274
400  
401 18 my (@errors, %ro);
402 18 ▶ for my $name (keys %{$schema->{properties} || {}}) {
Condition50%1/2
$schema->{'properties'} || {}
l!l
180
MC/DC0%0/1
$schema->{'properties'} || {}
$schema->{'properties'}
Truth table50%1/2
A
A= $schema->{'properties'}
Aresult
11
00
403 32 ▶ next unless $self->_get(['readOnly'], {path => [], schema => $schema->{properties}{$name}});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_get(["readOnly"], {"path", [], "schema", $schema->{"properties"}{$name}}) 30 2 32
404 2 ▶ push @errors, E [@$path, $name], "Read-only." if exists $data->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{$name} 1 1 2
405 2 $ro{$name} = 1;
406 }
407  
408 18 ▶ local $schema->{required} = [grep { !$ro{$_} } @{$schema->{required} || []}];
Condition50%1/2
$schema->{'required'} || []
l!l
180
MC/DC0%0/1
$schema->{'required'} || []
$schema->{'required'}
Truth table50%1/2
A
A= $schema->{'required'}
Aresult
11
00
409  
410 18 my $discriminator = $schema->{discriminator};
411 18 ▶ if ($discriminator and !$self->{inside_discriminator}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($discriminator and not $self->{"inside_discriminator"}) 6 12 18
Condition100%3/3
$discriminator and not $self->{"inside_discriminator"}
!ll&&!rl&&r
846
MC/DC100%2/2
$discriminator and not $self->{"inside_discriminator"}
$discriminator not $self->{"inside_discriminator"}
Truth table100%3/3
A and not(B)
A= $discriminatorB= not $self->{"inside_discriminator"}
ABresult
0X0
100
111
412 6 ▶ return E $path, "Discriminator $discriminator has no value." unless my $name = $data->{$discriminator};
Branch: 1 branch
BranchTrueFalseTotal
unless my $name = $data->{$discriminator} 1 5 6
413 5 ▶ return E $path, "No definition for discriminator $name." unless my $ds = $self->get("/definitions/$name");
Branch: 1 branch
BranchTrueFalseTotal
unless my $ds = $self->get("/definitions/$name") 1 4 5
414 4 local $self->{inside_discriminator} = 1;
415 4 return $self->_validate($data, $self->_state($state, schema => $ds));
416 }
417  
418 return (
419 12 @errors,
420 $self->_validate_type_object_min_max($_[1], $state),
421 $self->_validate_type_object_dependencies($_[1], $state),
422 $self->_validate_type_object_properties($_[1], $state),
423 );
424 }
425  
426 20 1;
427  
428 =encoding utf8
429  
430 =head1 NAME
431  
432 JSON::Validator::Schema::OpenAPIv2 - OpenAPI version 2 / Swagger
433  
434 =head1 SYNOPSIS
435  
436 use JSON::Validator;
437 my $schema = JSON::Validator->new->schema("...")->schema;
438  
439 # Check for specification errors
440 my $errors = $schema->errors;
441  
442 # Returns a list of zero or more JSON::Validator::Error objects
443 my @request_errors = $schema->validate_request(
444 [get => "/path"],
445 {body => sub { return {exists => 1, value => {}} }},
446 );
447  
448 # Returns a list of zero or more JSON::Validator::Error objects
449 my @response_errors = $schema->validate_response(
450 [get => "/path", 200],
451 {body => sub { return {exists => 1, value => {}} }},
452 );
453  
454 =head1 DESCRIPTION
455  
456 This class represents L<http://swagger.io/v2/schema.json>.
457  
458 =head1 ATTRIBUTES
459  
460 =head2 errors
461  
462 my $array_ref = $schema->errors;
463  
464 See L<JSON::Validator::Schema/errors>.
465  
466 =head2 moniker
467  
468 $str = $schema->moniker;
469 $schema = $schema->moniker("openapiv2");
470  
471 Used to get/set the moniker for the given schema. Default value is "openapiv2".
472  
473 =head2 specification
474  
475 my $str = $schema->specification;
476 my $schema = $schema->specification($str);
477  
478 Defaults to "L<http://swagger.io/v2/schema.json>".
479  
480 =head1 METHODS
481  
482 =head2 add_default_response
483  
484 $schema = $schema->add_default_response(\%params);
485  
486 Used to add a default response schema for operations that does not already have
487 one. C<%params> can be:
488  
489 =over 2
490  
491 =item * description
492  
493 The human readable description added to the operation.
494  
495 Defaults: "Default response."
496  
497 =item * name
498  
499 The name used in the specification under "/components/schemas/".
500  
501 Defaults: "DefaultResponse"
502  
503 =item * schema
504  
505 The schema to add. The default schema below might change, but the basics will
506 stay the same:
507  
508 {
509 type: "object",
510 required: ["errors"],
511 properties: {
512 errors: {
513 type: "array",
514 items: {
515 type: "object",
516 required: ["message"],
517 properties: {
518 message: {type: "string"},
519 path: {type: "string"}
520 }
521 }
522 }
523 }
524 }
525  
526 =item * status
527  
528 A list of status codes to apply the default schema to.
529  
530 Default: C<[400, 401, 404, 500, 501]>.
531  
532 =back
533  
534  
535 =head2 base_url
536  
537 $url = $schema->base_url;
538 $schema = $schema->base_url($url);
539  
540 Can get or set the default URL for this schema. C<$url> can be either a
541 L<Mojo::URL> object or a plain string.
542  
543 This method will read or write "basePath", "host" and/or "schemas" in L</data>.
544  
545 =head2 coerce
546  
547 my $schema = $schema->coerce({booleans => 1, numbers => 1, strings => 1});
548 my $hash_ref = $schema->coerce;
549  
550 Coercion is enabled by default, since headers, path parts, query parameters,
551 ... are in most cases strings.
552  
553 See also L<JSON::Validator/coerce>.
554  
555 =head2 new
556  
557 $schema = JSON::Validator::Schema::OpenAPIv2->new(\%attrs);
558 $schema = JSON::Validator::Schema::OpenAPIv2->new;
559  
560 Same as L<JSON::Validator::Schema/new>, but will also build L/coerce>.
561  
562 =head2 parameters_for_request
563  
564 $parameters = $schema->parameters_for_request([$method, $path]);
565  
566 Finds all the request parameters defined in the schema, including inherited
567 parameters. Returns C<undef> if the C<$path> and C<$method> cannot be found.
568  
569 Example return value:
570  
571 [
572 {in => "query", name => "q"},
573 {in => "body", name => "body", accepts => ["application/json"]},
574 ]
575  
576 The return value MUST not be mutated.
577  
578 =head2 parameters_for_response
579  
580 $array_ref = $schema->parameters_for_response([$method, $path, $status]);
581  
582 Finds the response parameters defined in the schema. Returns C<undef> if the
583 C<$path>, C<$method> and C<$status> cannot be found. Will default to the
584 "default" response definition if C<$status> could not be found and "default"
585 exists.
586  
587 Example return value:
588  
589 [
590 {in => "header", name => "X-Foo"},
591 {in => "body", name => "body", accepts => ["application/json"]},
592 ]
593  
594 The return value MUST not be mutated.
595  
596 =head2 routes
597  
598 $collection = $schema->routes;
599  
600 Used to gather all available routes in the schema and return them sorted. The
601 result is a L<Mojo::Collection> object, where each item has a hash looking like
602 this:
603  
604 {
605 method => 'get',
606 path => '/user/{id}',
607 operation_id => 'getUser', # Might be undef()
608 }
609  
610 =head2 validate_request
611  
612 @errors = $schema->validate_request([$method, $path], \%req);
613  
614 This method can be used to validate a HTTP request. C<%req> should contain
615 key/value pairs representing the request parameters. Example:
616  
617 %req = (
618 body => sub {
619 my ($name, $param) = shift;
620 # $param = {name => $name, in => ..., schema => ..., ...}
621 return {exists => 1, value => \%all_params} unless defined $name;
622 return {exists => 1, value => "..."};
623 },
624 formData => {email => "..."},
625 header => {"X-Request-Base" => "..."},
626 path => {id => "..."},
627 query => {limit => 42},
628 );
629  
630 "formData", "header", "path" and "query" can be either a hash-ref, a hash-like
631 object or a code ref, while "body" MUST be a code ref. The return value from
632 the code ref will get mutated, making it possible to check if an individual
633 parameter was validated or not.
634  
635 # Before: "exists" and "value" must be present
636 my @evaluated;
637 $req{query} = sub { push @evaluated, {exists => 1, value => 42}, return $evaluated[-1] };
638  
639 # Validate
640 $schema->validate_request(get => "/user"], \%req);
641  
642 # After: "in", "name" and "valid" are added
643 $evaluated[-1] ==> {exists => 1, value => 42, in => "query", name => "foo", valid => 1};
644  
645 A plain hash-ref will I</not> get mutated.
646  
647 The body hash-ref can also have a "content_type" key. This will be checked
648 against the list of valid request or response content types in the spec.
649  
650 =head2 validate_response
651  
652 @errors = $schema->validate_response([$method, $path, $status], \%res);
653  
654 This method can be used to validate a HTTP response. C<%res> should contain
655 key/value pairs representing the response parameters. Example:
656  
657 %res = (
658 body => sub {
659 my ($name, $param) = shift;
660 # $param = {name => $name, in => ..., ...}
661 return {exists => 1, value => \%all_params} unless defined $name;
662 return {accept => "application/json", exists => 1, value => "..."};
663 },
664 header => {"Location" => "..."},
665 );
666  
667 C<%res> follows the same rules as C<%req> in L</validate_request>, but also
668 supports "accept", instead of specifying "content_type". "accept" should have
669 the same format as an "Accept" HTTP header.
670  
671 =head1 SEE ALSO
672  
673 L<JSON::Validator>, L<Mojolicious::Plugin::OpenAPI>,
674 L<http://openapi-specification-visual-documentation.apihandyman.io/>
675  
676 =cut
« Validator/Schema/Draft201909.pm ↑ Summary Validator/URI.pm »