/[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 666 by dpavlin, Mon Sep 11 12:32:51 2006 UTC revision 827 by dpavlin, Sun May 20 16:19:16 2007 UTC
# Line 18  WebPAC::Validate - provide simple valida Line 18  WebPAC::Validate - provide simple valida
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    
# Line 41  configuration file in following format: Line 41  configuration file in following format:
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    
# Line 50  Create new validation object Line 52  Create new validation object
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 {
# Line 86  sub new { Line 93  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));
# Line 110  sub new { Line 119  sub new {
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');
# Line 142  sub validate_errors { Line 156  sub validate_errors {
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}) ) {
# Line 194  sub validate_errors { Line 227  sub validate_errors {
227    
228                                                  foreach my $sf (@r_sf) {                                                  foreach my $sf (@r_sf) {
229                                                          $errors->{$f}->{subfield}->{extra_repeatable}->{$sf}++;                                                          $errors->{$f}->{subfield}->{extra_repeatable}->{$sf}++;
230                                                          $errors->{$f}->{dump} =                                                          $errors->{$f}->{dump} = _pack_subfields_hash( $h, 1 );
                                                                 join('', _pack_subfields_hash( $h, 1 ) );  
231                                                  }                                                  }
232    
233                                          }                                          }
# Line 211  sub validate_errors { Line 243  sub validate_errors {
243                                  }                                  }
244                          } elsif (ref($v) eq 'HASH') {                          } elsif (ref($v) eq 'HASH') {
245                                  $errors->{$f}->{unexpected_subfields}++;                                  $errors->{$f}->{unexpected_subfields}++;
246                                  $errors->{$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->{$must}->{missing}++;                  $errors->{$must}->{missing}++;
# Line 224  sub validate_errors { Line 257  sub validate_errors {
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;
# Line 261  sub all_errors { Line 294  sub all_errors {
294          return $self->{errors};          return $self->{errors};
295  }  }
296    
297  =head2 report  =head2 report_error
298    
299  Produce nice humanly readable report of errors  Produce nice humanly readable report of single error
300    
301    print $validate->report;    print $validate->report_error( $error_hash );
302    
303  =cut  =cut
304    
305  sub report {  sub report_error {
306          my $self = shift;          my $self = shift;
307    
308          sub unroll {          my $h = shift || die "no hash?";
309    
310            sub _unroll {
311                  my ($self, $tree, $accumulated) = @_;                  my ($self, $tree, $accumulated) = @_;
312    
313                  my $log = $self->_get_logger();                  my $log = $self->_get_logger();
# Line 294  sub report { Line 329  sub report {
329    
330                          if ($k eq 'dump') {                          if ($k eq 'dump') {
331                                  $dump = $tree->{dump};                                  $dump = $tree->{dump};
332                                  warn "## dump: ",dump($dump),"\n";  #                               warn "## dump: ",dump($dump),"\n";
333                                  next;                                  next;
334                          }                          }
335    
336                          $log->debug("current: $k");                          $log->debug("current: $k");
337    
338                          my ($new_results, $new_dump) = $self->unroll($tree->{$k},                          my ($new_results, $new_dump) = $self->_unroll($tree->{$k},
339                                  $accumulated ? "$accumulated\t$k" : $k                                  $accumulated ? "$accumulated\t$k" : $k
340                          );                          );
341    
# Line 324  sub report { Line 359  sub report {
359                  }                  }
360          }          }
361    
         my $log = $self->_get_logger();  
362    
363          my $out = '';          sub _reformat {
         my $e = $self->{errors} || return;  
   
         sub reformat {  
364                  my $l = shift;                  my $l = shift;
365                  $l =~ s/\t/ /g;                  $l =~ s/\t/ /g;
366                  $l =~ s/_/ /;                  $l =~ s/_/ /;
367                  return $l;                  return $l;
368          }          }
369    
370          foreach my $mfn (sort keys %$e) {          my $out = '';
                 $out .= "MFN $mfn\n";  
371    
372                  for my $f (sort keys %{ $e->{$mfn} }) {          for my $f (sort keys %{ $h }) {
373                          my ($r, $d) = $self->unroll( $e->{$mfn}->{$f} );                  $out .= "$f: ";
374                          my $e = $f . ': ';                  
375                          if (ref($r) eq 'ARRAY') {                  my ($r, $d) = $self->_unroll( $h->{$f} );
376                                  $e .= join(", ", map { reformat( $_ ) } @$r);                  my $e;
377                          } else {                  if (ref($r) eq 'ARRAY') {
378                                  $e .= reformat( $r );                          $e .= join(", ", map { _reformat( $_ ) } @$r);
379                          }                  } else {
380                          $e .= "\n\t$d" if ($d);                          $e .= _reformat( $r );
                         $e .= "\n";  
                         $log->debug("MFN $mfn | $e");  
                         $out .= $e;  
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;          return $out;
408    
409  }  }
410    
411  =head1 AUTHOR  =head1 AUTHOR

Legend:
Removed from v.666  
changed lines
  Added in v.827

  ViewVC Help
Powered by ViewVC 1.1.26