The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

JSONSchema::Validator::OAS30 - Validator for OpenAPI Specification 3.0

VERSION

version 0.011

SYNOPSIS

$validator = JSONSchema::Validator::OAS30->new(schema => {...});
my ($result, $errors, $warnings) = $validator->validate_request(
    method => 'GET',
    openapi_path => '/user/{id}/profile',
    parameters => {
        path => {
            id => 1234
        },
        query => {
            details => 'short'
        },
        header => {
            header => 'header value'
        },
        cookie => {
            name => 'value'
        },
        body => [$is_exists, $content_type, $data]
    }
);
my ($result, $errors, $warnings) = $validator->validate_response(
    method => 'GET',
    openapi_path => '/user/{id}/profile',
    status => '200',
    parameters => {
        header => {
            header => 'header value'
        },
        body => [$is_exists, $content_type, $data]
    }
);

DESCRIPTION

OpenAPI specification 3.0 validator with minimum dependencies.

CLASS METHODS

new

Creates JSONSchema::Validator::OAS30 object.

$validator = JSONSchema::Validator::OAS30->new(schema => {...});

Parameters

schema

Scheme according to which validation occurs.

strict

Use strong type checks. Default value is 0.

scheme_handlers

At the moment, the validator can load a resource using the http, https protocols. You can add other protocols yourself.

sub loader {
    my $uri = shift;
    ...
}
$validator = JSONSchema::Validator::Draft4->new(schema => {...}, scheme_handlers => {ftp => \&loader});

validate_deprecated

Validate method/parameter/schema with deprecated mark. Default value is 1.

METHODS

validate_request

Validate request specified by method and openapi_path.

Parameters

method

HTTP method of request.

openapi_path

OpenAPI path of request.

Need to specify OpenAPI path, not the real path of request.

parameters

Parameters of request. It is an object that contains the following keys: query, path, header, cookie and body. Keys query, path, header, cookie are hash objects which contains key/value pairs. Key body is a array reference which contains 3 values. The first value is a boolean flag that means if there is a body. The second value is a Content-Type of request. The third value is a data of value.

# post params
my ($result, $errors, $warnings) = $validator->validate_request(
    method => 'POST',
    parameters => {
        path => {user => 'adam'},
        body => [1, 'application/x-www-form-urlencoded', {key => 'value'}]
    }
);

# for file upload
my ($result, $errors, $warnings) = $validator->validate_request(
    method => 'POST',
    parameters => {
        body => [1, 'multipart/form-data', {key => 'value', file => 'binary data'}]
    }
);

# for multiple file upload for the same name
my ($result, $errors, $warnings) = $validator->validate_request(
    method => 'POST',
    parameters => {
        body => [1, 'multipart/form-data', {key => 'value', files => ['binary data1', 'binary data2']}]
    }
);

validate_response

Validate response specified by method, openapi_path and http status code.

Parameters

method

HTTP method of request.

openapi_path

OpenAPI path of request.

Need to specify OpenAPI path, not the real path of request.

status

HTTP response status code.

parameters

Parameters of response. It is an object that contains the following keys: header and body. Key header are hash objects which contains key/value pairs. Key body is a array reference which contains 3 values. The first value is a boolean flag that means if there is a body. The second value is a Content-Type of response. The third value is a data of value.

# to validate application/json response
my ($result, $errors, $warnings) = $validator->validate_response(
    method => 'GET',
    openapi_path => '/user/{id}',
    status => '404',
    parameters => {
        header => {name => 'value'},
        body => [1, 'application/json', {message => 'user not found'}]
    }
);

AUTHORS

  • Alexey Stavrov <logioniz@ya.ru>

  • Ivan Putintsev <uid@rydlab.ru>

  • Anton Fedotov <tosha.fedotov.2000@gmail.com>

  • Denis Ibaev <dionys@gmail.com>

  • Andrey Khozov <andrey@rydlab.ru>

COPYRIGHT AND LICENSE

This software is Copyright (c) 2021 by Alexey Stavrov.

This is free software, licensed under:

The MIT (X11) License