Skip to content

Commit

Permalink
return collections with errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Sep 24, 2013
1 parent 199a67f commit e2c3a2b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
18 changes: 12 additions & 6 deletions lib/Mojolicious/Validator/Validation.pm
Expand Up @@ -3,6 +3,7 @@ use Mojo::Base -base;

use Carp 'croak';
use Scalar::Util 'blessed';
use Mojo::Collection;

has [qw(input output)] => sub { {} };
has [qw(topic validator)];
Expand Down Expand Up @@ -42,9 +43,11 @@ sub error {
return $self;
}

sub errors { @{shift->{errors}{shift()} // []} }
sub errors { Mojo::Collection->new(@{shift->{errors}{shift()} // []}) }

sub has_errors { !!keys %{shift->{errors}} }
sub has_errors {
$_[1] ? exists $_[0]{errors}{$_[1]} : !!keys %{$_[0]{errors}};
}

sub is_valid { exists $_[0]->output->{$_[1] // $_[0]->topic} }

Expand Down Expand Up @@ -167,22 +170,25 @@ Set custom error message for next validation check.
=head2 errors
my @messages = $validation->errors('foo');
my $collection = $validation->errors('foo');
Get error messages for failed validation checks.
Return L<Mojo::Collection> object containing all error messages for failed
validation checks.
=head2 has_errors
my $success = $validation->has_errors;
my $success = $validation->has_errors('foo');
Check if this validation has error messages.
Check if validation resulted in errors, defaults to checking all fields.
=head2 is_valid
my $success = $validation->is_valid;
my $success = $validation->is_valid('foo');
Check if validation was successful, defaults to checking the current C<topic>.
Check if validation was successful and field has a value, defaults to checking
the current C<topic>.
=head2 optional
Expand Down
23 changes: 14 additions & 9 deletions t/mojolicious/validation_lite_app.t
Expand Up @@ -36,8 +36,8 @@ ok !$validation->has_errors, 'no errors';
ok !$validation->required('does_not_exist')->is_valid, 'not valid';
is_deeply $validation->output, {foo => 'bar', baz => 'yada'}, 'right result';
ok $validation->has_errors, 'has errors';
is_deeply [$validation->errors('does_not_exist')], ['Value is required.'],
'right error';
is_deeply [$validation->errors('does_not_exist')->each],
['Value is required.'], 'right error';

# Range
$validation = $t->app->validation;
Expand All @@ -48,36 +48,41 @@ ok !$validation->has_errors, 'no errors';
ok !$validation->required('baz')->range(1, 3)->is_valid, 'not valid';
is_deeply $validation->output, {foo => 'bar'}, 'right result';
ok $validation->has_errors, 'has errors';
is_deeply [$validation->errors('baz')],
is_deeply [$validation->errors('baz')->each],
['Value needs to be 1-3 characters long.'], 'right error';
ok !$validation->required('yada')->range(5, 10)->is_valid, 'not valid';
is_deeply $validation->output, {foo => 'bar'}, 'right result';
ok $validation->has_errors, 'has errors';
is_deeply [$validation->errors('yada')],
is_deeply [$validation->errors('yada')->each],
['Value needs to be 5-10 characters long.'], 'right error';

# Custom errors
$validation = $t->app->validation;
ok !$validation->has_errors('bar'), 'no errors';
$validation->input({foo => 'bar', yada => 'yada'});
ok !$validation->error('Bar is required.')->required('bar')->is_valid,
'not valid';
is_deeply $validation->output, {}, 'right result';
ok $validation->has_errors, 'has errors';
is_deeply [$validation->errors('bar')], ['Bar is required.'], 'right error';
is_deeply [$validation->errors('bar')->each], ['Bar is required.'],
'right error';
ok !$validation->required('baz')->is_valid, 'not valid';
ok $validation->has_errors, 'has errors';
is_deeply [$validation->errors('baz')], ['Value is required.'], 'right error';
is_deeply [$validation->errors('baz')->each], ['Value is required.'],
'right error';
ok !$validation->required('foo')->error('Foo is too small.')->range(25, 100)
->is_valid, 'not valid';
ok $validation->has_errors, 'has errors';
is_deeply [$validation->errors('foo')], ['Foo is too small.'], 'right error';
is_deeply [$validation->errors('foo')->each], ['Foo is too small.'],
'right error';
is $validation->topic, 'foo', 'right topic';
ok !$validation->error('Failed!')->required('yada')->range(25, 100)->is_valid,
'not valid';
ok $validation->has_errors, 'has errors';
is_deeply [$validation->errors('yada')],
is_deeply [$validation->errors('yada')->each],
['Value needs to be 25-100 characters long.'], 'right error';
is $validation->topic, 'yada', 'right topic';
ok $validation->has_errors('bar'), 'has errors';

# Successful validation
$t->get_ok('/?name=sri')->status_is(200)->content_is("\n");
Expand All @@ -91,4 +96,4 @@ done_testing();
__DATA__
@@ index.html.ep
%= $_ for validation->errors('name')
%= $_ for validation->errors('name')->each

0 comments on commit e2c3a2b

Please sign in to comment.