18 |
|
|
19 |
=head1 VERSION |
=head1 VERSION |
20 |
|
|
21 |
Version 0.08 |
Version 0.11 |
22 |
|
|
23 |
=cut |
=cut |
24 |
|
|
25 |
our $VERSION = '0.08'; |
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 |
|
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}) ) { |
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->{$must}->{missing}++; |
$errors->{$must}->{missing}++; |
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] || $log->logconfess("record ", dump( $rec ), " doesn't have MFN"); |
my $mfn = $rec->{'000'}->[0] || $log->logconfess("record ", dump( $rec ), " doesn't have MFN"); |
262 |
$self->{errors}->{$mfn} = $errors; |
$self->{errors}->{$mfn} = $errors; |
293 |
return $self->{errors}; |
return $self->{errors}; |
294 |
} |
} |
295 |
|
|
296 |
=head2 report |
=head2 report_error |
297 |
|
|
298 |
Produce nice humanly readable report of errors |
Produce nice humanly readable report of single error |
299 |
|
|
300 |
print $validate->report; |
print $validate->report_error( $error_hash ); |
301 |
|
|
302 |
=cut |
=cut |
303 |
|
|
304 |
sub report { |
sub report_error { |
305 |
my $self = shift; |
my $self = shift; |
306 |
|
|
307 |
sub unroll { |
my $h = shift || die "no hash?"; |
308 |
|
|
309 |
|
sub _unroll { |
310 |
my ($self, $tree, $accumulated) = @_; |
my ($self, $tree, $accumulated) = @_; |
311 |
|
|
312 |
my $log = $self->_get_logger(); |
my $log = $self->_get_logger(); |
334 |
|
|
335 |
$log->debug("current: $k"); |
$log->debug("current: $k"); |
336 |
|
|
337 |
my ($new_results, $new_dump) = $self->unroll($tree->{$k}, |
my ($new_results, $new_dump) = $self->_unroll($tree->{$k}, |
338 |
$accumulated ? "$accumulated\t$k" : $k |
$accumulated ? "$accumulated\t$k" : $k |
339 |
); |
); |
340 |
|
|
358 |
} |
} |
359 |
} |
} |
360 |
|
|
|
my $log = $self->_get_logger(); |
|
|
|
|
|
my $out = ''; |
|
|
my $e = $self->{errors} || return; |
|
361 |
|
|
362 |
sub reformat { |
sub _reformat { |
363 |
my $l = shift; |
my $l = shift; |
364 |
$l =~ s/\t/ /g; |
$l =~ s/\t/ /g; |
365 |
$l =~ s/_/ /; |
$l =~ s/_/ /; |
366 |
return $l; |
return $l; |
367 |
} |
} |
368 |
|
|
369 |
foreach my $mfn (sort keys %$e) { |
my $out = ''; |
|
$out .= "MFN $mfn\n"; |
|
370 |
|
|
371 |
for my $f (sort keys %{ $e->{$mfn} }) { |
for my $f (sort keys %{ $h }) { |
372 |
my ($r, $d) = $self->unroll( $e->{$mfn}->{$f} ); |
$out .= "$f: "; |
373 |
my $e = $f . ': '; |
|
374 |
if (ref($r) eq 'ARRAY') { |
my ($r, $d) = $self->_unroll( $h->{$f} ); |
375 |
$e .= join(", ", map { reformat( $_ ) } @$r); |
my $e; |
376 |
} else { |
if (ref($r) eq 'ARRAY') { |
377 |
$e .= reformat( $r ); |
$e .= join(", ", map { _reformat( $_ ) } @$r); |
378 |
} |
} else { |
379 |
$e .= "\n\t$d" if ($d); |
$e .= _reformat( $r ); |
|
$e .= "\n"; |
|
|
$log->debug("MFN $mfn | $e"); |
|
|
$out .= $e; |
|
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; |
return $out; |
407 |
|
|
408 |
} |
} |
409 |
|
|
410 |
=head1 AUTHOR |
=head1 AUTHOR |