Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?


by Dylan (Monk)
on Aug 30, 2002 at 17:06 UTC ( #194183=sourcecode: print w/replies, xml ) Need Help??
Author/Contact Info /msg Dylan

This is an OO module for 'validating' hashes.

I could post the module code here, but then you'd not get the examples, and such. The tarball is located Here, and is standard CPAN-ish. In fact, I am thinking of submitting it to CPAN.

use Validate::Hash;
my $vh = new Validate::Hash(
            required => ['Fred', 'Wilma'],
            optional => ['Barney', 'Betty'],
            verify   => {Barney => qr/\d+/, 
                         Fred => qr/\w+/},
            -errs    => {illegal =>  "#KEY# is illegal",
                         required => "#KEY# is required!",
                         notmatch => 
                     "the value #KEY#, #VALUE#, doesn't match /#PATTER
            -strict => 1,

my %hash = (Fred => 'A', Barney => 1);

$vh->valid(\%hash); # returns false, 
$vh->get_error();   # returns 'Wilma is required!'
From the README:

 /*************** README ***************\
|            ValidateHash v1.4           |

***** Summary
This module provides Object-Oriented way to validate the contents of a hash.
It should be useful for CGI-ish forms or for validating the arguments of functions.
Whatever floats your boat.

To install, just:
% perl Makefile.PL
% make
% make install

Validate::Hash is documented by its own POD. I suggest you read it by typing:
% perldoc Validate::Hash

***** Copyright:
This perl module is Copyright (c) 2002 Dylan William Hardison. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

***** BUGS:
None known. If you find a bug, please email me with the details, or better yet, a patch,
and I'll see to it that it is fixed ASAP.
Replies are listed 'Best First'.
Re: Validate::Hash
by gav^ (Curate) on Aug 30, 2002 at 17:50 UTC

      My other reply is rude, I appologize for that.

      All the hash validation modules on CPAN are either very specialized or very complicated. I wanted to see if I could write one that was general *and* simple. I also have this idea for turning the 'hash profile' (coined by Data::FormValidator, I think) into a single independant function. This is quite possible (I'd need B::Deparse for verify =>{}'s that use subrefs, though), and the next revision should feature it.

      Once Embperl 2 is out of beta, I'll likely use Embperl::Form::Validate, which looks nice. (I love HTML::Embperl :)

      If I hadn't wrote Validate::Hash, I'd have gone mad. The form it is validating (address book thingy for my family) has 20 fields, and growing. Well, perhaps I already am mad, but we don't want us to get any worse.
      Yes we do!
      No we don't!

      I tried Data::FormValidator and Params::Validate, and I didn't like 'em.
Re: Validate::Hash
by Dylan (Monk) on Aug 30, 2002 at 17:31 UTC
    No longer dublicate, and maybe someone will find it useful.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://194183]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (2)
As of 2023-06-08 05:13 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (29 votes). Check out past polls.