File Coverage

blib/lib/Dancer2/Plugin/DataTransposeValidator.pm
Criterion Covered Total %
statement 71 72 98.6
branch 26 30 86.6
condition 9 13 69.2
subroutine 11 11 100.0
pod 1 2 50.0
total 118 128 92.1


line stmt bran cond sub pod time code
1             package Dancer2::Plugin::DataTransposeValidator;
2              
3 1     1   472913 use strict;
  1         1  
  1         30  
4 1     1   3 use warnings;
  1         2  
  1         25  
5              
6 1     1   4 use Carp 'croak';
  1         1  
  1         52  
7 1     1   5 use Dancer2::Core::Types qw(HashRef Maybe Str);
  1         1  
  1         45  
8 1     1   583 use Data::Transpose::Validator;
  1         32725  
  1         54  
9 1     1   957 use Path::Tiny;
  1         8986  
  1         76  
10 1     1   9 use Module::Runtime qw/use_module/;
  1         1  
  1         9  
11              
12 1     1   689 use Dancer2::Plugin 0.200000;
  1         13091  
  1         9  
13              
14             =head1 NAME
15              
16             Dancer2::Plugin::DataTransposeValidator - Data::Transpose::Validator plugin for Dancer2
17              
18             =head1 VERSION
19              
20             Version 0.101
21              
22             =cut
23              
24             our $VERSION = '0.101';
25              
26             has css_error_class => (
27             is => 'ro',
28             isa => Str,
29             from_config => sub { 'has-error' },
30             );
31              
32             has errors_hash => (
33             is => 'ro',
34             isa => Maybe [Str],
35             from_config => sub { undef },
36             );
37              
38             has rules => (
39             is => 'ro',
40             isa => HashRef,
41             default => sub { +{} },
42             );
43              
44             has rules_class => (
45             is => 'ro',
46             isa => Maybe[Str],
47             from_config => sub { undef },
48             );
49              
50             has rules_dir => (
51             is => 'ro',
52             isa => sub {
53             eval { path( $_[0] )->is_dir; 1 }
54             or do { croak "rules directory does not exist" };
55             },
56             default => sub {
57             my $plugin = shift;
58             my $dir =
59             $plugin->config->{rules_dir}
60             ? $plugin->config->{rules_dir}
61             : 'validation';
62             path($plugin->app->setting('appdir'))->child($dir)->stringify;
63             },
64             );
65              
66             plugin_keywords 'validator';
67              
68             sub BUILD {
69 7     7 0 707 my $plugin = shift;
70             croak __PACKAGE__ . " cannot use both of rules_class and rules_dir"
71             if exists $plugin->config->{rules_class}
72 7 50 66     142 && exists $plugin->config->{rules_dir};
73              
74 7 100       182 if ( exists $plugin->config->{rules_class} ) {
75 1         20 use_module($plugin->config->{rules_class});
76             }
77             }
78              
79             sub validator {
80 16     16 1 373295 my ( $plugin, $params, $name, @additional_args ) = @_;
81 16         37 my $rules;
82              
83 16 50       78 croak "params must be a hash reference" unless ref($params) eq 'HASH';
84              
85 16 100       91 if ( ref($name) eq '' ) {
    100          
    100          
86 13 100       102 if ( !$plugin->rules->{$name} ) {
87 8 100       173 if ( my $class = $plugin->rules_class ) {
88 1 50       145 if ( $class->can($name) ) {
89 1         2 $plugin->rules->{$name} = \&{"${class}::$name"};
  1         8  
90             }
91             else {
92 0         0 croak "Rules class \"$class\" has no rule sub named: $name";
93             }
94             }
95             else {
96             # nasty old rules_dir
97 7         412 my $path = path( $plugin->rules_dir )->child($name);
98 7 100       535 croak "rules_file does not exist" unless $path->is_file;
99              
100 6 50       227 my $eval = do $path or croak "bad rules file: $path - $! $@";
101 6 100       2298 if ( ref($eval) eq 'CODE' ) {
102 1         6 $plugin->rules->{$name} = $eval;
103             }
104             else {
105 5     7   67 $plugin->rules->{$name} = sub { $eval };
  7         18  
106             }
107             }
108             }
109 12         51 $rules = $plugin->rules->{$name}->(@additional_args);
110             }
111             elsif (ref($name) eq 'HASH') {
112 1         2 $rules = $name;
113             }
114             elsif (ref($name) eq 'CODE') {
115 1         4 $rules = $name->(@additional_args);
116             }
117             else {
118 1         5 my $ref = ref($name);
119 1         245 croak "rules option reference type $ref not allowed";
120             }
121              
122 14   50     159 my $options = $rules->{options} || {};
123 14   50     45 my $prepare = $rules->{prepare} || {};
124              
125 14         444 my $dtv = Data::Transpose::Validator->new(%$options);
126 14         12883 $dtv->prepare(%$prepare);
127              
128 14         152301 my $clean = $dtv->transpose($params);
129 14         63670 my $ret;
130              
131 14 100       49 if ($clean) {
132 3         11 $ret->{valid} = 1;
133 3         9 $ret->{values} = $clean;
134             }
135             else {
136 11         32 $ret->{valid} = 0;
137 11         46 $ret->{values} = $dtv->transposed_data;
138              
139 11         41 my $v_hash = $dtv->errors_hash;
140 11         656 while ( my ( $key, $value ) = each %$v_hash ) {
141              
142 19         411 $ret->{css}->{$key} = $plugin->css_error_class;
143              
144 19         421 my @errors = map { $_->{value} } @{$value};
  31         74  
  19         37  
145              
146 19 100 100     452 if ( $plugin->errors_hash && $plugin->errors_hash eq 'joined' ) {
    100 66        
147 2         165 $ret->{errors}->{$key} = join( ". ", @errors );
148             }
149             elsif ( $plugin->errors_hash && $plugin->errors_hash eq 'arrayref' )
150             {
151 2         230 $ret->{errors}->{$key} = \@errors;
152             }
153             else {
154 15         714 $ret->{errors}->{$key} = $errors[0];
155             }
156             }
157             }
158 14         277 return $ret;
159              
160             }
161              
162             1;
163             __END__