I just noticed a difference between the behaviour of 'validated_hash' and 'validated_list' in MX::Params::Validate when dealing with coercing optional params:
- validated_list doesn't try to coerce options that are optional and haven't been specified (correct) - validated_hash does try to coerce optional params (and will die when they're not specified) MooseX/Params/Validate.pm sub validated_hash { ... $args{$_} = $spec{$_}{constraint}->coerce( $args{$_} ) for grep { $spec{$_}{coerce} } keys %spec; ... sub validated_list { ... $args{$_} = $spec{$_}{constraint}->coerce( $args{$_} ) for grep { $spec{$_}{coerce} && exists $args{$_} } keys %spec; ... Assuming this is just a typo rather than designed (there's a test for the validated_list case but not validated_hash) - I've attached a patch with the extra test and fix. Cheers, Ian
diff -crB MooseX-Params-Validate-0.12/lib/MooseX/Params/Validate.pm MooseX-Params-Validate-new/lib/MooseX/Params/Validate.pm *** MooseX-Params-Validate-0.12/lib/MooseX/Params/Validate.pm Tue Jul 7 21:46:38 2009 --- MooseX-Params-Validate-new/lib/MooseX/Params/Validate.pm Thu Nov 26 12:45:03 2009 *************** *** 52,58 **** my %args = @$args; $args{$_} = $spec{$_}{constraint}->coerce( $args{$_} ) ! for grep { $spec{$_}{coerce} } keys %spec; %args = Params::Validate::validate_with( params => \%args, --- 52,58 ---- my %args = @$args; $args{$_} = $spec{$_}{constraint}->coerce( $args{$_} ) ! for grep { $spec{$_}{coerce} && exists $args{$_} } keys %spec; %args = Params::Validate::validate_with( params => \%args, diff -crB MooseX-Params-Validate-0.12/t/005_coercion.t MooseX-Params-Validate-new/t/005_coercion.t *** MooseX-Params-Validate-0.12/t/005_coercion.t Tue Jul 7 18:51:37 2009 --- MooseX-Params-Validate-new/t/005_coercion.t Thu Nov 26 12:39:21 2009 *************** *** 3,9 **** use strict; use warnings; ! use Test::More tests => 10; use Test::Exception; { --- 3,9 ---- use strict; use warnings; ! use Test::More tests => 11; use Test::Exception; { *************** *** 27,32 **** --- 27,45 ---- [ $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,101 **** '... the number param cannot be coerced'; is_deeply( $foo->quux( size2 => 4 ), [ undef, 4, undef ], ! '... does not try to coerce keys which are not provided' ); --- 107,119 ---- '... 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 ], ! '... validated_list does not try to coerce keys which are not provided' );