Added postcode validation and test for validators

This commit is contained in:
Tom Bloor 2017-04-09 12:50:50 +01:00
parent 0a32b05b6c
commit 704f429fa5
4 changed files with 50 additions and 8 deletions

View file

@ -2,6 +2,7 @@ package Pear::LocalLoop::Plugin::Validators;
use Mojo::Base 'Mojolicious::Plugin';
use Email::Valid;
use Geo::UK::Postcode;
sub register {
my ( $plugin, $app, $conf ) = @_;
@ -10,10 +11,16 @@ sub register {
my ( $validation, $name, $email ) = @_;
return Email::Valid->address( $email ) ? undef : 1;
});
$app->validator->add_check( in_resultset => sub {
my ( $validation, $name, $value, $key, $rs ) = @_;
return $rs->search({ $key => $value })->count ? undef : 1;
});
$app->validator->add_check( postcode => sub {
my ( $validation, $name, $value ) = @_;
return Geo::UK::Postcode->new( $value )->valid ? undef : 1;
});
}
1;