Service/Profiles.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Service/Search.pm ↑ Summary Service/Resource.pm »
1 package XAS::Service::Profiles;
2  
3 our $VERSION = '0.01';
4  
5 1 ▶use Hash::Merge;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Data::FormValidator;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use XAS::Utils ':validation';
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use XAS::Constants 'HASHREF';
9 use Data::FormValidator::Results;
10 use Badger::Class import => 'class';
11  
12 #use Data::Dumper;
13  
14 # -----------------------------------------------------------------
15 # Overrides - WARNING Will Robinson WARNING here be dragons
16 # -----------------------------------------------------------------
17  
18 class('Data::FormValidator::Results')->methods(
19 _generate_msgs => sub {
20 my $self = shift;
21 my $controls = shift || {};
22  
23 if (defined $controls and ref $controls ne 'HASH') {
24  
25 die "$0: parameter passed to msgs must be a hash ref";
26  
27 }
28  
29 # Allow msgs to be called more than one to accumulate error messages
30  
31 $self->{msgs} ||= {};
32 $self->{profile}{msgs} ||= {};
33 $self->{msgs} = { %{ $self->{msgs} }, %$controls };
34  
35 # Legacy typo support.
36  
37 for my $href ($self->{msgs}, $self->{profile}{msgs}) {
38  
39 if ((not defined $href->{invalid_separator}) &&
40 (defined $href->{invalid_seperator})) {
41  
42 $href->{invalid_separator} = $href->{invalid_seperator};
43  
44 }
45  
46 }
47  
48 my %profile = (
49 prefix => '',
50 missing => 'Missing',
51 invalid => 'Invalid',
52 invalid_separator => ' ',
53  
54 format => '<span style="color:red;font-weight:bold" class="dfv_errors">* %s</span>',
55 %{ $self->{msgs} },
56 %{ $self->{profile}{msgs} },
57 );
58  
59 my %msgs = ();
60  
61 # Add invalid messages to hash
62 # look at all the constraints, look up their messages (or provide a default)
63 # add field + formatted constraint message to hash
64  
65 if ($self->has_invalid) {
66  
67 my @invalids = $self->invalid;
68  
69 foreach my $i (@invalids) {
70  
71 $msgs{$i} = join(
72 $profile{invalid_separator},
73 $self->_error_msg_fmt($profile{format}, ($profile{constraints}{$i} || $profile{invalid}))
74 );
75  
76 }
77  
78 }
79  
80 # Add missing messages, if any
81  
82 if ($self->has_missing) {
83  
84 my $missing = $self->missing;
85  
86 for my $m (@$missing) {
87  
88 $msgs{$m} = $self->_error_msg_fmt($profile{format},$profile{missing});
89  
90 }
91  
92 }
93  
94 my $msgs_ref = $self->prefix_hash($profile{prefix},\%msgs);
95  
96 unless ($self->success) {
97  
98 $msgs_ref->{ $profile{any_errors} } = 1 if defined $profile{any_errors};
99  
100 }
101  
102 return $msgs_ref;
103  
104 },
105 _error_msg_fmt => sub {
106 my $self = shift;
107 my $fmt = shift;
108 my $msg = shift;
109  
110 $fmt ||= '<span style="color:red;font-weight:bold" class="dfv_errors">* %s</span>';
111  
112 ($fmt =~ m/%s/) || die 'format must contain %s';
113  
114 return sprintf $fmt, $msg;
115  
116 },
117 prefix_hash => sub {
118 my $self = shift;
119 my $pre = shift;
120 my $href = shift;
121  
122 die "prefix_hash: need two arguments" unless (defined($pre) && defined($href));
123 die "prefix_hash: second argument must be a hash ref" unless (ref $href eq 'HASH');
124  
125 my %out;
126  
127 for (keys %$href) {
128  
129 $out{$pre.$_} = $href->{$_};
130  
131 }
132  
133 return \%out;
134  
135 }
136 );
137  
138 # -----------------------------------------------------------------
139 # Public Methods
140 # -----------------------------------------------------------------
141  
142 sub new {
143 my $class = shift;
144 my @profiles = validate_params(\@_, [
145 { type => HASHREF },
146 ({ optional => 1, type => HASHREF}) x (@_ - 1),
147 ]);
148  
149 my $profile = {};
150 my $merger = Hash::Merge->new('RIGHT_PRECEDENT');
151  
152 foreach my $p (@profiles) {
153  
154 $profile = \%{ $merger->merge($profile, $p) };
155  
156 }
157  
158 return Data::FormValidator->new($profile);
159  
160 }
161  
162 # -----------------------------------------------------------------
163 # Private Methods
164 # -----------------------------------------------------------------
165  
166 1;
167  
168 =head1 NAME
169  
170 XAS::Service::Profiles - A class for creating standard validation profiles.
171  
172 =head1 SYNOPSIS
173  
174 use XAS::Service::Profiles;
175 use XAS::Service::Profiles::Search;
176  
177 my $params = {
178 start => 0,
179 limit => 25,
180 sort => qq/[{"field":"server',"direction":"DESC"}]/
181 };
182  
183 my @fields = [qw(id server queue requestor typeofrequest status startdatetime)];
184  
185 my $search = XAS::Service::Profiles::Search->new(\@fields);
186 my $profile = XAS::Service::Profiles->new($search);
187  
188 my $results = $profile->check($params, 'pager');
189  
190 if ($results->has_invalid) {
191  
192 my @invalids = $results->invalid;
193  
194 foreach my $invalid (@invalids) {
195  
196 printf("%s %s\n", $invalid, $results->msgs->{$invalid});
197  
198 }
199  
200 }
201  
202 =head1 DESCRIPTION
203  
204 This module combines multiple validation profiles into one
205 L<Data::FormValidator|https://metacpan.org/pod/Data::FormValidator> validator.
206  
207 =head1 METHODS
208  
209 =head2 new($hash, ...)
210  
211 This method initilizes the validator by combining multiple profiles.
212  
213 =over 4
214  
215 =item B<$hash>
216  
217 A hash of validation profiles, there may be more then one. They are combined
218 such that later profiles may overwrite earlier ones.
219  
220 =back
221  
222 =head1 OVERRIDES
223  
224 This module overrides the following methods in L<Data::FormValidator::Results|https://metacpan.org/pod/Data::FormValidator::Results>.
225  
226 =head2 _generate_msgs
227  
228 For whatever reason, it wouldn't find error messages for constraints. Not
229 sure why. No bugs reports have been filed about this. But it wouldn't work
230 for me as documented, now it does.
231  
232 =head2 _error_msg_fmt
233  
234 A supporting routine for _generate_msgs(). Allowed it to be referenced
235 from $self.
236  
237 =head2 prefix_hash
238  
239 A supporting routine for _generate_msgs(). Allowed it to be referenced
240 from $self.
241  
242 =head1 SEE ALSO
243  
244 =over 4
245  
246 =item L<Data::FormValidator|https://metacpan.org/pod/Data::FormValidator>
247  
248 =item L<XAS::Service|XAS::Service>
249  
250 =item L<XAS|XAS>
251  
252 =back
253  
254 =head1 AUTHOR
255  
256 Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt>
257  
258 =head1 COPYRIGHT AND LICENSE
259  
260 Copyright (c) 2012-2016 Kevin L. Esteb
261  
262 This is free software; you can redistribute it and/or modify it under
263 the terms of the Artistic License 2.0. For details, see the full text
264 of the license at http://www.perlfoundation.org/artistic_license_2_0.
265  
266 =cut
« Service/Search.pm ↑ Summary Service/Resource.pm »