File Coverage

blib/lib/Syccess/Validator/Code.pm
Criterion Covered Total %
statement 14 14 100.0
branch 3 4 75.0
condition 2 6 33.3
subroutine 3 3 100.0
pod 0 1 0.0
total 22 28 78.5


line stmt bran cond sub pod time code
1             package Syccess::Validator::Code;
2             our $AUTHORITY = 'cpan:GETTY';
3             # ABSTRACT: A validator to check a value through a simple coderef
4             $Syccess::Validator::Code::VERSION = '0.104';
5 2     2   2236 use Moo;
  2         2  
  2         8  
6              
7             with qw(
8             Syccess::Validator
9             );
10              
11             has message => (
12             is => 'lazy',
13             );
14              
15             sub _build_message {
16 1     1   8 return 'Your value for %s is not valid.';
17             }
18              
19             sub validate {
20 6     6 0 10 my ( $self, %params ) = @_;
21 6         12 my $name = $self->syccess_field->name;
22             return if !exists($params{$name})
23             || !defined($params{$name})
24 6 50 33     35 || $params{$name} eq '';
      33        
25 6         6 my $value = $params{$name};
26 6         7 my $code = $self->arg;
27 6         5 my @return;
28 6         6 for ($value) {
29 6         10 push @return, $code->($self,%params);
30             }
31 6 100       27 return map { !defined $_ ? $self->message : $_ } @return;
  4         55  
32             }
33              
34             1;
35              
36             __END__
37              
38             =pod
39              
40             =head1 NAME
41              
42             Syccess::Validator::Code - A validator to check a value through a simple coderef
43              
44             =head1 VERSION
45              
46             version 0.104
47              
48             =head1 SYNOPSIS
49              
50             Syccess->new(
51             fields => [
52             foo => [ code => sub { $_ > 3 ? () : ('You are WRONG!') } ],
53             bar => [ code => {
54             arg => sub { $_ > 5 ? () : (undef) },
55             message => 'You have 5 seconds to comply.'
56             } ],
57             ],
58             );
59              
60             =head1 DESCRIPTION
61              
62             This validator allows checking against a CodeRef. The CodeRef will be getting
63             all parameters on B<@_> as Hash, and the specific parameter value for to check
64             against will be in B<$_>, so the coderef can decide which way he want to check.
65              
66             The CodeRef should give back nothing (not even B<undef>) if its a success. Else
67             if should give back B<undef> to release the error message given on L</message>
68             or the default error message B<'Your value for %s is not valid.'>. Alternative
69             it can also give back a string which will be used as B<message> for the error.
70              
71             =head1 ATTRIBUTES
72              
73             =head2 message
74              
75             This contains the error message or the format for the error message
76             generation. See L<Syccess::Error/validator_message>.
77              
78             =encoding utf8
79              
80             =head1 SUPPORT
81              
82             IRC
83              
84             Join irc.perl.org and msg Getty
85              
86             Repository
87              
88             http://github.com/Getty/p5-syccess
89             Pull request and additional contributors are welcome
90              
91             Issue Tracker
92              
93             http://github.com/Getty/p5-syccess/issues
94              
95             =head1 AUTHOR
96              
97             Torsten Raudssus <torsten@raudss.us>
98              
99             =head1 COPYRIGHT AND LICENSE
100              
101             This software is copyright (c) 2017 by Torsten Raudssus.
102              
103             This is free software; you can redistribute it and/or modify it under
104             the same terms as the Perl 5 programming language system itself.
105              
106             =cut