Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
vti committed Jan 2, 2011
1 parent 3439295 commit 6f8c61b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
34 changes: 17 additions & 17 deletions lib/MojoX/Validator.pm
Expand Up @@ -5,7 +5,7 @@ use warnings;

use base 'Mojo::Base';

our $VERSION = '0.0010';
our $VERSION = '0.0011';

use MojoX::Validator::Bulk;
use MojoX::Validator::Condition;
Expand All @@ -14,7 +14,7 @@ use MojoX::Validator::Group;

require Carp;

__PACKAGE__->attr([qw/ bulk explicit has_unknown_params /]);
__PACKAGE__->attr([qw/bulk explicit has_unknown_params/]);
__PACKAGE__->attr(conditions => sub { [] });
__PACKAGE__->attr(fields => sub { {} });
__PACKAGE__->attr(groups => sub { [] });
Expand Down Expand Up @@ -155,8 +155,8 @@ sub _flag_unknown {
if (!defined $self->fields->{$param}) {
$self->has_unknown_params(1);

$self->field($param)->unknown(1)->error($param => 'NOT_SPECIFIED')
if $self->explicit;
$self->field($param)->unknown(1)->error($param => 'NOT_SPECIFIED')
if $self->explicit;
}
}
}
Expand Down Expand Up @@ -264,13 +264,13 @@ generation, B<NO> other stuff that does something else. Only data validation!
=over 4
A value is considered empty when its value is B<NOT> C<undef>, C<''> or
contains only spaces
contains only spaces.
If a value is not required and during validation is empty there is B<NO>
error
error.
If explicit is set to true, then all values not explicitly required
generate an error
generate an error.
If a value is passed as an array reference and an appropriate field is
not multiple, than only the first value is taken, otherwise every value of
Expand Down Expand Up @@ -306,13 +306,13 @@ Trim field values. B<ON> by default.
my $validator = MojoX::Validator->new;
Created a new L<MojoX::Validator> object.
Create a new L<MojoX::Validator> object.
=head2 C<clear_errors>
$validator->clear_errors;
Clears errors.
Clear errors.
=head2 C<field>
Expand All @@ -322,10 +322,10 @@ Clears errors.
$validator->field(qw/foo bar baz/); # MojoX::Validator::Bulk object is returned
$validator->field([qw/foo bar baz/]); # MojoX::Validator::Bulk object is returned
When a single value is passed creates L<MojoX::Validator::Field> object or
returns an already created field object.
When a single value is passed create L<MojoX::Validator::Field> object or
return an already created field object.
When an array or an array reference is passed returns L<MojoX::Validator::Bulk> object. You can
When an array or an array reference is passed return L<MojoX::Validator::Bulk> object. You can
call C<each> method to apply setting to multiple fields.
$validator->field(qw/foo bar baz/)->each(sub { shift->required(1) });
Expand All @@ -335,7 +335,7 @@ call C<each> method to apply setting to multiple fields.
$validator->field(qw/foo bar/)->each(sub { shift->required(1) });
$validator->group('all_or_none' => [qw/foo bar/])->equal;
Registers a group constraint that will be called on group of fields. If group
Register a group constraint that will be called on group of fields. If group
validation fails the C<errors> hashref will have the B<group> name with an
appropriate error message, B<NOT> fields' names.
Expand All @@ -346,7 +346,7 @@ appropriate error message, B<NOT> fields' names.
$validator->when('document')->regexp(qr/^1$/)
->then(sub { shift->field('number')->required(1) });
Registers a condition that is called when some conditions are met. You can do
Register a condition that is called when some conditions are met. You can do
whatever you want in condition's callback. Validation will be remade.
=head2 C<validate>
Expand All @@ -355,7 +355,7 @@ whatever you want in condition's callback. Validation will be remade.
$validator->validate({a => ['b', 'c']});
$validator->validate({a => ['b', 'c'], b => 'd'});
Accepts and validated a hash reference that represents data that is being
Accept and validate a hash reference that represents data that is being
validated. Hash values can be either a C<SCALAR> value or an C<ARRAREF> value,
which means that a field has multiple values. In case of an array reference, it
is checked if a field can have multiple values. Otherwise only the first value
Expand All @@ -371,7 +371,7 @@ Set a custom error.
$validator->errors; # {a => 'Required'}
Returns a hash reference of errors.
Return a hash reference of errors.
=head2 C<has_errors>
Expand All @@ -383,7 +383,7 @@ Check if there are any errors.
$validator->values;
Returns a hash reference of validated values. Only registered fields are returned,
Return a hash reference of validated values. Only registered fields are returned,
that means that if some other values were passed to the C<validate> method they
are ignored.
Expand Down
11 changes: 5 additions & 6 deletions lib/Mojolicious/Plugin/Validator.pm
Expand Up @@ -56,15 +56,15 @@ sub register {
return 1 if $validator->validate($params);

$self->stash(validator_errors => $validator->errors);
$self->stash(validator_has_unknown_params => $validator->has_unknown_params);
$self->stash(validator_has_unknown_params =>
$validator->has_unknown_params);

return;
}
);

$app->helper(
validator_has_unknown_params => sub { shift->stash('validator_has_unknown_params') }
);
$app->helper(validator_has_unknown_params =>
sub { shift->stash('validator_has_unknown_params') });

$app->helper(
validator_has_errors => sub {
Expand Down Expand Up @@ -217,15 +217,14 @@ Render the appropriate error.
=item validator_has_unknown_params
%= if(validator_has_unknown_params) {
%= if (validator_has_unknown_params) {
<div class="error">Unspecified parameters were detected.</div>
% }
Returns true if unspecified parameters were passed
=back
=head1 METHODS
L<Mojolicious::Plugin::Validator> inherits all methods from
Expand Down
14 changes: 8 additions & 6 deletions t/validator-explicit.t
Expand Up @@ -10,16 +10,18 @@ use MojoX::Validator;
# Unknown params, but no errors without explicit => 1
ok(my $validator = MojoX::Validator->new);
ok($validator->validate({firstname => 'bar'}));
ok!($validator->errors->{firstname});
ok!($validator->has_errors);
ok !($validator->errors->{firstname});
ok !($validator->has_errors);
ok($validator->has_unknown_params);

# Unknown params and custom errors with explicit => 1
ok($validator = MojoX::Validator->new(
ok( $validator = MojoX::Validator->new(
explicit => 1,
messages => { NOT_SPECIFIED => 'custom error' }));
messages => {NOT_SPECIFIED => 'custom error'}
)
);

ok!($validator->validate({firstname => 'bar'}));
ok !($validator->validate({firstname => 'bar'}));
is($validator->errors->{firstname}, 'custom error');
ok!($validator->values->{firstname});
ok !($validator->values->{firstname});
ok($validator->has_unknown_params);

0 comments on commit 6f8c61b

Please sign in to comment.