/[webpac2]/trunk/lib/WebPAC/Validate.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/lib/WebPAC/Validate.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 646 by dpavlin, Wed Sep 6 22:42:37 2006 UTC revision 671 by dpavlin, Mon Sep 11 15:59:35 2006 UTC
# Line 8  use blib; Line 8  use blib;
8  use base 'WebPAC::Common';  use base 'WebPAC::Common';
9  use File::Slurp;  use File::Slurp;
10  use List::Util qw/first/;  use List::Util qw/first/;
11  use Data::Dumper;  use Data::Dump qw/dump/;
12  use WebPAC::Normalize qw/_pack_subfields_hash/;  use WebPAC::Normalize qw/_pack_subfields_hash/;
13  use Storable qw/dclone/;  use Storable qw/dclone/;
14    
# Line 18  WebPAC::Validate - provide simple valida Line 18  WebPAC::Validate - provide simple valida
18    
19  =head1 VERSION  =head1 VERSION
20    
21  Version 0.04  Version 0.08
22    
23  =cut  =cut
24    
25  our $VERSION = '0.04';  our $VERSION = '0.08';
26    
27  =head1 SYNOPSIS  =head1 SYNOPSIS
28    
# Line 104  sub new { Line 104  sub new {
104    
105          }          }
106    
107          $log->debug("current validation rules: ", Dumper($v));          $log->debug("current validation rules: ", dump($v));
108    
109          $self->{rules} = $v;          $self->{rules} = $v;
110    
# Line 117  sub new { Line 117  sub new {
117    
118  Validate record and return errors  Validate record and return errors
119    
120    my @errors = $validate->validate_errors( $rec );    my @errors = $validate->validate_errors( $rec, $rec_dump );
121    
122  =cut  =cut
123    
# Line 127  sub validate_errors { Line 127  sub validate_errors {
127          my $log = $self->_get_logger();          my $log = $self->_get_logger();
128    
129          my $rec = shift || $log->logdie("validate_errors need record");          my $rec = shift || $log->logdie("validate_errors need record");
130            my $rec_dump = shift;
131    
132          $log->logdie("rec isn't HASH") unless (ref($rec) eq 'HASH');          $log->logdie("rec isn't HASH") unless (ref($rec) eq 'HASH');
133          $log->logdie("can't find validation rules") unless (my $r = $self->{rules});          $log->logdie("can't find validation rules") unless (my $r = $self->{rules});
134    
135          my @errors;          my $errors;
136    
137          $log->debug("rec = ", sub { Dumper($rec) }, "keys = ", keys %{ $rec });          $log->debug("rec = ", sub { dump($rec) }, "keys = ", keys %{ $rec });
138    
139          my $fields;          my $fields;
140    
# Line 144  sub validate_errors { Line 145  sub validate_errors {
145                  $fields->{$f}++;                  $fields->{$f}++;
146    
147                  if ( ! defined($r->{$f}) ) {                  if ( ! defined($r->{$f}) ) {
148                          push @errors, "field '$f' shouldn't exists";                          $errors->{ $f }->{unexpected} = "this field is not expected";
149                          next;                          next;
150                  }                  }
151    
152    
153                  if (ref($rec->{$f}) ne 'ARRAY') {                  if (ref($rec->{$f}) ne 'ARRAY') {
154                          push @errors, "field '$f' isn't repetable, probably bug in parsing input data";                          $errors->{ $f }->{not_repeatable} = "probably bug in parsing input data";
155                          next;                          next;
156                  }                  }
157    
# Line 158  sub validate_errors { Line 159  sub validate_errors {
159                          # can we have subfields?                          # can we have subfields?
160                          if (ref($r->{$f}) eq 'ARRAY') {                          if (ref($r->{$f}) eq 'ARRAY') {
161                                  # are values hashes? (has subfields)                                  # are values hashes? (has subfields)
162                                  if (ref($v) ne 'HASH') {                                  if (! defined($v)) {
163                                          push @errors, "$f has value without subfields: $v";  #                                       $errors->{$f}->{empty} = undef;
164    #                                       $errors->{dump} = $rec_dump if ($rec_dump);
165                                    } elsif (ref($v) ne 'HASH') {
166                                            $errors->{$f}->{missing_subfield} = join(",", @{ $r->{$f} }) . " required";
167                                          next;                                          next;
168                                  } else {                                  } else {
169    
# Line 181  sub validate_errors { Line 185  sub validate_errors {
185                                                                  $sf_repeatable->{$sf}++;                                                                  $sf_repeatable->{$sf}++;
186                                                          };                                                          };
187                                                          if (! first { $_ eq $sf } @{ $r->{$f} }) {                                                          if (! first { $_ eq $sf } @{ $r->{$f} }) {
188                                                                  push @errors, "$f has unknown subfield: $sf";                                                                  $errors->{ $f }->{subfield}->{extra}->{$sf}++;
189                                                          }                                                          }
190                                                  }                                                  }
191    
192                                          }                                          }
193                                          if (my @r_sf = sort keys( %$sf_repeatable )) {                                          if (my @r_sf = sort keys( %$sf_repeatable )) {
                                                 my $plural = $#r_sf > 0 ? 1 : 0;  
194    
195                                                  push @errors, "$f subfield" .                                                  foreach my $sf (@r_sf) {
196                                                  ( $plural ? 's ' : ' ' ) .                                                          $errors->{$f}->{subfield}->{extra_repeatable}->{$sf}++;
197                                                  join(', ', @r_sf) .                                                          $errors->{$f}->{dump} = _pack_subfields_hash( $h, 1 );
198                                                  ( $plural ? ' are ' : ' is ' ) .                                                  }
199                                                  'repeatable in: ' .  
                                                 join('', _pack_subfields_hash( $h, 1) );  
200                                          }                                          }
201    
202                                          if ( defined( $self->{must_exist_sf}->{$f} ) ) {                                          if ( defined( $self->{must_exist_sf}->{$f} ) ) {
203                                                  foreach my $sf (sort keys %{ $self->{must_exist_sf}->{$f} }) {                                                  foreach my $sf (sort keys %{ $self->{must_exist_sf}->{$f} }) {
204  warn "====> $f $sf must exist\n";  #warn "====> $f $sf must exist\n";
205                                                          push @errors, "$f missing required subfield $sf"                                                          $errors->{$f}->{subfield}->{missing}->{$sf}++
206                                                                  unless (                                                                  unless defined( $subfields->{$sf} );
                                                                         defined( $subfields->{$f} ) &&  
                                                                         defined( $subfields->{$f}->{$sf} )  
                                                                 )  
207                                                  }                                                  }
208                                          }                                          }
209    
210                                  }                                  }
211                          } elsif (ref($v) eq 'HASH') {                          } elsif (ref($v) eq 'HASH') {
212                                  push @errors, "$f has subfields which is not valid";                                  $errors->{$f}->{unexpected_subfields}++;
213                                    $errors->{$f}->{dump} = _pack_subfields_hash( $v, 1 );
214                          }                          }
215                  }                  }
216          }          }
217    
218          foreach my $must (sort keys %{ $self->{must_exist} }) {          foreach my $must (sort keys %{ $self->{must_exist} }) {
219                  next if ($fields->{$must});                  next if ($fields->{$must});
220                  push @errors,                  $errors->{$must}->{missing}++;
221                          "field $must should exist, but it doesn't";                  $errors->{dump} = $rec_dump if ($rec_dump);
222            }
223    
224            if ($errors) {
225                    $log->debug("errors: ", $self->report_error( $errors ) );
226    
227                    my $mfn = $rec->{'000'}->[0] || $log->logconfess("record ", dump( $rec ), " doesn't have MFN");
228                    $self->{errors}->{$mfn} = $errors;
229          }          }
230    
231          #$log->logcluck("return from this function is ARRAY") unless wantarray;          #$log->logcluck("return from this function is ARRAY") unless wantarray;
232    
233          $log->debug("errors: ", join(", ", @errors)) if (@errors);          return $errors;
234    }
235    
236    =head2 reset_errors
237    
238    Clean all accumulated errors for this input
239    
240      $validate->reset_errors;
241    
242    =cut
243    
244    sub reset_errors {
245            my $self = shift;
246            delete ($self->{errors});
247    }
248    
249    =head2 all_errors
250    
251    Return hash with all errors
252    
253      print dump( $validate->all_errors );
254    
255    =cut
256    
257    sub all_errors {
258            my $self = shift;
259            return $self->{errors};
260    }
261    
262    =head2 report_error
263    
264    Produce nice humanly readable report of single error
265    
266      print $validate->report_error( $error_hash );
267    
268    =cut
269    
270    sub report_error {
271            my $self = shift;
272    
273            my $h = shift || die "no hash?";
274    
275            sub _unroll {
276                    my ($self, $tree, $accumulated) = @_;
277    
278                    my $log = $self->_get_logger();
279    
280                    $log->debug("# ",
281                            ( $tree                 ? "tree: $tree "                                        : '' ),
282                            ( $accumulated  ? "accumulated: $accumulated "          : '' ),
283                    );
284    
285                    my $results;
286    
287                    if (ref($tree) ne 'HASH') {
288                            return ("$accumulated\t($tree)", undef);
289                    }
290    
291                    my $dump;
292    
293                    foreach my $k (sort keys %{ $tree }) {
294    
295                            if ($k eq 'dump') {
296                                    $dump = $tree->{dump};
297    #                               warn "## dump: ",dump($dump),"\n";
298                                    next;
299                            }
300    
301                            $log->debug("current: $k");
302    
303                            my ($new_results, $new_dump) = $self->_unroll($tree->{$k},
304                                    $accumulated ? "$accumulated\t$k" : $k
305                            );
306    
307                            $log->debug(
308                                    ( $new_results          ? "new_results: " . dump($new_results) ." "     : '' ),
309                            );
310    
311                            push @$results, $new_results if ($new_results);
312                            $dump = $new_dump if ($new_dump);
313    
314                    }
315    
316                    $log->debug(
317                            ( $results              ? "results: " . dump($results) ." "     : '' ),
318                    );
319    
320                    if ($#$results == 0) {
321                            return ($results->[0], $dump);
322                    } else {
323                            return ($results, $dump);
324                    }
325            }
326    
327    
328            sub _reformat {
329                    my $l = shift;
330                    $l =~ s/\t/ /g;
331                    $l =~ s/_/ /;
332                    return $l;
333            }
334    
335            my $out = '';
336    
337            for my $f (sort keys %{ $h }) {
338                    $out .= "$f: ";
339                    
340                    my ($r, $d) = $self->_unroll( $h->{$f} );
341                    my $e;
342                    if (ref($r) eq 'ARRAY') {
343                            $e .= join(", ", map { _reformat( $_ ) } @$r);
344                    } else {
345                            $e .= _reformat( $r );
346                    }
347                    $e .= "\n\t$d" if ($d);
348    
349                    $out .= $e . "\n";
350            }
351            return $out;
352    }
353    
354    
355    =head2 report
356    
357    Produce nice humanly readable report of errors
358    
359      print $validate->report;
360    
361    =cut
362    
363    sub report {
364            my $self = shift;
365            my $e = $self->{errors} || return;
366    
367            my $out;
368            foreach my $mfn (sort { $a <=> $b } keys %$e) {
369                    $out .= "MFN $mfn\n" . $self->report_error( $e->{$mfn} ) . "\n";
370            }
371    
372            return $out;
373    
         return @errors;  
374  }  }
375    
376  =head1 AUTHOR  =head1 AUTHOR

Legend:
Removed from v.646  
changed lines
  Added in v.671

  ViewVC Help
Powered by ViewVC 1.1.26