18 |
|
|
19 |
=head1 VERSION |
=head1 VERSION |
20 |
|
|
21 |
Version 0.07 |
Version 0.11 |
22 |
|
|
23 |
=cut |
=cut |
24 |
|
|
25 |
our $VERSION = '0.07'; |
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, $rec_dump ); |
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; |
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'); |
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 |
|
my $l = _pack_subfields_hash( $v, 1 ); |
164 |
|
my $subfield_dump = $l; |
165 |
|
my $template = ''; |
166 |
|
$l =~ s/$regex/$template.=$1/eg; |
167 |
|
#warn "## template: $template\n"; |
168 |
|
|
169 |
|
if ( $template ) { |
170 |
|
$self->{_delimiters_templates}->{$f}->{$template}++; |
171 |
|
|
172 |
|
if ( my $v = $self->{_validate_delimiters_templates} ) { |
173 |
|
if ( ! defined( $v->{$template} ) ) { |
174 |
|
$errors->{$f}->{invalid_delimiters_combination} = $template; |
175 |
|
$errors->{$f}->{dump} = $subfield_dump; |
176 |
|
} else { |
177 |
|
warn "## $f $template ok\n"; |
178 |
|
} |
179 |
|
} |
180 |
|
} |
181 |
|
} |
182 |
|
} |
183 |
|
|
184 |
|
next if (defined( $self->{dont_validate}->{$f} )); |
185 |
|
|
186 |
|
# track field usage |
187 |
$fields->{$f}++; |
$fields->{$f}++; |
188 |
|
|
189 |
if ( ! defined($r->{$f}) ) { |
if ( ! defined($r->{$f}) ) { |
236 |
|
|
237 |
foreach my $sf (@r_sf) { |
foreach my $sf (@r_sf) { |
238 |
$errors->{$f}->{subfield}->{extra_repeatable}->{$sf}++; |
$errors->{$f}->{subfield}->{extra_repeatable}->{$sf}++; |
239 |
$errors->{$f}->{dump} = |
$errors->{$f}->{dump} = _pack_subfields_hash( $h, 1 ); |
|
join('', _pack_subfields_hash( $h, 1 ) ); |
|
240 |
} |
} |
241 |
|
|
242 |
} |
} |
252 |
} |
} |
253 |
} elsif (ref($v) eq 'HASH') { |
} elsif (ref($v) eq 'HASH') { |
254 |
$errors->{$f}->{unexpected_subfields}++; |
$errors->{$f}->{unexpected_subfields}++; |
255 |
$errors->{$f}->{dump} = |
$errors->{$f}->{dump} = _pack_subfields_hash( $v, 1 ); |
|
join('', _pack_subfields_hash( $v, 1 ) ); |
|
256 |
} |
} |
257 |
} |
} |
258 |
} |
} |
259 |
|
|
260 |
|
$log->debug("_delimiters_templates = ", dump( $self->{_delimiters_templates} ) ); |
261 |
|
|
262 |
foreach my $must (sort keys %{ $self->{must_exist} }) { |
foreach my $must (sort keys %{ $self->{must_exist} }) { |
263 |
next if ($fields->{$must}); |
next if ($fields->{$must}); |
264 |
$errors->{$must}->{missing}++; |
$errors->{$must}->{missing}++; |
266 |
} |
} |
267 |
|
|
268 |
if ($errors) { |
if ($errors) { |
269 |
$log->debug("errors: ", sub { dump( $errors ) } ); |
$log->debug("errors: ", $self->report_error( $errors ) ); |
270 |
|
|
271 |
my $mfn = $rec->{'000'}->[0] || $log->logconfess("record ", dump( $rec ), " doesn't have MFN"); |
my $mfn = $rec->{'000'}->[0] || $log->logconfess("record ", dump( $rec ), " doesn't have MFN"); |
272 |
$self->{errors}->{$mfn} = $errors; |
$self->{errors}->{$mfn} = $errors; |
303 |
return $self->{errors}; |
return $self->{errors}; |
304 |
} |
} |
305 |
|
|
306 |
=head2 report |
=head2 report_error |
307 |
|
|
308 |
Produce nice humanly readable report of errors |
Produce nice humanly readable report of single error |
309 |
|
|
310 |
print $validate->report; |
print $validate->report_error( $error_hash ); |
311 |
|
|
312 |
=cut |
=cut |
313 |
|
|
314 |
sub report { |
sub report_error { |
315 |
my $self = shift; |
my $self = shift; |
316 |
|
|
317 |
sub unroll { |
my $h = shift || die "no hash?"; |
318 |
|
|
319 |
|
sub _unroll { |
320 |
my ($self, $tree, $accumulated) = @_; |
my ($self, $tree, $accumulated) = @_; |
321 |
|
|
322 |
my $log = $self->_get_logger(); |
my $log = $self->_get_logger(); |
338 |
|
|
339 |
if ($k eq 'dump') { |
if ($k eq 'dump') { |
340 |
$dump = $tree->{dump}; |
$dump = $tree->{dump}; |
341 |
warn "## dump: ",dump($dump),"\n"; |
#warn "## dump ",dump($dump),"\n"; |
342 |
next; |
next; |
343 |
} |
} |
344 |
|
|
345 |
$log->debug("current: $k"); |
$log->debug("current: $k"); |
346 |
|
|
347 |
my ($new_results, $new_dump) = $self->unroll($tree->{$k}, |
my ($new_results, $new_dump) = $self->_unroll($tree->{$k}, |
348 |
$accumulated ? "$accumulated\t$k" : $k |
$accumulated ? "$accumulated\t$k" : $k |
349 |
); |
); |
350 |
|
|
368 |
} |
} |
369 |
} |
} |
370 |
|
|
|
my $log = $self->_get_logger(); |
|
|
|
|
|
my $out = ''; |
|
|
my $e = $self->{errors} || return; |
|
371 |
|
|
372 |
sub reformat { |
sub _reformat { |
373 |
my $l = shift; |
my $l = shift; |
374 |
$l =~ s/\t/ /g; |
$l =~ s/\t/ /g; |
375 |
$l =~ s/_/ /; |
$l =~ s/_/ /g; |
376 |
return $l; |
return $l; |
377 |
} |
} |
378 |
|
|
379 |
foreach my $mfn (sort keys %$e) { |
my $out = ''; |
|
$out .= "MFN $mfn\n"; |
|
380 |
|
|
381 |
for my $f (sort keys %{ $e->{$mfn} }) { |
for my $f (sort keys %{ $h }) { |
382 |
my ($r, $d) = $self->unroll( $e->{$mfn}->{$f} ); |
$out .= "$f: "; |
383 |
my $e = $f . ': '; |
|
384 |
if (ref($r) eq 'ARRAY') { |
my ($r, $d) = $self->_unroll( $h->{$f} ); |
385 |
$e .= join(", ", map { reformat( $_ ) } @$r); |
my $e; |
386 |
} else { |
if (ref($r) eq 'ARRAY') { |
387 |
$e .= reformat( $r ); |
$e .= join(", ", map { _reformat( $_ ) } @$r); |
388 |
} |
} else { |
389 |
$e .= "\n\t$d" if ($d); |
$e .= _reformat( $r ); |
390 |
$e .= "\n"; |
} |
391 |
$log->debug("MFN $mfn | $e"); |
$e .= "\n\t$d" if ($d); |
392 |
$out .= $e; |
|
393 |
|
$out .= $e . "\n"; |
394 |
|
} |
395 |
|
return $out; |
396 |
|
} |
397 |
|
|
398 |
|
|
399 |
|
=head2 report |
400 |
|
|
401 |
|
Produce nice humanly readable report of errors |
402 |
|
|
403 |
|
print $validate->report; |
404 |
|
|
405 |
|
=cut |
406 |
|
|
407 |
|
sub report { |
408 |
|
my $self = shift; |
409 |
|
my $e = $self->{errors} || return; |
410 |
|
|
411 |
|
my $out; |
412 |
|
foreach my $mfn (sort { $a <=> $b } keys %$e) { |
413 |
|
$out .= "MFN $mfn\n" . $self->report_error( $e->{$mfn} ) . "\n"; |
414 |
|
} |
415 |
|
|
416 |
|
return $out; |
417 |
|
|
418 |
|
} |
419 |
|
|
420 |
|
=head2 delimiters_templates |
421 |
|
|
422 |
|
Generate report of delimiter tamplates |
423 |
|
|
424 |
|
my $report = $validate->delimiter_teplates( |
425 |
|
report => 1, |
426 |
|
); |
427 |
|
|
428 |
|
Options: |
429 |
|
|
430 |
|
=over 4 |
431 |
|
|
432 |
|
=item report |
433 |
|
|
434 |
|
Generate humanly readable report with single fields |
435 |
|
|
436 |
|
=back |
437 |
|
|
438 |
|
=cut |
439 |
|
|
440 |
|
sub delimiters_templates { |
441 |
|
my $self = shift; |
442 |
|
|
443 |
|
my $args = {@_}; |
444 |
|
|
445 |
|
my $t = $self->{_delimiters_templates}; |
446 |
|
|
447 |
|
my $log = $self->_get_logger; |
448 |
|
|
449 |
|
unless ($t) { |
450 |
|
$log->error("called without delimiters"); |
451 |
|
return; |
452 |
|
} |
453 |
|
|
454 |
|
my $out; |
455 |
|
|
456 |
|
foreach my $f (sort { $a <=> $b } keys %$t) { |
457 |
|
$out .= "$f\n" if ( $args->{report} ); |
458 |
|
foreach my $template (sort { $a cmp $b } keys %{ $t->{$f} }) { |
459 |
|
my $count = $t->{$f}->{$template}; |
460 |
|
$out .= |
461 |
|
( $count ? "" : "# " ) . |
462 |
|
( $args->{report} ? "" : "$f\t" ) . |
463 |
|
"\t$count\t$template\n"; |
464 |
} |
} |
465 |
} |
} |
466 |
|
|