NAME
Validator::Custom - Custom validator
VERSION
Version 0.0207
CAUTION
Validator::Custom is yew experimental stage.
SYNOPSIS
### How to use Validator::Custom
# data
my $hash = { title => 'aaa', content => 'bbb' };
# validator functions
my $validators = [
title => [
[sub{$_[0]}, "Specify title"],
[sub{length $_[0] < 128}, "Too long title"]
],
content => [
[sub{$_[0]}, "Specify content"],
[sub{length $_[0] < 1024}, "Too long content"]
]
];
# validate
my $vc = Validator::Custom->new;
my @errors = $vc->validate($hash,$validators)->errors;
# or
my $vc = Validator::Custom->new( validators => $validators);
my @errors = $vc->validate($hash)->errors;
# process in error case
if($errors){
foreach my $error (@$errors) {
# process all errors
}
}
### How to costomize Validator::Custom
package Validator::Custom::Yours;
use base 'Validator::Custom';
# regist custom type
__PACKAGE__->add_constraint(
Int => sub {$_[0] =~ /^\d+$/},
Num => sub {
require Scalar::Util;
Scalar::Util::looks_like_number($_[0]);
},
Str => sub {!ref $_[0]}
);
### How to use customized validator class
use Validator::Custom::Yours;
my $hash = { age => 'aaa', weight => 'bbb', favarite => [qw/sport food/};
my $validators = [
title => [
['Int', "Must be integer"],
],
content => [
['Num', "Must be number"],
],
favorite => [
['@Str', "Must be string"]
]
];
my $vc = Validator::Custom::Yours->new;
my $errors = $vc->validate($hash,$validators)->errors;
# corelative check
my $validators => [
[qw/password1 password2/] => [
['Same', 'passwor is not same']
]
]
CLASS METHOD
constraints
get constraints
# get
my $constraints = Validator::Custom::Your->constraints;
add_constraint
You can use this method in custom class. New validator functions is added.
package Validator::Custom::Yours;
use base 'Validator::Custom';
__PACKAGE__->add_constraint(
Int => sub {$_[0] =~ /^\d+$/}
);
You can merge multiple custom class
package Validator::Custom::YoursNew;
use base 'Validator::Custom';
use Validator::Custum::Yours1;
use Validatro::Cumtum::Yours2;
__PACAKGE__->add_constraint(
%{Validator::Custom::Yours1->constraints},
%{Validator::Custom::Yours2->constraints}
);
ACCESSORS
errors
You can get validating errors
my @errors = $vc->errors;
You can use this method after calling validate
my @errors = $vc->validate($hash,$validators)->errors;
invalid_keys
You can get invalid keys by hash
my $invalid_keys = $c->invalid_keys;
You can use this method after calling validate
my $invalid_keys = $vc->validate($hash,$validators)->invalid_keys;
invalid_positions
You can get invalid position by hash
my $invalid_positions = $c->invalid_positions;
You can use this method after calling validate
my $invalid_positions = $vc->validate($hash,$validators)->invalid_positions;
error_stock
If you stock error, set 1, or set 0.
Default is 1.
validators
You can set validators
$vc->validators($validators);
results
You can get converted result if any.
$vc->results
METHOD
new
create instance
my $vc = Validator::Costom->new;
validate
validate
$vc->validate($hash,$validators);
validator format is like the following.
my $validators = [
# Function
key1 => [
[ \&validator_function1, "Error message1-1"],
[ \&validator_function2, "Error message1-2"]
],
# Custom Type
key2 => [
[ 'CustomType' , "Error message2-1"],
],
# Array of Custom Type
key3 => [
[ '@CustomType', "Error message3-1"]
]
];
this method retrun self.
AUTHOR
Yuki Kimoto, <kimoto.yuki at gmail.com>
BUGS
Please report any bugs or feature requests to bug-validator-custom at rt.cpan.org
, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Validator-Custom. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Validator::Custom
You can also look for information at:
RT: CPAN's request tracker
AnnoCPAN: Annotated CPAN documentation
CPAN Ratings
Search CPAN
ACKNOWLEDGEMENTS
COPYRIGHT & LICENSE
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.