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   1983 use warnings;
  15         30  
  15         367  
4 15     15   63  
  15         29  
  15         357  
5             use Moo;
6 15     15   65 use List::Util 1.45 qw(uniqstr);
  15         30  
  15         75  
7 15     15   4258 use Dancer2::Plugin::FormValidator::Validator;
  15         310  
  15         1021  
8 15     15   4578 use Dancer2::Plugin::FormValidator::Factory::Messages;
  15         46  
  15         486  
9 15     15   5972 use Dancer2::Plugin::FormValidator::Result;
  15         55  
  15         459  
10 15     15   5259 use Types::Standard qw(InstanceOf ConsumerOf);
  15         42  
  15         476  
11 15     15   92 use namespace::clean;
  15         27  
  15         68  
12 15     15   7784  
  15         30  
  15         67  
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   338 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   431 config => $_[0]->config,
56             registry => $_[0]->registry,
57             );
58             }
59             );
60              
61             my ($self) = @_;
62              
63 17     17 0 20297 my $profile = $self->profile->profile;
64             my $input = $self->input->get;
65 17         131 my $messages = {};
66 17         185  
67 17         43 # Run hook before.
68             $profile = $self->profile->hook_before($profile, $input);
69              
70 17         119 # Now run validation.
71             my ($success, $valid, $invalid) = $self->validator->validate($profile, $input);
72              
73 17         297 # If no success, build messages.
74             if ($success != 1) {
75             $messages = $self->_build_messages($invalid);
76 17 100       250 }
77 13         161  
78             # Flatten $invalid array ref and leave only unique fields.
79             my @invalid_fields = uniqstr map { $_->[0] } @{ $invalid };
80              
81 17         479 # Collect valid values from input.
  22         98  
  17         69  
82             my $valid_input = $self->_collect_valid($valid);
83              
84 17         111 return Dancer2::Plugin::FormValidator::Result->new(
85             success => $success,
86 17         228 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   55 return $self->messages_factory->build($invalid, $self->profile->messages);
96             }
97 13 100       484  
98 2         78 return $self->messages_factory->build($invalid);
99             }
100              
101 11         613 my ($self, $valid) = @_;
102              
103             my $input = $self->input->get;
104              
105 17     17   223 my %valid_input;
106             for my $field (@ { $valid }) {
107 17         204 if (exists $input->{$field}) {
108             $valid_input{$field} = $input->{$field};
109 17         43 }
110 17         36 }
  17         81  
111 15 50       44  
112 15         41 return \%valid_input;
113             }
114              
115             1;