NAME
Validator::Custom - Custom validator
VERSION
Version 0.0707
SYNOPSYS
### How to use Validator::Custom
# Validate
my $vc = Validator::Custom->new
# Constraint
$vc->add_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;
},
not_blank => sub {
my $value = shift;
my $is_valid = $value ne '';
return $is_valid;
}
length => sub {
my ($value, $args) = @_;
my ($min, $max) = @$args;
my $is_valid = $min <= $length && $length <= $max;
return $is_valid;
}
);
# Data
my $data = { age => 19, names => ['abc', 'def'] };
# Validation rule
$vc->validation_rule([
age => [
'int'
],
'@names' => [
['not_blank', "name must exist"],
['ascii', "name must be ascii"],
[{'length' => [1, 5]}, "name must be 1 to 5"]
]
]);
# Validation
my $result = $vc->validate($data, $validation_rule);
# Get errors
my @errors = $result->errors;
# Handle errors
foreach my $error (@errors) {
# ...
}
# Get invalid keys
my @invalid_keys = $result->invalid_keys;
# Get producted value
my $products = $result->products;
# Check valid or not
if($result->is_valid) {
# ...
}
# Corelative check
my $validation_rule => [
[qw/password1 password2/] => [
['duplicate', 'Passwor is not same']
]
]
# Specify key
my $validation_rule => [
{password_check => [qw/password1 password2/]} => [
['duplicate', 'Passwor is not same']
]
]
Accessors
constraints
get constraints
Set and get constraint functions
$vc = $vc->constraints($constraints); # hash or hash ref
$constraints = $vc->constraints;
constraints sample
$vc->constraints(
int => sub { ... },
string => sub { ... }
);
See also 'add_constraint' method
error_stock
Set and get whether error is stocked or not.
$vc = $vc->error_stock(1);
$error_stock = $vc->error_stcok;
If you set stock_error 1, occured error on validation is stocked, and you can get all errors by errors mehtods.
If you set stock_error 0, you can get only first error by errors method.
This is very high performance if you know only whether error occur or not.
$vc->stock_error(0);
$is_valid = $vc->validate($data, $validation_rule)->is_valid;
error_stock default is 1.
validation_rule
Set and get validation rule
$vc = $vc->validation_rule($validation_rule);
$validation_rule = $vc->validation_rule;
Validation rule has the following syntax
### Syntax of validation rule
my $validation_rule = [ # 1.Validation rule must be array ref
key1 => [ # 2.Constraints must be array ref
'constraint1_1', # 3.Constraint can be string
['constraint1_2', 'error1_2'],# or arrya ref (error message)
{'constraint1_3' => 'string'} # or hash ref (arguments)
],
key2 => [
{'constraint2_1' # 4.Argument can be string
=> 'string'}, #
{'constraint2_2' # or array ref
=> ['arg1', 'arg2']}, #
{'constraint1_3' # or hash ref
=> {k1 => 'v1', k2 => 'v2}} #
],
key3 => [
[{constraint3_1' => 'string'},# 5.Combination argument
'error3_1' ] # and error message
],
{ key4 => ['key4_1', 'key4_2'] } # 6.Multi key validation
=> [
'constraint4_1'
]
key5 => [
'@constraint5_1' # 7. array ref each value validation
]
];
You can see this syntax using 'syntax' method
print $vc->syntax;
Mehtods
new
Create object
$vc = Validator::Costom->new;
validate
validate data
# Validation
$result = $vc->validate($data, $validation_rule);
$result = $vc->validate($data);
If you omit $validation_rule, $vc->validation_rule is used.
See 'validation_rule' description about validation rule.
This method return Validator::Custom::Result object,
See also Validator::Custom::Result.
add_constraint
Add constraint
$vc->add_constraint($constraint); # hash or hash ref
'add_constraint' sample
$vc->add_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;
}
);
syntax
Set and get syntax of validation rule
$vc = $vc->syntax($syntax);
$syntax = $vc->syntax;
Validator::Custom::Result
'validate' method return Validator::Custom::Result object.
See Validator::Custom::Result.
The following is Validator::Custom::Result sample
# Restlt
$result = $vc->validate($data, $validation_rule);
# Error message
@errors = $result->errors;
# Invalid keys
@invalid_keys = $result->invalid_keys;
# Producted values
$products = $result->products;
$product = $products->{key1};
# Is it valid?
$is_valid = $result->is_valid;
Constraint function
You can resist your constraint function using 'add_constraint' method.
canstrant function can receive two argument.
1. value in validating data
2. argument passed in validation rule
I explain using sample. You can pass argument in validation rule. and you can receive the argument in constraint function
my $data = {key => 'value'}; # 1. value
my $validation_rule => {
key => [
{'name' => $args} # 2. arguments
],
}
$vc->add_constraint(name => sub {
my ($value, $args) = @_;
# ...
return $is_valid;
});
constraint function also can return producted value.
$vc->add_constraint(name => sub {
my ($value, $args) = @_;
# ...
return ($is_valid, $product);
});
Validator::Custom::HTML::Form 'time' constraint function is good sample.
Create custom class extending Validator::Custom
You can create your custom class extending Validator::Custom.
package Validator::Custom::Yours;
use base 'Validator::Custom';
__PACKAGE__->add_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;
}
);
This class is avalilable same way as Validator::Custom
$vc = Validator::Custom::Yours->new;
Validator::Custom::Trim, Validator::Custom::HTMLForm is good sample.
Author
Yuki Kimoto, <kimoto.yuki at gmail.com>
Github http://github.com/yuki-kimoto
I develope this module at http://github.com/yuki-kimoto/Validator-Custom
I also support at IRC irc.perl.org#validator-custom
Copyright & licence
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.