Validate(3pm)

πŸ“¦
version 0.09 Β· 3 versions
, in debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

🌸 Validate(3pm) β€” the Data::Validate era πŸ’…

okay bestie, gather round, because this is the manual page for Data::Validate (that's the Validate(3pm) one, perl's own little validation baddie) and she is serving common data validation methods. let's get into it πŸŽ€


✨ SYNOPSIS ✨

use Data::Validate qw(:math);

if(defined(is_integer($suspect))){
      print "Looks like an integer\n";
}

my $name = is_alphanumeric($suspect);
if(defined($name)){
      print "$name is alphanumeric, and has been untainted\n";
} else {
      print "$suspect was not alphanumeric"
}

# or as an object
my $v = Data::Validate->new();

die "'foo' is not an integer" unless defined($v->is_integer('foo'));

πŸ¦‹ DESCRIPTION πŸ¦‹

so basically this module collects common validation routines, and its whole thing is making input validation and untainting easier and more readable. lowkey most of these functions are not even much shorter than their direct perl equivalent (and in some cases they're way longer πŸ’€) β€” but their names make it crystal clear what you're trying to test for. it's giving self-documenting code, and honestly? iconic.

almost all functions return an untainted value if the test passes, and undef if it fails. which means β€” and this is the part people fumble, so listen β€” you should always check for a defined status explicitly. do not assume the return will be true. (e.g. is_integer(0)) 😭 the value 0 is real and valid and it will absolutely ruin your day if you just do a truthiness check.

the value to test is always the first (and often only) argument. periodt.


πŸ‘‘ FUNCTIONS πŸ‘‘

new β€” constructor for OO usage

new();

Description returns a Data::Validator object. this lets you access all the validator function calls as methods, without importing them into your namespace, or without using that clumsy Data::Validate::function_name() format. no cap, it's cleaner.

Arguments none

Returns returns a Data::Validate object


is_integer β€” is the value an integer?

is_integer($value);

Description returns the untainted number if the test value is an integer, or can be cast to one without a loss of precision. (i.e. 1.0 is considered an integer, but 1.0001 is not.) so close bestie, but no 🫢

Arguments

  • $value β€” the potential integer to test.

Returns returns the untainted integer on success, undef on failure. note that the return can be 0, so always check with defined()

Notes, Exceptions, & Bugs number translation is done by POSIX casting tools (strtol).


is_numeric β€” is the value numeric?

is_numeric($value);

Description returns the untainted number if the test value is numeric according to Perl's own internal rules. (actually a wrapper on Scalar::Util::looks_like_number) ✨

Arguments

  • $value β€” the potential number to test.

Returns returns the untainted number on success, undef on failure. note that the return can be 0, so always check with defined()

Notes, Exceptions, & Bugs number translation is done by POSIX casting tools (strtol).


is_hex β€” is the value a hex number?

is_hex($value);

Description returns the untainted number if the test value is a hex number.

Arguments

  • $value β€” the potential number to test.

Returns returns the untainted number on success, undef on failure. note that the return can be 0, so always check with defined()

Notes, Exceptions, & Bugs none


is_oct β€” is the value an octal number?

is_oct($value);

Description returns the untainted number if the test value is a octal number.

Arguments

  • $value β€” the potential number to test.

Returns returns the untainted number on success, undef on failure. note that the return can be 0, so always check with defined()

Notes, Exceptions, & Bugs none


is_between β€” is the value between two numbers?

is_between($value, $min, $max);

Description returns the untainted number if the test value is numeric, and falls between $min and $max inclusive. and here's the cute part: either $min or $max can be undef, which means 'unlimited'. i.e. is_between($val, 0, undef) would pass for any number zero or larger. the range is unlimited, bestie πŸ’–

Arguments

  • $value β€” the potential number to test.
  • $min β€” the minimum valid value. unlimited if set to undef
  • $max β€” the maximum valid value. unlimited if set to undef

Returns returns the untainted number on success, undef on failure. note that the return can be 0, so always check with defined()


is_greater_than β€” is the value greater than a threshold?

is_greater_than($value, $threshold);

Description returns the untainted number if the test value is numeric, and is greater than $threshold. (not inclusive)

Arguments

  • $value β€” the potential number to test.
  • $threshold β€” the minimum value (non-inclusive)

Returns returns the untainted number on success, undef on failure. note that the return can be 0, so always check with defined()


is_less_than β€” is the value less than a threshold?

is_less_than($value, $threshold);

Description returns the untainted number if the test value is numeric, and is less than $threshold. (not inclusive)

Arguments

  • $value β€” the potential number to test.
  • $threshold β€” the maximum value (non-inclusive)

Returns returns the untainted number on success, undef on failure. note that the return can be 0, so always check with defined()


is_equal_to β€” do a string/number neutral ==

is_equal_to($value, $target);

Description returns the target if $value is equal to it. does a math comparison if both $value and $target are numeric, or a string comparison otherwise. both the $value and $target must be defined to get a true return. (i.e. undef != undef) 🫠 yeah. undefined does not equal undefined here. it's giving chaos but it's giving documented chaos.

Arguments

  • $value β€” the value to test.
  • $target β€” the value to test against

Returns unlike most validator routines, this one does not necessarily untaint its return value β€” it just returns $target. this has the effect of untainting if the target is a constant or other clean value. (i.e. is_equal_to($bar, 'foo')). note that the return can be 0, so always check with defined()


is_even β€” is a number even?

is_even($value);

Description returns the untainted $value if it's numeric, an integer, and even.

Arguments

  • $value β€” the value to test.

Returns returns $value (untainted). note that the return can be 0, so always check with defined().


is_odd β€” is a number odd?

is_odd($value);

Description returns the untainted $value if it's numeric, an integer, and odd.

Arguments

  • $value β€” the value to test.

Returns returns $value (untainted). note that the return can be 0, so always check with defined().


is_alphanumeric β€” does it only contain letters and numbers?

is_alphanumeric($value);

Description returns the untainted $value if it is defined and only contains letters (upper or lower case) and numbers. also allows an empty string β€” ''. yes, empty string is valid, she's allowed to be nothing πŸ’…

Arguments

  • $value β€” the value to test.

Returns returns $value (untainted). note that the return can be 0, so always check with defined().


is_printable β€” does it only contain printable characters?

is_alphanumeric($value);

Description returns the untainted $value if it is defined and only contains printable characters as defined by the composite POSIX character class [[:print:][:space:]]. also allows an empty string β€” ''.

Arguments

  • $value β€” the value to test.

Returns returns $value (untainted). note that the return can be 0, so always check with defined().


length_is_between β€” is the string length between two limits?

length_is_between($value, $min, $max);

Description returns $value if it is defined and its length is between $min and $max inclusive. note that this function does not untaint the value. πŸ™ˆ

if either $min or $max are undefined they are treated as no-limit.

Arguments

  • $value β€” the value to test.
  • $min β€” the minimum length of the string (inclusive).
  • $max β€” the maximum length of the string (inclusive).

Returns returns $value. note that the return can be 0, so always check with defined(). the value is not automatically untainted.


πŸŽ€ AUTHOR πŸŽ€

Richard Sonnen <sonnen@richardsonnen.com>.


Copyright (c) 2004 Richard Sonnen. All rights reserved.

this program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


πŸ’€ POD ERRORS πŸ’€

hey! the above document had some coding errors, which are explained below:

around line 89:

You can't have =items (as at line 97) unless the first thing after
the =over is an =item
not the doc dragging itself in its own pod errors 😭 even the manual page isn't immune to a little syntax slip, and honestly? relatable. slay anyway.

and that's the whole thing, bestie β€” perl v5.38.2, dated 2024-03-05, page Validate(3pm), signing off 🫢✨ validate ur inputs, check for defined(), and stay untainted πŸ’œ

πŸ“¦ where this page comes from