Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'Strict' mode #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ WriteMakefile(
},
PREREQ_PM => {
'Email::Valid' => '0',
'List::Util' => '1.45',
'Regexp::Common' => '0'
},
( $mm < 6.46
Expand Down
27 changes: 22 additions & 5 deletions lib/Validator/LIVR.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use strict;
use warnings FATAL => 'all';

use Carp qw/croak/;
use List::Util qw/uniq/;

use Validator::LIVR::Rules::Common;
use Validator::LIVR::Rules::String;
Expand Down Expand Up @@ -60,17 +61,19 @@ my %DEFAULT_RULES = (
);

my $IS_DEFAULT_AUTO_TRIM = 0;
my $IS_DEFAULT_STRICT = 0;

sub new {
my ($class, $livr_rules, $is_auto_trim) = @_;
my ($class, $livr_rules, $is_auto_trim, $is_strict) = @_;

my $self = bless {
is_prepared => 0,
livr_rules => $livr_rules,
validators => {},
validator_builders => {},
errors => undef,
is_auto_trim => ( $is_auto_trim // $IS_DEFAULT_AUTO_TRIM )
is_auto_trim => ( $is_auto_trim // $IS_DEFAULT_AUTO_TRIM ),
is_strict => ( $is_strict // $IS_DEFAULT_STRICT )
}, $class;

$self->register_rules(%DEFAULT_RULES);
Expand Down Expand Up @@ -109,6 +112,11 @@ sub default_auto_trim {
$IS_DEFAULT_AUTO_TRIM = !!$is_auto_trim;
}

sub default_strict {
my ($class, $is_strict) = @_;
$IS_DEFAULT_STRICT = !!$is_strict;
}

sub prepare {
my $self = shift;

Expand Down Expand Up @@ -143,9 +151,14 @@ sub validate {

my ( %errors, %result );

foreach my $field_name ( keys %{ $self->{validators} } ) {
foreach my $field_name ( uniq(keys %{ $self->{validators} }, keys %$data ) ) {
my $validators = $self->{validators}{$field_name};
next unless $validators && @$validators;
unless ($validators && @$validators) {
if ($self->{is_strict}) {
$errors{$field_name} = "EXTRA_FIELD";
}
next;
}

my $value = $data->{$field_name};
my $is_ok = 1;
Expand Down Expand Up @@ -308,6 +321,7 @@ Validator::LIVR - Lightweight validator supporting Language Independent Validati

# Common usage
Validator::LIVR->default_auto_trim(1);
Validator::LIVR->default_strict(0);

my $validator = Validator::LIVR->new({
name => 'required',
Expand Down Expand Up @@ -410,7 +424,7 @@ Features:

=head1 CLASS METHODS

=head2 Validator::LIVR->new( $LIVR [, $IS_AUTO_TRIM] )
=head2 Validator::LIVR->new( $LIVR [, $IS_AUTO_TRIM, $IS_STRICT] )

Contructor creates validator objects.

Expand All @@ -419,6 +433,9 @@ $LIVR - validations rules. Rules description is available here - L<https://githu
$IS_AUTO_TRIM - asks validator to trim all values before validation. Output will be also trimmed.
if $IS_AUTO_TRIM is undef than default_auto_trim value will be used.

$IS_STRICT - validator will emit 'EXTRA_FIELD' error on fields that are not explicitly present in the rules.
if $IS_STRICT is undef than default_strict value will be used.

=head2 Validator::LIVR->register_aliased_default_rule( $ALIAS )

$ALIAS - is a hash that contains: name, rules, error (optional).
Expand Down
1 change: 0 additions & 1 deletion lib/Validator/LIVR/Rules/String.pm
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ sub length_between {
my $value = shift;
return if !defined($value) || $value eq '';
return 'FORMAT_ERROR' if ref($value);
print "AAAAA=" . length($value);
return 'TOO_SHORT' if length($value) < $min_length;
return 'TOO_LONG' if length($value) > $max_length;
return;
Expand Down
58 changes: 58 additions & 0 deletions t/05-strict.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use strict;
use warnings;
use v5.10;
use lib '../lib';

use Test::More;
use Test::Exception;

use Validator::LIVR;

my $data = {
valid => 1,
extra => 1
};

my $rules = {
valid => 'required'
};


subtest 'Validate data with DEFAULT_STRICT' => sub {
Validator::LIVR->default_strict(1);
my $validator = Validator::LIVR->new($rules);
my $output = $validator->validate($data);

ok( !$output, 'Validation fails' );

is_deeply($validator->get_errors(), {
extra =>'EXTRA_FIELD'
}, 'Should contain error codes' );
};

subtest 'Validate data with DEFAULT_STRICT set back to 0' => sub {
Validator::LIVR->default_strict(0);

my $validator = Validator::LIVR->new($rules);
my $output = $validator->validate($data);

is_deeply( $output, {
valid => 1
}, 'Validation succeeds' );

ok( !$validator->get_errors(), 'Should NOT contain error codes' );
};

subtest 'Validate data with IS_STRICT set by the constructor' => sub {

my $validator = Validator::LIVR->new($rules, undef, 1);
my $output = $validator->validate($data);

ok( !$output, 'Validation fails' );

is_deeply($validator->get_errors(), {
extra =>'EXTRA_FIELD'
}, 'Should contain error codes' );
};

done_testing();