racke/Data-Transpose

Subrefs validator fails when validated field value is zero

Closed this issue · 1 comments

This is fine when $field == 1 but not for 0

            validator => sub {
                my $field = shift;
                if ( $field =~ /^[01]$/ ) {
                    return $field;
                }
                else {
                    return ( undef, "Not a boolean yes/no (1/0)" );
                }
            },

Ok, it looks like here there is some API confusion.

The doc says:

The subroutine is expected to return the variable itself on success, or a false value.

To set a custom error, the subroutine in case of error should return 2 elements, where the first should be undefined.

Now, the false value can be the a valid string like "0". Now, the alternatives are two. Either the doc should be changed and state: "Return a true value if valid", or change the behavior, but I don't know if there is code out there where the subref itself returns 0 without setting a custom error.

I guess the documentation fix is the less intrusive and keeps the existing behavior.