Name
QBit::Validator - It is used for validation of input parameters.
GitHub
https://github.com/QBitFramework/QBit-Validator
Install
cpanm QBit::Validator
Package methods
new
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,
},
},
},
);
template
get or set template (Use only into pre_run)
Example:
my $template = $qv->template;
$qv->template($template);
data
set or get data
Example:
$self->db->table->edit($qv->data) unless $qv->has_errors;
validate
set data and validate it
Example:
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;
}
has_errors
return boolean result (TRUE if an error has occurred or FALSE)
Example:
if ($qv->has_errors) {
...
}
has_error
return boolean result (TRUE if an error has occurred in field or FALSE)
Example:
$qv->get_error('field') if $qv->has_error('field');
$qv->get_error('/field') if $qv->has_error('/field');
get_error
return error by path (string or array)
Example:
if ($qv->has_errors) {
my $error = $qv->get_error('hello');
#or '/hello'
print $error; # 'Error'
}
get_fields_with_error
return list fields with error
Example:
if ($qv->has_errors) {
my @fields = $qv->get_fields_with_error;
ldump(\@fields);
# [
# {
# messsage => 'Error',
# path => '/hello/'
# }
# ]
#
# path => '/' - error in root
}
get_all_errors
return all errors join "\n"
Example:
if ($qv->has_errors) {
my $errors = $qv->get_all_errors();
print $errors; # 'Error'
}
throw_exception
throw Exception::Validator with error message from get_all_errors
Example:
$qv->throw_exception if $qv->has_errors;
Default types
scalar (string/number)
optional
eq
regexp
min
max
len_min
len
len_max
in
For more information see tests
array (ref array)
optional
size_min
size
size_max
all
contents
For more information see tests
hash (ref hash)
optional
deps
fields
extra
one_of
any_of
For more information see tests
variable
conditions
For more information see tests