Subject: | Auto-reporting? |
When using Types::Standard for CSV validation, I could add this to the Text::CSV_XS docs:
--8<---
This hook can be used for validation:
X<data_validation>
=over 2
=item FAIL
Die if any of the records does not validate a rule:
after_parse => sub {
$_[1][4] =~ m/^[0-9]{4}\s?[A-Z]{2}$/ or
die "5th field does not have a valid Dutch zipcode";
}
=item DEFAULT
Replace invalid fields with a default value:
after_parse => sub { $_[1][2] =~ m/^\d+$/ or $_[1][2] = 0 }
=item SKIP
Skip records that have invalid fields (only applies to L</getline_all>):
after_parse => sub { $_[1][0] =~ m/^\d+$/ or return \"skip"; }
=back
One could also use modules like L<Types::Standard>:
use Types::Standard -types;
my $row_type = Tuple [ Str, Str, Int, Bool, Optional[Num] ];
my $row_check = $row_type->compiled_check;
while (my $row = $csv->get_line ($fh)) {
$row_check->($row) or die $row_type->get_message ($row);
}
or
after_parse => sub { $row_check->($_[1]) }
-->8---
In that last call, it would be easier to have automatic diagnostics
--8<---
my $row_check = $row_type->compiled_check (auto_diag => 1);
-->8---
or some variation of that