Subject: | options Constraint doesn't work |
use Data::TreeValidator::Constraints 'options';
use Data::TreeValidator::Sugar qw/leaf branch/;
my $validator = branch {foo => leaf(constraints => [options("A","B")])};
# Should be invalid, since "C" is not in set("A", "B");
my $object = {foo => "C"};
my $process = $validator->process($object);
# prints 1
print $process->valid, "\n";
I believe the problem is that in the options() function in Data::TreeValidator::Constraints
fail_constraint is never called.
In version 0.03 we have the follow excerpt:
sub options {
my $valid = set(@_);
return sub {
my ($input) = @_;
$valid->contains($input);
};
}
The line
$valid->contains($input);
should be
$valid->contains($input) or fail_constraint("Input must be in set (@$valid)");
That patch fixes the function.
Based on looking at the source code, the type() Constaint also appears to be broken in the
same way.
$type->check(@_)
should be changed to
$type->check(@_) or fail_constraint("Input must be a " . $type->name)
However, I haven't tested this example.