Validator::Custom - Validates user input easily
Basic usages
use Validator::Custom; my $vc = Validator::Custom->new; my $data = {age => 19, name => 'Ken Suzuki'}; my $rule = [ age => [ 'int' ], name => [ ['not_blank', "Name must be exists"], [{length => [1, 5]}, "Name length must be 1 to 5"] ], price => {default => 1000, message => 'price must be integer'} => [ 'int' ] ]; my $result = $vc->validate($data, $rule);
Result of validation
if ($result->has_missing) { # Found missing parameters } elsif ($result->has_invalid) { unless ($result->is_valid('title') { my $message = $result->message('title'); pritn $message; } }
More features
# Register constraint $vc->register_constraint( email => sub { require Email::Valid; return 0 unless $_[0]; return Email::Valid->address(-address => $_[0]) ? 1 : 0; } ); # Multi parameters validation $data = {password1 => 'xxx', password2 => 'xxx'}; $vc->register_constraint( same => sub { my $values = shift; my $is_valid = $values->[0] eq $values->[1]; return [$is_valid, $values->[0]]; } ); $rule = [ {password_check => [qw/password1 password2/]} => [ ['same', 'Two password must be equal'] ] ]; $result = $vc->validate($data, $rule); # Negative validateion $rule = [ 'age' => [ '!int' ] ];
Validator::Custom validates user input. The features are the following ones.
Can set a message for each parameter. the messages is added to the result when the parameter is invalid. the messages keeps the order.
Can register constraint function. such as "int", "defined". constraint function can receive any arguments, other than parameter value.
Can create original class, extending Validator::Custom (See Validator::Custom::HTMLForm)
Support multi-parameters validation, multi-values validation, OR condition validation, negative validation.
See Validator::Custom::Guides to know usages of Validator::Custom.
constraints
my $constraints = $vc->constraints; $vc = $vc->constraints(\%constraints);
Constraint functions.
data_filter
my $filter = $vc->data_filter; $vc = $vc->data_filter(\&filter);
Filter input data. If data is not hash reference, you can convert the data to hash reference.
$vc->data_filter( sub { my $data = shift; # Convert data to hash reference. return $data; } );
error_stock
my $error_stock = $vc->error_stcok; $vc = $vc->error_stock(1);
If error_stock is set to 1, all validation error is stocked. If error_stock is set 0, Validation is finished soon after a error occur.
Default to 1.
rule
my $rule = $vc->rule; $vc = $vc->rule($rule);
Rule for validation. Validation rule has the following syntax.
# Rule syntax my $rule = [ # 1. Rule is array ref key => [ # 2. Constraints is array ref 'constraint', # 3. Constraint is string {'constraint' => 'args'} # or hash ref (arguments) ['constraint', 'err'], # or arrya ref (message) ], key => [ [{constraint => 'args'}, 'err'] # 4. With argument and message ], {key => ['key1', 'key2']} => [ # 5. Multi-parameters validation 'constraint' ], key => [ '@constraint' # 6. Multi-values validation ], key => \%OPTIONS => [ # 7. With options 'constraint' ] ];
shared_rule
my $shared_rule = $vc->shared_rule; $vc = $vc->shared_rule(\@rule);
Shared rule. Shared rule is added the head of normal rule.
$vc->shared_rule([ ['defined', 'Must be defined'], ['not_blank', 'Must be not blank'] ]);
syntax
my $syntax = $vc->syntax; $vc = $vc->syntax($syntax);
Syntax of rule.
Validator::Custom inherits all methods from Object::Simple and implements the following new ones.
validate
$result = $vc->validate($data, $rule); $result = $vc->validate($data);
Validate the data. Return value is Validator::Custom::Result object. If the rule of second arument is ommited, rule attribute is used for validation.
register_constraint
$vc->register_constraint(%constraint); $vc->register_constraint(\%constraint);
Register constraint. constraint must be sub reference, which check if the value is valid.
$vc->register_constraint( int => sub { my $value = shift; my $is_valid = $value =~ /^\-?[\d]+$/; return $is_valid; }, ascii => sub { my $value = shift; my $is_valid = $value =~ /^[\x21-\x7E]+$/; return $is_valid; } );
ascii
check is the data consists of only ascii code.
between
Numeric comparison
my $rule = [ age => [ {between => [1, 20]} ] ];
blank
Check if the is blank.
decimal
my $data = { num => '123.45678' }; my $rule => [ num => [ {'decimal' => [3, 5]} ] ]; Validator::Custom::HTMLForm->new->validate($data,$rule);
Each numbers (3,5) mean maximum digits before/after '.'
defined
Check if the data is defined.
duplication
Check if the two data are same or not.
my $data = {mail1 => 'a@somehost.com', mail2 => 'a@somehost.com'}; my $rule => [ [qw/mail1 mail2/] => [ 'duplication' ] ];
equal_to
my $rule = [ age => [ {equal_to => 25} ] ];
greater_than
my $rule = [ age => [ {greater_than => 25} ] ];
http_url
Verify it is a http(s)-url
my $data = { url => 'http://somehost.com' }; my $rule => [ url => [ 'http_url' ] ];
int
Check if the data is integer.
# valid data 123 -134
in_array
Check if the food ordered is in menu
my $rule = [ food => [ {in_array => [qw/sushi bread apple/]} ] ];
length
Check the length of the data.
The following sample check if the length of the data is 4 or not.
my $data = { str => 'aaaa' }; my $rule => [ num => [ {'length' => 4} ] ];
When you set two arguments, it checks if the length of data is in the range between 4 and 10.
my $data = { str => 'aaaa' }; my $rule => [ num => [ {'length' => [4, 10]} ] ];
less_than
my $rule = [ age => [ {less_than => 25} ] ];
not_blank
Check if the data is not blank.
not_defined
Check if the data is not defined.
not_space
Check if the data do not containe space.
uint
Check if the data is unsigned integer.
# valid data 123
regex
Check with regular expression.
my $data = {str => 'aaa'}; my $rule => [ str => [ {regex => qr/a{3}/} ] ];
selected_at_least
Verify the quantity of selected parameters is counted over allowed minimum.
<input type="checkbox" name="hobby" value="music" /> Music <input type="checkbox" name="hobby" value="movie" /> Movie <input type="checkbox" name="hobby" value="game" /> Game my $data = {hobby => ['music', 'movie' ]}; my $rule => [ hobby => [ {selected_at_least => 1} ] ];
shift
Shift the head of array reference.
my $data = {nums => [1, 2]}; my $rule => [ nums => [ 'shift' ] ];
trim
Trim leading and trailing white space
my $rule = [ key1 => [ ['trim'] # ' 123 ' -> '123' ], ];
trim_collapse
Trim leading and trailing white space, and collapse all whitespace characters into a single space.
my $rule = [ key1 => [ ['trim_collapse'] # " \n a \r\n b\nc \t" -> 'a b c' ], ];
trim_lead
Trim leading white space
my $rule = [ key1 => [ ['trim_lead'] # ' abc ' -> 'abc ' ], ];
trim_trail
Trim trailing white space
my $rule = [ key1 => [ ['trim_trail'] # ' def ' -> ' def' ] ];
Please tell me the bugs.
<kimoto.yuki at gmail.com>
Validator::Custom and Validator::Custom::Result is stable. All methods in this documentation (except for experimantal marking ones) keep backword compatible in the future.
Yuki Kimoto, <kimoto.yuki at gmail.com>
http://github.com/yuki-kimoto/Validator-Custom
Copyright 2009 Yuki Kimoto, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Validator::Custom, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Validator::Custom
CPAN shell
perl -MCPAN -e shell install Validator::Custom
For more information on module installation, please visit the detailed CPAN module installation guide.