/[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 652 by dpavlin, Thu Sep 7 15:01:45 2006 UTC revision 664 by dpavlin, Mon Sep 11 11:57:18 2006 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.06  Version 0.07
22    
23  =cut  =cut
24    
25  our $VERSION = '0.06';  our $VERSION = '0.07';
26    
27  =head1 SYNOPSIS  =head1 SYNOPSIS
28    
# 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});
# 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                          $errors->{field}->{ $f }->{extra} = "not expected";                          $errors->{field}->{ $f }->{unexpected} = "this field is not expected";
149                          next;                          next;
150                  }                  }
151    
# Line 159  sub validate_errors { Line 160  sub validate_errors {
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 (! defined($v)) {                                  if (! defined($v)) {
163                                          $errors->{field}->{$f}->{empty} = undef;  #                                       $errors->{field}->{$f}->{empty} = undef;
164                                          $errors->{dump}->{record}++;  #                                       $errors->{dump} = $rec_dump if ($rec_dump);
165                                  } elsif (ref($v) ne 'HASH') {                                  } elsif (ref($v) ne 'HASH') {
166                                          $errors->{field}->{$f}->{missing_subfield} = "value without subfields: $v";                                          $errors->{field}->{$f}->{missing_subfield} = join(",", @{ $r->{$f} }) . " required";
167                                          next;                                          next;
168                                  } else {                                  } else {
169    
# Line 184  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                                                                  $errors->{field}->{ $f }->{subfield}->{$sf} = "unknown";                                                                  $errors->{field}->{ $f }->{subfield}->{extra}->{$sf}++;
189                                                          }                                                          }
190                                                  }                                                  }
191    
# Line 192  sub validate_errors { Line 193  sub validate_errors {
193                                          if (my @r_sf = sort keys( %$sf_repeatable )) {                                          if (my @r_sf = sort keys( %$sf_repeatable )) {
194    
195                                                  foreach my $sf (@r_sf) {                                                  foreach my $sf (@r_sf) {
196                                                          $errors->{field}->{$f}->{subfield}->{$sf} = "repeatable";                                                          $errors->{field}->{$f}->{subfield}->{extra_repeatable}->{$sf}++;
197                                                          $errors->{dump}->{field}->{$f} =                                                          $errors->{field}->{$f}->{dump} =
198                                                                  join('', _pack_subfields_hash( $h, 1 ) );                                                                  join('', _pack_subfields_hash( $h, 1 ) );
199                                                  }                                                  }
200    
# Line 202  sub validate_errors { Line 203  sub validate_errors {
203                                          if ( defined( $self->{must_exist_sf}->{$f} ) ) {                                          if ( defined( $self->{must_exist_sf}->{$f} ) ) {
204                                                  foreach my $sf (sort keys %{ $self->{must_exist_sf}->{$f} }) {                                                  foreach my $sf (sort keys %{ $self->{must_exist_sf}->{$f} }) {
205  #warn "====> $f $sf must exist\n";  #warn "====> $f $sf must exist\n";
206                                                          $errors->{field}->{$f}->{subfield}->{$sf} = "missing"                                                          $errors->{field}->{$f}->{subfield}->{missing}->{$sf}++
207                                                                  unless defined( $subfields->{$sf} );                                                                  unless defined( $subfields->{$sf} );
208                                                  }                                                  }
209                                          }                                          }
# Line 210  sub validate_errors { Line 211  sub validate_errors {
211                                  }                                  }
212                          } elsif (ref($v) eq 'HASH') {                          } elsif (ref($v) eq 'HASH') {
213                                  $errors->{field}->{$f}->{unexpected_subfields}++;                                  $errors->{field}->{$f}->{unexpected_subfields}++;
214                                  $errors->{dump}->{field}->{$f} =                                  $errors->{field}->{$f}->{dump} =
215                                          join('', _pack_subfields_hash( $v, 1 ) );                                          join('', _pack_subfields_hash( $v, 1 ) );
216                          }                          }
217                  }                  }
# Line 219  sub validate_errors { Line 220  sub validate_errors {
220          foreach my $must (sort keys %{ $self->{must_exist} }) {          foreach my $must (sort keys %{ $self->{must_exist} }) {
221                  next if ($fields->{$must});                  next if ($fields->{$must});
222                  $errors->{field}->{$must}->{missing}++;                  $errors->{field}->{$must}->{missing}++;
223                  $errors->{dump}->{record}++;                  $errors->{dump} = $rec_dump if ($rec_dump);
224          }          }
225    
226          if ($errors) {          if ($errors) {
227                  $log->debug("errors: ", sub { dump( $errors ) } );                  $log->debug("errors: ", sub { dump( $errors ) } );
228    
229                  my $mfn = $rec->{'000'}->[0];                  my $mfn = $rec->{'000'}->[0] || $log->logconfess("record ", dump( $rec ), " doesn't have MFN");
230                  $self->{errors}->{$mfn} = $errors;                  $self->{errors}->{$mfn} = $errors;
231          }          }
232    
# Line 234  sub validate_errors { Line 235  sub validate_errors {
235          return $errors;          return $errors;
236  }  }
237    
238    =head2 reset_errors
239    
240    Clean all accumulated errors for this input
241    
242      $validate->reset_errors;
243    
244    =cut
245    
246    sub reset_errors {
247            my $self = shift;
248            delete ($self->{errors});
249    }
250    
251    =head2 all_errors
252    
253    Return hash with all errors
254    
255      print dump( $validate->all_errors );
256    
257    =cut
258    
259    sub all_errors {
260            my $self = shift;
261            return $self->{errors};
262    }
263    
264    =head2 report
265    
266    Produce nice humanly readable report of errors
267    
268      print $validate->report;
269    
270    =cut
271    
272    sub report {
273            my $self = shift;
274    
275            sub unroll {
276                    my ($rest,$o, $dump) = @_;
277    
278    #warn "# rest: $rest o: $o\n";
279    
280                    return unless ($rest);
281    
282                    if (ref($rest) ne 'HASH') {
283                            $o .= "($rest)";
284                            return ($o,$dump);
285                    }
286    
287                    foreach my $k (sort keys %{ $rest }) {
288    
289                            if ($k eq 'dump') {
290                                    $dump = $rest->{dump};
291                                    warn "## dump: $dump\n";
292                                    next;
293                            }
294                            my $u;
295                            ($u, $dump) = unroll($rest->{$k}, $o, $dump);
296                            $o .= "$k $u";
297    
298    
299                    }
300                    return ($o,$dump);
301            }
302    
303            my $out = '';
304    
305            foreach my $mfn (sort keys %{ $self->{errors} }) {
306                    my ($msg,$dump) = unroll( $self->{errors}->{$mfn}, '', '' );
307                    $out .= "MFN $mfn\n$msg\t$dump\n\n";
308            }
309    
310            return $out;
311    }
312    
313  =head1 AUTHOR  =head1 AUTHOR
314    
315  Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>  Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>

Legend:
Removed from v.652  
changed lines
  Added in v.664

  ViewVC Help
Powered by ViewVC 1.1.26