File Coverage

blib/lib/Slovo/Validator.pm
Criterion Covered Total %
statement 19 28 67.8
branch 0 2 0.0
condition n/a
subroutine 5 6 83.3
pod 1 1 100.0
total 25 37 67.5


line stmt bran cond sub pod time code
1             package Slovo::Validator;
2 14     14   92 use Mojo::Base 'Mojolicious::Validator', -signatures;
  14         25  
  14         85  
3              
4             # can this $name with $value do $sub with @args?
5 4     4   258 my sub _can ($v, $name, $value, $sub, @args) {
  4         9  
  4         8  
  4         7  
  4         7  
  4         9  
  4         6  
6 4         20 return !$sub->($v, $name, $value, @args);
7             }
8              
9             sub new {
10 4     4 1 41 my $self = shift->SUPER::new(@_);
11              
12             # new filters
13 4     4   1102 $self->add_filter(xml_escape => sub { Mojo::Util::xml_escape($_[2]) });
  4         103  
14 4     4   92 $self->add_filter(slugify => sub { Mojo::Util::slugify($_[2], 1) });
  4         95  
15              
16             # new checks
17 4         35 $self->add_check(is => \&_can);
18 0           $self->add_check(
19 0     0   0 equals => sub ($v, $name, $value, $eq) {
  0            
  0            
  0            
  0            
20 0 0       0 unless ($value eq $eq) {
21 0         0 return 1;
22             }
23 0         0 return;
24 4         169 });
25 4         156 return $self;
26             }
27              
28             1;
29              
30             =encoding utf8
31              
32             =head1 NAME
33              
34             Slovo::Validator - additional validator filters and checks
35              
36             =head1 CHECKS
37              
38             Slovo::Validator inherits all checks from Mojolicious::Validator and implements
39             the following new ones.
40              
41             =head2 is
42              
43             A custom check -- some code reference which returns true on success, false
44             otherwise.
45              
46             # in the action
47             $v->required('id')->is(\&_writable_by, $c->stranici, $c->user);
48              
49             # in the same or parent controller
50             sub _writable_by ($v, $id_name, $id_value, $m, $user) {
51             return !!$m->find_where({$id_name => $id_value, %{$m->writable_by($user)}});
52             }
53              
54             # or simply
55             $v->required('sum')->is(sub($v, $name, $value) {
56             $v->param('one') + $v->param('two') == $value
57             });
58              
59             =head1 FILTERS
60              
61             Slovo::Validator inherits all filters from Mojolicious::Validator and
62             implements the following new ones.
63              
64             =head2 slugify
65              
66             $v->required('alias', 'slugify')->size(0, 255);
67              
68             Generate URL slug for bytestream with L.
69              
70             =head2 xml_escape
71              
72             $c->validation->optional(title => xml_escape => 'trim')->size(10, 255);
73              
74             Uses L to escape unsafe characters. Returns the escaped
75             string.
76              
77             =head1 SEE ALSO
78              
79             L, L
80              
81             =cut
82