Skip Menu |

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

Report information
The Basics
Id: 71926
Status: rejected
Priority: 0/
Queue: Params-Validate

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

Bug Information
Severity: Wishlist
Broken in: 1.00
Fixed in: (no value)



Subject: How to specify optional objects that might be undef (and still use isa)
I could be missing something, but this need comes up all the time in code I'm working on: I have a method that needs to take several optional parameters, but if some of the args exist I need them to be a certain class of objects. This is pretty simple on the surface: validate(@_, { date => { isa => 'DateTime', optional => 1}, bar => { isa => 'Bar::Class', optional => 1}, }); But this doesn't work if the optional args are undef. I could do: validate(@_, { date => { type => UNDEF | OBJECT, optional => 1}, bar => { type => UNDEF | OBJECT, optional => 1}, }); But then if I combine that with isa() it still complains that it's not a DateTime, but an "undef". I think this is a pretty common pattern to use especially if your arguments are coming from some other function. Something like this: foo(date => next_date(), bar => 1); Now if I want to validate that date is a DateTime if it's passed, then I need to do something like this instead: my %foo_args = (bar => 1); my $date = next_date(); $foo_args{date} = $date if $date; foo(%foo_args); Which is way more verbose and clumsy. So is there a way to do what I want without having to write my own callback everytime I want to repeat this pattern? If not, are you open to some sort of feature to make this work?
On Tue Oct 25 14:21:28 2011, WONKO wrote: Show quoted text
> So is there a way to do what I want without having to write my own > callback everytime I want to repeat this pattern? If not, are you open > to some sort of feature to make this work?
No, currently this needs a callback. I'm not really too excited about adding lots of features to this module. I think it's API has more or less reached the end of its useful lifespan. I think something more like Moose types is the way to go for data validation, and I'm working on a standalone module to do that.
On Tue Oct 25 14:21:28 2011, WONKO wrote: Show quoted text
> I could be missing something, but this need comes up all the time in > code I'm working on: I have a method that needs to take several
optional Show quoted text
> parameters, but if some of the args exist I need them to be a certain > class of objects. This is pretty simple on the surface: > > validate(@_, { > date => { isa => 'DateTime', optional => 1}, > bar => { isa => 'Bar::Class', optional => 1}, > }); > > But this doesn't work if the optional args are undef. I could do:
I looked at this a little more closely and realized that my first response was wrong. The "optional" flag means "may not be present", not "may be undef". In other words, given the above spec, passing "date => undef" is an error, and it _should_ be an error. Instead of passing an undef, don't pass it at all. That said, some sort of "undef tolerant" feature would be nice, but ultimately Params::Validate probably has too limited an API for that sort of pluggability.