SYNOPSIS

use CGI;
use Data::Validate::OpenAPI;

my $validator = Data::Validate::OpenAPI->new( $parsed_openapi_json );
my $params = $validator->validate( '/', 'post', CGI->new );

DESCRIPTION

Data::Validate::OpenAPI validates and untaints CGI parameters using a supplied OpenAPI schema. It applies format-specific validation and untainting using appropriate Data::Validate subclasses, including email, IP, URI and other. Also it checks values against enumerators and patterns, if provided. At this point values without supported formats, enumerators or patterns are returned as they are, tainted. This behavior may change in the future.

Data::Validate::OpenAPI does not validate OpenAPI schemas. To do so, refer to JSON::Validator.

METHODS

new( $api )

Takes a parsed OpenAPI schema as returned by JSON module's decode_json(). Returns validator ready to validate CGI parameters.

validate( $path, $method, $cgi )

Takes a call path, HTTP method and a CGI object. Returns a hash of validated pairs of CGI parameter keys and their values. At this point values failing to validate are not reported. Keys for parameters having no valid values are omitted from the returned hash.

The interface for this method is bound to change, but backwards compatibility will be preserved on best effort basis.

VALIDATION ERROR REPORTING

By default validation errors are silent, but there are two ways to handle validation errors: by setting validator-specific subroutine or by setting module variable:

my $reporter_sub = sub { warn "value for '$_[0]' is incorrect" };

# Set a reporter for this particular validator instance:
$validator->reporter( $reporter_sub );

# Set a reporter for all instances of this class:
$Data::Validate::OpenAPI::reporter = $reporter_sub;

If any of them is set, reporter subroutine is called with the following parameters:

$reporter_sub->( $parameter_name, @bad_values );

Validator-specific reporter takes precedence. At this point the module does not indicate which particular check failed during the validation.

reporter( $reporter_sub ) method

Set reporter subroutine to be called for each parameter failing the validation:

$reporter_sub->( $parameter_name, @bad_values );

SEE ALSO

https://spec.openapis.org/oas/v3.0.2.html