dzilize this distro
[gitmo/MooseX-Params-Validate.git] / lib / MooseX / Params / Validate.pm
index 2379008..f507d60 100644 (file)
@@ -29,6 +29,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
@@ -55,9 +57,10 @@ sub validated_hash {
         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' )
@@ -102,9 +107,10 @@ sub validated_list {
         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 +131,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' )
@@ -149,9 +157,10 @@ sub pos_validated_list {
         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 +239,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;
@@ -249,7 +256,7 @@ MooseX::Params::Validate - an extension of Params::Validate for using Moose's ty
           \@_,
           bar => { isa => 'Str', default => 'Moose' },
       );
-      return "Horray for $params{bar}!";
+      return "Hooray for $params{bar}!";
   }
 
   sub bar {
@@ -270,11 +277,11 @@ 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 C<MooseX::Method::Signatures> and
-C<MooseX::Declare>
+C<MooseX::Declare>.
 
 =head1 CAVEATS
 
-It is not possible to introspect the method parameter specs, they are
+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.
 
@@ -284,9 +291,9 @@ calls.
 
 =item B<validated_hash( \@_, %parameter_spec )>
 
-This behaves similar to the standard Params::Validate C<validate>
+This behaves similarly to the standard Params::Validate C<validate>
 function and returns the captured values in a HASH. The one exception
-being that if it spots an instance in the C<@_>, then it will handle
+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).
 
@@ -389,6 +396,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<MX_PARAMS_VALIDATE_ALLOW_EXTRA> to a true value when calling
+a validation subroutine.
+
+When calling C<validated_hash> or C<pos_validated_list> the extra parameters
+are simply returned in the hash or list as appropriate. However, when you call
+C<validated_list> 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<validated_hash>,
@@ -400,11 +419,10 @@ them.
 
 =head1 IMPORTANT NOTE ON CACHING
 
-When C<validate> or C<validatep> 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 +460,16 @@ 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 E<lt>stevan.little@iinteractive.comE<gt>
 
 Dave Rolsky E<lt>autarch@urth.orgE<gt>
 
-=head1 COPYRIGHT AND LICENSE
-
-Copyright 2007-2009 by Infinity Interactive, Inc.
-
-L<http://www.iinteractive.com>
+=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