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, $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 |
|
my $template = ''; |
163 |
|
|
164 |
|
foreach my $v (@{ $rec->{$f} }) { |
165 |
|
warn "## v = ", dump( $v ); |
166 |
|
my $l = _pack_subfields_hash( $v, 1 ); |
167 |
|
warn "## $l [$regex]\n"; |
168 |
|
$l =~ s/$regex/$template.=$1/eg && warn "## new: $l\n"; |
169 |
|
warn "## template: $template\n"; |
170 |
|
$self->{_delimiters_templates}->{$f}->{$template}++; |
171 |
|
} |
172 |
|
|
173 |
|
} |
174 |
|
|
175 |
|
next if (defined( $self->{dont_validate}->{$f} )); |
176 |
|
|
177 |
|
# track field usage |
178 |
$fields->{$f}++; |
$fields->{$f}++; |
179 |
|
|
180 |
if ( ! defined($r->{$f}) ) { |
if ( ! defined($r->{$f}) ) { |
181 |
$errors->{field}->{ $f }->{unexpected} = "this field is not expected"; |
$errors->{ $f }->{unexpected} = "this field is not expected"; |
182 |
next; |
next; |
183 |
} |
} |
184 |
|
|
185 |
|
|
186 |
if (ref($rec->{$f}) ne 'ARRAY') { |
if (ref($rec->{$f}) ne 'ARRAY') { |
187 |
$errors->{field}->{ $f }->{not_repeatable} = "probably bug in parsing input data"; |
$errors->{ $f }->{not_repeatable} = "probably bug in parsing input data"; |
188 |
next; |
next; |
189 |
} |
} |
190 |
|
|
193 |
if (ref($r->{$f}) eq 'ARRAY') { |
if (ref($r->{$f}) eq 'ARRAY') { |
194 |
# are values hashes? (has subfields) |
# are values hashes? (has subfields) |
195 |
if (! defined($v)) { |
if (! defined($v)) { |
196 |
$errors->{field}->{$f}->{empty} = undef; |
# $errors->{$f}->{empty} = undef; |
197 |
$errors->{dump} = $rec_dump if ($rec_dump); |
# $errors->{dump} = $rec_dump if ($rec_dump); |
198 |
} elsif (ref($v) ne 'HASH') { |
} elsif (ref($v) ne 'HASH') { |
199 |
$errors->{field}->{$f}->{missing_subfield} = "subfields required for this field"; |
$errors->{$f}->{missing_subfield} = join(",", @{ $r->{$f} }) . " required"; |
200 |
next; |
next; |
201 |
} else { |
} else { |
202 |
|
|
218 |
$sf_repeatable->{$sf}++; |
$sf_repeatable->{$sf}++; |
219 |
}; |
}; |
220 |
if (! first { $_ eq $sf } @{ $r->{$f} }) { |
if (! first { $_ eq $sf } @{ $r->{$f} }) { |
221 |
$errors->{field}->{ $f }->{subfield}->{extra}->{$sf}++; |
$errors->{ $f }->{subfield}->{extra}->{$sf}++; |
222 |
} |
} |
223 |
} |
} |
224 |
|
|
226 |
if (my @r_sf = sort keys( %$sf_repeatable )) { |
if (my @r_sf = sort keys( %$sf_repeatable )) { |
227 |
|
|
228 |
foreach my $sf (@r_sf) { |
foreach my $sf (@r_sf) { |
229 |
$errors->{field}->{$f}->{subfield}->{repeatable}->{$sf}++; |
$errors->{$f}->{subfield}->{extra_repeatable}->{$sf}++; |
230 |
$errors->{field}->{$f}->{dump}->{$f} = |
$errors->{$f}->{dump} = _pack_subfields_hash( $h, 1 ); |
|
join('', _pack_subfields_hash( $h, 1 ) ); |
|
231 |
} |
} |
232 |
|
|
233 |
} |
} |
235 |
if ( defined( $self->{must_exist_sf}->{$f} ) ) { |
if ( defined( $self->{must_exist_sf}->{$f} ) ) { |
236 |
foreach my $sf (sort keys %{ $self->{must_exist_sf}->{$f} }) { |
foreach my $sf (sort keys %{ $self->{must_exist_sf}->{$f} }) { |
237 |
#warn "====> $f $sf must exist\n"; |
#warn "====> $f $sf must exist\n"; |
238 |
$errors->{field}->{$f}->{subfield}->{$sf} = "missing" |
$errors->{$f}->{subfield}->{missing}->{$sf}++ |
239 |
unless defined( $subfields->{$sf} ); |
unless defined( $subfields->{$sf} ); |
240 |
} |
} |
241 |
} |
} |
242 |
|
|
243 |
} |
} |
244 |
} elsif (ref($v) eq 'HASH') { |
} elsif (ref($v) eq 'HASH') { |
245 |
$errors->{field}->{$f}->{unexpected_subfields}++; |
$errors->{$f}->{unexpected_subfields}++; |
246 |
$errors->{field}->{$f}->{dump} = |
$errors->{$f}->{dump} = _pack_subfields_hash( $v, 1 ); |
|
join('', _pack_subfields_hash( $v, 1 ) ); |
|
247 |
} |
} |
248 |
} |
} |
249 |
} |
} |
250 |
|
|
251 |
|
$log->debug("_delimiters_templates = ", dump( $self->{_delimiters_templates} ) ); |
252 |
|
|
253 |
foreach my $must (sort keys %{ $self->{must_exist} }) { |
foreach my $must (sort keys %{ $self->{must_exist} }) { |
254 |
next if ($fields->{$must}); |
next if ($fields->{$must}); |
255 |
$errors->{field}->{$must}->{missing}++; |
$errors->{$must}->{missing}++; |
256 |
$errors->{dump} = $rec_dump if ($rec_dump); |
$errors->{dump} = $rec_dump if ($rec_dump); |
257 |
} |
} |
258 |
|
|
259 |
if ($errors) { |
if ($errors) { |
260 |
$log->debug("errors: ", sub { dump( $errors ) } ); |
$log->debug("errors: ", $self->report_error( $errors ) ); |
261 |
|
|
262 |
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"); |
263 |
$self->{errors}->{$mfn} = $errors; |
$self->{errors}->{$mfn} = $errors; |
294 |
return $self->{errors}; |
return $self->{errors}; |
295 |
} |
} |
296 |
|
|
297 |
|
=head2 report_error |
298 |
|
|
299 |
|
Produce nice humanly readable report of single error |
300 |
|
|
301 |
|
print $validate->report_error( $error_hash ); |
302 |
|
|
303 |
|
=cut |
304 |
|
|
305 |
|
sub report_error { |
306 |
|
my $self = shift; |
307 |
|
|
308 |
|
my $h = shift || die "no hash?"; |
309 |
|
|
310 |
|
sub _unroll { |
311 |
|
my ($self, $tree, $accumulated) = @_; |
312 |
|
|
313 |
|
my $log = $self->_get_logger(); |
314 |
|
|
315 |
|
$log->debug("# ", |
316 |
|
( $tree ? "tree: $tree " : '' ), |
317 |
|
( $accumulated ? "accumulated: $accumulated " : '' ), |
318 |
|
); |
319 |
|
|
320 |
|
my $results; |
321 |
|
|
322 |
|
if (ref($tree) ne 'HASH') { |
323 |
|
return ("$accumulated\t($tree)", undef); |
324 |
|
} |
325 |
|
|
326 |
|
my $dump; |
327 |
|
|
328 |
|
foreach my $k (sort keys %{ $tree }) { |
329 |
|
|
330 |
|
if ($k eq 'dump') { |
331 |
|
$dump = $tree->{dump}; |
332 |
|
# warn "## dump: ",dump($dump),"\n"; |
333 |
|
next; |
334 |
|
} |
335 |
|
|
336 |
|
$log->debug("current: $k"); |
337 |
|
|
338 |
|
my ($new_results, $new_dump) = $self->_unroll($tree->{$k}, |
339 |
|
$accumulated ? "$accumulated\t$k" : $k |
340 |
|
); |
341 |
|
|
342 |
|
$log->debug( |
343 |
|
( $new_results ? "new_results: " . dump($new_results) ." " : '' ), |
344 |
|
); |
345 |
|
|
346 |
|
push @$results, $new_results if ($new_results); |
347 |
|
$dump = $new_dump if ($new_dump); |
348 |
|
|
349 |
|
} |
350 |
|
|
351 |
|
$log->debug( |
352 |
|
( $results ? "results: " . dump($results) ." " : '' ), |
353 |
|
); |
354 |
|
|
355 |
|
if ($#$results == 0) { |
356 |
|
return ($results->[0], $dump); |
357 |
|
} else { |
358 |
|
return ($results, $dump); |
359 |
|
} |
360 |
|
} |
361 |
|
|
362 |
|
|
363 |
|
sub _reformat { |
364 |
|
my $l = shift; |
365 |
|
$l =~ s/\t/ /g; |
366 |
|
$l =~ s/_/ /; |
367 |
|
return $l; |
368 |
|
} |
369 |
|
|
370 |
|
my $out = ''; |
371 |
|
|
372 |
|
for my $f (sort keys %{ $h }) { |
373 |
|
$out .= "$f: "; |
374 |
|
|
375 |
|
my ($r, $d) = $self->_unroll( $h->{$f} ); |
376 |
|
my $e; |
377 |
|
if (ref($r) eq 'ARRAY') { |
378 |
|
$e .= join(", ", map { _reformat( $_ ) } @$r); |
379 |
|
} else { |
380 |
|
$e .= _reformat( $r ); |
381 |
|
} |
382 |
|
$e .= "\n\t$d" if ($d); |
383 |
|
|
384 |
|
$out .= $e . "\n"; |
385 |
|
} |
386 |
|
return $out; |
387 |
|
} |
388 |
|
|
389 |
|
|
390 |
|
=head2 report |
391 |
|
|
392 |
|
Produce nice humanly readable report of errors |
393 |
|
|
394 |
|
print $validate->report; |
395 |
|
|
396 |
|
=cut |
397 |
|
|
398 |
|
sub report { |
399 |
|
my $self = shift; |
400 |
|
my $e = $self->{errors} || return; |
401 |
|
|
402 |
|
my $out; |
403 |
|
foreach my $mfn (sort { $a <=> $b } keys %$e) { |
404 |
|
$out .= "MFN $mfn\n" . $self->report_error( $e->{$mfn} ) . "\n"; |
405 |
|
} |
406 |
|
|
407 |
|
return $out; |
408 |
|
|
409 |
|
} |
410 |
|
|
411 |
=head1 AUTHOR |
=head1 AUTHOR |
412 |
|
|
413 |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |