blib/lib/Slovo/Validator.pm

filter: Statementstmt 62.5% 10 / 16 Branchbran 0.0% 0 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 33.3% 2 / 6 Podpod 100% 1 / 1 totaltotal 52.0% 13 / 25 CC 2 SCAR 9.7
CC
2
Cov
56%
__ANON__
6.0
_can
2.0
new
1.2
CRAP 2.6
« blib/lib/Slovo/Cache.pm ↑ Summary blib/lib/Slovo/Controller.pm »
1 package Slovo::Validator;
2 15 ▶use Mojo::Base 'Mojolicious::Validator', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 # can this $name with $value do $sub with @args?
5 0 ▶my sub _can ($v, $name, $value, $sub, @args) {
Subroutine _can: not called CC=1 SCAR=6.9
6 0 return !$sub->($v, $name, $value, @args);
7 }
8  
9 4 ▶sub new {
Subroutine new: called CC=1 SCAR=1.5
Pod documented
10 4 my $self = shift->SUPER::new(@_);
11  
12 # new filters
13 0 ▶ $self->add_filter(xml_escape => sub { Mojo::Util::xml_escape($_[2]) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
14 0 ▶ $self->add_filter(slugify => sub { Mojo::Util::slugify($_[2], 1) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
15  
16 # new checks
17 4 $self->add_check(is => \&_can);
18 $self->add_check(
19 0 ▶ equals => sub ($v, $name, $value, $eq) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
20 0 ▶ unless ($value eq $eq) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value eq $eq) 0 0 0
21 0 return 1;
22 }
23 0 return;
24 4 });
25 4 return $self;
26 }
27  
28 15 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<Mojo::Util/"slugify">.
69  
70 =head2 xml_escape
71  
72 $c->validation->optional(title => xml_escape => 'trim')->size(10, 255);
73  
74 Uses L<Mojo::Util/xml_escape> to escape unsafe characters. Returns the escaped
75 string.
76  
77 =head1 SEE ALSO
78  
79 L<Mojolicious::Validator>, L<Mojolicious::Guides::Rendering/Form-validation>
80  
81 =cut
82  
« blib/lib/Slovo/Cache.pm ↑ Summary blib/lib/Slovo/Controller.pm »