logo
Free, unlimited AI code reviews that run on commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

MooseX::Params::Validate - an extension of Params::Validate using Moose's types

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.

       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 @_.

Authors

       •   Stevan Little <stevan@cpan.org>

       •   Dave Rolsky <autarch@urth.org>

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.

Caveats

       It is not possible to introspect the method parameter specs; they are created as needed when the method
       is called and cached for subsequent calls.

Contributors

       •   Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>

       •   Hans Staugaard <h.staugaard@tweakker.com>

       •   Karen Etheridge <ether@cpan.org>

Description

       This module fills a gap in Moose by adding method parameter validation to Moose. This is just one of many
       developing options, it should not be considered the "official" one by any means though.

       You might also want to explore "MooseX::Method::Signatures" and "MooseX::Declare".

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).

Exports

       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.

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.

       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',
                 );

             }

Maintainer

       Dave Rolsky <autarch@urth.org>

Name

       MooseX::Params::Validate - an extension of Params::Validate using Moose's types

Synopsis

         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 ];
         }

Version

       version 0.21

See Also