QBit::Validator - It is used for validation of input parameters.
https://github.com/QBitFramework/QBit-Validator
cpanm QBit::Validator
create object QBit::Validator and check data using template
Arguments:
data - checking data
template - template for check
pre_run - function is executed before checking (deprecated)
app - model using in check
throw - throw (boolean type, throw exception if an error has occurred)
sys_errors_handler - handler for system errors in sub "check" (default empty function: sub {})
# global set handler $QBit::Validator::SYS_ERRORS_HANDLER = sub {log($_[0])}; # first argument is error #or local set handler my $qv = QBit::Validator->new(template => {}, sys_errors_handler => sub {log($_[0])});
path_manager - path manager (default QBit::Validator::PathManager)
# global set path_manager $QBit::Validator::PATH_MANAGER = 'MyPathManager::For::Data::DPath'; #or local set path_manager my $qv = QBit::Validator->new(template => {}, path_manager => MyPathManager::For::Data::DPath->new()});
path - data path for validator (see: QBit::Validator::PathManager)
parent - ref to a parent validator
Example:
my $data = { hello => 'hi, qbit-validator' }; my $qv = QBit::Validator->new( data => $data, template => { type => 'hash', fields => { hello => { max_len => 5, }, }, }, );
get or set template (Use only into pre_run)
my $template = $qv->template; $qv->template($template);
set or get data
$self->db->table->edit($qv->data) unless $qv->has_errors;
set data and validate it
my $qv = QBit::Validator->new( template => { type => 'scalar', min => 50, max => 60, } ); foreach (45 .. 65) { $qv->validate($_); print $qv->get_error() if $qv->has_errors; }
return boolean result (TRUE if an error has occurred or FALSE)
if ($qv->has_errors) { ... }
return boolean result (TRUE if an error has occurred in field or FALSE)
$qv->get_error('field') if $qv->has_error('field'); $qv->get_error('/field') if $qv->has_error('/field');
return error by path (string or array)
if ($qv->has_errors) { my $error = $qv->get_error('hello'); #or '/hello' print $error; # 'Error' }
return list fields with error
if ($qv->has_errors) { my @fields = $qv->get_fields_with_error; ldump(\@fields); # [ # { # messsage => 'Error', # path => '/hello/' # } # ] # # path => '/' - error in root }
return all errors join "\n"
if ($qv->has_errors) { my $errors = $qv->get_all_errors(); print $errors; # 'Error' }
throw Exception::Validator with error message from get_all_errors
$qv->throw_exception if $qv->has_errors;
optional
eq
regexp
min
max
len_min
len
len_max
in
For more information see tests
size_min
size
size_max
all
contents
deps
fields
extra
one_of
any_of
conditions
To install QBit::Validator, copy and paste the appropriate command in to your terminal.
cpanm
CPAN shell
perl -MCPAN -e shell install QBit::Validator
For more information on module installation, please visit the detailed CPAN module installation guide.