Skip Menu |

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

Maintainer(s)' notes

This is the bug queue for Data::FormValidator.

Report information
The Basics
Id: 42985
Status: open
Priority: 0/
Queue: Data-FormValidator

People
Owner: Nobody in particular
Requestors: amelia.ireland [...] gmail.com
Cc:
AdminCc:

Bug Information
Severity: Important
Broken in:
  • 4.61
  • 4.62
  • 4.63
Fixed in: (no value)



Subject: Dependencies and required fields
In the code that checks the dependencies hash for optional fields that should become required, there is a bug that means the dependent field does not become required if the field upon which is depends has more than one value. e.g. if the dependencies hash looks like this: dependencies => { # if pay_type eq 'check', require check_no pay_type => { check => [ qw( check_no ) ], } }, and I have two (perfectly valid) values for pay_type, 'check_no' will not be added to the list of required fields. The problem is in the D::FV::Results code preceded by the comment " # Handle case of a key with a single value given as an arrayref # There is probably a better, more general solution to this problem." (~ line 225) I fixed it using the following code: for my $key (keys %$deps) { # line 224 # Handle case of a key with a single value given as an arrayref # There is probably a better, more general solution to this problem. my $val_to_compare; if (ref $valid{$field} eq 'ARRAY') { $val_to_compare = $valid{$field}; } else { $val_to_compare = [ $valid{$field} ]; } if (grep { $key } @$val_to_compare){ for my $dep (_arrayify($deps->{$key})){ $required{$dep} = 1; } } }
Subject: Re: [rt.cpan.org #42985] Dependencies and required fields
Date: Tue, 3 Feb 2009 09:44:26 -0500
To: bug-Data-FormValidator [...] rt.cpan.org
From: Mark Stosberg <mark [...] summersault.com>
Show quoted text
> In the code that checks the dependencies hash for optional fields that should become > required, there is a bug that means the dependent field does not become required if the field > upon which is depends has more than one value. > > e.g. if the dependencies hash looks like this: > > dependencies => { > # if pay_type eq 'check', require check_no > pay_type => { > check => [ qw( check_no ) ], > } > }, > > and I have two (perfectly valid) values for pay_type, 'check_no' will not be added to the list of > required fields. > > The problem is in the D::FV::Results code preceded by the comment > " # Handle case of a key with a single value given as an arrayref > # There is probably a better, more general solution to this problem." > (~ line 225) > > I fixed it using the following code: > > for my $key (keys %$deps) { # line 224 > # Handle case of a key with a single value given as an arrayref > # There is probably a better, more general solution to this problem. > my $val_to_compare; > if (ref $valid{$field} eq 'ARRAY') > { $val_to_compare = $valid{$field}; > } > else { > $val_to_compare = [ $valid{$field} ]; > } > > if (grep { $key } @$val_to_compare){ > for my $dep (_arrayify($deps->{$key})){ > $required{$dep} = 1; > } > } > } >
Thanks for the report and patch, Amelia. Could you also submit an automated test that fails before the fix but passes afterwards? You'll find examples in the test suite shipped with DFV. Credit will be given to you in the next release of DFV. Mark
Attached a test for the new functioning of the dependencies code.
use strict; $^W = 1; use Test::More 'no_plan'; #tests => 24; use Data::FormValidator; my %code_results = ( ); my $input_hashref = { }; my $input_profile = { dependencies => { pay_type => { check => [ qw( check_no ) ], }, }, }; my $validator = Data::FormValidator->new({default => $input_profile}); my $result; # # if pay_type eq 'check', require check_no # "pay_type" => { # check => [ qw( check_no ) ], # } ## Values that should cause a missing dependency. ############################################################################# # no 'check_no' field, scalar in 'pay_type' $input_hashref->{pay_type} = 'check'; $result = undef; eval { $result = $validator->check($input_hashref, 'default'); }; ok(!$@, "checking a value that has a dependency"); isa_ok($result, "Data::FormValidator::Results", " returned object"); ok($result->has_missing, " has_missing returned true"); ok($result->missing('check_no'), " missing('check_no') returned true"); # no 'check_no' field, array in 'pay_type' # This will fail with the old code as the missing field will not be picked up $input_hashref->{pay_type} = [ 'check', 'cash' ]; $result = undef; eval { $result = $validator->check($input_hashref, 'default'); }; ok(!$@, "checking a value that has a dependency"); isa_ok($result, "Data::FormValidator::Results", " returned object"); ## These tests fail with the old code ok($result->has_missing, " has_missing returned true"); ok($result->missing('check_no'), " missing('check_no') returned true"); ## Values that should NOT cause a missing dependency. ############################################################################# undef $input_hashref; ## will trigger the dependency, but check_no is present, so we're OK # single value in 'pay_type' $input_hashref = { check_no => '90210', pay_type => 'check' }; $result = undef; eval { $result = $validator->check($input_hashref, 'default'); }; ok(!$@, "checking a value that has no dependencies"); isa_ok($result, "Data::FormValidator::Results", " returned object"); ok(!$result->has_missing, " has_missing returned false"); is($result->missing('check_no'), undef, " missing('check_no') returned false"); # several values in 'pay_type' $input_hashref = { check_no => '90210', pay_type => [ 'cash', 'check' ] }; $result = undef; eval { $result = $validator->check($input_hashref, 'default'); }; ok(!$@, "checking a value that has no dependencies"); isa_ok($result, "Data::FormValidator::Results", " returned object"); ok(!$result->has_missing, " has_missing returned false"); is($result->missing('check_no'), undef, " missing('check_no') returned false"); ## there's no dependence the other way, though. $input_hashref->{check_no} = '90210'; $result = undef; eval { $result = $validator->check($input_hashref, 'default'); }; ok(!$@, "checking a value that has no dependencies"); isa_ok($result, "Data::FormValidator::Results", " returned object"); ok(!$result->has_missing, " has_missing returned false"); is($result->missing('check_no'), undef, " missing('check_no') returned false"); undef $input_hashref; $input_hashref = { check_no => '90210', pay_type => [ 'payment_in_kind', 'cash_in_hand' ] }; $result = undef; eval { $result = $validator->check($input_hashref, 'default'); }; ok(!$@, "checking a value that has no dependencies"); isa_ok($result, "Data::FormValidator::Results", " returned object"); ok(!$result->has_missing, " has_missing returned false"); is($result->missing('check_no'), undef, " missing('check_no') returned false");
Subject: Re: [rt.cpan.org #42985] Dependencies and required fields
Date: Wed, 4 Feb 2009 09:39:08 -0500
To: bug-Data-FormValidator [...] rt.cpan.org
From: Mark Stosberg <mark [...] summersault.com>
On Tue, 03 Feb 2009 19:32:07 -0500 "girlwithglasses via RT" <bug-Data-FormValidator@rt.cpan.org> wrote: Show quoted text
> Queue: Data-FormValidator > Ticket <URL: https://rt.cpan.org/Ticket/Display.html?id=42985 > > > Attached a test for the new functioning of the dependencies code.
Awesome! You've been a start bug reporter. Here's one small testing improvement to consider in the future: Instead of this: ok(!$@, "checking a value that has no dependencies"); Use this: is($@, '', "checking a value that has no dependencies"); The functionality is the same, but the diagnostics with the latter are better, because it will dump the value of "$@" if the eval failed. Mark
RT-Send-CC: mark [...] summersault.com
Amelia,

I am interested in making a patch like this, but when I apply the suggested change, two other tests start to fail:

Show quoted text
##
t/02_code_ref.t  (Wstat: 256 Tests: 8 Failed: 1)
  Failed test:  8
  Non-zero exit status: 1
t/03_dependency.t (Wstat: 1024 Tests: 23 Failed: 4)
  Failed tests:  3-4, 9-10
  Non-zero exit status: 4
##

Could you look into this further? I expect to put out a new release in the next few days.

Thanks for your help!

    Mark


On Mon Feb 02 20:15:24 2009, girlwithglasses wrote:
Show quoted text
> In the code that checks the dependencies hash for optional fields that
> should become
> required, there is a bug that means the dependent field does not
> become required if the field
> upon which is depends has more than one value.
>
> e.g. if the dependencies hash looks like this:
>
> dependencies => {
> # if pay_type eq 'check', require check_no
> pay_type => {
> check => [ qw( check_no ) ],
> }
> },
>
> and I have two (perfectly valid) values for pay_type, 'check_no' will
> not be added to the list of
> required fields.
>
> The problem is in the D::FV::Results code preceded by the comment
> " # Handle case of a key with a single value given as an arrayref
> # There is probably a better, more general solution to this
> problem."
> (~ line 225)
>
> I fixed it using the following code:
>
> for my $key (keys %$deps) { # line 224
> # Handle case of a key with a single value given as an arrayref
> # There is probably a better, more general solution to this
> problem.
> my $val_to_compare;
> if (ref $valid{$field} eq 'ARRAY')
> { $val_to_compare = $valid{$field};
> }
> else {
> $val_to_compare = [ $valid{$field} ];
> }
>
> if (grep { $key } @$val_to_compare){
> for my $dep (_arrayify($deps->{$key})){
> $required{$dep} = 1;
> }
> }
> }
>