File Coverage

blib/lib/Syccess/Validator/Length.pm
Criterion Covered Total %
statement 31 33 93.9
branch 30 34 88.2
condition 14 18 77.7
subroutine 6 6 100.0
pod 0 2 0.0
total 81 93 87.1


line stmt bran cond sub pod time code
1             package Syccess::Validator::Length;
2             our $AUTHORITY = 'cpan:GETTY';
3             # ABSTRACT: A validator to check the length of the value in chars
4             $Syccess::Validator::Length::VERSION = '0.104';
5 2     2   2634 use Moo;
  2         4  
  2         6  
6 2     2   361 use Carp qw( croak );
  2         2  
  2         688  
7              
8             with qw(
9             Syccess::ValidatorSimple
10             );
11              
12             has min => (
13             is => 'ro',
14             predicate => 1,
15             );
16              
17             has max => (
18             is => 'ro',
19             predicate => 1,
20             );
21              
22             sub BUILD {
23 4     4 0 1073 my ( $self ) = @_;
24 4 50 33     33 croak __PACKAGE__.' cant have arg (specific length) and min/max'
      66        
25             if $self->has_arg && ( $self->has_min || $self->has_max );
26             }
27              
28             has message => (
29             is => 'lazy',
30             );
31              
32             sub _build_message {
33 4     4   15 my ( $self ) = @_;
34 4 100       9 if ($self->has_arg) {
35 1         12 return [ $self->format, $self->arg ];
36             } else {
37 3 100 100     15 if ($self->has_min && $self->has_max) {
    100          
    50          
38 1         14 return [ $self->format, $self->min, $self->max ];
39             } elsif ($self->has_min) {
40 1         14 return [ $self->format, $self->min ];
41             } elsif ($self->has_max) {
42 1         13 return [ $self->format, $self->max ];
43             }
44             }
45 0         0 croak __PACKAGE__.' needs an arg, min or max value';
46             }
47              
48             has format => (
49             is => 'lazy',
50             );
51              
52             sub _build_format {
53 4     4   15 my ( $self ) = @_;
54 4 100       6 if ($self->has_arg) {
55 1         7 return '%s must be exactly %s characters.';
56             } else {
57 3 100 100     14 if ($self->has_min && $self->has_max) {
    100          
    50          
58 1         7 return '%s must be between %s and %s characters.';
59             } elsif ($self->has_min) {
60 1         6 return '%s must be at least %s characters.';
61             } elsif ($self->has_max) {
62 1         6 return '%s is not allowed to be more than %s characters.';
63             }
64             }
65 0         0 croak __PACKAGE__.' needs an arg, min or max value';
66             }
67              
68             sub validator {
69 10     10 0 11 my ( $self, $value ) = @_;
70 10         10 my $length = length($value);
71 10 100       14 if ($self->has_arg) {
72 3 100       93 return $self->message if $length != $self->arg;
73             } else {
74 7 100 100     28 if ($self->has_min && $self->has_max) {
    100          
    50          
75 2 100 66     22 return $self->message if $length < $self->min || $length > $self->max;
76             } elsif ($self->has_min) {
77 3 100       32 return $self->message if $length < $self->min;
78             } elsif ($self->has_max) {
79 2 100       17 return $self->message if $length > $self->max;
80             }
81             }
82 4         14 return;
83             }
84              
85             1;
86              
87             __END__
88              
89             =pod
90              
91             =head1 NAME
92              
93             Syccess::Validator::Length - A validator to check the length of the value in chars
94              
95             =head1 VERSION
96              
97             version 0.104
98              
99             =head1 SYNOPSIS
100              
101             Syccess->new(
102             fields => [
103             pin => [ length => 4 ],
104             username => [ length => {
105             min => 3,
106             max => 12,
107             message => 'Username must be between %s and %s characters.'
108             } ],
109             tweet => [ length => { max => 140 } ],
110             ],
111             );
112              
113             =head1 DESCRIPTION
114              
115             This validator allows to check for the amount of characters in the value.
116             The default error message depends on the parameter given. The default
117             functionality is using the parameter as the required length for the value.
118             Longer or shorter would be denied. This can't be combined with L</min> or
119             L</max>.
120              
121             =head1 ATTRIBUTES
122              
123             =head2 min
124              
125             Given this parameter, allows to define a minimum length for the value. This
126             can be combined with L</max>.
127              
128             =head2 max
129              
130             Given this parameter, allows to define a maximum length for the value. This
131             can be combined with L</min>.
132              
133             =head2 message
134              
135             This contains the error message or the format for the error message
136             generation. See L<Syccess::Error/validator_message>.
137              
138             =encoding utf8
139              
140             =head1 SUPPORT
141              
142             IRC
143              
144             Join irc.perl.org and msg Getty
145              
146             Repository
147              
148             http://github.com/Getty/p5-syccess
149             Pull request and additional contributors are welcome
150              
151             Issue Tracker
152              
153             http://github.com/Getty/p5-syccess/issues
154              
155             =head1 AUTHOR
156              
157             Torsten Raudssus <torsten@raudss.us>
158              
159             =head1 COPYRIGHT AND LICENSE
160              
161             This software is copyright (c) 2017 by Torsten Raudssus.
162              
163             This is free software; you can redistribute it and/or modify it under
164             the same terms as the Perl 5 programming language system itself.
165              
166             =cut