Skip to content

haarg/Mojolicious-Plugin-AdditionalValidationChecks

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

99 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status Kwalitee status GitHub issues

NAME

Mojolicious::Plugin::AdditionalValidationChecks - Add addtional validation checks

VERSION

version 0.16

SYNOPSIS

# Mojolicious
$self->plugin('AdditionalValidationChecks');

# Controller
my $validation = $self->validation;
$validation->input({ nr => 3 });
$validation->required( 'nr' )->max( 10 );

DESCRIPTION

Mojolicious::Plugin::AdditionalValidationChecks adds a few validation checks to the Mojolicious validator.

NOTE

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();

CHECKS

These checks are added:

email

Checks that the given value is a valid email. It uses Email::Valid.

simple check

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 also MX

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);

phone

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

min

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

max

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

length

In contrast to the size "built-in", this check also allows to omit the maximum length.

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

int

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

http_url

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

not

The opposite of 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

color

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
    

uuid

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
    

hex

my $validation = $self->validation;
$validation->input({ hex => 'afe' });
$validation->required( 'hex' )->hex(); # valid
$validation->input({ hex => 'affe12' });
$validation->required( 'hex' )->hex(); # valid

float

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

ip

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

ACKNOWLEDGEMENT

Some checks are inspired by https://github.com/chriso/validator.js

MORE COMMON CHECKS?

If you know some commonly used checks, please add an issue at https://github.com/reneeb/Mojolicious-Plugin-AdditionalValidationChecks/issues.

CONTRIBUTORS

Those people contributed to this addon:

  • Florian Heyer

SEE ALSO

Mojolicious, Mojolicious::Guides, http://mojolicio.us.

Development

The distribution is contained in a Git repository, so simply clone the repository

$ git clone http://github.com/reneeb/Mojolicious-Plugin-AdditionalValidationChecks.git

and change into the newly-created directory.

$ cd Mojolicious-Plugin-AdditionalValidationChecks

The project uses Dist::Zilla to build the distribution, hence this will need to be installed before continuing:

$ cpanm Dist::Zilla

To install the required prequisite packages, run the following set of commands:

$ dzil authordeps --missing | cpanm
$ dzil listdeps --author --missing | cpanm

The distribution can be tested like so:

$ dzil test

To run the full set of tests (including author and release-process tests), add the --author and --release options:

$ dzil test --author --release

AUTHOR

Renee Baecker reneeb@cpan.org

COPYRIGHT AND LICENSE

This software is Copyright (c) 2014 by Renee Baecker.

This is free software, licensed under:

The Artistic License 2.0 (GPL Compatible)

About

Add some common checks for field validation in Mojolicious

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Perl 97.7%
  • Raku 2.3%