MooseX...lidate(3pm)
๐ MooseX::Params::Validate ๐
okay so bestie, welcome to a page from the User Contributed Perl Documentation (yes that's the manual name, very demure, very mindful). We're talking MooseX::Params::Validate, and this is version 0.21, and no cap it's about to make your Moose methods so much nicer.
โจ NAME โจ
MooseX::Params::Validate โ it's an extension of Params::Validate but it uses Moose's types instead. that's the whole vibe. ๐
๐ VERSION ๐
version 0.21 โ write it down, it's giving current. โญ
๐ฆ SYNOPSIS ๐ฆ
here's the moment you've been waiting for:
package Foo;
use Moose;
use MooseX::Params::Validate;
sub foo {
my ( $self, %params ) = validated_hash(
\@_,
bar => { isa => 'Str', default => 'Moose' },
);
return "Hooray for $params{bar}!";
}
sub bar {
my $self = shift;
my ( $foo, $baz, $gorch ) = validated_list(
\@_,
foo => { isa => 'Foo' },
baz => { isa => 'ArrayRef | HashRef', optional => 1 },
gorch => { isa => 'ArrayRef[Int]', optional => 1 }
);
[ $foo, $baz, $gorch ];
}
look at that. clean. immaculate. ๐ซถ
๐ธ DESCRIPTION ๐ธ
so this module literally fills a gap in Moose by adding method parameter validation to Moose. And like โ it's just one of many developing options, okay? It should not be considered the "official" one by any means though. it's humble like that. ๐
you might also want to explore MooseX::Method::Signatures and MooseX::Declare โ go on, they're cute too. โจ
๐ CAVEATS ๐
lowkey important: it is not possible to introspect the method parameter specs. they're created as needed when the method is called, and then cached for subsequent calls. that's just how it is. ๐ญ
๐ EXPORTS ๐
validated_hash( \@_, %parameter_spec )
this behaves similarly to the standard Params::Validate validate function, and it returns the captured values in a HASH. The one exception is that if it spots an instance in the @_, then it will handle it appropriately (unlike Params::Validate which forces you to shift your $self first โ so rude of it, honestly).
the values in @_ can either be a set of name-value pairs or a single hash reference.
the %parameter_spec accepts the following options:
isaโ theisaoption can be either; a class name, a Moose type constraint name, or an anon Moose type constraint. your pick, bestie. ๐doesโ thedoesoption can be either; a role name or an anon Moose type constraint.defaultโ this is the default value to be used if the value is not supplied. the fallback fit. ๐optionalโ as withParams::Validate, all options are considered required unless otherwise specified. This option is passed directly toParams::Validate.coerceโ if this is true and the parameter has a type constraint which has coercions, then the coercion will be called for this parameter. If the type does have coercions, then this parameter is ignored.dependsโ another parameter that this one depends on. See theParams::Validatedocumentation for more details. ๐
this function is also available under its old name, validate. (yes, the deprecated one. we'll get there. ๐ฐ๏ธ)
validated_list( \@_, %parameter_spec )
the %parameter_spec accepts the same options as above, but returns the parameters as positional values instead of a HASH. this is best explained by example:
sub foo {
my ( $self, $foo, $bar ) = validated_list(
\@_,
foo => { isa => 'Foo' },
bar => { isa => 'Bar' },
);
$foo->baz($bar);
}
we capture the order in which you defined the parameters and then return them as a list in the same order. If a param is marked optional and not included, then it will be set to undef. no drama. ๐
the values in @_ can either be a set of name-value pairs or a single hash reference.
like validated_hash, if it spots an object instance as the first parameter of @_, it will handle it appropriately, returning it as the first argument.
this function is also available under its old name, validatep.
pos_validated_list( \@_, $spec, $spec, ... )
this function validates a list of positional parameters. each $spec should validate one of the parameters in the list:
sub foo {
my $self = shift;
my ( $foo, $bar ) = pos_validated_list(
\@_,
{ isa => 'Foo' },
{ isa => 'Bar' },
);
...
}
unlike the other functions, this one cannot find $self in the argument list. Make sure to shift it off yourself before doing validation. do it yourself, queen. ๐
the values in @_ must be a list of values. You cannot pass the values as an array reference, because this cannot be distinguished from passing one value which is itself an array reference. (a genuine tragedy of ambiguity ๐ญ)
If a parameter is marked as optional and is not present, it will simply not be returned.
If you want to pass in any of the cache control parameters described below, simply pass them after the list of parameter validation specs:
sub foo {
my $self = shift;
my ( $foo, $bar ) = pos_validated_list(
\@_,
{ isa => 'Foo' },
{ isa => 'Bar' },
MX_PARAMS_VALIDATE_NO_CACHE => 1,
);
...
}
๐ฑ EXCEPTION FOR FAILED VALIDATION ๐ฑ
if a type constraint check for a parameter fails, then the error is thrown as a MooseX::Params::Validate::Exception::ValidationFailedForTypeConstraint object. When stringified, this object will use the error message generated by the type constraint that failed.
other errors are simply percolated up from Params::Validate as-is, and are not turned into exception objects. This may change in the future (or more likely, Params::Validate may start throwing objects of its own). foreshadowing? maybe. ๐ฎ
๐ ALLOWING EXTRA PARAMETERS ๐
by default, any parameters not mentioned in the parameter spec cause this module to throw an error. however, you can have this module simply ignore them by setting MX_PARAMS_VALIDATE_ALLOW_EXTRA to a true value when calling a validation subroutine. live a little. โจ
when calling validated_hash or pos_validated_list the extra parameters are simply returned in the hash or list as appropriate. However, when you call validated_list the extra parameters will not be returned at all. You can get them by looking at the original value of @_.
๐ EXPORTS (the sequel) ๐
by default, this module exports the validated_hash, validated_list, and pos_validated_list.
if you would prefer to import the now deprecated functions validate and validatep instead, you can use the :deprecated tag to import them. (blink twice if you're still using them ๐)
๐ฅ IMPORTANT NOTE ON CACHING ๐ฅ
when a validation subroutine is called the first time, the parameter spec is prepared and cached to avoid unnecessary regeneration. It uses the fully qualified name of the subroutine (package + subname) as the cache key. In 99.999% of the use cases for this module, that will be the right thing to do. slay. ๐
however, I have (ab)used this module occasionally to handle dynamic sets of parameters. In this special use case you can do a couple things to better control the caching behavior.
- passing in the
MX_PARAMS_VALIDATE_NO_CACHEflag in the parameter spec this will prevent the parameter spec from being cached.
sub foo {
my ( $self, %params ) = validated_hash(
\@_,
foo => { isa => 'Foo' },
MX_PARAMS_VALIDATE_NO_CACHE => 1,
);
}
- passing in
MX_PARAMS_VALIDATE_CACHE_KEYwith a value to be used as the cache key will bypass the normal cache key generation.
sub foo {
my ( $self, %params ) = validated_hash(
\@_,
foo => { isa => 'Foo' },
MX_PARAMS_VALIDATE_CACHE_KEY => 'foo-42',
);
}
two flags, two moods. pick your fighter. ๐ฅ
๐ง MAINTAINER ๐ง
Dave Rolsky <autarch@urth.org> โ the maintainer, the moment, the main character. ๐
๐ BUGS ๐
please submit bugs to the CPAN RT system at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=moosex-params-validate or via email at bug-moosex-params-validate@rt.cpan.org. we love a bug report bestie, don't be shy. ๐
๐ AUTHORS ๐
- Stevan Little <stevan@cpan.org>
- Dave Rolsky <autarch@urth.org>
๐ซถ CONTRIBUTORS ๐ซถ
- Dagfinn Ilmari Mannsรฅker <ilmari@ilmari.org>
- Hans Staugaard <h.staugaard@tweakker.com>
- Karen Etheridge <ether@cpan.org>
give it up for the whole squad, they ate and left no crumbs. ๐
โญ COPYRIGHT AND LICENSE โญ
this software is copyright (c) 2013 - 2015 by Stevan Little <stevan@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. open source, iconic, periodt. ๐
and that's the whole page, bestie!! this gorgeous little doc came to you via perl v5.34.0, dated 2022-06-15, and it lives under the name MooseX...lidate(3pm). go validate some params, you're doing amazing. ๐
โจ
๐ฆ where this page comes from
- debian bookworm: libmoosex-params-validate-perl 0.21-2 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libmoosex-params-validate-perl 0.21-2 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libmoosex-params-validate-perl 0.21-2 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libmoosex-params-validate-perl 0.21-2 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libmoosex-params-validate-perl 0.21-2 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms