File Coverage

blib/lib/Syccess/ValidatorSimple.pm
Criterion Covered Total %
statement 10 12 83.3
branch 5 8 62.5
condition 7 18 38.8
subroutine 3 5 60.0
pod 0 4 0.0
total 25 47 53.1


line stmt bran cond sub pod time code
1             package Syccess::ValidatorSimple;
2             our $AUTHORITY = 'cpan:GETTY';
3             # ABSTRACT: Syccess validator
4             $Syccess::ValidatorSimple::VERSION = '0.104';
5 7     7   2409 use Moo::Role;
  7         7  
  7         38  
6              
7             with qw(
8             Syccess::Validator
9             );
10              
11             requires qw(
12             validator
13             );
14              
15             sub validate {
16 31     31 0 46 my ( $self, %params ) = @_;
17 31         55 my $name = $self->syccess_field->name;
18 31 100 66     74 return if !exists($params{$name})
19             && $self->missing_ok;
20             return if exists($params{$name})
21 28 50 33     131 && !defined($params{$name})
      33        
22             && $self->undef_ok;
23             return if exists($params{$name})
24             && defined($params{$name})
25 28 50 33     150 && $params{$name} eq ''
      33        
      33        
26             && $self->empty_ok;
27             return exists($params{$name})
28 28 50       83 ? $self->validator($params{$name})
29             : $self->validator();
30             }
31              
32 3     3 0 13 sub missing_ok { 1 }
33 0     0 0   sub undef_ok { 1 }
34 0     0 0   sub empty_ok { 1 }
35              
36             1;
37              
38             __END__
39              
40             =pod
41              
42             =head1 NAME
43              
44             Syccess::ValidatorSimple - Syccess validator
45              
46             =head1 VERSION
47              
48             version 0.104
49              
50             =head1 SYNOPSIS
51              
52             package MyValidators::Custom;
53              
54             use Moo;
55              
56             with qw(
57             Syccess::ValidatorSimple
58             );
59              
60             sub validator {
61             my ( $self, $value ) = @_;
62             return if $value eq 'ok';
63             return 'Your value for %s is not ok.';
64             }
65              
66             sub missing_ok { 1 }
67             sub undef_ok { 1 }
68             sub empty_ok { 1 }
69              
70             1;
71              
72             =head1 DESCRIPTION
73              
74             Please first see L<Syccess::Validator>. This role is a wrapper around it,
75             which requires a function B<validator>, which will be called with the value
76             given on the parameters for the field where the validator is used. By default,
77             it ignores a not existing value, an undefined value or an empty string. You
78             can override this behaviour by overloading the functions B<missing_ok>,
79             B<undef_ok> or B<empty_ok> with a sub that returns a false value. Then this
80             specific case will still be dispatched to the B<validator> function and can
81             then there produce an error, or not ;).
82              
83             If the value is missing, then B<@_> will only contain a reference to the
84             validator object, but no value itself (if you override B<missing_ok>).
85              
86             =encoding utf8
87              
88             =head1 SUPPORT
89              
90             IRC
91              
92             Join irc.perl.org and msg Getty
93              
94             Repository
95              
96             http://github.com/Getty/p5-syccess
97             Pull request and additional contributors are welcome
98              
99             Issue Tracker
100              
101             http://github.com/Getty/p5-syccess/issues
102              
103             =head1 AUTHOR
104              
105             Torsten Raudssus <torsten@raudss.us>
106              
107             =head1 COPYRIGHT AND LICENSE
108              
109             This software is copyright (c) 2017 by Torsten Raudssus.
110              
111             This is free software; you can redistribute it and/or modify it under
112             the same terms as the Perl 5 programming language system itself.
113              
114             =cut