MojoX::Validate::Util - A very convenient wrapper around Mojolicious::Validator
MojoX::Validate::Util
This program ships as scripts/synopsis.pl.
#!/usr/bin/env perl # # This is a copy of t/01.range.t, without the Test::More parts. use strict; use warnings; use MojoX::Validate::Util; # ------------------------------------------------ my(%count) = (fail => 0, pass => 0, total => 0); my($checker) = MojoX::Validate::Util -> new; $checker -> add_dimension_check; my(@data) = ( {height => ''}, # Pass. {height => '1'}, # Fail. No unit. {height => '1cm'}, # Pass. {height => '1 cm'}, # Pass. {height => '1m'}, # Pass. {height => '40-70.5cm'}, # Pass. {height => '1.5 -2m'}, # Pass. {height => 'z1'}, # Fail. Not numeric. ); my($expected); my($params); for my $i (0 .. $#data) { $count{total}++; $params = $data[$i]; $expected = ( ($i == 1) || ($i == $#data) ) ? 0 : 1; $count{fail}++ if ($expected == 0); $count{pass}++ if ($checker -> check_dimension($params, 'height', ['cm', 'm']) == 1); } print "Test counts: \n", join("\n", map{"$_: $count{$_}"} sort keys %count), "\n";
This is the printout of synopsis.pl:
Test counts: fail: 2 pass: 6 total: 8
See also scripts/demo.pl and t/*.t.
MojoX::Validate::Util is a wrapper around Mojolicious::Validator which provides a suite of convenience methods for validation.
This module is available as a Unix-style distro (*.tgz).
See http://savage.net.au/Perl-modules/html/installing-a-module.html for help on unpacking and installing distros.
Install MojoX::Validate::Util as you would any Perl module:
Perl
Run:
cpanm MojoX::Validate::Util
or run:
sudo cpan Text::Balanced::Marpa
or unpack the distro, and then run:
perl Makefile.PL make (or dmake or nmake) make test make install
new() is called as my($parser) = MojoX::Validate::Util -> new.
new()
my($parser) = MojoX::Validate::Util -> new
It returns a new object of type MojoX::Validate::Util.
new() does not take any parameters.
Called in BEGIN(). The check itself is called dimension, and it is used by calling "check_dimension($params, $topic, $units)".
dimension
Called in BEGIN(). The check itself is called url, and it is used by calling "check_url($params, $topic)".
url
This method uses URI::Find::Schemeless.
This test uses 2 steps:
Parameters:
E.g.: $params = {age => $value, ...}.
E.g.: $topic = 'age'.
Return value: Integer (0 or 1):
See also "check_equal_to($params, $topic, $other_topic)" and "check_number($params, $topic, $expected)".
Note: This method uses neither Mojolicious::Validator nor Mojolicious::Validator::Validation.
E.g.: $params = {height => $value, ...}.
E.g.: $topic = 'height'.
Or, the string can contain 2 floating point numbers separated by a hyphen, followed by one of the abbreviations.
Spaces can be used liberally within the string, but of course not within the numbers.
So the code tests $$params{$topic} = $value.
E.g.: $units = ['cm', 'm'].
Return value: Integer (0 or 1) as returned by Mojolicious::Validator::Validation#is_valid:
For some non-undef $topic, $value and $units, here are some sample values for the hashref and the corresponding return values (using $units = ['cm', 'm']):
This test uses eq. For a test using ==, see "check_number($params, $topic, $other_topic)".
E.g.: $params = {password => $value_1, confirm_password => $value_2, ...}.
E.g.: $topic = 'password'.
E.g.: $other_topic = 'confirm_password'.
So the code tests (using eq) $$params{$topic} = $value_1 with $$params{$other_topic} = $value_2.
Return value: Integer (0 or 1) as returned by Mojolicious::Validator::Validation#is_valid.
See also "check_ascii_digits($params, $topic)" and "check_number($params, $topic, $expected)".
E.g.: $params = {email_address => $value, ...}.
E.g.: $topic = 'email_address'.
For some non-undef $topic, here are some sample values for $params and the corresponding return values (using $topic = 'x'):
This method uses neither Mojolicious::Validator nor Mojolicious::Validator::Validation.
E.g.: $params = {love_popup_ads => $value, ...}.
E.g.: $topic = 'love_popup_ads'.
E.g.: ['Yes', 'No'].
This test uses ==. For a test using eq, see "check_equal_to($params, $topic, $other_topic)".
E.g.: 99.
For some non-undef $topic, $value and $expected, here are some sample values for $value and $expected, and the corresponding return values:
See also "check_ascii_digits($params, $topic)" and "check_equal_to($params, $topic, $other_topic)".
See also "check_required($params, $topic)".
See scripts/demo.pl and t/03.email.address.t.
See also "check_optional($params, $topic)".
E.g.: $params = {homepage => $value, ...}.
E.g.: $topic = 'homepage'.
For some non-undef $topic, here are some sample values for $params and the corresponding return values (using $topic = 'homepage'):
Returns an object of type URI::Find::Schemeless.
Returns an object of type Mojolicious::Validator::Validation
Returns an object of type Mojolicious::Validator
In order to clarify which methods are part of this module and which are within Mojolicious::Validator or Mojolicious::Validator::Validation.
Calling either required() or optional() within Mojolicious::Validator::Validation, and then calling is_valid() can return the same value, but the difference becomes apparent after (then) calling methods such as failed(), has_error(), output() and passed().
required()
optional()
is_valid()
failed()
has_error()
output()
passed()
This will be much clearer after you study the output of scripts/demo.pl and t/03.email.address.t.
I will add them if there is any interest, but ATM I take the attitude: Release early and release often.
boolean
I was tempted, but it would mean 2 extra, albeit small, complexities:
And that conflicts with the minimalistic philosophy of Mojolicious itself.
Mojolicious::Validator
Mojolicious::Validator::Validation
The file Changes was converted into Changelog.ini by Module::Metadata::Changes.
Version numbers < 1.00 represent development versions. From 1.00 up, they are production versions.
https://github.com/ronsavage/MojoX-Validate-Util
Email the author, or log a bug on RT:
https://rt.cpan.org/Public/Dist/Display.html?Name=MojoX::Validate::Util.
MojoX::Validate::Util was written by Ron Savage <ron@savage.net.au> in 2017.
My homepage: http://savage.net.au/.
Australian copyright (c) 2017, Ron Savage.
All Programs of mine are 'OSI Certified Open Source Software'; you can redistribute them and/or modify them under the terms of The Perl License, a copy of which is available at: http://dev.perl.org/licenses/.
To install MojoX::Validate::Util, copy and paste the appropriate command in to your terminal.
cpanm
CPAN shell
perl -MCPAN -e shell install MojoX::Validate::Util
For more information on module installation, please visit the detailed CPAN module installation guide.