Data::Verifier derefences fields with type "ArrayRef" if they contain only one element. Attached is
a fix and a testcase.
Subject: | arrayref_with_single_element.patch |
diff -ur Data-Verifier-0.36/lib/Data/Verifier.pm Data-Verifier-0.36-fixed/lib/Data/Verifier.pm
--- Data-Verifier-0.36/lib/Data/Verifier.pm 2010-08-12 15:21:58.000000000 +0200
+++ Data-Verifier-0.36-fixed/lib/Data/Verifier.pm 2010-08-17 16:51:39.000000000 +0200
@@ -194,11 +194,16 @@
sub _filter_value {
my ($self, $filters, $values) = @_;
+
+ my $created_ref = 0;
+
if(ref($filters) ne 'ARRAY') {
$filters = [ $filters ];
+ $created_ref = 1;
}
if(!ref($values)) {
$values = [ $values ];
+ $created_ref = 1;
}
foreach my $f (@{ $filters }) {
@@ -213,8 +218,7 @@
}
}
- # Return an arrayref if we have multiple values or a scalar if we have one
- scalar(@{ $values }) == 1 ? $values->[0] : $values;
+ return $created_ref ? $values->[0] : $values;
}
__PACKAGE__->meta->make_immutable;
diff -ur Data-Verifier-0.36/t/array.t Data-Verifier-0.36-fixed/t/array.t
--- Data-Verifier-0.36/t/array.t 2010-03-10 21:23:43.000000000 +0100
+++ Data-Verifier-0.36-fixed/t/array.t 2010-08-17 16:49:27.000000000 +0200
@@ -13,6 +13,26 @@
}
);
+ my $results = $verifier->verify({ name => [ 'foo' ] });
+
+ ok($results->success, 'success');
+ cmp_ok($results->valid_count, '==', 1, '1 valid');
+ cmp_ok($results->invalid_count, '==', 0, 'none invalid');
+ cmp_ok($results->missing_count, '==', 0, 'none missing');
+ is_deeply($results->get_value('name'), [ 'foo' ], 'got my name back');
+ ok($results->is_valid('name'), 'name is valid');
+}
+
+{
+ my $verifier = Data::Verifier->new(
+ profile => {
+ name => {
+ required => 1,
+ type => 'ArrayRef[Str]'
+ }
+ }
+ );
+
my $results = $verifier->verify({ name => [ 'foo', 'bar' ], bar => 'reject me' });
ok($results->success, 'success');