Skip to content

Commit

Permalink
added in check
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Sep 27, 2013
1 parent 2587e03 commit d05705c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 23 deletions.
15 changes: 11 additions & 4 deletions lib/Mojolicious/Validator.pm
Expand Up @@ -3,8 +3,9 @@ use Mojo::Base -base;

use Mojolicious::Validator::Validation;

has checks =>
sub { {equal_to => \&_equal_to, regex => \&_regex, size => \&_size} };
has checks => sub {
{equal_to => \&_equal_to, in => \&_in, regex => \&_regex, size => \&_size};
};
has errors => sub {
{
equal_to => sub {'Values are not equal.'},
Expand Down Expand Up @@ -32,6 +33,12 @@ sub _equal_to {
return $value eq $other;
}

sub _in {
my ($validation, $name, $value) = (shift, shift, shift);
$value eq $_ && return 1 for @_;
return undef;
}

sub _regex { $_[2] =~ $_[3] }

sub _size {
Expand Down Expand Up @@ -68,8 +75,8 @@ L<Mojolicious::Validator> implements the following attributes.
my $checks = $validator->checks;
$validator = $validator->checks({size => sub {...}});
Registered checks, by default only C<equal_to>, C<regex> and C<size> are
already defined.
Registered checks, by default only C<equal_to>, C<in>, C<regex> and C<size>
are already defined.
=head2 errors
Expand Down
35 changes: 16 additions & 19 deletions lib/Mojolicious/Validator/Validation.pm
Expand Up @@ -28,12 +28,16 @@ sub check {
my $err = delete $self->{error};
return $self unless $self->is_valid;

my $cb = $self->validator->checks->{$check};
my $name = $self->topic;
return $self if $self->_check($name, $self->input->{$name}, $cb, @_);
my $cb = $self->validator->checks->{$check};
my $name = $self->topic;
my $input = $self->input->{$name};
for my $value (ref $input eq 'ARRAY' ? @$input : $input) {
next if $self->$cb($name, $value, @_);
delete $self->output->{$name};
$self->_error($check, $err, $name, $value, @_);
last;
}

delete $self->output->{$name};
$self->_error($check, $err, $name, delete $self->input->{$name}, @_);
return $self;
}

Expand All @@ -53,9 +57,12 @@ sub is_valid { exists $_[0]->output->{$_[1] // $_[0]->topic} }

sub optional {
my ($self, $name) = @_;

my $input = $self->input->{$name};
my @input = ref $input eq 'ARRAY' ? @$input : $input;
$self->output->{$name} = $input
if $self->_check($name, $input, sub { defined $_[2] && length $_[2] });
unless grep { !defined($_) || !length($_) } @input;

return $self->topic($name);
}

Expand All @@ -77,25 +84,15 @@ sub required {
my ($self, $name) = @_;
$self->optional($name);
my $err = delete $self->{error};
$self->_error('required', $err, $name, $self->input->{$name})
unless $self->is_valid;
$self->_error('required', $err, $name) unless $self->is_valid;
return $self;
}

sub _check {
my ($self, $name, $input, $cb) = (shift, shift, shift, shift);
for my $value (ref $input eq 'ARRAY' ? @$input : $input) {
return undef unless $self->$cb($name, $value, @_);
}
return 1;
}

sub _error {
my ($self, $check, $err, $name, $input)
my ($self, $check, $err, $name, $value)
= (shift, shift, shift, shift, shift);
my $cb = $self->validator->errors->{$check} // sub {'Value is not valid.'};
push @{$self->{errors}{$name}}, $err // $self->$cb($name, $_, @_)
for ref $input eq 'ARRAY' ? @$input : $input;
push @{$self->{errors}{$name}}, $err // $self->$cb($name, $value, @_);
}

1;
Expand Down
12 changes: 12 additions & 0 deletions t/mojolicious/validation_lite_app.t
Expand Up @@ -63,6 +63,18 @@ ok $validation->has_error, 'has error';
is_deeply [$validation->errors('yada')->each], ['Values are not equal.'],
'right error';

# In
$validation = $t->app->validation;
$validation->input({foo => [qw(bar whatever)], baz => [qw(yada ohoh)]});
ok $validation->required('foo')->in(qw(23 bar whatever))->is_valid, 'valid';
is_deeply $validation->output, {foo => [qw(bar whatever)]}, 'right result';
ok !$validation->has_error, 'no error';
ok !$validation->required('baz')->in(qw(yada whatever))->is_valid, 'not valid';
is_deeply $validation->output, {foo => [qw(bar whatever)]}, 'right result';
ok $validation->has_error, 'has error';
is_deeply [$validation->errors('baz')->each], ['Value is not valid.'],
'right error';

# Regex
$validation = $t->app->validation;
$validation->input({foo => 'bar', baz => 'yada'});
Expand Down

0 comments on commit d05705c

Please sign in to comment.