blib/lib/Smolder/Constraints.pm untested

filter: Statementstmt 0.0% 0 / 101 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 165 CC 8 SCAR 15.0
CC
8
Cov
0%
unique_field_value
12.0
email
6.0
enum_value
6.0
CRAP 4.5
1 package Smolder::Constraints;
2 use strict;
3 use warnings;
4 use Smolder::DB;
5 use Smolder::DB::SmokeReport;
6 use Smolder::DB::Preference;
7 use Email::Valid;
8 use File::Basename;
9 use File::Temp;
10 use File::MMagic;
11 use File::Spec::Functions qw(catdir tmpdir);
12  
13 our @ISA = qw(Exporter);
14 our @EXPORT_OK = qw(
15 email
16 unsigned_int
17 bool
18 length_max
19 length_min
20 length_between
21 enum_value
22 unique_field_value
23 existing_field_value
24 file_mtype
25 smoke_report_tags
26 );
27  
28 =head1 NAME
29  
30 Smolder::Constraints
31  
32 =head1 SYNOPSIS
33  
34 use Smolder::Constraints qw(email unsigned_int max_length);
35  
36 # then in a D::FV profile
37 my $form = {
38 required => [qw(email id last_name)],
39 constraint_methods => {
40 email => email(),
41 id => unsigned_int(),
42 last_name => max_length(255),
43 }
44 };
45  
46 =head1 DESCRIPTION
47  
48 This package provides/exports several routines that are useful
49 inside of Smolder for form validation, using L<Data::FormValidator>.
50 Each routine will return something suitable for use inside of
51 a C<constraint_methods> hash.
52  
53 =head1 ROUTINES
54  
55 =head2 email
56  
57 Returns a method which validates an email address
58  
59 =cut
60  
61 sub email {
62 return sub {
63 my ($dfv, $value) = @_;
64 if (Email::Valid->address($value)) {
65 return $value;
66 } else {
67 return;
68 }
69 };
70 }
71  
72 =head2 unsigned_int
73  
74 Returns regex that assures the data is simply an unsigned integer
75  
76 =cut
77  
78 sub unsigned_int {
79 return qr/^\d+$/;
80 }
81  
82 =head2 bool
83  
84 Returns a regex that assures the data is either a '1' or a '0'
85  
86 =cut
87  
88 sub bool {
89 return qr/^1|0$/;
90 }
91  
92 =head2 length_max
93  
94 Given an integer $max, returns a regex that assures the data has
95 at most $max number of printable characters.
96  
97 =cut
98  
99 sub length_max {
100 my $max = shift;
101 return qr/^[[:print:]\s]{1,$max}$/;
102 }
103  
104 =head2 length_min
105  
106 Given an integer $min, returns a regex that assures the data has
107 at least $min number of printable characters.
108  
109 =cut
110  
111 sub length_min {
112 my $min = shift;
113 return qr/^[[:print:]\s]{$min,}$/;
114 }
115  
116 =head2 length_between
117  
118 Given an integer $min and an integer $max, returns a regex that assures the data has
119 at least $min and at most $max number of printable characters.
120  
121 =cut
122  
123 sub length_between {
124 my ($min, $max) = sort { $a <=> $b } @_;
125 return qr/^[[:print:]]{$min,$max}$/;
126 }
127  
128 =head2 enum_value
129  
130 Returns a method which will make sure that the value is an allowable
131 enum value for the given table and column.
132  
133 enum_value('table', 'column');
134  
135 =cut
136  
137 sub enum_value {
138 my ($table, $column) = @_;
139 my $enums = Smolder::DB->enum_values($table, $column);
140 return sub {
141 my ($dfv, $value) = @_;
142 foreach my $enum (@$enums) {
143 if ($enum eq $value) {
144 return $value;
145 }
146 }
147 return;
148 };
149 }
150  
151 =head2 unique_field_value
152  
153 Returns a method which will make sure that the value
154 being updated does not currently exist in the table and field
155 specified. Can be passed an optional integer value which is
156 used as the table's primary id to not compare against
157 (this is useful when editing an existing row when you don't
158 care it still has the same value or not).
159  
160 unique_field_value('project', 'name')
161  
162 or
163  
164 unique_field_value('developer', 'username', 23)
165  
166 =cut
167  
168 sub unique_field_value {
169 my ($table, $field, $id) = @_;
170  
171 return sub {
172 my ($dfv, $value) = @_;
173 $dfv->set_current_constraint_name("unique_${table}_${field}");
174  
175 # get all the values of a certain field
176 my $sql = "SELECT $field FROM $table WHERE $field = ?";
177 $sql .= " AND id != $id" if ($id);
178 my $sth = Smolder::DB->db_Main->prepare_cached($sql);
179 $sth->execute($value);
180 my $row = $sth->fetchrow_arrayref();
181 $sth->finish();
182 if ($row) {
183 return;
184 } else {
185 return $value;
186 }
187 }
188 }
189  
190 =head2 existing_field_value
191  
192 Returns a sub that will verifiy that a value exists in a particular
193 table in a particular column.
194  
195 existing_field_value('developer', 'id')
196  
197 =cut
198  
199 sub existing_field_value {
200 my ($table, $column) = @_;
201 return sub {
202 my ($dfv, $value) = @_;
203 my $sth = Smolder::DB->db_Main->prepare_cached(
204 qq(
205 SELECT $column FROM $table WHERE $column = ?
206 )
207 );
208 $sth->execute($value);
209 my $row = $sth->fetchrow_arrayref();
210 $sth->finish();
211 if (defined $row->[0]) {
212 return $value;
213 } else {
214 return;
215 }
216 }
217 }
218  
219 =head2 file_mtype
220  
221 Returns a sub that will validate that the file is one of the given MIME types.
222 If it is valid, it will return the name of the temporary file currently
223 being used.
224  
225 file_mtype('text/plain', 'image/jpg'),
226  
227 =cut
228  
229 sub file_mtype {
230 my @types = @_;
231 return sub {
232 my ($dfv, $filename) = @_;
233 my $fh = $dfv->get_input_data()->upload($dfv->get_current_constraint_field);
234 my ($suffix) = (basename($filename) =~ /(\..*)$/);
235  
236 # save the file to a temp location
237 my $tmp = File::Temp->new(
238 UNLINK => 0,
239 SUFFIX => ($suffix || '.tmp'),
240 DIR => tmpdir(),
241 ) or die "Could not create tmp file!";
242 while (my $line = <$fh>) {
243 print $tmp $line or die "Could not print to file '$tmp': $!";
244 }
245 close($tmp) or die "Could not close file '$tmp': $!";
246 close($fh) or die "Could not close upload FH: $!";
247  
248 # now get the file's mime-type
249 my $mm = File::MMagic->new();
250 my $type = $mm->checktype_filename($tmp->filename);
251 foreach my $t (@types) {
252 if ($t eq $type) {
253 return $tmp->filename;
254 }
255 }
256  
257 # if we got here then it wasn't valid, so remove the temp file
258 unlink($tmp->filename) or die "Could not remove file '$tmp': $!";
259 return;
260 }
261 }
262  
263 =head2 smoke_report_tags
264  
265 Returns a sub that will verifiy that a value is a comma separated list
266 of tags that are no more than 255 characters each. If they are valid,
267 then an array ref of the tags will be returned.
268  
269 smoke_report_tags()
270  
271 =cut
272  
273 sub smoke_report_tags {
274 return sub {
275 my ($dfv, $value) = @_;
276  
277 my @words = split(/\s*,\s*/, $value);
278 foreach my $word (@words) {
279 return if length $word > 255;
280 }
281 return \@words;
282 }
283 }
284  
285 1;