X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMooseX%2FParams%2FValidate.pm;h=c2f691b2bf28739d924fae0e3a34326e1ca18b63;hb=refs%2Ftags%2Fv0.16;hp=b7293e59f4c887a5627d9312ab2cc6a45bf4906c;hpb=f5e350c10b1bd97315e9fe6679091496f5604dbc;p=gitmo%2FMooseX-Params-Validate.git diff --git a/lib/MooseX/Params/Validate.pm b/lib/MooseX/Params/Validate.pm index b7293e5..c2f691b 100644 --- a/lib/MooseX/Params/Validate.pm +++ b/lib/MooseX/Params/Validate.pm @@ -5,7 +5,7 @@ use warnings; use Carp 'confess'; use Devel::Caller 'caller_cv'; -use Scalar::Util 'blessed', 'refaddr'; +use Scalar::Util 'blessed', 'refaddr', 'reftype'; use Moose::Util::TypeConstraints qw( find_type_constraint class_type role_type ); use Params::Validate (); @@ -19,9 +19,6 @@ use Sub::Exporter -setup => { }, }; -our $VERSION = '0.14'; -our $AUTHORITY = 'cpan:STEVAN'; - my %CACHED_SPECS; sub validated_hash { @@ -29,6 +26,8 @@ sub validated_hash { my $cache_key = _cache_key( \%spec ); + my $allow_extra = delete $spec{MX_PARAMS_VALIDATE_ALLOW_EXTRA}; + if ( exists $CACHED_SPECS{$cache_key} ) { ( ref $CACHED_SPECS{$cache_key} eq 'HASH' ) || confess @@ -49,15 +48,19 @@ sub validated_hash { my $instance; $instance = shift @$args if blessed $args->[0]; - my %args = @$args; + my %args + = @$args == 1 + && ref $args->[0] + && reftype( $args->[0] ) eq 'HASH' ? %{ $args->[0] } : @$args; $args{$_} = $spec{$_}{constraint}->coerce( $args{$_} ) for grep { $spec{$_}{coerce} && exists $args{$_} } keys %spec; %args = Params::Validate::validate_with( - params => \%args, - spec => \%spec, - called => _caller_name(), + params => \%args, + spec => \%spec, + allow_extra => $allow_extra, + called => _caller_name(), ); return ( ( defined $instance ? $instance : () ), %args ); @@ -72,6 +75,8 @@ sub validated_list { my $cache_key = _cache_key( \%spec ); + my $allow_extra = delete $spec{MX_PARAMS_VALIDATE_ALLOW_EXTRA}; + my @ordered_spec; if ( exists $CACHED_SPECS{$cache_key} ) { ( ref $CACHED_SPECS{$cache_key} eq 'ARRAY' ) @@ -96,15 +101,19 @@ sub validated_list { my $instance; $instance = shift @$args if blessed $args->[0]; - my %args = @$args; + my %args + = @$args == 1 + && ref $args->[0] + && reftype( $args->[0] ) eq 'HASH' ? %{ $args->[0] } : @$args; $args{$_} = $spec{$_}{constraint}->coerce( $args{$_} ) for grep { $spec{$_}{coerce} && exists $args{$_} } keys %spec; %args = Params::Validate::validate_with( - params => \%args, - spec => \%spec, - called => _caller_name(), + params => \%args, + spec => \%spec, + allow_extra => $allow_extra, + called => _caller_name(), ); return ( @@ -125,6 +134,8 @@ sub pos_validated_list { my $cache_key = _cache_key( \%extra ); + my $allow_extra = delete $extra{MX_PARAMS_VALIDATE_ALLOW_EXTRA}; + my @pv_spec; if ( exists $CACHED_SPECS{$cache_key} ) { ( ref $CACHED_SPECS{$cache_key} eq 'ARRAY' ) @@ -143,15 +154,16 @@ sub pos_validated_list { if $should_cache; } - my @args = @{$args}; + my @args = @$args; $args[$_] = $pv_spec[$_]{constraint}->coerce( $args[$_] ) for grep { $pv_spec[$_] && $pv_spec[$_]{coerce} } 0 .. $#args; @args = Params::Validate::validate_with( - params => \@args, - spec => \@pv_spec, - called => _caller_name(), + params => \@args, + spec => \@pv_spec, + allow_extra => $allow_extra, + called => _caller_name(), ); return @args; @@ -230,14 +242,12 @@ sub _caller_name { 1; +# ABSTRACT: an extension of Params::Validate using Moose's types + __END__ =pod -=head1 NAME - -MooseX::Params::Validate - an extension of Params::Validate for using Moose's types - =head1 SYNOPSIS package Foo; @@ -290,6 +300,9 @@ is where if it spots an instance in the C<@_>, then it will handle it appropriately (unlike Params::Validate which forces you to shift you C<$self> first). +The values in C<@_> can either be a set of name-value pairs or a single hash +reference. + The C<%parameter_spec> accepts the following options: =over 4 @@ -343,6 +356,9 @@ 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 C. +The values in C<@_> can either be a set of name-value pairs or a single hash +reference. + Like C, if it spots an object instance as the first parameter of C<@_>, it will handle it appropriately, returning it as the first argument. @@ -369,6 +385,10 @@ Unlike the other functions, this function I find C<$self> in the argument list. Make sure to shift it off yourself before doing validation. +The values in C<@_> 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. + If a parameter is marked as optional and is not present, it will simply not be returned. @@ -389,6 +409,18 @@ below, simply pass them after the list of parameter validation specs: =back +=head1 ALLOWING EXTRA PARAMETERS + +By default, any parameters not mentioned in the parameter spec cause this +module to throw an error. However, you can have have this module simply ignore +them by setting C to a true value when calling +a validation subroutine. + +When calling C or C the extra parameters +are simply returned in the hash or list as appropriate. However, when you call +C the extra parameters will not be returned at all. You can +get them by looking at the original value of C<@_>. + =head1 EXPORTS By default, this module exports the C, @@ -400,11 +432,10 @@ them. =head1 IMPORTANT NOTE ON CACHING -When C or C are 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. +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 @@ -442,25 +473,14 @@ the cache key will bypass the normal cache key generation. =back -=head1 BUGS - -All complex software has bugs lurking in it, and this module is no -exception. If you find a bug please either email me, or add the bug to -cpan-RT. - -=head1 AUTHORS - -Stevan Little Estevan.little@iinteractive.comE +=head1 MAINTAINER Dave Rolsky Eautarch@urth.orgE -=head1 COPYRIGHT AND LICENSE - -Copyright 2007-2009 by Infinity Interactive, Inc. - -L +=head1 BUGS -This library is free software; you can redistribute it and/or modify -it under the same terms as Perl itself. +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. =cut