Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Validate::Hash

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

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.

Example:
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
+N#/",
            -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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-04-23 22:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found