Added receipt datetime string submission validation & validator test

This commit is contained in:
Finn 2017-08-14 15:35:23 +01:00
parent 65d5bab444
commit d078a09ccd
3 changed files with 16 additions and 3 deletions

View file

@ -99,8 +99,8 @@ sub post_upload {
$validation->required('transaction_value')->number->gt_num(0); $validation->required('transaction_value')->number->gt_num(0);
$validation->required('transaction_type')->in( 1, 2, 3 ); $validation->required('transaction_type')->in( 1, 2, 3 );
#Check a purchase time was submitted #Check a proper purchase time was submitted
$validation->required('purchase_time'); $validation->required('purchase_time')->is_full_iso_datetime;
# First pass of required items # First pass of required items
return $c->api_validation_error if $validation->has_error; return $c->api_validation_error if $validation->has_error;

View file

@ -58,6 +58,12 @@ sub register {
$value = $app->datetime_formatter->parse_datetime( $value ); $value = $app->datetime_formatter->parse_datetime( $value );
return defined $value ? undef : 1; return defined $value ? undef : 1;
}); });
$app->validator->add_check( is_full_iso_datetime => sub {
my ( $validation, $name, $value ) = @_;
$value = $app->parse_iso_datetime( $value );
return defined $value ? undef : 1;
});
} }
1; 1;

View file

@ -4,6 +4,7 @@ use warnings;
use Mojolicious::Lite; use Mojolicious::Lite;
use Test::More; use Test::More;
plugin 'Pear::LocalLoop::Plugin::Datetime';
plugin 'Pear::LocalLoop::Plugin::Validators'; plugin 'Pear::LocalLoop::Plugin::Validators';
my $validator = app->validator; my $validator = app->validator;
@ -16,6 +17,8 @@ my $valid_postcode = 'WC1H 9EB';
my $invalid_postcode = 'AB1 2CD'; my $invalid_postcode = 'AB1 2CD';
my $not_a_postcode = 'a'; my $not_a_postcode = 'a';
my $not_a_whole_postcode = 'LA1'; my $not_a_whole_postcode = 'LA1';
my $valid_purchase_time = '2017-08-14T11:29:07.965+01:00';
my $invalid_purchase_time = '2017081411:29:07';
$validation->input({ $validation->input({
valid_email => $valid_email, valid_email => $valid_email,
@ -24,6 +27,8 @@ $validation->input({
invalid_postcode => $invalid_postcode, invalid_postcode => $invalid_postcode,
not_a_postcode => $not_a_postcode, not_a_postcode => $not_a_postcode,
not_a_whole_postcode => $not_a_whole_postcode, not_a_whole_postcode => $not_a_whole_postcode,
valid_purchase_time => $valid_purchase_time,
invalid_purchase_time => $invalid_purchase_time,
}); });
$validation->required('valid_email')->email; $validation->required('valid_email')->email;
@ -32,8 +37,10 @@ $validation->required('valid_postcode')->postcode;
$validation->required('invalid_postcode')->postcode; $validation->required('invalid_postcode')->postcode;
$validation->required('not_a_postcode')->postcode; $validation->required('not_a_postcode')->postcode;
$validation->required('not_a_whole_postcode')->postcode; $validation->required('not_a_whole_postcode')->postcode;
$validation->required('valid_purchase_time')->is_full_iso_datetime;
$validation->required('invalid_purchase_time')->is_full_iso_datetime;
ok $validation->has_error, 'Have Errors'; ok $validation->has_error, 'Have Errors';
is_deeply $validation->failed, [ qw/ invalid_email invalid_postcode not_a_postcode not_a_whole_postcode / ], 'Correct Errors'; is_deeply $validation->failed, [ qw/ invalid_email invalid_postcode invalid_purchase_time not_a_postcode not_a_whole_postcode / ], 'Correct Errors';
done_testing; done_testing;