Perinci::Sub::ValidateArgs - Validate function arguments using schemas in Rinci function metadata
This document describes version 0.013 of Perinci::Sub::ValidateArgs (from Perl distribution Perinci-Sub-ValidateArgs), released on 2020-03-10.
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); ... }
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.
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.
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.
%SPEC
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.
eval()
Return value: (any)
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.
gen_args_validator_from_meta
Note: currently cannot handle args_as => 'array', only args_as => 'arrayref.
args_as => 'array'
args_as => 'arrayref
args* => hash|array
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.
Please visit the project's homepage at https://metacpan.org/release/Perinci-Sub-ValidateArgs.
Source repository is at https://github.com/perlancar/perl-Perinci-Sub-ValidateArgs.
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.
Rinci, Data::Sah
Dist::Zilla::Plugin::IfBuilt
Dist::Zilla::Plugin::Rinci::Validate
perlancar <perlancar@cpan.org>
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.
To install Perinci::Sub::ValidateArgs, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Perinci::Sub::ValidateArgs
CPAN shell
perl -MCPAN -e shell install Perinci::Sub::ValidateArgs
For more information on module installation, please visit the detailed CPAN module installation guide.