2017-04-08 17:25:34 +00:00
|
|
|
package Pear::LocalLoop::Plugin::Validators;
|
|
|
|
use Mojo::Base 'Mojolicious::Plugin';
|
|
|
|
|
|
|
|
use Email::Valid;
|
2017-07-25 16:07:15 +00:00
|
|
|
use Geo::UK::Postcode::Regex qw/ is_valid_pc /;
|
2017-04-20 00:27:18 +00:00
|
|
|
use Scalar::Util qw/ looks_like_number /;
|
2017-04-23 16:23:35 +00:00
|
|
|
use File::Basename qw/ fileparse /;
|
2017-04-23 15:59:35 +00:00
|
|
|
use DateTime::Format::Strptime;
|
2017-04-25 19:50:34 +00:00
|
|
|
use Try::Tiny;
|
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 ) = @_;
|
2017-07-25 16:07:15 +00:00
|
|
|
return is_valid_pc( $value ) ? undef : 1;
|
2017-04-09 11:50:50 +00:00
|
|
|
});
|
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
|
|
|
|
2017-06-13 20:02:24 +00:00
|
|
|
$app->validator->add_check( lt_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-23 15:59:35 +00:00
|
|
|
|
|
|
|
$app->validator->add_check( is_iso_datetime => sub {
|
|
|
|
my ( $validation, $name, $value ) = @_;
|
|
|
|
$value = $app->datetime_formatter->parse_datetime( $value );
|
|
|
|
return defined $value ? undef : 1;
|
|
|
|
});
|
2017-08-14 14:35:23 +00:00
|
|
|
|
|
|
|
$app->validator->add_check( is_full_iso_datetime => sub {
|
|
|
|
my ( $validation, $name, $value ) = @_;
|
|
|
|
$value = $app->parse_iso_datetime( $value );
|
|
|
|
return defined $value ? undef : 1;
|
|
|
|
});
|
2017-04-08 17:25:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|