andreas1234567 has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

I currently looking at Data::Validator::Item to verify that the input data my application receives is according to specification. I am concerned about that the documentation says "alpha" and the module hasn't been updated since Jan 2003.

I wonder what (modules) the enlightened monks use to perform data validation.

use strict; use warnings; use Date::Calc qw(check_date); use Data::Validator::Item; my @variables = qw (FIRSTNAME GENDER BIRTHDATE_DDMMYYYY); my %dictionary = (); foreach my $variable (@variables) { $dictionary{$variable} = Data::Validator::Item->new(); $dictionary{$variable}->name($variable); $dictionary{$variable}->missing(q{}); # default blank } my $gender_coderef = sub { my $input = shift; my %transform = ( 1 => 'M', 2 => 'F', 3 => 'U', ); return $transform{$input}; }; my $ddmmyyyy_coderef = sub { my $ddmmyyyy = shift; $ddmmyyyy =~ /^(\d{2})(\d{2})(\d{4})$/; my ($year, $month, $day) = ($3, $2, $1); return check_date($year, $month, $day); }; $dictionary{GENDER}->transform($gender_coderef); $dictionary{GENDER}->values([ 1, 2, 3 ]); # Valid values *before* t +ransform $dictionary{BIRTHDATE_DDMMYYYY}->verify($ddmmyyyy_coderef); # ------ main ------ my $line = 0; while (<DATA>) { $line++; chomp; my ($index, $error) = (0, 0); my @fields = split(q{;}, $_); my @output = (); foreach my $elem (@fields) { if ($dictionary{ $variables[$index] }->validate($elem) != 1) { print "Error on line " . $line . " in column " . $variables[$index] . " value:" . $elem; $error++; } else { push @output, $dictionary{ $variables[$index] }->put($elem); } $index++; } # Print lines without errors only print join(q{;}, @output) if (!$error); } __DATA__ Kermit;1;21131986 Swedish Chef;3;21031959 Miss Piggy;4;07111987
Output:
$ perl -wl dvi.pl Error on line 1 in column BIRTHDATE_DDMMYYYY value:21131986 Swedish Chef;U;21031959 Error on line 3 in column GENDER value:4
--
Andreas

Replies are listed 'Best First'.
Re: Alternatives to Data::Validator::Item
by samtregar (Abbot) on Sep 14, 2007 at 16:10 UTC
    I use Params::Validate to validate subroutine args and Data::FormValidator to validate input data. Despite the name the latter is usable outside the context of a web app.

    -sam

Re: Alternatives to Data::Validator::Item
by SFLEX (Chaplain) on Sep 14, 2007 at 11:56 UTC
    if "alpha" is in the version of the module then most of the time it means something is missing. =P
    Not a reashoring sign if your using it for security.

    I check the veriables by knowing what characters it should be or what i will allow.

    my $this_is_bad = '../A#\/*Bad|-Name'; my $this_is_ok = 'John Smith'; $this_is_bad = untaint($this_is_bad, '0-9a-zA-Z \_\-'); $this_is_ok = untaint($this_is_ok, '0-9a-zA-Z \_\-'); print 'Bad input at $this_is_ok' if !$this_is_ok; print 'Bad input at $this_is_bad' if !$this_is_bad; # if the input did not pass the veriable retured is blank sub untaint { my $value = shift || ''; my $pattern = shift || '0-9a-zA-Z\_'; return '' unless $value; $value !~ m!^([$pattern]+)$!i ? return : return $1; }