Skip Menu |

This queue is for tickets about the MooseX-Params-Validate CPAN distribution.

Report information
The Basics
Id: 46344
Status: resolved
Priority: 0/
Queue: MooseX-Params-Validate

People
Owner: Nobody in particular
Requestors: kane [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Critical
Broken in: 0.09
Fixed in: 0.11



Subject: Coercion is done on non-required, non-provided keys
Hi, I ran into an annoying bug with MX:P:V, demonstrated by the attached script. In short, due to this line in MX:P:V->validate_hash: $args{$_} = $spec{$_}{constraint}->coerce( $args{$_} ) for grep { $spec{$_}{coerce} } keys %spec; coercion is done regardless, even if the key is specified is optional and/or not even provided. So with a spec like: key => { isa => .., optional => 1, coerce => 1 } coercion's always called, and will throw an exception on failure, before the spec is even validated by P:V.
Subject: x.pl
package Foo; use Moose; use MooseX::Params::Validate; use MooseX::Types::URI qw[Uri]; sub bar { $DB::single = 1; my ( $self, %args ) = validated_hash( \@_, url => { isa => Uri, default => '', coerce => 1 }, name => { isa => 'Str', optional => 1 }, ); return join ' ', keys %args; } package main; my $foo = Foo->new; warn "No args: " . (eval { $foo->bar } || $@); warn "Just url: " . (eval { $foo->bar( url => 'http://example.com' )} || $@); warn "Just name: ". (eval { $foo->bar( name => $$ ) } || $@); __END__ No args: The 'url' parameter (undef) to Foo::bar did not pass the 'checking type constraint for MooseX::Types::URI::Uri' callback at /home/demo/perl/5.10/lib/site_perl/5.10.0/MooseX/Params/Validate.pm line 56 MooseX::Params::Validate::validated_hash('ARRAY(0x9b0fc40)', 'url', 'HASH(0x9b04078)', 'name', 'HASH(0x99931e0)') called at tmp/x.pl line 8 Foo::bar(undef) called at tmp/x.pl line 20 eval {...} called at tmp/x.pl line 20 Just url: url at tmp/x.pl line 21. Just name: The 'url' parameter (undef) to Foo::bar did not pass the 'checking type constraint for MooseX::Types::URI::Uri' callback at /home/demo/perl/5.10/lib/site_perl/5.10.0/MooseX/Params/Validate.pm line 56 MooseX::Params::Validate::validated_hash('ARRAY(0x9b0f880)', 'url', 'HASH(0x9434668)', 'name', 'HASH(0x9941f68)') called at tmp/x.pl line 8 Foo::bar(undef, 'name', 12583) called at tmp/x.pl line 22 eval {...} called at tmp/x.pl line 22
I've applied the attached patch and the test script now runs like: No args: at tmp/x.pl line 21. Just url: url at tmp/x.pl line 22. Just name: name at tmp/x.pl line 23. Which is as expected. I'd appreciate if you could roll a stable release with this fix in it, so we don't have to run a locally patched copy. Thank you,
Index: /home/demo/perl/5.10/lib/site_perl/5.10.0/MooseX/Params/Validate.pm =================================================================== --- /home/demo/perl/5.10/lib/site_perl/5.10.0/MooseX/Params/Validate.pm (revision 1298) +++ /home/demo/perl/5.10/lib/site_perl/5.10.0/MooseX/Params/Validate.pm (working copy) @@ -18,7 +18,7 @@ }, }; -our $VERSION = '0.09'; +our $VERSION = '0.09_01'; our $AUTHORITY = 'cpan:STEVAN'; my %CACHED_SPECS; @@ -51,7 +51,7 @@ my %args = @$args; $args{$_} = $spec{$_}{constraint}->coerce( $args{$_} ) - for grep { $spec{$_}{coerce} } keys %spec; + for grep { $spec{$_}{coerce} and exists $args{$_} } keys %spec; %args = Params::Validate::validate_with( params => \%args,