Skip Menu |

Preferred bug tracker

Please visit the preferred bug tracker to report your issue.

This queue is for tickets about the Data-Verifier CPAN distribution.

Report information
The Basics
Id: 60425
Status: resolved
Priority: 0/
Queue: Data-Verifier

People
Owner: Nobody in particular
Requestors: mail [...] dennis-schoen.de
Cc:
AdminCc:

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



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');
Fixed in the just-uploaded 0.37