in reply to Data Validation Tests

Maybe it's just me, but I like to have validation routines that go with the modules which actually deal with the data type at hand. For example, Business::CreditCard has a validate function. The documentation of URI shows the "official" regex to match an URI. Date::Calc functions will return an error when fed an invalid date.

The terms strong cohesion and loose coupling spring to mind. Functions for validating data should be part of the module processing that data. Having a separate module with all kinds of unrelated validation routines increases the risk of (possibly) not keeping up with changes in the format, and I don't really need credit card number validation routines in my logfile processing script which extracts IP addresses.

Arjen

Replies are listed 'Best First'.
Re^2: Data Validation Tests
by Aristotle (Chancellor) on Jan 26, 2003 at 14:09 UTC
    While your concern is valid, the idea is to have a way to summarize all validation in a declaration. The benefit is that you avoid manually writing the validation logistics, which is incredibly boring work. Something like
    my @_id = $cgi->params('id'); error_exit("You must select at least one ID.") unless @_id; my @id; for(@_id) { my ($match) = /\A(\d{5,9}[ABC])\z/; error_exit("Invalid ID: $_") unless defined $match; push @id, $match; }
    Even if you replace the regex by ID::validate(), the whole thing remains rather clunky - esp if you imagine that you have to write such a snippet for 25 different parameters: pure drone work. It is much easier to use a Data::FormValidatoresque declaration like
    my $validator = Data::FormValidator->new({ delete_items_page => { required => [qw(id .. ..)], constraints => { id => '/\A(\d{5,9}[ABC])\z/', # .. }, }, });

    While this doesn't compare favourably so far, adding more parameters to validate would be trivial and quick with the latter code. Adding another two dozen checks is easy and doesn't result in an unmanageable amount of code.

    I do agree that the validator should not contain its own actual validation routines. That's why I don't like Data::FormValidator as is; I would prefer there being plugin modules that integrate other modules' own supplied validation routines - much the way File::Find::Rule works.

    Makeshifts last the longest.

      Well then you should be glad to hear that the current design does not have any 'built in' routines. It has some default checks, held in a seperate module that comes with the main verification package, but it is very easy to include checks from other places. Basically anything that returns true on sucess and false on failure can be used, via the 'custom' test declaration (which accepts coderef's and regex's.)



      My code doesn't have bugs, it just develops random features.

      Flame ~ Lead Programmer: GMS | GMS

        It sure would be cool if you took Data::FormValidator or one of the others and added the ability to plug in new tests, instead of creating a new module. Then lots of people would get access to it right away without having to discover your new module and change all of their code.