On Wed Feb 06 10:52:41 2008, stocks29 wrote:
Show quoted text> Very nice, but I would like to request an extension of the existing
> functionality.
>
> It would be nice to be able to set an 'or' condition for depends.
> Example:
>
> foo is optional
> bar is optional
> baz is optional
>
> if foo is passed, one of bar or baz is required.
>
> On Mon Jan 28 11:52:27 2008, autarch@urth.org wrote:
> > On Mon, 28 Jan 2008, Robert Stockdale via RT wrote:
> >
> > > It would be nice to be able to set a parameter as mandatory iff another
> > > specific parameter is passed.
> > >
> > > Example:
> > >
> > > foo is optional
> > > bar is required
> > > baz is required if foo is passed.
> >
> > Look in the docs for "depends". That feature exists.
> >
> >
> > -dave
> >
> > /*===================================================
> > VegGuide.Org www.BookIRead.com
> > Your guide to all that's veg. My book blog
> > ===================================================*/
>
>
Robert - if you are still interested, check out Params::Validate::Dependencies (
https://metacpan.org/module/Params::Validate::Dependencies ) which wraps Params::Validate to provides "any_of", "all_of", "one_of" and "none_of" options (which can be combined)
The attached file shows your second request (bar or baz must be present if foo is)
#!/usr/bin/perl
use Test::Most;
use Params::Validate::Dependencies ':all';
sub options {
my $args = validate(
@_,
{ foo => { type => SCALAR, optional => 1 },
bar => { type => SCALAR, optional => 1 },
baz => { type => SCALAR, optional => 1 },
},
one_of( #
none_of('foo'), #
all_of( 'foo', one_of( 'bar', 'baz' ) )
),
);
}
ok options(), "no foo";
ok options( foo => 1, bar => 1 ), "foo with bar";
ok options( foo => 1, baz => 1 ), "foo with baz";
dies_ok { options( foo => 1 ) } "foo with no bar or baz";