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   2460 use warnings;
  15         30  
  15         404  
4 15     15   71  
  15         28  
  15         334  
5             use Moo;
6 15     15   94 use List::Util 1.45 qw(uniqstr);
  15         35  
  15         79  
7 15     15   4468 use Dancer2::Plugin::FormValidator::Validator;
  15         319  
  15         1000  
8 15     15   5352 use Dancer2::Plugin::FormValidator::Factory::Messages;
  15         44  
  15         497  
9 15     15   6191 use Dancer2::Plugin::FormValidator::Result;
  15         50  
  15         511  
10 15     15   5994 use Types::Standard qw(InstanceOf ConsumerOf);
  15         44  
  15         560  
11 15     15   100 use namespace::clean;
  15         29  
  15         81  
12 15     15   8481  
  15         34  
  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   421 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   465 config => $_[0]->config,
56             registry => $_[0]->registry,
57             );
58             }
59             );
60              
61             my ($self) = @_;
62              
63 17     17 0 23547 my $profile = $self->profile->profile;
64             my $input = $self->input->get;
65 17         125 my $messages = {};
66 17         204  
67 17         46 # Run hook before.
68             $profile = $self->profile->hook_before($profile, $input);
69              
70 17         139 # Now run validation.
71             my ($success, $valid, $invalid) = $self->validator->validate($profile, $input);
72              
73 17         338 # If no success, build messages.
74             if ($success != 1) {
75             $messages = $self->_build_messages($invalid);
76 17 100       190 }
77 13         103  
78             # Flatten $invalid array ref and leave only unique fields.
79             my @invalid_fields = uniqstr map { $_->[0] } @{ $invalid };
80              
81 17         473 # Collect valid values from input.
  22         117  
  17         78  
82             my $valid_input = $self->_collect_valid($valid);
83              
84 17         277 return Dancer2::Plugin::FormValidator::Result->new(
85             success => $success,
86 17         265 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   368 return $self->messages_factory->build($invalid, $self->profile->messages);
96             }
97 13 100       171  
98 2         83 return $self->messages_factory->build($invalid);
99             }
100              
101 11         660 my ($self, $valid) = @_;
102              
103             my $input = $self->input->get;
104              
105 17     17   60 my %valid_input;
106             for my $field (@ { $valid }) {
107 17         286 if (exists $input->{$field}) {
108             $valid_input{$field} = $input->{$field};
109 17         71 }
110 17         39 }
  17         71  
111 15 50       45  
112 15         48 return \%valid_input;
113             }
114              
115             1;