File Coverage

blib/lib/Dancer2/Plugin/FormValidator/Processor.pm
Criterion Covered Total %
statement 54 54 100.0
branch 5 6 83.3
condition n/a
subroutine 14 14 100.0
pod 0 1 0.0
total 73 75 97.3


line stmt bran cond sub pod time code
1              
2             use strict;
3 15     15   2159 use warnings;
  15         35  
  15         446  
4 15     15   71  
  15         31  
  15         361  
5             use Moo;
6 15     15   97 use List::Util 1.45 qw(uniqstr);
  15         30  
  15         79  
7 15     15   4466 use Dancer2::Plugin::FormValidator::Validator;
  15         340  
  15         1034  
8 15     15   5604 use Dancer2::Plugin::FormValidator::Factory::Messages;
  15         77  
  15         537  
9 15     15   6532 use Dancer2::Plugin::FormValidator::Result;
  15         43  
  15         551  
10 15     15   6236 use Types::Standard qw(InstanceOf ConsumerOf);
  15         46  
  15         603  
11 15     15   101 use namespace::clean;
  15         28  
  15         92  
12 15     15   8935  
  15         30  
  15         73  
13             has input => (
14             is => 'ro',
15             isa => InstanceOf['Dancer2::Plugin::FormValidator::Input'],
16             required => 1,
17             );
18              
19             has profile => (
20             is => 'ro',
21             isa => ConsumerOf['Dancer2::Plugin::FormValidator::Role::Profile'],
22             required => 1,
23             );
24              
25             has config => (
26             is => 'ro',
27             isa => InstanceOf['Dancer2::Plugin::FormValidator::Config'],
28             required => 1,
29             );
30              
31             has registry => (
32             is => 'ro',
33             isa => InstanceOf['Dancer2::Plugin::FormValidator::Registry'],
34             required => 1,
35             );
36              
37             has validator => (
38             is => 'ro',
39             isa => InstanceOf ['Dancer2::Plugin::FormValidator::Validator'],
40             lazy => 1,
41             builder => sub {
42             return Dancer2::Plugin::FormValidator::Validator->new(
43 15     15   369 config => $_[0]->config,
44             registry => $_[0]->registry,
45             );
46             }
47             );
48              
49             has messages_factory => (
50             is => 'ro',
51             isa => InstanceOf ['Dancer2::Plugin::FormValidator::Factory::Messages'],
52             lazy => 1,
53             builder => sub {
54             return Dancer2::Plugin::FormValidator::Factory::Messages->new(
55 11     11   469 config => $_[0]->config,
56             registry => $_[0]->registry,
57             );
58             }
59             );
60              
61             my ($self) = @_;
62              
63 17     17 0 23024 my $profile = $self->profile->profile;
64             my $input = $self->input->get;
65 17         132 my $messages = {};
66 17         195  
67 17         42 # Run hook before.
68             $profile = $self->profile->hook_before($profile, $input);
69              
70 17         144 # Now run validation.
71             my ($success, $valid, $invalid) = $self->validator->validate($profile, $input);
72              
73 17         351 # If no success, build messages.
74             if ($success != 1) {
75             $messages = $self->_build_messages($invalid);
76 17 100       164 }
77 13         112  
78             # Flatten $invalid array ref and leave only unique fields.
79             my @invalid_fields = uniqstr map { $_->[0] } @{ $invalid };
80              
81 17         497 # Collect valid values from input.
  22         129  
  17         83  
82             my $valid_input = $self->_collect_valid($valid);
83              
84 17         257 return Dancer2::Plugin::FormValidator::Result->new(
85             success => $success,
86 17         310 valid => $valid_input,
87             invalid => \@invalid_fields,
88             messages => $messages,
89             );
90             }
91              
92             my ($self, $invalid) = @_;
93              
94             if ($self->profile->does('Dancer2::Plugin::FormValidator::Role::HasMessages')) {
95 13     13   386 return $self->messages_factory->build($invalid, $self->profile->messages);
96             }
97 13 100       195  
98 2         78 return $self->messages_factory->build($invalid);
99             }
100              
101 11         651 my ($self, $valid) = @_;
102              
103             my $input = $self->input->get;
104              
105 17     17   66 my %valid_input;
106             for my $field (@ { $valid }) {
107 17         273 if (exists $input->{$field}) {
108             $valid_input{$field} = $input->{$field};
109 17         45 }
110 17         35 }
  17         70  
111 15 50       52  
112 15         74 return \%valid_input;
113             }
114              
115             1;