2017-04-08 17:25:34 +00:00
|
|
|
package Pear::LocalLoop::Plugin::Validators;
|
|
|
|
use Mojo::Base 'Mojolicious::Plugin';
|
|
|
|
|
|
|
|
use Email::Valid;
|
2017-04-09 11:50:50 +00:00
|
|
|
use Geo::UK::Postcode;
|
2017-04-20 00:27:18 +00:00
|
|
|
use Scalar::Util qw/ looks_like_number /;
|
2017-04-20 12:58:45 +00:00
|
|
|
use File::Basename;
|
2017-04-08 17:25:34 +00:00
|
|
|
|
|
|
|
sub register {
|
|
|
|
my ( $plugin, $app, $conf ) = @_;
|
|
|
|
|
|
|
|
$app->validator->add_check( email => sub {
|
|
|
|
my ( $validation, $name, $email ) = @_;
|
|
|
|
return Email::Valid->address( $email ) ? undef : 1;
|
|
|
|
});
|
2017-04-09 11:50:50 +00:00
|
|
|
|
2017-04-08 17:25:34 +00:00
|
|
|
$app->validator->add_check( in_resultset => sub {
|
|
|
|
my ( $validation, $name, $value, $key, $rs ) = @_;
|
|
|
|
return $rs->search({ $key => $value })->count ? undef : 1;
|
|
|
|
});
|
2017-04-09 11:50:50 +00:00
|
|
|
|
2017-04-09 13:17:12 +00:00
|
|
|
$app->validator->add_check( not_in_resultset => sub {
|
|
|
|
my ( $validation, $name, $value, $key, $rs ) = @_;
|
|
|
|
return $rs->search({ $key => $value })->count ? 1 : undef;
|
|
|
|
});
|
|
|
|
|
2017-04-09 11:50:50 +00:00
|
|
|
$app->validator->add_check( postcode => sub {
|
|
|
|
my ( $validation, $name, $value ) = @_;
|
|
|
|
return Geo::UK::Postcode->new( $value )->valid ? undef : 1;
|
|
|
|
});
|
2017-04-20 00:27:18 +00:00
|
|
|
|
|
|
|
$app->validator->add_check( number => sub {
|
|
|
|
my ( $validation, $name, $value ) = @_;
|
|
|
|
return looks_like_number( $value ) ? undef : 1;
|
|
|
|
});
|
|
|
|
|
|
|
|
$app->validator->add_check( gt_num => sub {
|
|
|
|
my ( $validation, $name, $value, $check ) = @_;
|
|
|
|
return $value > $check ? undef : 1;
|
|
|
|
});
|
2017-04-20 12:58:45 +00:00
|
|
|
|
|
|
|
$app->validator->add_check( filetype => sub {
|
|
|
|
my ( $validation, $name, $value, $filetype ) = @_;
|
|
|
|
my ( undef, undef, $extension ) = fileparse $value->filename, qr/\.[^.]*/;
|
|
|
|
$extension =~ s/^\.//;
|
|
|
|
return $app->types->type($extension) eq $filetype ? undef : 1;
|
|
|
|
});
|
2017-04-08 17:25:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|