18 |
|
|
19 |
=head1 VERSION |
=head1 VERSION |
20 |
|
|
21 |
Version 0.06 |
Version 0.09 |
22 |
|
|
23 |
=cut |
=cut |
24 |
|
|
25 |
our $VERSION = '0.06'; |
our $VERSION = '0.09'; |
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 |
|
|
88 |
|
|
89 |
if ($fld =~ s/!$//) { |
if ($fld =~ s/!$//) { |
90 |
$self->{must_exist}->{$fld}++; |
$self->{must_exist}->{$fld}++; |
91 |
|
} elsif ($fld =~ s/-$//) { |
92 |
|
$self->{dont_validate}->{$fld}++; |
93 |
} |
} |
94 |
|
|
95 |
$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)); |
121 |
|
|
122 |
Validate record and return errors |
Validate record and return errors |
123 |
|
|
124 |
my @errors = $validate->validate_errors( $rec ); |
my @errors = $validate->validate_errors( $rec, $rec_dump ); |
125 |
|
|
126 |
=cut |
=cut |
127 |
|
|
131 |
my $log = $self->_get_logger(); |
my $log = $self->_get_logger(); |
132 |
|
|
133 |
my $rec = shift || $log->logdie("validate_errors need record"); |
my $rec = shift || $log->logdie("validate_errors need record"); |
134 |
|
my $rec_dump = shift; |
135 |
|
|
136 |
$log->logdie("rec isn't HASH") unless (ref($rec) eq 'HASH'); |
$log->logdie("rec isn't HASH") unless (ref($rec) eq 'HASH'); |
137 |
$log->logdie("can't find validation rules") unless (my $r = $self->{rules}); |
$log->logdie("can't find validation rules") unless (my $r = $self->{rules}); |
146 |
|
|
147 |
next if (!defined($f) || $f eq '' || $f eq '000'); |
next if (!defined($f) || $f eq '' || $f eq '000'); |
148 |
|
|
149 |
|
next if (defined( $self->{dont_validate}->{$f} )); |
150 |
|
|
151 |
|
# track field usage |
152 |
$fields->{$f}++; |
$fields->{$f}++; |
153 |
|
|
154 |
if ( ! defined($r->{$f}) ) { |
if ( ! defined($r->{$f}) ) { |
155 |
$errors->{field}->{ $f }->{extra} = "not expected"; |
$errors->{ $f }->{unexpected} = "this field is not expected"; |
156 |
next; |
next; |
157 |
} |
} |
158 |
|
|
159 |
|
|
160 |
if (ref($rec->{$f}) ne 'ARRAY') { |
if (ref($rec->{$f}) ne 'ARRAY') { |
161 |
$errors->{field}->{ $f }->{not_repeatable} = "probably bug in parsing input data"; |
$errors->{ $f }->{not_repeatable} = "probably bug in parsing input data"; |
162 |
next; |
next; |
163 |
} |
} |
164 |
|
|
167 |
if (ref($r->{$f}) eq 'ARRAY') { |
if (ref($r->{$f}) eq 'ARRAY') { |
168 |
# are values hashes? (has subfields) |
# are values hashes? (has subfields) |
169 |
if (! defined($v)) { |
if (! defined($v)) { |
170 |
$errors->{field}->{$f}->{empty} = undef; |
# $errors->{$f}->{empty} = undef; |
171 |
$errors->{dump}->{record}++; |
# $errors->{dump} = $rec_dump if ($rec_dump); |
172 |
} elsif (ref($v) ne 'HASH') { |
} elsif (ref($v) ne 'HASH') { |
173 |
$errors->{field}->{$f}->{missing_subfield} = "value without subfields: $v"; |
$errors->{$f}->{missing_subfield} = join(",", @{ $r->{$f} }) . " required"; |
174 |
next; |
next; |
175 |
} else { |
} else { |
176 |
|
|
192 |
$sf_repeatable->{$sf}++; |
$sf_repeatable->{$sf}++; |
193 |
}; |
}; |
194 |
if (! first { $_ eq $sf } @{ $r->{$f} }) { |
if (! first { $_ eq $sf } @{ $r->{$f} }) { |
195 |
$errors->{field}->{ $f }->{subfield}->{$sf} = "unknown"; |
$errors->{ $f }->{subfield}->{extra}->{$sf}++; |
196 |
} |
} |
197 |
} |
} |
198 |
|
|
200 |
if (my @r_sf = sort keys( %$sf_repeatable )) { |
if (my @r_sf = sort keys( %$sf_repeatable )) { |
201 |
|
|
202 |
foreach my $sf (@r_sf) { |
foreach my $sf (@r_sf) { |
203 |
$errors->{field}->{$f}->{subfield}->{$sf} = "repeatable"; |
$errors->{$f}->{subfield}->{extra_repeatable}->{$sf}++; |
204 |
$errors->{dump}->{field}->{$f} = |
$errors->{$f}->{dump} = _pack_subfields_hash( $h, 1 ); |
|
join('', _pack_subfields_hash( $h, 1 ) ); |
|
205 |
} |
} |
206 |
|
|
207 |
} |
} |
209 |
if ( defined( $self->{must_exist_sf}->{$f} ) ) { |
if ( defined( $self->{must_exist_sf}->{$f} ) ) { |
210 |
foreach my $sf (sort keys %{ $self->{must_exist_sf}->{$f} }) { |
foreach my $sf (sort keys %{ $self->{must_exist_sf}->{$f} }) { |
211 |
#warn "====> $f $sf must exist\n"; |
#warn "====> $f $sf must exist\n"; |
212 |
$errors->{field}->{$f}->{subfield}->{$sf} = "missing" |
$errors->{$f}->{subfield}->{missing}->{$sf}++ |
213 |
unless defined( $subfields->{$sf} ); |
unless defined( $subfields->{$sf} ); |
214 |
} |
} |
215 |
} |
} |
216 |
|
|
217 |
} |
} |
218 |
} elsif (ref($v) eq 'HASH') { |
} elsif (ref($v) eq 'HASH') { |
219 |
$errors->{field}->{$f}->{unexpected_subfields}++; |
$errors->{$f}->{unexpected_subfields}++; |
220 |
$errors->{dump}->{field}->{$f} = |
$errors->{$f}->{dump} = _pack_subfields_hash( $v, 1 ); |
|
join('', _pack_subfields_hash( $v, 1 ) ); |
|
221 |
} |
} |
222 |
} |
} |
223 |
} |
} |
224 |
|
|
225 |
foreach my $must (sort keys %{ $self->{must_exist} }) { |
foreach my $must (sort keys %{ $self->{must_exist} }) { |
226 |
next if ($fields->{$must}); |
next if ($fields->{$must}); |
227 |
$errors->{field}->{$must}->{missing}++; |
$errors->{$must}->{missing}++; |
228 |
$errors->{dump}->{record}++; |
$errors->{dump} = $rec_dump if ($rec_dump); |
229 |
} |
} |
230 |
|
|
231 |
if ($errors) { |
if ($errors) { |
232 |
$log->debug("errors: ", sub { dump( $errors ) } ); |
$log->debug("errors: ", $self->report_error( $errors ) ); |
233 |
|
|
234 |
my $mfn = $rec->{'000'}->[0]; |
my $mfn = $rec->{'000'}->[0] || $log->logconfess("record ", dump( $rec ), " doesn't have MFN"); |
235 |
$self->{errors}->{$mfn} = $errors; |
$self->{errors}->{$mfn} = $errors; |
236 |
} |
} |
237 |
|
|
240 |
return $errors; |
return $errors; |
241 |
} |
} |
242 |
|
|
243 |
|
=head2 reset_errors |
244 |
|
|
245 |
|
Clean all accumulated errors for this input |
246 |
|
|
247 |
|
$validate->reset_errors; |
248 |
|
|
249 |
|
=cut |
250 |
|
|
251 |
|
sub reset_errors { |
252 |
|
my $self = shift; |
253 |
|
delete ($self->{errors}); |
254 |
|
} |
255 |
|
|
256 |
|
=head2 all_errors |
257 |
|
|
258 |
|
Return hash with all errors |
259 |
|
|
260 |
|
print dump( $validate->all_errors ); |
261 |
|
|
262 |
|
=cut |
263 |
|
|
264 |
|
sub all_errors { |
265 |
|
my $self = shift; |
266 |
|
return $self->{errors}; |
267 |
|
} |
268 |
|
|
269 |
|
=head2 report_error |
270 |
|
|
271 |
|
Produce nice humanly readable report of single error |
272 |
|
|
273 |
|
print $validate->report_error( $error_hash ); |
274 |
|
|
275 |
|
=cut |
276 |
|
|
277 |
|
sub report_error { |
278 |
|
my $self = shift; |
279 |
|
|
280 |
|
my $h = shift || die "no hash?"; |
281 |
|
|
282 |
|
sub _unroll { |
283 |
|
my ($self, $tree, $accumulated) = @_; |
284 |
|
|
285 |
|
my $log = $self->_get_logger(); |
286 |
|
|
287 |
|
$log->debug("# ", |
288 |
|
( $tree ? "tree: $tree " : '' ), |
289 |
|
( $accumulated ? "accumulated: $accumulated " : '' ), |
290 |
|
); |
291 |
|
|
292 |
|
my $results; |
293 |
|
|
294 |
|
if (ref($tree) ne 'HASH') { |
295 |
|
return ("$accumulated\t($tree)", undef); |
296 |
|
} |
297 |
|
|
298 |
|
my $dump; |
299 |
|
|
300 |
|
foreach my $k (sort keys %{ $tree }) { |
301 |
|
|
302 |
|
if ($k eq 'dump') { |
303 |
|
$dump = $tree->{dump}; |
304 |
|
# warn "## dump: ",dump($dump),"\n"; |
305 |
|
next; |
306 |
|
} |
307 |
|
|
308 |
|
$log->debug("current: $k"); |
309 |
|
|
310 |
|
my ($new_results, $new_dump) = $self->_unroll($tree->{$k}, |
311 |
|
$accumulated ? "$accumulated\t$k" : $k |
312 |
|
); |
313 |
|
|
314 |
|
$log->debug( |
315 |
|
( $new_results ? "new_results: " . dump($new_results) ." " : '' ), |
316 |
|
); |
317 |
|
|
318 |
|
push @$results, $new_results if ($new_results); |
319 |
|
$dump = $new_dump if ($new_dump); |
320 |
|
|
321 |
|
} |
322 |
|
|
323 |
|
$log->debug( |
324 |
|
( $results ? "results: " . dump($results) ." " : '' ), |
325 |
|
); |
326 |
|
|
327 |
|
if ($#$results == 0) { |
328 |
|
return ($results->[0], $dump); |
329 |
|
} else { |
330 |
|
return ($results, $dump); |
331 |
|
} |
332 |
|
} |
333 |
|
|
334 |
|
|
335 |
|
sub _reformat { |
336 |
|
my $l = shift; |
337 |
|
$l =~ s/\t/ /g; |
338 |
|
$l =~ s/_/ /; |
339 |
|
return $l; |
340 |
|
} |
341 |
|
|
342 |
|
my $out = ''; |
343 |
|
|
344 |
|
for my $f (sort keys %{ $h }) { |
345 |
|
$out .= "$f: "; |
346 |
|
|
347 |
|
my ($r, $d) = $self->_unroll( $h->{$f} ); |
348 |
|
my $e; |
349 |
|
if (ref($r) eq 'ARRAY') { |
350 |
|
$e .= join(", ", map { _reformat( $_ ) } @$r); |
351 |
|
} else { |
352 |
|
$e .= _reformat( $r ); |
353 |
|
} |
354 |
|
$e .= "\n\t$d" if ($d); |
355 |
|
|
356 |
|
$out .= $e . "\n"; |
357 |
|
} |
358 |
|
return $out; |
359 |
|
} |
360 |
|
|
361 |
|
|
362 |
|
=head2 report |
363 |
|
|
364 |
|
Produce nice humanly readable report of errors |
365 |
|
|
366 |
|
print $validate->report; |
367 |
|
|
368 |
|
=cut |
369 |
|
|
370 |
|
sub report { |
371 |
|
my $self = shift; |
372 |
|
my $e = $self->{errors} || return; |
373 |
|
|
374 |
|
my $out; |
375 |
|
foreach my $mfn (sort { $a <=> $b } keys %$e) { |
376 |
|
$out .= "MFN $mfn\n" . $self->report_error( $e->{$mfn} ) . "\n"; |
377 |
|
} |
378 |
|
|
379 |
|
return $out; |
380 |
|
|
381 |
|
} |
382 |
|
|
383 |
=head1 AUTHOR |
=head1 AUTHOR |
384 |
|
|
385 |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |