File Coverage

blib/lib/WWW/FieldValidator.pm
Criterion Covered Total %
statement 48 65 73.8
branch 10 20 50.0
condition 4 13 30.7
subroutine 12 15 80.0
pod 3 3 100.0
total 77 116 66.3


line stmt bran cond sub pod time code
1             package WWW::FieldValidator;
2              
3 2     2   145947 use 5.014;
  2         28  
4 2     2   11 use strict;
  2         4  
  2         41  
5 2     2   9 use warnings;
  2         3  
  2         88  
6              
7             our $VERSION = "1.19";
8              
9             # The following constants represent the validator types that can be used to
10             # validate the specified user input.
11 2     2   23 use constant WELL_FORMED_EMAIL => 1;
  2         4  
  2         215  
12 2     2   14 use constant MIN_STR_LENGTH => 2;
  2         13  
  2         118  
13 2     2   14 use constant MAX_STR_LENGTH => 5;
  2         4  
  2         85  
14 2     2   10 use constant REGEX_MATCH => 3;
  2         5  
  2         104  
15 2     2   13 use constant USER_DEFINED_SUB => 4;
  2         3  
  2         1524  
16              
17             # Create a new Validator object.
18             sub new {
19 2     2 1 170 my $class = shift;
20              
21             # The type of validation the instance will use
22 2         6 my $validatorType = shift;
23              
24             # The error feedback to return if the test data does not pass validation
25 2         4 my $feedback = shift;
26              
27 2         5 my $self = {};
28              
29 2         5 bless($self, $class);
30              
31 2         12 $self->{feedback} = $feedback;
32 2         6 $self->{validatorType} = $validatorType;
33              
34 2 50       14 if ($validatorType == MIN_STR_LENGTH) {
    100          
    50          
    50          
35              
36             # Set min length to 1 by default
37 0   0     0 my $minLength = shift || 1;
38 0         0 $self->{minLength} = $minLength;
39              
40             }
41             elsif ($validatorType == REGEX_MATCH) {
42              
43 1         2 my $regex = shift;
44 1         3 $self->{regex} = $regex;
45              
46             }
47             elsif ($validatorType == USER_DEFINED_SUB) {
48              
49 0         0 my $subRef = shift;
50 0         0 $self->{usub} = $subRef;
51              
52             }
53             elsif ($validatorType == MAX_STR_LENGTH) {
54              
55             # Set max_length to 30 by default
56 0   0     0 my $maxLength = shift || 30;
57 0         0 $self->{maxLength} = $maxLength;
58             }
59              
60             # is_optional - if true, field is only validated if it's not empty
61 2   50     23 my $isOptional = shift || 0;
62 2         6 $self->{isOptional} = $isOptional;
63              
64 2         18 return $self;
65             }
66              
67             #-----------------------------------------------------------------------------
68             # These methods should really be treated as private because the Form module
69             # handles calling these methods, for most purposes all you will need to know
70             # how to do is to instantiate FieldValidators.
71             #-----------------------------------------------------------------------------
72              
73             # Checks to see what the validation type of the instance is and conditionally
74             # calls the appropriate validation method.
75             sub validate {
76 13     13 1 2298 my $self = shift;
77 13         22 my $input = shift; # User entered data
78              
79             # If the field is optional and is empty, don't validate
80 13 50 33     38 return 1 if ($self->{isOptional} && !$input);
81              
82 13 100       40 if ($self->{validatorType} == WELL_FORMED_EMAIL) {
    50          
    50          
    0          
    0          
83              
84 2         8 return $self->_validateEmail($input);
85              
86             }
87             elsif ($self->{validatorType} == MIN_STR_LENGTH) {
88              
89 0         0 return $self->_validateStrLength($input, $self->{minLength});
90              
91             }
92             elsif ($self->{validatorType} == REGEX_MATCH) {
93              
94 11         29 return $self->_validateRegex($input, $self->{regex});
95              
96             }
97             elsif ($self->{validatorType} == USER_DEFINED_SUB) {
98              
99 0         0 return $self->{usub}($input);
100              
101             }
102             elsif ($self->{validatorType} == MAX_STR_LENGTH) {
103              
104 0         0 return $self->_validateMaxStrLength($input, $self->{maxLength});
105             }
106             }
107              
108             sub getFeedback {
109 0     0 1 0 my $self = shift;
110 0         0 return $self->{feedback};
111             }
112              
113             *get_feedback = \&getFeedback;
114              
115             # Checks to see if input is a well formed email address.
116             # TODO: Maybe use Email::Valid for this?
117             sub _validateEmail {
118 2     2   4 my $self = shift;
119 2   50     6 my $input = shift || '';
120 2         22 return ($input =~ /^[\w\-\.\+]+@([\w\-]+)(\.([\w\-]+))+$/);
121             }
122              
123             # Checks to see if input is a minimum string length.
124             sub _validateStrLength {
125 0     0   0 my $self = shift;
126 0         0 my $input = shift;
127 0         0 return (length($input) >= $self->{minLength});
128             }
129              
130             sub _validateMaxStrLength {
131 0     0   0 my $self = shift;
132 0         0 my $input = shift;
133 0         0 return (length($input) <= $self->{maxLength});
134             }
135              
136             # Checks to see if input matches the specified pattern.
137             sub _validateRegex {
138 11     11   13 my $self = shift;
139 11   50     22 my $input = shift // '';
140              
141 11         95 return ($input =~ /$self->{regex}/);
142             }
143              
144             1;
145              
146             __END__