++ed by:
KARJALA DBOEHMER LEEJO AZJADFTRE TYLDUM

18 PAUSE users
15 non-PAUSE users.

Jan Henning Thorsen
and 3 contributors

NAME

JSON::Validator::OpenAPI::Mojolicious - JSON::Validator request/response adapter for Mojolicious

SYNOPSIS

  my $validator = JSON::Validator::OpenAPI::Mojolicious->new;
  $validator->load_and_validate_schema("myschema.json");

  my @errors = $validator->validate_request(
                 $c,
                 $validator->get([paths => "/wharever", "get"]),
                 $c->validation->output,
               );

  @errors = $validator->validate_response(
              $c,
              $validator->get([paths => "/wharever", "get"]),
              200,
              {some => {response => "data"}},
            );

DESCRIPTION

JSON::Validator::OpenAPI::Mojolicious is a module for validating request and response data from/to your Mojolicious application.

Do not use this module directly. Use Mojolicious::Plugin::OpenAPI instead.

STASH VARIABLES

openapi_negotiated_content_type

  $str = %c->stash("openapi_negotiated_content_type");

This value will be set when the Accept header has been validated successfully against an OpenAPI v3 schema. Note that this could have the value of "*/*" or other invalid "Content-Header" values. It will be undef if the "Accept" header is not accepteed.

Unfortunately, this variable is not set until you call "render" in Mojolicious::Controller, since we need a status code to figure out which types are accepted.

This means that if you want to validate the "Accept" header on input, then you have to specify that as a parameter in the spec.

ATTRIBUTES

JSON::Validator::OpenAPI::Mojolicious inherits all attributes from JSON::Validator.

formats

  $validator = $validator->formats({});
  $hash_ref = $validator->formats;

Open API support the same formats as JSON::Validator, but adds the following to the set:

  • byte

    A padded, base64-encoded string of bytes, encoded with a URL and filename safe alphabet. Defined by RFC4648.

  • date

    An RFC3339 date in the format YYYY-MM-DD

  • double

    Cannot test double values with higher precision then what the "number" type already provides.

  • float

    Will always be true if the input is a number, meaning there is no difference between "float" and "double". Patches are welcome.

  • int32

    A signed 32 bit integer.

  • int64

    A signed 64 bit integer. Note: This check is only available if Perl is compiled to use 64 bit integers.

version

  $str = $validator->version;

Used to get the OpenAPI Schema version to use. Will be set automatically when using "load_and_validate_schema", unless already set. Supported values are "2" an "3".

METHODS

JSON::Validator::OpenAPI::Mojolicious inherits all attributes from JSON::Validator.

load_and_validate_schema

  $validator = $validator->load_and_validate_schema($schema, \%args);

Will load and validate $schema against the OpenAPI specification. $schema can be anything "schema" in JSON::Validator accepts. The expanded specification will be stored in "schema" in JSON::Validator on success. See "schema" in JSON::Validator for the different version of $url that can be accepted.

%args can be used to further instruct the expansion and validation process:

  • allow_invalid_ref

    Setting this to a true value, will disable the first pass. This is useful if you don't like the restrictions set by OpenAPI, regarding where you can use $ref in your specification.

  • version_from_class

    Setting this to a module/class name will use the version number from the class and overwrite the version in the specification:

      {
        "info": {
          "version": "1.00" // <-- this value
        }
      }

The validation is done with a two pass process:

  1. First it will check if the $ref is only specified on the correct places. This can be disabled by setting "allow_invalid_ref" to a true value.

  2. Validate the expanded version of the spec, (without any $ref) against the OpenAPI schema.

generate_definitions_path

See "generate_definitions_path" in JSON::Validator.

validate_input

  @errors = $validator->validate_input($data, $schema);

This method will make sure "readOnly" is taken into account, when validating data sent to your API.

validate_request

  @errors = $validator->validate_request($c, $schema, \%input);

Takes an Mojolicious::Controller and a schema definition and returns a list of errors, if any. Validated input parameters are moved into the %input hash.

validate_response

  @errors = $validator->validate_response($c, $schema, $status, $data);

SEE ALSO

Mojolicious::Plugin::OpenAPI.

JSON::Validator.

http://openapi-specification-visual-documentation.apihandyman.io/