File Coverage

blib/lib/Data/FormValidator/Filters.pm
Criterion Covered Total %
statement 78 89 87.6
branch 30 36 83.3
condition 1 2 50.0
subroutine 23 24 95.8
pod 2 19 10.5
total 134 170 78.8


line stmt bran cond sub pod time code
1             # Filters.pm - Common filters for use in Data::FormValidator.
2             # This file is part of Data::FormValidator.
3             #
4             # Author: Francis J. Lacoste
5             # Maintainer: Mark Stosberg
6             #
7             # Copyright (C) 1999,2000 iNsu Innovations Inc.
8             #
9             # This program is free software; you can redistribute it and/or modify
10             # it under the terms same terms as perl itself.
11              
12             package Data::FormValidator::Filters;
13 62     62   14239 use Exporter 'import';
  62         75  
  62         1803  
14 62     62   209 use strict;
  62         74  
  62         54245  
15              
16             our $VERSION = 4.86;
17              
18             our @EXPORT_OK = qw(
19             filter_alphanum
20             filter_decimal
21             filter_digit
22             filter_dollars
23             filter_integer
24             filter_lc
25             filter_neg_decimal
26             filter_neg_integer
27             filter_phone
28             filter_pos_decimal
29             filter_pos_integer
30             filter_quotemeta
31             filter_sql_wildcard
32             filter_strip
33             filter_trim
34             filter_uc
35             filter_ucfirst
36             FV_split
37             FV_replace
38             );
39              
40             our %EXPORT_TAGS = (
41             filters => \@EXPORT_OK,
42             );
43              
44       0     sub DESTROY {}
45              
46             =pod
47              
48             =head1 NAME
49              
50             Data::FormValidator::Filters - Basic set of filters available in an Data::FormValidator profile.
51              
52             =head1 SYNOPSIS
53              
54             use Data::FormValidator;
55              
56             %profile = (
57             filters => 'trim',
58             ...
59             );
60              
61             my $results = Data::FormValidator->check( \%data, \%profile );
62              
63             =head1 DESCRIPTION
64              
65             These are the builtin filters which may be specified as a name in the
66             I, I, and I parameters of the
67             input profile.
68              
69             Filters are applied as the first step of validation, possibly modifying a copy
70             of the validation before any constraints are checked.
71              
72             =head1 RECOMMENDED USE
73              
74             As a long time maintainer and user of Data::FormValidator, I recommend that
75             filters be used with caution. They are immediately modifying the input
76             provided, so the original data is lost. The few I recommend include C,
77             which removes leading and trailing whitespace. I have this turned on by default
78             by using L. It's also generally safe to use
79             the C and C filters if you need that kind of data transformation.
80              
81             Beyond simple filters, I recommend transforming the C<"valid"> hash returned
82             from validation if further changes are needed.
83              
84             =head1 PROCEDURAL INTERFACE
85              
86             You may also call these functions directly through the
87             procedural interface by either importing them directly or importing the whole
88             I<:filters> group. For example, if you want to access the I function
89             directly, you could either do:
90              
91             use Data::FormValidator::Filters (qw/filter_trim/);
92             # or
93             use Data::FormValidator::Filters (qw/:filters/);
94              
95             $string = filter_trim($string);
96              
97             Notice that when you call filters directly, you'll need to prefix the filter name with
98             "filter_".
99              
100             =head1 THE FILTERS
101              
102             =head2 FV_split
103              
104             use Data::FormValidator::Filters qw(FV_split);
105              
106             # Validate every e-mail in a comma separated list
107              
108             field_filters => {
109             several_emails => FV_split(qr/\s*,\s*/),
110              
111             # Any pattern that can be used by the 'split' builtin works.
112             tab_sep_field => FV_split('\t'),
113             },
114             constraint_methods => {
115             several_emails => email(),
116             },
117              
118             With this filter, you can split a field into multiple values. The constraint for
119             the field will then be applied to every value.
120              
121             This filter has a different naming convention because it is a higher-order
122             function. Rather than returning a value directly, it returns a code reference
123             to a standard Data::FormValidator filter.
124              
125             After successfully being validated the values will appear as an arrayref.
126              
127             =cut
128              
129             sub FV_split {
130 1   50 1 1 5 my $splitter = shift || die "nothing to split on!";
131             return sub {
132 3     3   5 my $value = shift;
133 3 100       8 return undef unless defined $value;
134 2         20 my @a = split $splitter, $value;
135 2         10 return \@a;
136 1         5 };
137             }
138              
139             =head2 FV_replace
140              
141             use Data::FormValidator::Filters qw(FV_replace);
142              
143             field_filters => {
144             first_name => FV_replace(qr/Mark/,'Don'),
145             },
146              
147             FV_replace is a shorthand for writing simple find-and-replace filters.
148             The above filter would be translated to this:
149              
150             sub { my $v = shift; $v =~ s/Mark/Don/; $v }
151              
152             For more complex filters, just write your own.
153              
154             =cut
155              
156             sub FV_replace {
157 2     2 1 246 my ($find,$replace) = @_;
158             return sub {
159 3     3   10 my $v = shift;
160 3         13 $v =~ s/$find/$replace/;
161 3         9 return $v;
162 2         7 };
163             }
164              
165             =head2 trim
166              
167             Remove white space at the front and end of the fields.
168              
169             =cut
170              
171             sub filter_trim {
172 23     23 0 186 my $value = shift;
173 23 100       35 return unless defined $value;
174              
175             # Remove whitespace at the front
176 22         44 $value =~ s/^\s+//o;
177              
178             # Remove whitespace at the end
179 22         47 $value =~ s/\s+$//o;
180              
181 22         56 return $value;
182             }
183              
184             =pod
185              
186             =head2 strip
187              
188             Runs of white space are replaced by a single space.
189              
190             =cut
191              
192             sub filter_strip {
193 1     1 0 162 my $value = shift;
194 1 50       3 return unless defined $value;
195              
196             # Strip whitespace
197 0         0 $value =~ s/\s+/ /g;
198              
199 0         0 return $value;
200             }
201              
202             =pod
203              
204             =head2 digit
205              
206             Remove non digits characters from the input.
207              
208             =cut
209              
210             sub filter_digit {
211 1     1 0 177 my $value = shift;
212 1 50       4 return unless defined $value;
213              
214 0         0 $value =~ s/\D//g;
215              
216 0         0 return $value;
217             }
218              
219             =pod
220              
221             =head2 alphanum
222              
223             Remove non alphanumeric characters from the input.
224              
225             =cut
226              
227             sub filter_alphanum {
228 1     1 0 163 my $value = shift;
229 1 50       3 return unless defined $value;
230 0         0 $value =~ s/\W//g;
231 0         0 return $value;
232             }
233              
234             =pod
235              
236             =head2 integer
237              
238             Extract from its input a valid integer number.
239              
240             =cut
241              
242             sub filter_integer {
243 2     2 0 170 my $value = shift;
244 2 100       7 return unless defined $value;
245 1         1 $value =~ tr/0-9+-//dc;
246 1         3 ($value) =~ m/([-+]?\d+)/;
247 1         3 return $value;
248             }
249              
250             =pod
251              
252             =head2 pos_integer
253              
254             Extract from its input a valid positive integer number.
255              
256             Bugs: This filter won't extract "9" from "a9+", it will instead extract "9+"
257              
258             =cut
259              
260             sub filter_pos_integer {
261 2     2 0 559 my $value = shift;
262 2 100       6 return unless defined $value;
263 1         2 $value =~ tr/0-9+//dc;
264 1         3 ($value) =~ m/(\+?\d+)/;
265 1         4 return $value;
266             }
267              
268             =pod
269              
270             =head2 neg_integer
271              
272             Extract from its input a valid negative integer number.
273              
274             Bugs: This filter will currently filter the case of "a9-" to become "9-",
275             which it should leave it alone.
276              
277             =cut
278              
279             sub filter_neg_integer {
280 2     2 0 163 my $value = shift;
281 2 100       7 return unless defined $value;
282 1         2 $value =~ tr/0-9-//dc;
283 1         2 ($value) =~ m/(-\d+)/;
284 1         2 return $value;
285             }
286              
287             =pod
288              
289             =head2 decimal
290              
291             Extract from its input a valid decimal number.
292              
293             Bugs: Given "1,000.23", it will currently return "1.000.23"
294              
295             =cut
296              
297             sub filter_decimal {
298 2     2 0 163 my $value = shift;
299 2 100       7 return unless defined $value;
300             # This is a localization problem, but anyhow...
301 1         2 $value =~ tr/,/./;
302 1         2 $value =~ tr/0-9.+-//dc;
303 1         3 ($value) =~ m/([-+]?\d+\.?\d*)/;
304 1         10 return $value;
305             }
306              
307             =pod
308              
309             =head2 pos_decimal
310              
311             Extract from its input a valid positive decimal number.
312              
313             Bugs: Given "1,000.23", it will currently return "1.000.23"
314              
315             =cut
316              
317             sub filter_pos_decimal {
318 2     2 0 171 my $value = shift;
319 2 100       7 return unless defined $value;
320             # This is a localization problem, but anyhow...
321 1         1 $value =~ tr/,/./;
322 1         2 $value =~ tr/0-9.+//dc;
323 1         2 ($value) =~ m/(\+?\d+\.?\d*)/;
324 1         3 return $value;
325             }
326              
327             =pod
328              
329             =head2 neg_decimal
330              
331             Extract from its input a valid negative decimal number.
332              
333             Bugs: Given "1,000.23", it will currently return "1.000.23"
334              
335             =cut
336              
337             sub filter_neg_decimal {
338 2     2 0 165 my $value = shift;
339 2 100       6 return unless defined $value;
340             # This is a localization problem, but anyhow...
341 1         2 $value =~ tr/,/./;
342 1         1 $value =~ tr/0-9.-//dc;
343 1         4 ($value) =~ m/(-\d+\.?\d*)/;
344 1         2 return $value;
345             }
346              
347             =pod
348              
349             =head2 dollars
350              
351             Extract from its input a valid number to express dollars like currency.
352              
353             Bugs: This filter won't currently remove trailing numbers like "1.234".
354              
355             =cut
356              
357             sub filter_dollars {
358 3     3 0 394 my $value = shift;
359 3 100       11 return unless defined $value;
360 2         4 $value =~ tr/,/./;
361 2         3 $value =~ tr/0-9.+-//dc;
362 2         6 ($value) =~ m/(\d+\.?\d?\d?)/;
363 2         5 return $value;
364             }
365              
366             =pod
367              
368             =head2 phone
369              
370             Filters out characters which aren't valid for an phone number. (Only
371             accept digits [0-9], space, comma, minus, parenthesis, period and pound [#].)
372              
373             =cut
374              
375             sub filter_phone {
376 1     1 0 166 my $value = shift;
377 1 50       3 return unless defined $value;
378 0         0 $value =~ s/[^\d,\(\)\.\s,\-#]//g;
379 0         0 return $value;
380             }
381              
382             =pod
383              
384             =head2 sql_wildcard
385              
386             Transforms shell glob wildcard (*) to the SQL like wildcard (%).
387              
388             =cut
389              
390             sub filter_sql_wildcard {
391 1     1 0 162 my $value = shift;
392 1 50       4 return unless defined $value;
393 0         0 $value =~ tr/*/%/;
394 0         0 return $value;
395             }
396              
397             =pod
398              
399             =head2 quotemeta
400              
401             Calls the quotemeta (quote non alphanumeric character) builtin on its
402             input.
403              
404             =cut
405              
406             sub filter_quotemeta {
407 1 50   1 0 170 return unless defined $_[0];
408 0         0 quotemeta $_[0];
409             }
410              
411             =pod
412              
413             =head2 lc
414              
415             Calls the lc (convert to lowercase) builtin on its input.
416              
417             =cut
418              
419             sub filter_lc {
420 2 100   2 0 169 return unless defined $_[0];
421 1         3 lc $_[0];
422             }
423              
424             =pod
425              
426             =head2 uc
427              
428             Calls the uc (convert to uppercase) builtin on its input.
429              
430             =cut
431              
432             sub filter_uc {
433 5 100   5 0 176 return unless defined $_[0];
434 4         7 uc $_[0];
435             }
436              
437             =pod
438              
439             =head2 ucfirst
440              
441             Calls the ucfirst (Uppercase first letter) builtin on its input.
442              
443             =cut
444              
445             sub filter_ucfirst {
446 10 100   10 0 205 return unless defined $_[0];
447 9         33 ucfirst $_[0];
448             }
449              
450              
451             1;
452              
453             __END__