1 |
package WebPAC::Validate; |
2 |
|
3 |
use warnings; |
4 |
use strict; |
5 |
|
6 |
use blib; |
7 |
|
8 |
use base 'WebPAC::Common'; |
9 |
use File::Slurp; |
10 |
use List::Util qw/first/; |
11 |
use Data::Dump qw/dump/; |
12 |
use WebPAC::Normalize qw/_pack_subfields_hash/; |
13 |
use Storable qw/dclone/; |
14 |
|
15 |
=head1 NAME |
16 |
|
17 |
WebPAC::Validate - provide simple validation for records |
18 |
|
19 |
=head1 VERSION |
20 |
|
21 |
Version 0.12 |
22 |
|
23 |
=cut |
24 |
|
25 |
our $VERSION = '0.12'; |
26 |
|
27 |
=head1 SYNOPSIS |
28 |
|
29 |
This module provide a simple way to validate your file against a simple |
30 |
configuration file in following format: |
31 |
|
32 |
# field 10 doesn't have any subfields |
33 |
10 |
34 |
# same with 101 |
35 |
101 |
36 |
# field 200 have valid subfields a-g |
37 |
# and field e is repeatable |
38 |
200 a b c d e* f g |
39 |
# field 205 can have only subfield a |
40 |
# and must exists |
41 |
205! a |
42 |
# while 210 can have a c or d |
43 |
210 a c d |
44 |
# field which is ignored in validation |
45 |
999- |
46 |
|
47 |
=head1 FUNCTIONS |
48 |
|
49 |
=head2 new |
50 |
|
51 |
Create new validation object |
52 |
|
53 |
my $validate = new WebPAC::Validate( |
54 |
path => 'conf/validate/file', |
55 |
delimiters => [ ' : ', ' / ', ' ; ', ' , ' ], |
56 |
delimiters_path => 'conf/validate/delimiters/file', |
57 |
); |
58 |
|
59 |
Optional parametar C<delimiters> will turn on validating of delimiters. Be |
60 |
careful here, those delimiters are just stuck into regex, so they can |
61 |
contain L<perlre> regexpes. |
62 |
|
63 |
C<path> and C<delimiters_path> can be specified by L<read_validate_file> and |
64 |
L<read_validate_delimiters> calls. |
65 |
|
66 |
=cut |
67 |
|
68 |
sub new { |
69 |
my $class = shift; |
70 |
my $self = {@_}; |
71 |
bless($self, $class); |
72 |
|
73 |
my $log = $self->_get_logger(); |
74 |
|
75 |
$self->read_validate_file( $self->{path} ) if ( $self->{path} ); |
76 |
|
77 |
if ( $self->{delimiters} ) { |
78 |
$self->{delimiters_regex} = '(\^[a-z0-9]|' . join('|', @{ $self->{delimiters} }) . ')'; |
79 |
$log->info("validation check delimiters with regex $self->{delimiters_regex}"); |
80 |
} |
81 |
|
82 |
$self->read_validate_delimiters_file( $self->{delimiters_path} ) if ( $self->{delimiters_path} ); |
83 |
|
84 |
return $self; |
85 |
} |
86 |
|
87 |
|
88 |
=head2 read_validate_file |
89 |
|
90 |
Specify validate rules file |
91 |
|
92 |
$validate->read_validate_file( 'conf/validate/file' ); |
93 |
|
94 |
Returns number of lines in file |
95 |
|
96 |
=cut |
97 |
|
98 |
sub read_validate_file { |
99 |
my $self = shift; |
100 |
|
101 |
my $path = shift || die "no path?"; |
102 |
|
103 |
my $log = $self->_get_logger(); |
104 |
|
105 |
my $v_file = read_file( $path ) || |
106 |
$log->logdie("can't open validate path $path: $!"); |
107 |
|
108 |
my $v; |
109 |
my $curr_line = 1; |
110 |
|
111 |
foreach my $l (split(/[\n\r]+/, $v_file)) { |
112 |
$curr_line++; |
113 |
|
114 |
# skip comments and whitespaces |
115 |
next if ($l =~ /^#/ || $l =~ /^\s*$/); |
116 |
|
117 |
$l =~ s/^\s+//; |
118 |
$l =~ s/\s+$//; |
119 |
|
120 |
my @d = split(/\s+/, $l); |
121 |
|
122 |
my $fld = shift @d; |
123 |
|
124 |
if ($fld =~ s/!$//) { |
125 |
$self->{must_exist}->{$fld}++; |
126 |
} elsif ($fld =~ s/-$//) { |
127 |
$self->{dont_validate}->{$fld}++; |
128 |
} |
129 |
|
130 |
$log->logdie("need field name in line $curr_line: $l") unless (defined($fld)); |
131 |
|
132 |
if (@d) { |
133 |
$v->{$fld} = [ map { |
134 |
my $sf = $_; |
135 |
if ( $sf =~ s/!(\*)?$/$1/ ) { |
136 |
$self->{must_exist_sf}->{ $fld }->{ $sf }++; |
137 |
}; |
138 |
$sf; |
139 |
} @d ]; |
140 |
} else { |
141 |
$v->{$fld} = 1; |
142 |
} |
143 |
|
144 |
} |
145 |
|
146 |
$log->debug("current validation rules: ", dump($v)); |
147 |
|
148 |
$self->{rules} = $v; |
149 |
|
150 |
$log->info("validation uses rules from $path"); |
151 |
|
152 |
return $curr_line; |
153 |
} |
154 |
|
155 |
=head2 read_validate_delimiters_file |
156 |
|
157 |
$validate->read_validate_delimiters_file( 'conf/validate/delimiters/file' ); |
158 |
|
159 |
=cut |
160 |
|
161 |
sub read_validate_delimiters_file { |
162 |
my $self = shift; |
163 |
|
164 |
my $path = shift || die "no path?"; |
165 |
|
166 |
my $log = $self->_get_logger(); |
167 |
|
168 |
if ( -e $path ) { |
169 |
$log->info("using delimiter validation rules from $path"); |
170 |
open(my $d, $path) || $log->fatal("can't open $path: $!"); |
171 |
while(<$d>) { |
172 |
chomp($d); |
173 |
if (/^\s*(#*)\s*(\d+)\t+(\d+)\t+(.*)$/) { |
174 |
my ($comment,$field,$count,$template) = ($1,$2,$3,$4); |
175 |
$self->{_validate_delimiters_templates}->{$field}->{$template} = $count unless ($comment); |
176 |
} else { |
177 |
warn "## ignored $d\n"; |
178 |
} |
179 |
} |
180 |
close($d); |
181 |
#warn "_validate_delimiters_templates = ",dump( $self->{_validate_delimiters_templates} ); |
182 |
} else { |
183 |
$log->warn("delimiters path $path doesn't exist, it will be created after this run"); |
184 |
} |
185 |
} |
186 |
|
187 |
=head2 validate_rec |
188 |
|
189 |
Validate record and return errors |
190 |
|
191 |
my @errors = $validate->validate_rec( $rec, $rec_dump ); |
192 |
|
193 |
=cut |
194 |
|
195 |
sub validate_rec { |
196 |
my $self = shift; |
197 |
|
198 |
my $log = $self->_get_logger(); |
199 |
|
200 |
my $rec = shift || $log->logdie("validate_rec need record"); |
201 |
my $rec_dump = shift; |
202 |
|
203 |
$log->logdie("rec isn't HASH") unless (ref($rec) eq 'HASH'); |
204 |
# $log->logdie("can't find validation rules") unless (my $r = $self->{rules}); |
205 |
my $r = $self->{rules}; |
206 |
|
207 |
my $errors; |
208 |
|
209 |
$log->debug("rec = ", sub { dump($rec) }, "keys = ", keys %{ $rec }); |
210 |
|
211 |
my $fields; |
212 |
|
213 |
foreach my $f (keys %{ $rec }) { |
214 |
|
215 |
next if (!defined($f) || $f eq '' || $f eq '000'); |
216 |
|
217 |
# first check delimiters |
218 |
if ( my $regex = $self->{delimiters_regex} ) { |
219 |
|
220 |
foreach my $v (@{ $rec->{$f} }) { |
221 |
my $l = _pack_subfields_hash( $v, 1 ); |
222 |
my $subfield_dump = $l; |
223 |
my $template = ''; |
224 |
$l =~ s/$regex/$template.=$1/eg; |
225 |
#warn "## template: $template\n"; |
226 |
|
227 |
if ( $template ) { |
228 |
$self->{_delimiters_templates}->{$f}->{$template}++; |
229 |
|
230 |
if ( my $v = $self->{_validate_delimiters_templates} ) { |
231 |
if ( ! defined( $v->{$f}->{$template} ) ) { |
232 |
$errors->{$f}->{potentially_invalid_combination} = $template; |
233 |
$errors->{$f}->{dump} = $subfield_dump; |
234 |
#} else { |
235 |
# warn "## $f $template ok\n"; |
236 |
} |
237 |
} |
238 |
} |
239 |
} |
240 |
} |
241 |
|
242 |
next unless ( $r ); # skip validation of no rules are specified |
243 |
|
244 |
next if (defined( $self->{dont_validate}->{$f} )); |
245 |
|
246 |
# track field usage |
247 |
$fields->{$f}++; |
248 |
|
249 |
if ( ! defined($r->{$f}) ) { |
250 |
$errors->{ $f }->{unexpected} = "this field is not expected"; |
251 |
next; |
252 |
} |
253 |
|
254 |
|
255 |
if (ref($rec->{$f}) ne 'ARRAY') { |
256 |
$errors->{ $f }->{not_repeatable} = "probably bug in parsing input data"; |
257 |
next; |
258 |
} |
259 |
|
260 |
foreach my $v (@{ $rec->{$f} }) { |
261 |
# can we have subfields? |
262 |
if (ref($r->{$f}) eq 'ARRAY') { |
263 |
# are values hashes? (has subfields) |
264 |
if (! defined($v)) { |
265 |
# $errors->{$f}->{empty} = undef; |
266 |
# $errors->{dump} = $rec_dump if ($rec_dump); |
267 |
} elsif (ref($v) ne 'HASH') { |
268 |
$errors->{$f}->{missing_subfield} = join(",", @{ $r->{$f} }) . " required"; |
269 |
next; |
270 |
} else { |
271 |
|
272 |
my $h = dclone( $v ); |
273 |
|
274 |
my $sf_repeatable; |
275 |
|
276 |
delete($v->{subfields}) if (defined($v->{subfields})); |
277 |
|
278 |
my $subfields; |
279 |
|
280 |
foreach my $sf (keys %{ $v }) { |
281 |
|
282 |
$subfields->{ $sf }++; |
283 |
|
284 |
# is non-repeatable but with multiple values? |
285 |
if ( ! first { $_ eq $sf.'*' } @{$r->{$f}} ) { |
286 |
if ( ref($v->{$sf}) eq 'ARRAY' ) { |
287 |
$sf_repeatable->{$sf}++; |
288 |
}; |
289 |
if (! first { $_ eq $sf } @{ $r->{$f} }) { |
290 |
$errors->{ $f }->{subfield}->{extra}->{$sf}++; |
291 |
} |
292 |
} |
293 |
|
294 |
} |
295 |
if (my @r_sf = sort keys( %$sf_repeatable )) { |
296 |
|
297 |
foreach my $sf (@r_sf) { |
298 |
$errors->{$f}->{subfield}->{extra_repeatable}->{$sf}++; |
299 |
$errors->{$f}->{dump} = _pack_subfields_hash( $h, 1 ); |
300 |
} |
301 |
|
302 |
} |
303 |
|
304 |
if ( defined( $self->{must_exist_sf}->{$f} ) ) { |
305 |
foreach my $sf (sort keys %{ $self->{must_exist_sf}->{$f} }) { |
306 |
#warn "====> $f $sf must exist\n"; |
307 |
$errors->{$f}->{subfield}->{missing}->{$sf}++ |
308 |
unless defined( $subfields->{$sf} ); |
309 |
} |
310 |
} |
311 |
|
312 |
} |
313 |
} elsif (ref($v) eq 'HASH') { |
314 |
$errors->{$f}->{unexpected_subfields}++; |
315 |
$errors->{$f}->{dump} = _pack_subfields_hash( $v, 1 ); |
316 |
} |
317 |
} |
318 |
} |
319 |
|
320 |
$log->debug("_delimiters_templates = ", sub { dump( $self->{_delimiters_templates} ) } ); |
321 |
|
322 |
foreach my $must (sort keys %{ $self->{must_exist} }) { |
323 |
next if ($fields->{$must}); |
324 |
$errors->{$must}->{missing}++; |
325 |
$errors->{dump} = $rec_dump if ($rec_dump); |
326 |
} |
327 |
|
328 |
if ($errors) { |
329 |
$log->debug("errors: ", $self->report_error( $errors ) ); |
330 |
|
331 |
my $mfn = $rec->{'000'}->[0] || $log->logconfess("record ", sub { dump( $rec ) }, " doesn't have MFN"); |
332 |
$self->{errors}->{$mfn} = $errors; |
333 |
} |
334 |
|
335 |
#$log->logcluck("return from this function is ARRAY") unless wantarray; |
336 |
|
337 |
return $errors; |
338 |
} |
339 |
|
340 |
=head2 reset |
341 |
|
342 |
Clean all accumulated errors for this input and remember delimiter templates |
343 |
for L<save_delimiters_templates> |
344 |
|
345 |
$validate->reset; |
346 |
|
347 |
This function B<must> be called after each input to provide accurate statistics. |
348 |
|
349 |
=cut |
350 |
|
351 |
sub reset { |
352 |
my $self = shift; |
353 |
|
354 |
my $log = $self->_get_logger; |
355 |
|
356 |
delete ($self->{errors}); |
357 |
|
358 |
if ( ! $self->{_delimiters_templates} ) { |
359 |
$log->debug("called without _delimiters_templates?"); |
360 |
return; |
361 |
} |
362 |
|
363 |
foreach my $f ( keys %{ $self->{_delimiters_templates} } ) { |
364 |
foreach my $t ( keys %{ $self->{_delimiters_templates}->{$f} } ) { |
365 |
$self->{_accumulated_delimiters_templates}->{$f}->{$t} += |
366 |
$self->{_delimiters_templates}->{$f}->{$t}; |
367 |
} |
368 |
} |
369 |
$log->debug("_accumulated_delimiters_templates = ", sub { dump( $self->{_accumulated_delimiter_templates} ) } ); |
370 |
delete ($self->{_delimiters_templates}); |
371 |
} |
372 |
|
373 |
=head2 all_errors |
374 |
|
375 |
Return hash with all errors |
376 |
|
377 |
print dump( $validate->all_errors ); |
378 |
|
379 |
=cut |
380 |
|
381 |
sub all_errors { |
382 |
my $self = shift; |
383 |
return $self->{errors}; |
384 |
} |
385 |
|
386 |
=head2 report_error |
387 |
|
388 |
Produce nice humanly readable report of single error |
389 |
|
390 |
print $validate->report_error( $error_hash ); |
391 |
|
392 |
=cut |
393 |
|
394 |
sub report_error { |
395 |
my $self = shift; |
396 |
|
397 |
my $h = shift || die "no hash?"; |
398 |
|
399 |
sub _unroll { |
400 |
my ($self, $tree, $accumulated) = @_; |
401 |
|
402 |
my $log = $self->_get_logger(); |
403 |
|
404 |
$log->debug("# ", |
405 |
( $tree ? "tree: $tree " : '' ), |
406 |
( $accumulated ? "accumulated: $accumulated " : '' ), |
407 |
); |
408 |
|
409 |
my $results; |
410 |
|
411 |
if (ref($tree) ne 'HASH') { |
412 |
return ("$accumulated\t($tree)", undef); |
413 |
} |
414 |
|
415 |
my $dump; |
416 |
|
417 |
foreach my $k (sort keys %{ $tree }) { |
418 |
|
419 |
if ($k eq 'dump') { |
420 |
$dump = $tree->{dump}; |
421 |
#warn "## dump ",dump($dump),"\n"; |
422 |
next; |
423 |
} |
424 |
|
425 |
$log->debug("current: $k"); |
426 |
|
427 |
my ($new_results, $new_dump) = $self->_unroll($tree->{$k}, |
428 |
$accumulated ? "$accumulated\t$k" : $k |
429 |
); |
430 |
|
431 |
$log->debug( "new_results: ", sub { dump($new_results) } ) if ( $new_results ); |
432 |
|
433 |
push @$results, $new_results if ($new_results); |
434 |
$dump = $new_dump if ($new_dump); |
435 |
|
436 |
} |
437 |
|
438 |
$log->debug( "results: ", sub { dump($results) } ) if ( $results ); |
439 |
|
440 |
if ($#$results == 0) { |
441 |
return ($results->[0], $dump); |
442 |
} else { |
443 |
return ($results, $dump); |
444 |
} |
445 |
} |
446 |
|
447 |
|
448 |
sub _reformat { |
449 |
my $l = shift; |
450 |
$l =~ s/\t/ /g; |
451 |
$l =~ s/_/ /g; |
452 |
return $l; |
453 |
} |
454 |
|
455 |
my $out = ''; |
456 |
|
457 |
for my $f (sort keys %{ $h }) { |
458 |
$out .= "$f: "; |
459 |
|
460 |
my ($r, $d) = $self->_unroll( $h->{$f} ); |
461 |
my $e; |
462 |
if (ref($r) eq 'ARRAY') { |
463 |
$e .= join(", ", map { _reformat( $_ ) } @$r); |
464 |
} else { |
465 |
$e .= _reformat( $r ); |
466 |
} |
467 |
$e .= "\n\t$d" if ($d); |
468 |
|
469 |
$out .= $e . "\n"; |
470 |
} |
471 |
return $out; |
472 |
} |
473 |
|
474 |
|
475 |
=head2 report |
476 |
|
477 |
Produce nice humanly readable report of errors |
478 |
|
479 |
print $validate->report; |
480 |
|
481 |
=cut |
482 |
|
483 |
sub report { |
484 |
my $self = shift; |
485 |
my $e = $self->{errors} || return; |
486 |
|
487 |
my $out; |
488 |
foreach my $mfn (sort { $a <=> $b } keys %$e) { |
489 |
$out .= "MFN $mfn\n" . $self->report_error( $e->{$mfn} ) . "\n"; |
490 |
} |
491 |
|
492 |
return $out; |
493 |
|
494 |
} |
495 |
|
496 |
=head2 delimiters_templates |
497 |
|
498 |
Generate report of delimiter tamplates |
499 |
|
500 |
my $report = $validate->delimiter_teplates( |
501 |
report => 1, |
502 |
current_input => 1, |
503 |
); |
504 |
|
505 |
Options: |
506 |
|
507 |
=over 4 |
508 |
|
509 |
=item report |
510 |
|
511 |
Generate humanly readable report with single fields |
512 |
|
513 |
=item current_input |
514 |
|
515 |
Report just current_input and not accumulated data |
516 |
|
517 |
=back |
518 |
|
519 |
=cut |
520 |
|
521 |
sub delimiters_templates { |
522 |
my $self = shift; |
523 |
|
524 |
my $args = {@_}; |
525 |
|
526 |
my $t = $self->{_accumulated_delimiters_templates}; |
527 |
$t = $self->{_delimiters_templates} if ( $args->{current_input} ); |
528 |
|
529 |
my $log = $self->_get_logger; |
530 |
|
531 |
unless ($t) { |
532 |
$log->error("called without delimiters"); |
533 |
return; |
534 |
} |
535 |
|
536 |
my $out; |
537 |
|
538 |
foreach my $f (sort { $a <=> $b } keys %$t) { |
539 |
$out .= "$f\n" if ( $args->{report} ); |
540 |
foreach my $template (sort { $a cmp $b } keys %{ $t->{$f} }) { |
541 |
my $count = $t->{$f}->{$template}; |
542 |
$out .= |
543 |
( $count ? "" : "# " ) . |
544 |
( $args->{report} ? "" : "$f" ) . |
545 |
"\t$count\t$template\n"; |
546 |
} |
547 |
} |
548 |
|
549 |
return $out; |
550 |
} |
551 |
|
552 |
=head2 save_delimiters_templates |
553 |
|
554 |
Save accumulated delimiter templates |
555 |
|
556 |
$validator->save_delimiters_template( '/path/to/validate/delimiters' ); |
557 |
|
558 |
=cut |
559 |
|
560 |
sub save_delimiters_templates { |
561 |
my $self = shift; |
562 |
|
563 |
my $path = $self->{delimiters_path}; |
564 |
|
565 |
return unless ( $path ); |
566 |
|
567 |
my $log = $self->_get_logger; |
568 |
|
569 |
if ( ! $self->{_accumulated_delimiters_templates} ) { |
570 |
$log->error('no _accumulated_delimiters_templates found, reset'); |
571 |
$self->reset; |
572 |
} |
573 |
|
574 |
if ( ! $self->{_delimiters_templates} ) { |
575 |
$log->error('found _delimiters_templates, calling reset'); |
576 |
$self->reset; |
577 |
} |
578 |
|
579 |
$path .= '.new' if ( -e $path ); |
580 |
|
581 |
open(my $d, '>', $path) || $log->fatal("can't open $path: $!"); |
582 |
print $d $self->delimiters_templates; |
583 |
close($d); |
584 |
|
585 |
$log->info("new delimiters templates saved to $path"); |
586 |
} |
587 |
|
588 |
=head1 AUTHOR |
589 |
|
590 |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
591 |
|
592 |
=head1 COPYRIGHT & LICENSE |
593 |
|
594 |
Copyright 2006 Dobrica Pavlinusic, All Rights Reserved. |
595 |
|
596 |
This program is free software; you can redistribute it and/or modify it |
597 |
under the same terms as Perl itself. |
598 |
|
599 |
=cut |
600 |
|
601 |
1; # End of WebPAC::Validate |