File Coverage

blib/lib/Syccess/ValidatorSimple.pm
Criterion Covered Total %
statement 11 13 84.6
branch 5 8 62.5
condition 7 18 38.8
subroutine 4 6 66.6
pod 0 4 0.0
total 27 49 55.1


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