diff --git a/Makefile.PL b/Makefile.PL index 9c38793..df472ea 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -23,6 +23,7 @@ WriteMakefile( }, PREREQ_PM => { 'Email::Valid' => '0', + 'List::Util' => '1.45', 'Regexp::Common' => '0' }, ( $mm < 6.46 diff --git a/lib/Validator/LIVR.pm b/lib/Validator/LIVR.pm index 4843461..467516d 100644 --- a/lib/Validator/LIVR.pm +++ b/lib/Validator/LIVR.pm @@ -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; @@ -60,9 +61,10 @@ 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, @@ -70,7 +72,8 @@ sub new { 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); @@ -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; @@ -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; @@ -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', @@ -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. @@ -419,6 +433,9 @@ $LIVR - validations rules. Rules description is available here - Lregister_aliased_default_rule( $ALIAS ) $ALIAS - is a hash that contains: name, rules, error (optional). diff --git a/lib/Validator/LIVR/Rules/String.pm b/lib/Validator/LIVR/Rules/String.pm index 58f9584..373c2cf 100644 --- a/lib/Validator/LIVR/Rules/String.pm +++ b/lib/Validator/LIVR/Rules/String.pm @@ -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; diff --git a/t/05-strict.t b/t/05-strict.t new file mode 100644 index 0000000..763f358 --- /dev/null +++ b/t/05-strict.t @@ -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();