NAME

MouseX::Params::Validate - Extension of Params::Validate using Mouse's types.

VERSION

Version 0.05

DESCRIPTION

Method parameter validation extension to Mouse.

Borrowed code entirely from MooseX::Params::Validate and stripped Moose footprints.

EXPORTS

By default, this module exports the following:

  • validated_hash

  • validated_list

  • pos_validated_list

CAVEATS

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

CACHING

When a validation subroutine is called the first time, the parameter spec is prepared & 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. 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.

  • Passing in MX_PARAMS_VALIDATE_CACHE_KEY with value to be used as the cache key will bypass the normal cache key generation.

METHODS

validated_hash(\@_, %parameter_spec)

This behaves similarly to the standard Params::Validate validate function and returns the captured values in a HASH. The one exception is where if it spots an instance in the @_ , then it will handle it appropriately.

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; class name, Mouse type constraint name or an anon Mouse type constraint.

does

The does option can be either; role name or an anon Mouse type constraint.

default

This is the default value to be used if the value is not supplied.

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.

use Mouse;
use MouseX::Params::Validate;

sub foo
{
    my ($self, %params) = validated_hash(
        \@_,
        bar => {isa => 'Str', default => 'Mouse'},
    );
    ...
    ...
}

validated_list(\@_, %parameter_spec)

The %parameter_spec accepts the same options as above but returns the params as positional values instead of a HASH.

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.

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.

use Mouse;
use MouseX::Params::Validate;

sub foo
{
    my ($self, $foo, $bar) = validated_list(
        \@_,
        foo => {isa => 'Foo'},
        bar => {isa => 'Bar'},
    );
    ...
    ...
}

pos_validated_list(\@_, $spec, $spec, ...)

This function validates a list of positional parameters. Each $spec should validate one of the parameters in the list.

Unlike the other functions, this function cannot find $self in the argument list. Make sure to shift it off yourself before doing validation.

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 itself an array reference.

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.

use Mouse;
use MouseX::Params::Validate;

sub foo
{
    my $self = shift;
    my ($foo, $bar) = pos_validated_list(
        \@_,
        {isa => 'Foo'},
        {isa => 'Bar'},
        MX_PARAMS_VALIDATE_NO_CACHE => 1,
    );
    ...
    ...
}

AUTHOR

Mohammad S Anwar, <mohammad.anwar at yahoo.com>

BUGS

Please report any bugs or feature requests to bug-mousex-params-validate at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MouseX-Params-Validate. I will be notified and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc MouseX::Params::Validate

You can also look for information at:

ACKNOWLEDGEMENTS

LICENSE AND COPYRIGHT

Copyright 2011 Mohammad S Anwar.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.

DISCLAIMER

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.