NAME
Perinci::Sub::ValidateArgs - Validate function arguments using schemas in Rinci function metadata
VERSION
This document describes version 0.013 of Perinci::Sub::ValidateArgs (from Perl distribution Perinci-Sub-ValidateArgs), released on 2020-03-10.
SYNOPSIS
use Perinci::Sub::ValidateArgs qw(gen_args_validator_from_meta);
our %SPEC;
$SPEC{foo} = {
v => 1.1,
args => {
a1 => {
schema => 'int*',
req => 1,
},
a2 => {
schema => [array => of=>'int*'],
default => 'peach',
},
},
'x.func.validate_args' => 1,
};
sub foo {
state $validator = gen_args_validator_from_meta();
my %args = @_;
if (my $err = $validator->(\%args)) { return $err }
...
}
or, if you want the validator to die on failure:
...
sub foo {
state $validator = gen_args_validator_from_meta(die => 1);
my %args = @_;
$validator->(\%args);
...
}
DESCRIPTION
This module (PSV for short) can be used to validate function arguments using schema information in Rinci function metadata.
There are other ways if you want to validate function arguments using Sah schemas. See Data::Sah::Manual::ParamsValidating.
FUNCTIONS
gen_args_validator_from_meta
Usage:
gen_args_validator_from_meta(%args) -> any
Generate argument validator from Rinci function metadata.
If you don't intend to reuse the generated validator, you can also use validate_args_using_meta
.
This function is not exported by default, but exportable.
Arguments ('*' denotes required arguments):
die => bool
Whether validator should die or just return an error message/response.
meta => hash
If not specified, will be searched from caller's
%SPEC
package variable.source => bool
Whether we want to get the source code instead.
The default is to generate Perl validator code, compile it with
eval()
, and return the resulting coderef. When this option is set to true, the generated source string will be returned instead.
Return value: (any)
validate_args_using_meta
Usage:
validate_args_using_meta(%args) -> [status, msg, payload, meta]
Validate arguments using Rinci function metadata.
If you intend to reuse the generated validator, you can also use gen_args_validator_from_meta
.
Note: currently cannot handle args_as => 'array'
, only args_as => 'arrayref
.
This function is not exported by default, but exportable.
Arguments ('*' denotes required arguments):
args* => hash|array
die => bool
Whether validator should die or just return an error message/response.
meta* => hash
Returns an enveloped result (an array).
First element (status) is an integer containing HTTP status code (200 means OK, 4xx caller error, 5xx function error). Second element (msg) is a string containing error message, or 'OK' if status is 200. Third element (payload) is optional, the actual result. Fourth element (meta) is called result metadata and is optional, a hash that contains extra information.
Return value: (any)
HOMEPAGE
Please visit the project's homepage at https://metacpan.org/release/Perinci-Sub-ValidateArgs.
SOURCE
Source repository is at https://github.com/perlancar/perl-Perinci-Sub-ValidateArgs.
BUGS
Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Perinci-Sub-ValidateArgs
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.
SEE ALSO
Dist::Zilla::Plugin::Rinci::Validate
AUTHOR
perlancar <perlancar@cpan.org>
COPYRIGHT AND LICENSE
This software is copyright (c) 2020, 2016 by perlancar@cpan.org.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.