.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "JSON::Validator::Schema::OpenAPIv2 3pm" .TH JSON::Validator::Schema::OpenAPIv2 3pm "2023-03-06" "perl v5.36.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" JSON::Validator::Schema::OpenAPIv2 \- OpenAPI version 2 / Swagger .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 2 \& use JSON::Validator; \& my $schema = JSON::Validator\->new\->schema("...")\->schema; \& \& # Check for specification errors \& my $errors = $schema\->errors; \& \& # Returns a list of zero or more JSON::Validator::Error objects \& my @request_errors = $schema\->validate_request( \& [get => "/path"], \& {body => sub { return {exists => 1, value => {}} }}, \& ); \& \& # Returns a list of zero or more JSON::Validator::Error objects \& my @response_errors = $schema\->validate_response( \& [get => "/path", 200], \& {body => sub { return {exists => 1, value => {}} }}, \& ); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This class represents . .SH "ATTRIBUTES" .IX Header "ATTRIBUTES" .SS "errors" .IX Subsection "errors" .Vb 1 \& my $array_ref = $schema\->errors; .Ve .PP See \*(L"errors\*(R" in JSON::Validator::Schema. .SS "moniker" .IX Subsection "moniker" .Vb 2 \& $str = $schema\->moniker; \& $schema = $schema\->moniker("openapiv2"); .Ve .PP Used to get/set the moniker for the given schema. Default value is \*(L"openapiv2\*(R". .SS "specification" .IX Subsection "specification" .Vb 2 \& my $str = $schema\->specification; \& my $schema = $schema\->specification($str); .Ve .PP Defaults to "". .SH "METHODS" .IX Header "METHODS" .SS "add_default_response" .IX Subsection "add_default_response" .Vb 1 \& $schema = $schema\->add_default_response(\e%params); .Ve .PP Used to add a default response schema for operations that does not already have one. \f(CW%params\fR can be: .IP "\(bu" 2 description .Sp The human readable description added to the operation. .Sp Defaults: \*(L"Default response.\*(R" .IP "\(bu" 2 name .Sp The name used in the specification under \*(L"/components/schemas/\*(R". .Sp Defaults: \*(L"DefaultResponse\*(R" .IP "\(bu" 2 schema .Sp The schema to add. The default schema below might change, but the basics will stay the same: .Sp .Vb 10 \& { \& type: "object", \& required: ["errors"], \& properties: { \& errors: { \& type: "array", \& items: { \& type: "object", \& required: ["message"], \& properties: { \& message: {type: "string"}, \& path: {type: "string"} \& } \& } \& } \& } \& } .Ve .IP "\(bu" 2 status .Sp A list of status codes to apply the default schema to. .Sp Default: \f(CW\*(C`[400, 401, 404, 500, 501]\*(C'\fR. .SS "base_url" .IX Subsection "base_url" .Vb 2 \& $url = $schema\->base_url; \& $schema = $schema\->base_url($url); .Ve .PP Can get or set the default \s-1URL\s0 for this schema. \f(CW$url\fR can be either a Mojo::URL object or a plain string. .PP This method will read or write \*(L"basePath\*(R", \*(L"host\*(R" and/or \*(L"schemas\*(R" in \*(L"data\*(R". .SS "coerce" .IX Subsection "coerce" .Vb 2 \& my $schema = $schema\->coerce({booleans => 1, numbers => 1, strings => 1}); \& my $hash_ref = $schema\->coerce; .Ve .PP Coercion is enabled by default, since headers, path parts, query parameters, \&... are in most cases strings. .PP See also \*(L"coerce\*(R" in JSON::Validator. .SS "new" .IX Subsection "new" .Vb 2 \& $schema = JSON::Validator::Schema::OpenAPIv2\->new(\e%attrs); \& $schema = JSON::Validator::Schema::OpenAPIv2\->new; .Ve .PP Same as \*(L"new\*(R" in JSON::Validator::Schema, but will also build L/coerce>. .SS "parameters_for_request" .IX Subsection "parameters_for_request" .Vb 1 \& $parameters = $schema\->parameters_for_request([$method, $path]); .Ve .PP Finds all the request parameters defined in the schema, including inherited parameters. Returns \f(CW\*(C`undef\*(C'\fR if the \f(CW$path\fR and \f(CW$method\fR cannot be found. .PP Example return value: .PP .Vb 4 \& [ \& {in => "query", name => "q"}, \& {in => "body", name => "body", accepts => ["application/json"]}, \& ] .Ve .PP The return value \s-1MUST\s0 not be mutated. .SS "parameters_for_response" .IX Subsection "parameters_for_response" .Vb 1 \& $array_ref = $schema\->parameters_for_response([$method, $path, $status]); .Ve .PP Finds the response parameters defined in the schema. Returns \f(CW\*(C`undef\*(C'\fR if the \&\f(CW$path\fR, \f(CW$method\fR and \f(CW$status\fR cannot be found. Will default to the \&\*(L"default\*(R" response definition if \f(CW$status\fR could not be found and \*(L"default\*(R" exists. .PP Example return value: .PP .Vb 4 \& [ \& {in => "header", name => "X\-Foo"}, \& {in => "body", name => "body", accepts => ["application/json"]}, \& ] .Ve .PP The return value \s-1MUST\s0 not be mutated. .SS "routes" .IX Subsection "routes" .Vb 1 \& $collection = $schema\->routes; .Ve .PP Used to gather all available routes in the schema and return them sorted. The result is a Mojo::Collection object, where each item has a hash looking like this: .PP .Vb 5 \& { \& method => \*(Aqget\*(Aq, \& path => \*(Aq/user/{id}\*(Aq, \& operation_id => \*(AqgetUser\*(Aq, # Might be undef() \& } .Ve .SS "validate_request" .IX Subsection "validate_request" .Vb 1 \& @errors = $schema\->validate_request([$method, $path], \e%req); .Ve .PP This method can be used to validate a \s-1HTTP\s0 request. \f(CW%req\fR should contain key/value pairs representing the request parameters. Example: .PP .Vb 12 \& %req = ( \& body => sub { \& my ($name, $param) = shift; \& # $param = {name => $name, in => ..., schema => ..., ...} \& return {exists => 1, value => \e%all_params} unless defined $name; \& return {exists => 1, value => "..."}; \& }, \& formData => {email => "..."}, \& header => {"X\-Request\-Base" => "..."}, \& path => {id => "..."}, \& query => {limit => 42}, \& ); .Ve .PP \&\*(L"formData\*(R", \*(L"header\*(R", \*(L"path\*(R" and \*(L"query\*(R" can be either a hash-ref, a hash-like object or a code ref, while \*(L"body\*(R" \s-1MUST\s0 be a code ref. The return value from the code ref will get mutated, making it possible to check if an individual parameter was validated or not. .PP .Vb 3 \& # Before: "exists" and "value" must be present \& my @evaluated; \& $req{query} = sub { push @evaluated, {exists => 1, value => 42}, return $evaluated[\-1] }; \& \& # Validate \& $schema\->validate_request(get => "/user"], \e%req); \& \& # After: "in", "name" and "valid" are added \& $evaluated[\-1] ==> {exists => 1, value => 42, in => "query", name => "foo", valid => 1}; .Ve .PP A plain hash-ref will \fI/not\fR get mutated. .PP The body hash-ref can also have a \*(L"content_type\*(R" key. This will be checked against the list of valid request or response content types in the spec. .SS "validate_response" .IX Subsection "validate_response" .Vb 1 \& @errors = $schema\->validate_response([$method, $path, $status], \e%res); .Ve .PP This method can be used to validate a \s-1HTTP\s0 response. \f(CW%res\fR should contain key/value pairs representing the response parameters. Example: .PP .Vb 9 \& %res = ( \& body => sub { \& my ($name, $param) = shift; \& # $param = {name => $name, in => ..., ...} \& return {exists => 1, value => \e%all_params} unless defined $name; \& return {accept => "application/json", exists => 1, value => "..."}; \& }, \& header => {"Location" => "..."}, \& ); .Ve .PP \&\f(CW%res\fR follows the same rules as \f(CW%req\fR in \*(L"validate_request\*(R", but also supports \*(L"accept\*(R", instead of specifying \*(L"content_type\*(R". \*(L"accept\*(R" should have the same format as an \*(L"Accept\*(R" \s-1HTTP\s0 header. .SH "SEE ALSO" .IX Header "SEE ALSO" JSON::Validator, Mojolicious::Plugin::OpenAPI,