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