blib/lib/Data/FormValidator/Filters.pm

filter: Statementstmt 88.1% 82 / 93 Branchbran 83.3% 30 / 36 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 95.8% 23 / 24 Podpod 10.5% 2 / 19 totaltotal 78.8% 138 / 175 CC 20 SCAR 7.0
CC
20
Cov
86%
filter_strip
2.5
filter_digit
2.5
filter_alphanum
2.5
CRAP 2.0
« blib/lib/Data/FormValidator/Constraints/Dates.pm ↑ Summary test/00_base.badformat »
1 # Filters.pm - Common filters for use in Data::FormValidator.
2 # This file is part of Data::FormValidator.
3 #
4 # Author: Francis J. Lacoste <francis.lacoste@iNsu.COM>
5 # Maintainer: Mark Stosberg <mark@summersault.com>
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 60 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 60 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 60 our $VERSION = 4.88;
17  
18 60 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 60 our %EXPORT_TAGS = (
41 filters => \@EXPORT_OK,
42 );
43  
44 0 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
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<filters>, I<field_filters>, and I<field_filter_regexp_map> 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<trim>,
77 which removes leading and trailing whitespace. I have this turned on by default
78 by using L<CGI::Application::Plugin::ValidateRM>. It's also generally safe to use
79 the C<lc> and C<uc> 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<trim> 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 1 ▶sub FV_split {
Subroutine FV_split: called CC=2 SCAR=7.0
Pod documented
130 1 ▶ my $splitter = shift || die "nothing to split on!";
Condition50%1/2
shift() || die('nothing to split on!')
l!l
10
MC/DC0%0/1
shift() || die('nothing to split on!')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
131 3 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
132 3 my $value = shift;
133 3 ▶ return undef unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 2 3
134 2 my @a = split $splitter, $value;
135 2 return \@a;
136 1 };
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 2 ▶sub FV_replace {
Subroutine FV_replace: called CC=1 SCAR=0.0
Pod documented
157 2 my ($find,$replace) = @_;
158 3 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
159 3 my $v = shift;
160 3 $v =~ s/$find/$replace/;
161 3 return $v;
162 2 };
163 }
164  
165 =head2 trim
166  
167 Remove white space at the front and end of the fields.
168  
169 =cut
170  
171 23 ▶sub filter_trim {
Subroutine filter_trim: called CC=2 SCAR=6.9
Pod undocumented
172 23 my $value = shift;
173 23 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 22 23
174  
175 # Remove whitespace at the front
176 22 $value =~ s/^\s+//o;
177  
178 # Remove whitespace at the end
179 22 $value =~ s/\s+$//o;
180  
181 22 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 1 ▶sub filter_strip {
Subroutine filter_strip: called CC=2 SCAR=9.2
Pod undocumented
193 1 my $value = shift;
194 1 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 0 1
195  
196 # Strip whitespace
197 0 $value =~ s/\s+/ /g;
198  
199 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 1 ▶sub filter_digit {
Subroutine filter_digit: called CC=2 SCAR=9.2
Pod undocumented
211 1 my $value = shift;
212 1 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 0 1
213  
214 0 $value =~ s/\D//g;
215  
216 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 1 ▶sub filter_alphanum {
Subroutine filter_alphanum: called CC=2 SCAR=9.2
Pod undocumented
228 1 my $value = shift;
229 1 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 0 1
230 0 $value =~ s/\W//g;
231 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 2 ▶sub filter_integer {
Subroutine filter_integer: called CC=2 SCAR=6.9
Pod undocumented
243 2 my $value = shift;
244 2 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 1 2
245 1 $value =~ tr/0-9+-//dc;
246 1 ($value) =~ m/([-+]?\d+)/;
247 1 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 2 ▶sub filter_pos_integer {
Subroutine filter_pos_integer: called CC=2 SCAR=6.9
Pod undocumented
261 2 my $value = shift;
262 2 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 1 2
263 1 $value =~ tr/0-9+//dc;
264 1 ($value) =~ m/(\+?\d+)/;
265 1 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 2 ▶sub filter_neg_integer {
Subroutine filter_neg_integer: called CC=2 SCAR=6.9
Pod undocumented
280 2 my $value = shift;
281 2 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 1 2
282 1 $value =~ tr/0-9-//dc;
283 1 ($value) =~ m/(-\d+)/;
284 1 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 2 ▶sub filter_decimal {
Subroutine filter_decimal: called CC=2 SCAR=6.9
Pod undocumented
298 2 my $value = shift;
299 2 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 1 2
300 # This is a localization problem, but anyhow...
301 1 $value =~ tr/,/./;
302 1 $value =~ tr/0-9.+-//dc;
303 1 ($value) =~ m/([-+]?\d+\.?\d*)/;
304 1 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 2 ▶sub filter_pos_decimal {
Subroutine filter_pos_decimal: called CC=2 SCAR=6.9
Pod undocumented
318 2 my $value = shift;
319 2 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 1 2
320 # This is a localization problem, but anyhow...
321 1 $value =~ tr/,/./;
322 1 $value =~ tr/0-9.+//dc;
323 1 ($value) =~ m/(\+?\d+\.?\d*)/;
324 1 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 2 ▶sub filter_neg_decimal {
Subroutine filter_neg_decimal: called CC=2 SCAR=6.9
Pod undocumented
338 2 my $value = shift;
339 2 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 1 2
340 # This is a localization problem, but anyhow...
341 1 $value =~ tr/,/./;
342 1 $value =~ tr/0-9.-//dc;
343 1 ($value) =~ m/(-\d+\.?\d*)/;
344 1 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 3 ▶sub filter_dollars {
Subroutine filter_dollars: called CC=2 SCAR=6.9
Pod undocumented
358 3 my $value = shift;
359 3 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 2 3
360 2 $value =~ tr/,/./;
361 2 $value =~ tr/0-9.+-//dc;
362 2 ($value) =~ m/(\d+\.?\d?\d?)/;
363 2 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 1 ▶sub filter_phone {
Subroutine filter_phone: called CC=2 SCAR=9.2
Pod undocumented
376 1 my $value = shift;
377 1 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 0 1
378 0 $value =~ s/[^\d,\(\)\.\s,\-#]//g;
379 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 1 ▶sub filter_sql_wildcard {
Subroutine filter_sql_wildcard: called CC=2 SCAR=9.2
Pod undocumented
391 1 my $value = shift;
392 1 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 0 1
393 0 $value =~ tr/*/%/;
394 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 1 ▶sub filter_quotemeta {
Subroutine filter_quotemeta: called CC=2 SCAR=9.2
Pod undocumented
407 1 ▶ return unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 1 0 1
408 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 2 ▶sub filter_lc {
Subroutine filter_lc: called CC=2 SCAR=6.9
Pod undocumented
420 2 ▶ return unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 1 1 2
421 1 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 5 ▶sub filter_uc {
Subroutine filter_uc: called CC=2 SCAR=6.9
Pod undocumented
433 5 ▶ return unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 1 4 5
434 4 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 10 ▶sub filter_ucfirst {
Subroutine filter_ucfirst: called CC=2 SCAR=6.9
Pod undocumented
446 10 ▶ return unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 1 9 10
447 9 ucfirst $_[0];
448 }
449  
450  
451 60 1;
452  
453 __END__
« blib/lib/Data/FormValidator/Constraints/Dates.pm ↑ Summary test/00_base.badformat »