Mojolicious::Plugin::AdditionalValidationChecks - Add additional validation checks
version 0.18
# Mojolicious $self->plugin('AdditionalValidationChecks'); # Controller my $validation = $self->validation; $validation->input({ nr => 3 }); $validation->required( 'nr' )->max( 10 );
Mojolicious::Plugin::AdditionalValidationChecks adds a few validation checks to the Mojolicious validator.
The behaviour of empty strings changed in Mojolicious 8.34. So e.g. this was valid before 8.34 and is now invalid:
my $validation = $self->validation; $validation->input({ nr => '' }); $validation->required( 'nr' )->phone();
These checks are added:
Checks that the given value is a valid email. It uses Email::Valid.
Email::Valid
This does only check whether the given mailaddress is valid or not
my $validation = $self->validation; $validation->input({ email_address => 'dummy@test.example' }); $validation->required( 'email_address' )->email();
Check if there's a mail host for it
my $validation = $self->validation; $validation->input({ email_address => 'dummy@test.example' }); $validation->required( 'email_address' )->email(-mxcheck => 1);
Checks if the given value is a phone number:
my $validation = $self->validation; $validation->input({ nr => '+49 123 / 1321352' }); $validation->required( 'nr' )->phone(); # valid $validation->input({ nr => '00 123 / 1321352' }); $validation->required( 'nr' )->phone(); # valid $validation->input({ nr => '0123 / 1321352' }); $validation->required( 'nr' )->phone(); # valid
Checks a number for a minimum value. If a non-number is passed, it's always invalid
my $validation = $self->validation; $validation->input({ nr => 3 }); $validation->required( 'nr' )->min( 10 ); # not valid $validation->required( 'nr' )->min( 2 ); # valid $validation->input({ nr => 'abc' }); $validation->required( 'nr' )->min( 10 ); # not valid
Checks a number for a maximum value. If a non-number is passed, it's always invalid
my $validation = $self->validation; $validation->input({ nr => 3 }); $validation->required( 'nr' )->max( 10 ); # not valid $validation->required( 'nr' )->max( 2 ); # valid $validation->input({ nr => 'abc' }); $validation->required( 'nr' )->max( 10 ); # not valid
In contrast to the size "built-in", this check also allows to omit the maximum length.
size
my $validation = $self->validation; $validation->input({ word => 'abcde' }); $validation->required( 'word' )->length( 2, 5 ); # valid $validation->required( 'word' )->length( 2 ); # valid $validation->required( 'word' )->length( 8, 10 ); # not valid
Checks if a number is an integer. If a non-number is passed, it's always invalid
my $validation = $self->validation; $validation->input({ nr => 3 }); $validation->required( 'nr' )->int(); # valid $validation->input({ nr => 'abc' }); $validation->required( 'nr' )->int(); # not valid $validation->input({ nr => '3.0' }); $validation->required( 'nr' )->int(); # not valid
Checks if a given string is an absolute URL with http or https scheme.
my $validation = $self->validation; $validation->input({ url => 'http://perl-services.de' }); $validation->required( 'url' )->http_url(); # valid $validation->input({ url => 'https://metacpan.org' }); $validation->required( 'url' )->http_url(); # valid $validation->input({ url => 3 }); $validation->required( 'url' )->http_url(); # not valid $validation->input({ url => 'mailto:dummy@example.com' }); $validation->required( 'url' )->http_url(); # not valid
The opposite of in.
in
my $validation = $self->validation; $validation->input({ id => '3' }); $validation->required( 'id' )->not( 2, 5 ); # valid $validation->required( 'id' )->not( 2 ); # valid $validation->required( 'id' )->not( 3, 8, 10 ); # not valid $validation->required( 'id' )->not( 3 ); # not valid
Checks if the given value is a "color". There are three flavours of colors:
rgb
my $validation = $self->validation; $validation->input({ color => 'rgb(11,22,33)' }); $validation->required( 'color' )->color( 'rgb' ); # valid $validation->input({ color => 'rgb(11, 22, 33)' }); $validation->required( 'color' )->color( 'rgb' ); # valid $validation->input({ color => 'rgb(11%,22%,33%)' }); $validation->required( 'color' )->color( 'rgb' ); # valid $validation->input({ color => 'rgb(11%, 22%, 33%)' }); $validation->required( 'color' )->color( 'rgb' ); # valid
rgba
my $validation = $self->validation; $validation->input({ color => 'rgba(11,22,33,0)' }); $validation->required( 'color' )->color( 'rgba' ); # valid $validation->input({ color => 'rgb(11, 22, 33,0.0)' }); $validation->required( 'color' )->color( 'rgba' ); # valid $validation->input({ color => 'rgb(11, 22, 33,0.6)' }); $validation->required( 'color' )->color( 'rgba' ); # valid $validation->input({ color => 'rgb(11%,22%,33%, 1)' }); $validation->required( 'color' )->color( 'rgba' ); # valid $validation->input({ color => 'rgb(11%, 22%, 33%, 1.0)' }); $validation->required( 'color' )->color( 'rgba' ); # valid
hex
my $validation = $self->validation; $validation->input({ color => '#afe' }); $validation->required( 'color' )->color( 'hex' ); # valid $validation->input({ color => '#affe12' }); $validation->required( 'color' )->color( 'hex' ); # valid
As there are different variants of UUIDs, you can check for those variants
uuid - all
This is the default variant
my $validation = $self->validation; $validation->input({ uuid => 'A987FBC9-4BED-3078-CF07-9141BA07C9F3' }); $validation->required( 'uuid' )->uuid(); # valid $validation->required( 'uuid' )->uuid( 'all' ); # valid
variant 3
my $validation = $self->validation; $validation->input({ uuid => 'A987FBC9-4BED-3078-CF07-9141BA07C9F3' }); $validation->required( 'uuid' )->uuid( 3 ); # valid
variant 4
my $validation = $self->validation; $validation->input({ uuid => '713ae7e3-cb32-45f9-adcb-7c4fa86b90c1' }); $validation->required( 'uuid' )->uuid( 4 ); # valid
variant 5
my $validation = $self->validation; $validation->input({ uuid => '987FBC97-4BED-5078-AF07-9141BA07C9F3' }); $validation->required( 'uuid' )->uuid( 5 ); # valid
my $validation = $self->validation; $validation->input({ hex => 'afe' }); $validation->required( 'hex' )->hex(); # valid $validation->input({ hex => 'affe12' }); $validation->required( 'hex' )->hex(); # valid
my $validation = $self->validation; $validation->input({ float => '.31' }); $validation->required( 'float' )->float(); # valid $validation->input({ float => '+3.123' }); $validation->required( 'float' )->float(); # valid $validation->input({ float => '-3.123' }); $validation->required( 'float' )->float(); # valid $validation->input({ float => '0.123' }); $validation->required( 'float' )->float(); # valid $validation->input({ float => '0.123e1' }); $validation->required( 'float' )->float(); # valid $validation->input({ float => '0.123E-13' }); $validation->required( 'float' )->float(); # valid
my $validation = $self->validation; $validation->input({ ip => '1.1.1.1' }); $validation->required( 'ip' )->ip(); # valid $validation->input({ ip => '255.255.255.255' }); $validation->required( 'ip' )->ip(); # valid
IPv6
my $validation = $self->validation; $validation->input({ ip => 'fe80:0:0:0:200:f8ff:fe21:67cf' }); $validation->required( 'ip' )->ip(6); # valid $validation->required( 'ip' )->ip(); # invalid as IPv4 is the default
Some checks are inspired by https://github.com/chriso/validator.js
If you know some commonly used checks, please add an issue at https://github.com/reneeb/Mojolicious-Plugin-AdditionalValidationChecks/issues.
Those people contributed to this addon:
Florian Heyer
Mojolicious, Mojolicious::Guides, http://mojolicio.us.
Renee Baecker <reneeb@cpan.org>
This software is Copyright (c) 2014 by Renee Baecker.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
To install Mojolicious::Plugin::AdditionalValidationChecks, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojolicious::Plugin::AdditionalValidationChecks
CPAN shell
perl -MCPAN -e shell install Mojolicious::Plugin::AdditionalValidationChecks
For more information on module installation, please visit the detailed CPAN module installation guide.