File Coverage

blib/lib/Dancer2/Plugin/FormValidator/Validator.pm
Criterion Covered Total %
statement 43 44 97.7
branch 10 12 83.3
condition 3 3 100.0
subroutine 8 8 100.0
pod 0 1 0.0
total 64 68 94.1


line stmt bran cond sub pod time code
1              
2             use strict;
3 17     17   901 use warnings;
  17         36  
  17         414  
4 17     17   74  
  17         34  
  17         325  
5             use Moo;
6 17     17   80 use Carp;
  17         29  
  17         78  
7 17     17   4565 use Types::Standard qw(InstanceOf);
  17         41  
  17         858  
8 17     17   98 use namespace::clean;
  17         46  
  17         124  
9 17     17   7946  
  17         34  
  17         109  
10             has config => (
11             is => 'ro',
12             isa => InstanceOf['Dancer2::Plugin::FormValidator::Config'],
13             required => 1,
14             );
15              
16             has registry => (
17             is => 'ro',
18             isa => InstanceOf['Dancer2::Plugin::FormValidator::Registry'],
19             required => 1,
20             );
21              
22             # Apply validators to each fields.
23             # Collect valid and invalid fields.
24             my ($self, $profile, $input) = @_;
25              
26 19     19 0 22311 if (ref $profile ne 'HASH') {
27             Carp::croak("Profile should be a HashRef\n")
28 19 50       97 }
29 0         0  
30             my $success = 0;
31             my %profile = %{ $profile };
32 19         43 my $is_valid;
33 19         39 my @valid;
  19         93  
34 19         90 my @invalid;
35              
36 19         0 for my $field (keys %profile) {
37             $is_valid = 1;
38 19         63 my @validators = @{ $profile{$field} };
39 41         97  
40 41         73 for my $validator_declaration (@validators) {
  41         161  
41             if (my ($name, $params) = $self->_split_validator_declaration($validator_declaration)) {
42 41         99 my $validator = $self->registry->get($name);
43 65 50       197  
44 65         334 if (not $validator->validate($field, $input, split(',', $params))) {
45             push @invalid, [ $field, $name, $params ];
46 65 100       370 $is_valid = 0;
47 23         144 }
48 23         62  
49             if (!$is_valid && $validator->stop_on_fail) {
50             last;
51 65 100 100     594 }
52 9         68 }
53             }
54              
55             if ($is_valid == 1) {
56             push @valid, $field;
57 41 100       262 }
58 18         102 }
59              
60             if (not @invalid) {
61             $success = 1;
62 19 100       307 }
63 5         16  
64             return ($success, \@valid, \@invalid)
65             }
66 19         472  
67             # Because validator signatures could be validator:params, we need to split it.
68             return ($_[1] =~ /([^:]+):?(.*)/);
69             }
70              
71 65     65   477 1;