Skip Menu |

This queue is for tickets about the Brannigan CPAN distribution.

Report information
The Basics
Id: 124269
Status: new
Priority: 0/
Queue: Brannigan

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

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



Subject: Params should validate as scalars by default
There are ways to specify that a value must be a hash or an array, but none to limit a value to a scalar, even though this is probably the most common use case. An easy solution is to add a 'scalar' validator, but I'd argue that this should be the default if 'hash' or 'array' are not specified. Especially since some other validators may have odd behavior if the value is not a scalar. Here's an example of behavior I did not expect: #!/usr/bin/perl use Brannigan; use Data::Dumper 'Dumper'; my $v = Brannigan->new({ name => 'x', params => { foo => { required => 1, min_length => 1 } }, }); # All of these are valid print Dumper map $v->process(x => $_), {foo => 'a'}, {foo => [1,2,3]}, {foo => [{}, 'arbitrarily nested structure here']}, {foo => {a=>'b'}}; # This one is particularly fun, validates just fine. print Dumper Brannigan::process( { params => { foo => { matches => qr/^HASH/ }} }, { foo => {} } );
Turns out this is pretty easy to fix. Here's a patch.
Subject: Brannigan-scalar-test.patch
diff --git a/lib/Brannigan/Tree.pm b/lib/Brannigan/Tree.pm index 40d6a07..71530de 100644 --- a/lib/Brannigan/Tree.pm +++ b/lib/Brannigan/Tree.pm @@ -269,6 +269,7 @@ sub _validate_param { } elsif ($validations->{array}) { return $self->_validate_array($param, $value, $validations); } else { + return ['scalar(1)'] if defined $value && ref $value; return $self->_validate_scalar($param, $value, $validations); } }