The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.
ChangeLog 06
META.yml 12
Makefile.PL 13
lib/MooseX/Params/Validate.pm 33
t/005_coercion.t 358
5 files changed (This is a version diff) 872
@@ -1,5 +1,11 @@
 Revision history for Perl extension MooseX-Params-Validate
 
+0.13 Sun., Nov. 29, 2009
+    - Fix so that validated_hash does not try to coerce optional
+      parameters which are not present. Patch by Ian Sillitoe.
+
+    - Same fix for pos_validated_list. (Dave Rolsky)
+
 0.12 Tue. Jul. 7, 2009
     - Using the subroutine name as a cache key for validation specs
       broke in the face of method modifiers, which all appear to have
@@ -28,4 +28,5 @@ requires:
   Sub::Exporter: 0
 resources:
   license: http://dev.perl.org/licenses/
-version: 0.12
+  repository: http://code2.0beta.co.uk/moose/svn/MooseX-Params-Validate
+version: 0.13
@@ -1,6 +1,6 @@
 use strict;
 use warnings;
-use inc::Module::Install;
+use inc::Module::Install 0.91;
 
 name 'MooseX-Params-Validate';
 all_from 'lib/MooseX/Params/Validate.pm';
@@ -18,4 +18,6 @@ build_requires 'Test::Exception' => '0.21';
 
 license 'Perl';
 
+resources repository => 'http://code2.0beta.co.uk/moose/svn/MooseX-Params-Validate';
+
 WriteAll();
@@ -19,7 +19,7 @@ use Sub::Exporter -setup => {
     },
 };
 
-our $VERSION   = '0.12';
+our $VERSION   = '0.13';
 our $AUTHORITY = 'cpan:STEVAN';
 
 my %CACHED_SPECS;
@@ -52,7 +52,7 @@ sub validated_hash {
     my %args = @$args;
 
     $args{$_} = $spec{$_}{constraint}->coerce( $args{$_} )
-        for grep { $spec{$_}{coerce} } keys %spec;
+        for grep { $spec{$_}{coerce} && exists $args{$_} } keys %spec;
 
     %args = Params::Validate::validate_with(
         params => \%args,
@@ -146,7 +146,7 @@ sub pos_validated_list {
     my @args = @{$args};
 
     $args[$_] = $pv_spec[$_]{constraint}->coerce( $args[$_] )
-        for grep { $pv_spec[$_]{coerce} } 0 .. $#pv_spec;
+        for grep { $pv_spec[$_] && $pv_spec[$_]{coerce} } 0 .. $#args;
 
     @args = Params::Validate::validate_with(
         params => \@args,
@@ -3,9 +3,11 @@
 use strict;
 use warnings;
 
-use Test::More tests => 10;
+use Test::More tests => 15;
 use Test::Exception;
 
+# Note that setting coerce => 1 for the Num type tests that we don't try to do
+# coercions for a type which doesn't have any coercions.
 {
     package Foo;
     use Moose;
@@ -27,6 +29,19 @@ use Test::Exception;
         [ $params{size1}, $params{size2}, $params{number} ];
     }
 
+    # added to test 'optional' on validated_hash
+    sub baropt {
+        my $self = shift;
+        my %params = validated_hash(
+            \@_,
+            size1  => { isa => 'Size', coerce => 1, optional => 1 },
+            size2  => { isa => 'Size', coerce => 0, optional => 1 },
+            number => { isa => 'Num',  coerce => 1, optional => 1 },
+        );
+        [ $params{size1}, $params{size2}, $params{number} ];
+    }
+
+
     sub baz {
         my $self = shift;
         my ( $size1, $size2, $number ) = validated_list(
@@ -49,6 +64,17 @@ use Test::Exception;
         );
         [ $size1, $size2, $number ];
     }
+
+    sub ran_out {
+        my $self = shift;
+        my ( $size1, $size2, $number ) = pos_validated_list(
+            \@_,
+            { isa => 'Size', coerce => 1, optional => 1 },
+            { isa => 'Size', coerce => 0, optional => 1 },
+            { isa => 'Num',  coerce => 1, optional => 1 },
+        );
+        [ $size1, $size2, $number ];
+    }
 }
 
 my $foo = Foo->new;
@@ -72,7 +98,7 @@ qr/\QThe 'size2' parameter/,
 
 throws_ok { $foo->bar( size1 => 30, size2 => 10, number => 'something' ) }
 qr/\QThe 'number' parameter/,
-    '... the number param cannot be coerced';
+    '... the number param cannot be coerced because there is no coercion defined for Num';
 
 is_deeply(
     $foo->baz( size1 => 10, size2 => 20, number => 30 ),
@@ -95,7 +121,36 @@ qr/\QThe 'number' parameter/,
     '... the number param cannot be coerced';
 
 is_deeply(
+    $foo->baropt( size2 => 4 ),
+    [ undef, 4, undef ],
+    '... validated_hash does not try to coerce keys which are not provided'
+);
+
+is_deeply(
     $foo->quux( size2 => 4 ),
     [ undef, 4, undef ],
-    '... does not try to coerce keys which are not provided'
+    '... validated_list does not try to coerce keys which are not provided'
+);
+
+is_deeply(
+    $foo->ran_out( 1, 2, 3 ),
+    [ 1, 2, 3 ],
+    'got the return value right without coercions'
+);
+
+is_deeply(
+    $foo->ran_out( [1], 2, 3 ),
+    [ 1, 2, 3 ],
+    'got the return value right with coercion for the first param'
+);
+
+throws_ok { $foo->ran_out( [ 1, 2 ], [ 1, 2 ] ) }
+qr/\QParameter #2/,
+    '... did not attempt to coerce the second parameter';
+
+
+is_deeply(
+    $foo->ran_out(),
+    [ undef, undef, undef ],
+    'did not try to coerce non-existent parameters'
 );