/[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 645 by dpavlin, Wed Sep 6 22:07:21 2006 UTC revision 654 by dpavlin, Thu Sep 7 16:41:08 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.03  Version 0.06
22    
23  =cut  =cut
24    
25  our $VERSION = '0.03';  our $VERSION = '0.06';
26    
27  =head1 SYNOPSIS  =head1 SYNOPSIS
28    
# Line 91  sub new { Line 91  sub new {
91                  $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));
92    
93                  if (@d) {                  if (@d) {
94                          $v->{$fld} = \@d;                          $v->{$fld} = [ map {
95                                    my $sf = $_;
96                                    if ( $sf =~ s/!(\*)?$/$1/ ) {
97                                            $self->{must_exist_sf}->{ $fld }->{ $sf }++;
98                                    };
99                                    $sf;
100                            } @d ];
101                  } else {                  } else {
102                          $v->{$fld} = 1;                          $v->{$fld} = 1;
103                  }                  }
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 111  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 121  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 138  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->{field}->{ $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->{field}->{ $f }->{not_repeatable} = "probably bug in parsing input data";
155                          next;                          next;
156                  }                  }
157    
# Line 152  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->{field}->{$f}->{empty} = undef;
164                                            $errors->{dump} = $rec_dump if ($rec_dump);
165                                    } elsif (ref($v) ne 'HASH') {
166                                            $errors->{field}->{$f}->{missing_subfield} = "subfields required for this field";
167                                          next;                                          next;
168                                  } else {                                  } else {
169    
# Line 163  sub validate_errors { Line 173  sub validate_errors {
173    
174                                          delete($v->{subfields}) if (defined($v->{subfields}));                                          delete($v->{subfields}) if (defined($v->{subfields}));
175    
176                                            my $subfields;
177    
178                                          foreach my $sf (keys %{ $v }) {                                          foreach my $sf (keys %{ $v }) {
179    
180                                                    $subfields->{ $sf }++;
181    
182                                                  # is non-repeatable but with multiple values?                                                  # is non-repeatable but with multiple values?
183                                                  if ( ! first { $_ eq $sf.'*' } @{$r->{$f}} ) {                                                  if ( ! first { $_ eq $sf.'*' } @{$r->{$f}} ) {
184                                                          if ( ref($v->{$sf}) eq 'ARRAY' ) {                                                          if ( ref($v->{$sf}) eq 'ARRAY' ) {
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->{field}->{ $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->{field}->{$f}->{subfield}->{repeatable}->{$sf}++;
197                                                  join(', ', @r_sf) .                                                          $errors->{field}->{$f}->{dump}->{$f} =
198                                                  ( $plural ? ' are ' : ' is ' ) .                                                                  join('', _pack_subfields_hash( $h, 1 ) );
199                                                  'repeatable in: ' .                                                  }
200                                                  join('', _pack_subfields_hash( $h, 1) );  
201                                            }
202    
203                                            if ( defined( $self->{must_exist_sf}->{$f} ) ) {
204                                                    foreach my $sf (sort keys %{ $self->{must_exist_sf}->{$f} }) {
205    #warn "====> $f $sf must exist\n";
206                                                            $errors->{field}->{$f}->{subfield}->{$sf} = "missing"
207                                                                    unless defined( $subfields->{$sf} );
208                                                    }
209                                          }                                          }
210    
211                                  }                                  }
212                          } elsif (ref($v) eq 'HASH') {                          } elsif (ref($v) eq 'HASH') {
213                                  push @errors, "$f has subfields which is not valid";                                  $errors->{field}->{$f}->{unexpected_subfields}++;
214                                    $errors->{field}->{$f}->{dump} =
215                                            join('', _pack_subfields_hash( $v, 1 ) );
216                          }                          }
217                  }                  }
218          }          }
219    
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                  push @errors,                  $errors->{field}->{$must}->{missing}++;
223                          "field $must should exist, but it doesn't";                  $errors->{dump} = $rec_dump if ($rec_dump);
224            }
225    
226            if ($errors) {
227                    $log->debug("errors: ", sub { dump( $errors ) } );
228    
229                    my $mfn = $rec->{'000'}->[0] || $log->logconfess("record ", dump( $rec ), " doesn't have MFN");
230                    $self->{errors}->{$mfn} = $errors;
231          }          }
232    
233          #$log->logcluck("return from this function is ARRAY") unless wantarray;          #$log->logcluck("return from this function is ARRAY") unless wantarray;
234    
235          $log->debug("errors: ", join(", ", @errors)) if (@errors);          return $errors;
236    }
237    
238    =head2 reset_errors
239    
240          return @errors;  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  =head1 AUTHOR  =head1 AUTHOR

Legend:
Removed from v.645  
changed lines
  Added in v.654

  ViewVC Help
Powered by ViewVC 1.1.26