[Moose-commits] r7972 - in MooseX-Params-Validate/trunk: . lib/MooseX/Params t

t0m at code2.0beta.co.uk t0m at code2.0beta.co.uk
Sun Nov 29 12:31:54 GMT 2009


Author: t0m
Date: 2009-11-29 04:31:54 -0800 (Sun, 29 Nov 2009)
New Revision: 7972

Modified:
   MooseX-Params-Validate/trunk/ChangeLog
   MooseX-Params-Validate/trunk/lib/MooseX/Params/Validate.pm
   MooseX-Params-Validate/trunk/t/005_coercion.t
Log:
Patch from Ian Sillitoe to fix coercion of optional params in validated_hash

Modified: MooseX-Params-Validate/trunk/ChangeLog
===================================================================
--- MooseX-Params-Validate/trunk/ChangeLog	2009-11-29 10:59:10 UTC (rev 7971)
+++ MooseX-Params-Validate/trunk/ChangeLog	2009-11-29 12:31:54 UTC (rev 7972)
@@ -1,5 +1,8 @@
 Revision history for Perl extension MooseX-Params-Validate
 
+    - Fix so that validated_hash does not try to coerce optional
+      parameters which are not present from Ian Sillitoe
+
 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

Modified: MooseX-Params-Validate/trunk/lib/MooseX/Params/Validate.pm
===================================================================
--- MooseX-Params-Validate/trunk/lib/MooseX/Params/Validate.pm	2009-11-29 10:59:10 UTC (rev 7971)
+++ MooseX-Params-Validate/trunk/lib/MooseX/Params/Validate.pm	2009-11-29 12:31:54 UTC (rev 7972)
@@ -52,7 +52,7 @@
     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,

Modified: MooseX-Params-Validate/trunk/t/005_coercion.t
===================================================================
--- MooseX-Params-Validate/trunk/t/005_coercion.t	2009-11-29 10:59:10 UTC (rev 7971)
+++ MooseX-Params-Validate/trunk/t/005_coercion.t	2009-11-29 12:31:54 UTC (rev 7972)
@@ -3,7 +3,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 10;
+use Test::More tests => 11;
 use Test::Exception;
 
 {
@@ -27,6 +27,19 @@
         [ $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(
@@ -95,7 +108,13 @@
     '... 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'
 );




More information about the Moose-commits mailing list