/[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 643 by dpavlin, Wed Sep 6 21:10:30 2006 UTC revision 677 by dpavlin, Wed Sep 13 17:44:57 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.02  Version 0.09
22    
23  =cut  =cut
24    
25  our $VERSION = '0.02';  our $VERSION = '0.09';
26    
27  =head1 SYNOPSIS  =head1 SYNOPSIS
28    
# Line 34  configuration file in following format: Line 34  configuration file in following format:
34    # same with 101    # same with 101
35    101    101
36    # field 200 have valid subfields a-g    # field 200 have valid subfields a-g
37    200 a b c d e f g    # and field e is repeatable
38      200 a b c d e* f g
39    # field 205 can have only subfield a    # field 205 can have only subfield a
40    205 a    # and must exists
41      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 82  sub new { Line 86  sub new {
86    
87                  my $fld = shift @d;                  my $fld = shift @d;
88    
89                    if ($fld =~ s/!$//) {
90                            $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));
96    
97                  if (@d) {                  if (@d) {
98                          $v->{$fld} = \@d;                          $v->{$fld} = [ map {
99                                    my $sf = $_;
100                                    if ( $sf =~ s/!(\*)?$/$1/ ) {
101                                            $self->{must_exist_sf}->{ $fld }->{ $sf }++;
102                                    };
103                                    $sf;
104                            } @d ];
105                  } else {                  } else {
106                          $v->{$fld} = 1;                          $v->{$fld} = 1;
107                  }                  }
108    
109          }          }
110    
111          $log->debug("current validation rules: ", Dumper($v));          $log->debug("current validation rules: ", dump($v));
112    
113          $self->{rules} = $v;          $self->{rules} = $v;
114    
# Line 105  sub new { Line 121  sub new {
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    
# Line 115  sub validate_errors { Line 131  sub validate_errors {
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});
138    
139          my @errors;          my $errors;
140    
141            $log->debug("rec = ", sub { dump($rec) }, "keys = ", keys %{ $rec });
142    
143          $log->debug("rec = ", sub { Dumper($rec) }, "keys = ", keys %{ $rec });          my $fields;
144    
145          foreach my $f (keys %{ $rec }) {          foreach my $f (keys %{ $rec }) {
146    
147                  next if (!defined($f) || $f eq '' || $f eq '000');                  next if (!defined($f) || $f eq '' || $f eq '000');
148    
149                  if (! defined($r->{$f})) {                  next if (defined( $self->{dont_validate}->{$f} ));
150                          push @errors, "field '$f' shouldn't exists";  
151                    # track field usage
152                    $fields->{$f}++;
153    
154                    if ( ! defined($r->{$f}) ) {
155                            $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                          push @errors, "field '$f' isn't repetable, probably bug in parsing input data";                          $errors->{ $f }->{not_repeatable} = "probably bug in parsing input data";
162                          next;                          next;
163                  }                  }
164    
# Line 141  sub validate_errors { Line 166  sub validate_errors {
166                          # can we have subfields?                          # can we have subfields?
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 (ref($v) ne 'HASH') {                                  if (! defined($v)) {
170                                          push @errors, "$f has value without subfields: $v";  #                                       $errors->{$f}->{empty} = undef;
171    #                                       $errors->{dump} = $rec_dump if ($rec_dump);
172                                    } elsif (ref($v) ne 'HASH') {
173                                            $errors->{$f}->{missing_subfield} = join(",", @{ $r->{$f} }) . " required";
174                                          next;                                          next;
175                                  } else {                                  } else {
176    
177                                          my $h = dclone( $v );                                          my $h = dclone( $v );
178    
179                                            my $sf_repeatable;
180    
181                                          delete($v->{subfields}) if (defined($v->{subfields}));                                          delete($v->{subfields}) if (defined($v->{subfields}));
182    
183                                            my $subfields;
184    
185                                          foreach my $sf (keys %{ $v }) {                                          foreach my $sf (keys %{ $v }) {
186    
187                                                  # permited subfield?                                                  $subfields->{ $sf }++;
188                                                  if (! first { $_ eq $sf } @{ $r->{$f} }) {  
189                                                          push @errors, "$f has unknown subfield: $sf";                                                  # is non-repeatable but with multiple values?
190                                                    if ( ! first { $_ eq $sf.'*' } @{$r->{$f}} ) {
191                                                            if ( ref($v->{$sf}) eq 'ARRAY' ) {
192                                                                    $sf_repeatable->{$sf}++;
193                                                            };
194                                                            if (! first { $_ eq $sf } @{ $r->{$f} }) {
195                                                                    $errors->{ $f }->{subfield}->{extra}->{$sf}++;
196                                                            }
197                                                    }
198    
199                                            }
200                                            if (my @r_sf = sort keys( %$sf_repeatable )) {
201    
202                                                    foreach my $sf (@r_sf) {
203                                                            $errors->{$f}->{subfield}->{extra_repeatable}->{$sf}++;
204                                                            $errors->{$f}->{dump} = _pack_subfields_hash( $h, 1 );
205                                                  }                                                  }
206    
207                                                  # is repeatable?                                          }
208                                                  if ( ref($v->{$sf}) eq 'ARRAY' ) {  
209                                                          push @errors, "$f subfield $sf is repeatable: " .                                          if ( defined( $self->{must_exist_sf}->{$f} ) ) {
210                                                                  join('', _pack_subfields_hash( dclone($h), 1) );                                                  foreach my $sf (sort keys %{ $self->{must_exist_sf}->{$f} }) {
211                                                          ### FIXME  #warn "====> $f $sf must exist\n";
212                                                            $errors->{$f}->{subfield}->{missing}->{$sf}++
213                                                                    unless defined( $subfields->{$sf} );
214                                                  }                                                  }
215                                          }                                          }
216    
217                                  }                                  }
218                          } elsif (ref($v) eq 'HASH') {                          } elsif (ref($v) eq 'HASH') {
219                                  push @errors, "$f has subfields which is not valid";                                  $errors->{$f}->{unexpected_subfields}++;
220                                    $errors->{$f}->{dump} = _pack_subfields_hash( $v, 1 );
221                          }                          }
222                  }                  }
223          }          }
224    
225            foreach my $must (sort keys %{ $self->{must_exist} }) {
226                    next if ($fields->{$must});
227                    $errors->{$must}->{missing}++;
228                    $errors->{dump} = $rec_dump if ($rec_dump);
229            }
230    
231            if ($errors) {
232                    $log->debug("errors: ", $self->report_error( $errors ) );
233    
234                    my $mfn = $rec->{'000'}->[0] || $log->logconfess("record ", dump( $rec ), " doesn't have MFN");
235                    $self->{errors}->{$mfn} = $errors;
236            }
237    
238          #$log->logcluck("return from this function is ARRAY") unless wantarray;          #$log->logcluck("return from this function is ARRAY") unless wantarray;
239    
240          $log->debug("errors: ", join(", ", @errors)) if (@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    
         return @errors;  
381  }  }
382    
383  =head1 AUTHOR  =head1 AUTHOR

Legend:
Removed from v.643  
changed lines
  Added in v.677

  ViewVC Help
Powered by ViewVC 1.1.26