MooseX...lidate(3pm)

๐Ÿ“ฆ
version 0.21 ยท 4 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

๐Ÿ‘‘ 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 โ€” the isa option can be either; a class name, a Moose type constraint name, or an anon Moose type constraint. your pick, bestie. ๐Ÿ’…
  • does โ€” the does option 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 with Params::Validate, all options are considered required unless otherwise specified. This option is passed directly to Params::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 the Params::Validate documentation 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_CACHE flag 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_KEY with 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. ๐ŸŽ€


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