/[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 830 by dpavlin, Wed May 23 20:03:12 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                            foreach my $v (@{ $rec->{$f} }) {
163                                            my $l = _pack_subfields_hash( $v, 1 );
164                                            my $template = '';
165                                            $l =~ s/$regex/$template.=$1/eg;
166    #                                       warn "## template: $template\n";
167                                            $self->{_delimiters_templates}->{$f}->{$template}++ if ( $template );
168                            }
169    
170                    }
171    
172                    next if (defined( $self->{dont_validate}->{$f} ));
173    
174                    # track field usage
175                  $fields->{$f}++;                  $fields->{$f}++;
176    
177                  if ( ! defined($r->{$f}) ) {                  if ( ! defined($r->{$f}) ) {
# Line 194  sub validate_errors { Line 224  sub validate_errors {
224    
225                                                  foreach my $sf (@r_sf) {                                                  foreach my $sf (@r_sf) {
226                                                          $errors->{$f}->{subfield}->{extra_repeatable}->{$sf}++;                                                          $errors->{$f}->{subfield}->{extra_repeatable}->{$sf}++;
227                                                          $errors->{$f}->{dump} =                                                          $errors->{$f}->{dump} = _pack_subfields_hash( $h, 1 );
                                                                 join('', _pack_subfields_hash( $h, 1 ) );  
228                                                  }                                                  }
229    
230                                          }                                          }
# Line 211  sub validate_errors { Line 240  sub validate_errors {
240                                  }                                  }
241                          } elsif (ref($v) eq 'HASH') {                          } elsif (ref($v) eq 'HASH') {
242                                  $errors->{$f}->{unexpected_subfields}++;                                  $errors->{$f}->{unexpected_subfields}++;
243                                  $errors->{$f}->{dump} =                                  $errors->{$f}->{dump} = _pack_subfields_hash( $v, 1 );
                                         join('', _pack_subfields_hash( $v, 1 ) );  
244                          }                          }
245                  }                  }
246          }          }
247    
248            $log->debug("_delimiters_templates = ", dump( $self->{_delimiters_templates} ) );
249    
250          foreach my $must (sort keys %{ $self->{must_exist} }) {          foreach my $must (sort keys %{ $self->{must_exist} }) {
251                  next if ($fields->{$must});                  next if ($fields->{$must});
252                  $errors->{$must}->{missing}++;                  $errors->{$must}->{missing}++;
# Line 224  sub validate_errors { Line 254  sub validate_errors {
254          }          }
255    
256          if ($errors) {          if ($errors) {
257                  $log->debug("errors: ", sub { dump( $errors ) } );                  $log->debug("errors: ", $self->report_error( $errors ) );
258    
259                  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");
260                  $self->{errors}->{$mfn} = $errors;                  $self->{errors}->{$mfn} = $errors;
# Line 261  sub all_errors { Line 291  sub all_errors {
291          return $self->{errors};          return $self->{errors};
292  }  }
293    
294  =head2 report  =head2 report_error
295    
296  Produce nice humanly readable report of errors  Produce nice humanly readable report of single error
297    
298    print $validate->report;    print $validate->report_error( $error_hash );
299    
300  =cut  =cut
301    
302  sub report {  sub report_error {
303          my $self = shift;          my $self = shift;
304    
305          sub unroll {          my $h = shift || die "no hash?";
306    
307            sub _unroll {
308                  my ($self, $tree, $accumulated) = @_;                  my ($self, $tree, $accumulated) = @_;
309    
310                  my $log = $self->_get_logger();                  my $log = $self->_get_logger();
# Line 294  sub report { Line 326  sub report {
326    
327                          if ($k eq 'dump') {                          if ($k eq 'dump') {
328                                  $dump = $tree->{dump};                                  $dump = $tree->{dump};
329                                  warn "## dump: ",dump($dump),"\n";  #                               warn "## dump: ",dump($dump),"\n";
330                                  next;                                  next;
331                          }                          }
332    
333                          $log->debug("current: $k");                          $log->debug("current: $k");
334    
335                          my ($new_results, $new_dump) = $self->unroll($tree->{$k},                          my ($new_results, $new_dump) = $self->_unroll($tree->{$k},
336                                  $accumulated ? "$accumulated\t$k" : $k                                  $accumulated ? "$accumulated\t$k" : $k
337                          );                          );
338    
# Line 324  sub report { Line 356  sub report {
356                  }                  }
357          }          }
358    
         my $log = $self->_get_logger();  
   
         my $out = '';  
         my $e = $self->{errors} || return;  
359    
360          sub reformat {          sub _reformat {
361                  my $l = shift;                  my $l = shift;
362                  $l =~ s/\t/ /g;                  $l =~ s/\t/ /g;
363                  $l =~ s/_/ /;                  $l =~ s/_/ /;
364                  return $l;                  return $l;
365          }          }
366    
367          foreach my $mfn (sort keys %$e) {          my $out = '';
                 $out .= "MFN $mfn\n";  
368    
369                  for my $f (sort keys %{ $e->{$mfn} }) {          for my $f (sort keys %{ $h }) {
370                          my ($r, $d) = $self->unroll( $e->{$mfn}->{$f} );                  $out .= "$f: ";
371                          my $e = $f . ': ';                  
372                          if (ref($r) eq 'ARRAY') {                  my ($r, $d) = $self->_unroll( $h->{$f} );
373                                  $e .= join(", ", map { reformat( $_ ) } @$r);                  my $e;
374                          } else {                  if (ref($r) eq 'ARRAY') {
375                                  $e .= reformat( $r );                          $e .= join(", ", map { _reformat( $_ ) } @$r);
376                          }                  } else {
377                          $e .= "\n\t$d" if ($d);                          $e .= _reformat( $r );
                         $e .= "\n";  
                         $log->debug("MFN $mfn | $e");  
                         $out .= $e;  
378                  }                  }
379                    $e .= "\n\t$d" if ($d);
380    
381                    $out .= $e . "\n";
382            }
383            return $out;
384    }
385    
386    
387    =head2 report
388    
389    Produce nice humanly readable report of errors
390    
391      print $validate->report;
392    
393    =cut
394    
395    sub report {
396            my $self = shift;
397            my $e = $self->{errors} || return;
398    
399            my $out;
400            foreach my $mfn (sort { $a <=> $b } keys %$e) {
401                    $out .= "MFN $mfn\n" . $self->report_error( $e->{$mfn} ) . "\n";
402          }          }
403    
404          return $out;          return $out;
405    
406  }  }
407    
408  =head1 AUTHOR  =head1 AUTHOR

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

  ViewVC Help
Powered by ViewVC 1.1.26