18 |
|
|
19 |
=head1 VERSION |
=head1 VERSION |
20 |
|
|
21 |
Version 0.06 |
Version 0.11 |
22 |
|
|
23 |
=cut |
=cut |
24 |
|
|
25 |
our $VERSION = '0.06'; |
our $VERSION = '0.11'; |
26 |
|
|
27 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
28 |
|
|
41 |
205! a |
205! a |
42 |
# while 210 can have a c or d |
# while 210 can have a c or d |
43 |
210 a c d |
210 a c d |
44 |
|
# field which is ignored in validation |
45 |
|
999- |
46 |
|
|
47 |
=head1 FUNCTIONS |
=head1 FUNCTIONS |
48 |
|
|
52 |
|
|
53 |
my $validate = new WebPAC::Validate( |
my $validate = new WebPAC::Validate( |
54 |
path => 'conf/validate/file', |
path => 'conf/validate/file', |
55 |
|
delimiters => [ ' : ', ' / ', ' ; ', ' , ' ], |
56 |
); |
); |
57 |
|
|
58 |
|
Optional parametar C<delimiters> will turn on validating of delimiters. Be |
59 |
|
careful here, those delimiters are just stuck into regex, so they can |
60 |
|
contain L<perlre> regexpes. |
61 |
|
|
62 |
=cut |
=cut |
63 |
|
|
64 |
sub new { |
sub new { |
93 |
|
|
94 |
if ($fld =~ s/!$//) { |
if ($fld =~ s/!$//) { |
95 |
$self->{must_exist}->{$fld}++; |
$self->{must_exist}->{$fld}++; |
96 |
|
} elsif ($fld =~ s/-$//) { |
97 |
|
$self->{dont_validate}->{$fld}++; |
98 |
} |
} |
99 |
|
|
100 |
$log->logdie("need field name in line $curr_line: $l") unless (defined($fld)); |
$log->logdie("need field name in line $curr_line: $l") unless (defined($fld)); |
119 |
|
|
120 |
$log->info("validation uses rules from $self->{path}"); |
$log->info("validation uses rules from $self->{path}"); |
121 |
|
|
122 |
|
if ( $self->{delimiters} ) { |
123 |
|
$self->{delimiters_regex} = '(\^[a-z0-9]|' . join('|', @{ $self->{delimiters} }) . ')'; |
124 |
|
$log->info("validation check delimiters with regex $self->{delimiters_regex}"); |
125 |
|
} |
126 |
|
|
127 |
$self ? return $self : return undef; |
$self ? return $self : return undef; |
128 |
} |
} |
129 |
|
|
130 |
=head2 validate_errors |
=head2 validate_rec |
131 |
|
|
132 |
Validate record and return errors |
Validate record and return errors |
133 |
|
|
134 |
my @errors = $validate->validate_errors( $rec ); |
my @errors = $validate->validate_rec( $rec, $rec_dump ); |
135 |
|
|
136 |
=cut |
=cut |
137 |
|
|
138 |
sub validate_errors { |
sub validate_rec { |
139 |
my $self = shift; |
my $self = shift; |
140 |
|
|
141 |
my $log = $self->_get_logger(); |
my $log = $self->_get_logger(); |
142 |
|
|
143 |
my $rec = shift || $log->logdie("validate_errors need record"); |
my $rec = shift || $log->logdie("validate_rec need record"); |
144 |
|
my $rec_dump = shift; |
145 |
|
|
146 |
$log->logdie("rec isn't HASH") unless (ref($rec) eq 'HASH'); |
$log->logdie("rec isn't HASH") unless (ref($rec) eq 'HASH'); |
147 |
$log->logdie("can't find validation rules") unless (my $r = $self->{rules}); |
$log->logdie("can't find validation rules") unless (my $r = $self->{rules}); |
156 |
|
|
157 |
next if (!defined($f) || $f eq '' || $f eq '000'); |
next if (!defined($f) || $f eq '' || $f eq '000'); |
158 |
|
|
159 |
|
# first check delimiters |
160 |
|
if ( my $regex = $self->{delimiters_regex} ) { |
161 |
|
|
162 |
|
foreach my $v (@{ $rec->{$f} }) { |
163 |
|
warn "## v = ", dump( $v ); |
164 |
|
my $l = _pack_subfields_hash( $v, 1 ); |
165 |
|
warn "## $l [$regex]\n"; |
166 |
|
my $template = ''; |
167 |
|
$l =~ s/$regex/$template.=$1/eg && warn "## new: $l\n"; |
168 |
|
warn "## template: $template\n"; |
169 |
|
$self->{_delimiters_templates}->{$f}->{$template}++ if ( $template ); |
170 |
|
} |
171 |
|
|
172 |
|
} |
173 |
|
|
174 |
|
next if (defined( $self->{dont_validate}->{$f} )); |
175 |
|
|
176 |
|
# track field usage |
177 |
$fields->{$f}++; |
$fields->{$f}++; |
178 |
|
|
179 |
if ( ! defined($r->{$f}) ) { |
if ( ! defined($r->{$f}) ) { |
180 |
$errors->{field}->{ $f }->{extra} = "not expected"; |
$errors->{ $f }->{unexpected} = "this field is not expected"; |
181 |
next; |
next; |
182 |
} |
} |
183 |
|
|
184 |
|
|
185 |
if (ref($rec->{$f}) ne 'ARRAY') { |
if (ref($rec->{$f}) ne 'ARRAY') { |
186 |
$errors->{field}->{ $f }->{not_repeatable} = "probably bug in parsing input data"; |
$errors->{ $f }->{not_repeatable} = "probably bug in parsing input data"; |
187 |
next; |
next; |
188 |
} |
} |
189 |
|
|
192 |
if (ref($r->{$f}) eq 'ARRAY') { |
if (ref($r->{$f}) eq 'ARRAY') { |
193 |
# are values hashes? (has subfields) |
# are values hashes? (has subfields) |
194 |
if (! defined($v)) { |
if (! defined($v)) { |
195 |
$errors->{field}->{$f}->{empty} = undef; |
# $errors->{$f}->{empty} = undef; |
196 |
$errors->{dump}->{record}++; |
# $errors->{dump} = $rec_dump if ($rec_dump); |
197 |
} elsif (ref($v) ne 'HASH') { |
} elsif (ref($v) ne 'HASH') { |
198 |
$errors->{field}->{$f}->{missing_subfield} = "value without subfields: $v"; |
$errors->{$f}->{missing_subfield} = join(",", @{ $r->{$f} }) . " required"; |
199 |
next; |
next; |
200 |
} else { |
} else { |
201 |
|
|
217 |
$sf_repeatable->{$sf}++; |
$sf_repeatable->{$sf}++; |
218 |
}; |
}; |
219 |
if (! first { $_ eq $sf } @{ $r->{$f} }) { |
if (! first { $_ eq $sf } @{ $r->{$f} }) { |
220 |
$errors->{field}->{ $f }->{subfield}->{$sf} = "unknown"; |
$errors->{ $f }->{subfield}->{extra}->{$sf}++; |
221 |
} |
} |
222 |
} |
} |
223 |
|
|
225 |
if (my @r_sf = sort keys( %$sf_repeatable )) { |
if (my @r_sf = sort keys( %$sf_repeatable )) { |
226 |
|
|
227 |
foreach my $sf (@r_sf) { |
foreach my $sf (@r_sf) { |
228 |
$errors->{field}->{$f}->{subfield}->{$sf} = "repeatable"; |
$errors->{$f}->{subfield}->{extra_repeatable}->{$sf}++; |
229 |
$errors->{dump}->{field}->{$f} = |
$errors->{$f}->{dump} = _pack_subfields_hash( $h, 1 ); |
|
join('', _pack_subfields_hash( $h, 1 ) ); |
|
230 |
} |
} |
231 |
|
|
232 |
} |
} |
234 |
if ( defined( $self->{must_exist_sf}->{$f} ) ) { |
if ( defined( $self->{must_exist_sf}->{$f} ) ) { |
235 |
foreach my $sf (sort keys %{ $self->{must_exist_sf}->{$f} }) { |
foreach my $sf (sort keys %{ $self->{must_exist_sf}->{$f} }) { |
236 |
#warn "====> $f $sf must exist\n"; |
#warn "====> $f $sf must exist\n"; |
237 |
$errors->{field}->{$f}->{subfield}->{$sf} = "missing" |
$errors->{$f}->{subfield}->{missing}->{$sf}++ |
238 |
unless defined( $subfields->{$sf} ); |
unless defined( $subfields->{$sf} ); |
239 |
} |
} |
240 |
} |
} |
241 |
|
|
242 |
} |
} |
243 |
} elsif (ref($v) eq 'HASH') { |
} elsif (ref($v) eq 'HASH') { |
244 |
$errors->{field}->{$f}->{unexpected_subfields}++; |
$errors->{$f}->{unexpected_subfields}++; |
245 |
$errors->{dump}->{field}->{$f} = |
$errors->{$f}->{dump} = _pack_subfields_hash( $v, 1 ); |
|
join('', _pack_subfields_hash( $v, 1 ) ); |
|
246 |
} |
} |
247 |
} |
} |
248 |
} |
} |
249 |
|
|
250 |
|
$log->debug("_delimiters_templates = ", dump( $self->{_delimiters_templates} ) ); |
251 |
|
|
252 |
foreach my $must (sort keys %{ $self->{must_exist} }) { |
foreach my $must (sort keys %{ $self->{must_exist} }) { |
253 |
next if ($fields->{$must}); |
next if ($fields->{$must}); |
254 |
$errors->{field}->{$must}->{missing}++; |
$errors->{$must}->{missing}++; |
255 |
$errors->{dump}->{record}++; |
$errors->{dump} = $rec_dump if ($rec_dump); |
256 |
} |
} |
257 |
|
|
258 |
if ($errors) { |
if ($errors) { |
259 |
$log->debug("errors: ", sub { dump( $errors ) } ); |
$log->debug("errors: ", $self->report_error( $errors ) ); |
260 |
|
|
261 |
my $mfn = $rec->{'000'}->[0]; |
my $mfn = $rec->{'000'}->[0] || $log->logconfess("record ", dump( $rec ), " doesn't have MFN"); |
262 |
$self->{errors}->{$mfn} = $errors; |
$self->{errors}->{$mfn} = $errors; |
263 |
} |
} |
264 |
|
|
267 |
return $errors; |
return $errors; |
268 |
} |
} |
269 |
|
|
270 |
|
=head2 reset_errors |
271 |
|
|
272 |
|
Clean all accumulated errors for this input |
273 |
|
|
274 |
|
$validate->reset_errors; |
275 |
|
|
276 |
|
=cut |
277 |
|
|
278 |
|
sub reset_errors { |
279 |
|
my $self = shift; |
280 |
|
delete ($self->{errors}); |
281 |
|
} |
282 |
|
|
283 |
|
=head2 all_errors |
284 |
|
|
285 |
|
Return hash with all errors |
286 |
|
|
287 |
|
print dump( $validate->all_errors ); |
288 |
|
|
289 |
|
=cut |
290 |
|
|
291 |
|
sub all_errors { |
292 |
|
my $self = shift; |
293 |
|
return $self->{errors}; |
294 |
|
} |
295 |
|
|
296 |
|
=head2 report_error |
297 |
|
|
298 |
|
Produce nice humanly readable report of single error |
299 |
|
|
300 |
|
print $validate->report_error( $error_hash ); |
301 |
|
|
302 |
|
=cut |
303 |
|
|
304 |
|
sub report_error { |
305 |
|
my $self = shift; |
306 |
|
|
307 |
|
my $h = shift || die "no hash?"; |
308 |
|
|
309 |
|
sub _unroll { |
310 |
|
my ($self, $tree, $accumulated) = @_; |
311 |
|
|
312 |
|
my $log = $self->_get_logger(); |
313 |
|
|
314 |
|
$log->debug("# ", |
315 |
|
( $tree ? "tree: $tree " : '' ), |
316 |
|
( $accumulated ? "accumulated: $accumulated " : '' ), |
317 |
|
); |
318 |
|
|
319 |
|
my $results; |
320 |
|
|
321 |
|
if (ref($tree) ne 'HASH') { |
322 |
|
return ("$accumulated\t($tree)", undef); |
323 |
|
} |
324 |
|
|
325 |
|
my $dump; |
326 |
|
|
327 |
|
foreach my $k (sort keys %{ $tree }) { |
328 |
|
|
329 |
|
if ($k eq 'dump') { |
330 |
|
$dump = $tree->{dump}; |
331 |
|
# warn "## dump: ",dump($dump),"\n"; |
332 |
|
next; |
333 |
|
} |
334 |
|
|
335 |
|
$log->debug("current: $k"); |
336 |
|
|
337 |
|
my ($new_results, $new_dump) = $self->_unroll($tree->{$k}, |
338 |
|
$accumulated ? "$accumulated\t$k" : $k |
339 |
|
); |
340 |
|
|
341 |
|
$log->debug( |
342 |
|
( $new_results ? "new_results: " . dump($new_results) ." " : '' ), |
343 |
|
); |
344 |
|
|
345 |
|
push @$results, $new_results if ($new_results); |
346 |
|
$dump = $new_dump if ($new_dump); |
347 |
|
|
348 |
|
} |
349 |
|
|
350 |
|
$log->debug( |
351 |
|
( $results ? "results: " . dump($results) ." " : '' ), |
352 |
|
); |
353 |
|
|
354 |
|
if ($#$results == 0) { |
355 |
|
return ($results->[0], $dump); |
356 |
|
} else { |
357 |
|
return ($results, $dump); |
358 |
|
} |
359 |
|
} |
360 |
|
|
361 |
|
|
362 |
|
sub _reformat { |
363 |
|
my $l = shift; |
364 |
|
$l =~ s/\t/ /g; |
365 |
|
$l =~ s/_/ /; |
366 |
|
return $l; |
367 |
|
} |
368 |
|
|
369 |
|
my $out = ''; |
370 |
|
|
371 |
|
for my $f (sort keys %{ $h }) { |
372 |
|
$out .= "$f: "; |
373 |
|
|
374 |
|
my ($r, $d) = $self->_unroll( $h->{$f} ); |
375 |
|
my $e; |
376 |
|
if (ref($r) eq 'ARRAY') { |
377 |
|
$e .= join(", ", map { _reformat( $_ ) } @$r); |
378 |
|
} else { |
379 |
|
$e .= _reformat( $r ); |
380 |
|
} |
381 |
|
$e .= "\n\t$d" if ($d); |
382 |
|
|
383 |
|
$out .= $e . "\n"; |
384 |
|
} |
385 |
|
return $out; |
386 |
|
} |
387 |
|
|
388 |
|
|
389 |
|
=head2 report |
390 |
|
|
391 |
|
Produce nice humanly readable report of errors |
392 |
|
|
393 |
|
print $validate->report; |
394 |
|
|
395 |
|
=cut |
396 |
|
|
397 |
|
sub report { |
398 |
|
my $self = shift; |
399 |
|
my $e = $self->{errors} || return; |
400 |
|
|
401 |
|
my $out; |
402 |
|
foreach my $mfn (sort { $a <=> $b } keys %$e) { |
403 |
|
$out .= "MFN $mfn\n" . $self->report_error( $e->{$mfn} ) . "\n"; |
404 |
|
} |
405 |
|
|
406 |
|
return $out; |
407 |
|
|
408 |
|
} |
409 |
|
|
410 |
=head1 AUTHOR |
=head1 AUTHOR |
411 |
|
|
412 |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |