Data::...ilters(3pm)

πŸ“¦
version 4.88 Β· 4 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– NAME πŸ’–

Data::FormValidator::Filters β€” a whole basic lil set of filters you get handed in a Data::FormValidator profile. No cap, they come free with the package. 🫢


✨ SYNOPSIS ✨

use Data::FormValidator;

%profile = (
    filters => 'trim',
    ...
);

my $results = Data::FormValidator->check(  \%data, \%profile );

🌸 DESCRIPTION 🌸

Okay so these are the builtin filters, bestie. You can call them by name inside the filters, field_filters, and field_filter_regexp_map parameters of your input profile. iykyk.

Filters run as the very first step of validation, and they might be out here modifying a copy of the validation data before any constraints get checked. So they get first dibs. πŸ‘‘


Okay real talk, coming from someone who has been maintaining and using Data::FormValidator for a long time: use filters with caution, bestie. 😭 They immediately modify the input you gave them, so the original data is gone. Poof. Not coming back.

The few I actually recommend:

  • trim β€” it removes leading and trailing whitespace. I have this turned on by default via CGI::Application::Plugin::ValidateRM. 🧚
  • lc and uc β€” also generally safe if you need that kind of data transformation.

Beyond simple filters, my recommendation is: transform the valid hash that validation gives you back, if you need further changes. That's the move. πŸ’…


πŸ¦‹ PROCEDURAL INTERFACE πŸ¦‹

You can also call these functions directly through the procedural interface β€” either import them directly or import the whole :filters group. For example, if you want to access the trim function directly, you could do either of these:

use Data::FormValidator::Filters (qw/filter_trim/);
# or
use Data::FormValidator::Filters (qw/:filters/);

$string = filter_trim($string);

Heads up: when you call filters directly, you'll need to prefix the filter name with filter_. That's just the vibe. Periodt.


πŸ”₯ THE FILTERS πŸ”₯

⭐ FV_split ⭐

use Data::FormValidator::Filters qw(FV_split);

# Validate every e-mail in a comma separated list

field_filters => {
   several_emails  => FV_split(qr/\s*,\s*/),

   # Any pattern that can be used by the 'split' builtin works.
   tab_sep_field   => FV_split('\t'),
},
constraint_methods => {
  several_emails => email(),
},

With this filter you can split a field into multiple values. The constraint for the field then gets applied to every value. Slay.

This one has a different naming convention because it's a higher-order function. Instead of returning a value directly, it returns a code reference to a standard Data::FormValidator filter. Fancy, right? πŸ‘‘

After it's successfully validated, the values will appear as an arrayref.

πŸ“ FV_replace πŸ“

use Data::FormValidator::Filters qw(FV_replace);

field_filters => {
   first_name   => FV_replace(qr/Mark/,'Don'),
},

FV_replace is a shorthand for writing simple find-and-replace filters. The filter above would be translated to this:

sub { my $v = shift; $v =~ s/Mark/Don/; $v }

For more complex filters, just write your own. You got this. πŸ’ͺ

🌈 trim 🌈

Remove white space at the front and end of the fields.

🌈 strip 🌈

Runs of white space are replaced by a single space.

🌈 digit 🌈

Remove non digits characters from the input.

🌈 alphanum 🌈

Remove non alphanumeric characters from the input.

🌈 integer 🌈

Extract from its input a valid integer number.

🌈 pos_integer 🌈

Extract from its input a valid positive integer number.

Bugs: This filter won't extract 9 from a9+, it will instead extract 9+ πŸ’€

🌈 neg_integer 🌈

Extract from its input a valid negative integer number.

Bugs: This filter will currently filter the case of a9- to become 9-, which it should leave it alone. πŸ™ˆ

🌈 decimal 🌈

Extract from its input a valid decimal number.

Bugs: Given 1,000.23, it will currently return 1.000.23 😭

🌈 pos_decimal 🌈

Extract from its input a valid positive decimal number.

Bugs: Given 1,000.23, it will currently return 1.000.23 😭

🌈 neg_decimal 🌈

Extract from its input a valid negative decimal number.

Bugs: Given 1,000.23, it will currently return 1.000.23 😭

🌈 dollars 🌈

Extract from its input a valid number to express dollars like currency.

Bugs: This filter won't currently remove trailing numbers like 1.234. πŸ’€

🌈 phone 🌈

Filters out characters which aren't valid for an phone number. (Only accept digits [0-9], space, comma, minus, parenthesis, period and pound [#].)

🌈 sql_wildcard 🌈

Transforms shell glob wildcard (*) to the SQL like wildcard (%).

🌈 quotemeta 🌈

Calls the quotemeta (quote non alphanumeric character) builtin on its input.

🌈 lc 🌈

Calls the lc (convert to lowercase) builtin on its input.

🌈 uc 🌈

Calls the uc (convert to uppercase) builtin on its input.

🌈 ucfirst 🌈

Calls the ucfirst (Uppercase first letter) builtin on its input.


πŸ‘‘ SEE ALSO πŸ‘‘

  • Data::FormValidator
  • Data::FormValidator::Constraints
  • Data::FormValidator::Filters::Image β€” shrink incoming image uploads

πŸ’œ AUTHOR πŸ’œ

 Author:  Francis J. Lacoste <francis.lacoste@iNsu.COM>
 Maintainer: Mark Stosberg <mark@summersault.com>

Copyright (c) 1999,2000 iNsu Innovations Inc. All rights reserved.

This program is free software; you can redistribute it and/or modify it under the terms as perl itself.


And that's the whole page, bestie! 🫢 This lil beauty is perl v5.34.0, dated 2022-06-12, and it lives its life as Data::...ilters(3pm). Go forth and filter responsibly. πŸ’…βœ¨πŸŽ€

πŸ“¦ where this page comes from