/[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 652 by dpavlin, Thu Sep 7 15:01:45 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.06
22    
23  =cut  =cut
24    
25  our $VERSION = '0.02';  our $VERSION = '0.06';
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    
# Line 82  sub new { Line 84  sub new {
84    
85                  my $fld = shift @d;                  my $fld = shift @d;
86    
87                    if ($fld =~ s/!$//) {
88                            $self->{must_exist}->{$fld}++;
89                    }
90    
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 119  sub validate_errors { Line 131  sub validate_errors {
131          $log->logdie("rec isn't HASH") unless (ref($rec) eq 'HASH');          $log->logdie("rec isn't HASH") unless (ref($rec) eq 'HASH');
132          $log->logdie("can't find validation rules") unless (my $r = $self->{rules});          $log->logdie("can't find validation rules") unless (my $r = $self->{rules});
133    
134          my @errors;          my $errors;
135    
136            $log->debug("rec = ", sub { dump($rec) }, "keys = ", keys %{ $rec });
137    
138          $log->debug("rec = ", sub { Dumper($rec) }, "keys = ", keys %{ $rec });          my $fields;
139    
140          foreach my $f (keys %{ $rec }) {          foreach my $f (keys %{ $rec }) {
141    
142                  next if (!defined($f) || $f eq '' || $f eq '000');                  next if (!defined($f) || $f eq '' || $f eq '000');
143    
144                  if (! defined($r->{$f})) {                  $fields->{$f}++;
145                          push @errors, "field '$f' shouldn't exists";  
146                    if ( ! defined($r->{$f}) ) {
147                            $errors->{field}->{ $f }->{extra} = "not expected";
148                          next;                          next;
149                  }                  }
150    
151    
152                  if (ref($rec->{$f}) ne 'ARRAY') {                  if (ref($rec->{$f}) ne 'ARRAY') {
153                          push @errors, "field '$f' isn't repetable, probably bug in parsing input data";                          $errors->{field}->{ $f }->{not_repeatable} = "probably bug in parsing input data";
154                          next;                          next;
155                  }                  }
156    
# Line 141  sub validate_errors { Line 158  sub validate_errors {
158                          # can we have subfields?                          # can we have subfields?
159                          if (ref($r->{$f}) eq 'ARRAY') {                          if (ref($r->{$f}) eq 'ARRAY') {
160                                  # are values hashes? (has subfields)                                  # are values hashes? (has subfields)
161                                  if (ref($v) ne 'HASH') {                                  if (! defined($v)) {
162                                          push @errors, "$f has value without subfields: $v";                                          $errors->{field}->{$f}->{empty} = undef;
163                                            $errors->{dump}->{record}++;
164                                    } elsif (ref($v) ne 'HASH') {
165                                            $errors->{field}->{$f}->{missing_subfield} = "value without subfields: $v";
166                                          next;                                          next;
167                                  } else {                                  } else {
168    
169                                          my $h = dclone( $v );                                          my $h = dclone( $v );
170    
171                                            my $sf_repeatable;
172    
173                                          delete($v->{subfields}) if (defined($v->{subfields}));                                          delete($v->{subfields}) if (defined($v->{subfields}));
174    
175                                            my $subfields;
176    
177                                          foreach my $sf (keys %{ $v }) {                                          foreach my $sf (keys %{ $v }) {
178    
179                                                  # permited subfield?                                                  $subfields->{ $sf }++;
180                                                  if (! first { $_ eq $sf } @{ $r->{$f} }) {  
181                                                          push @errors, "$f has unknown subfield: $sf";                                                  # is non-repeatable but with multiple values?
182                                                    if ( ! first { $_ eq $sf.'*' } @{$r->{$f}} ) {
183                                                            if ( ref($v->{$sf}) eq 'ARRAY' ) {
184                                                                    $sf_repeatable->{$sf}++;
185                                                            };
186                                                            if (! first { $_ eq $sf } @{ $r->{$f} }) {
187                                                                    $errors->{field}->{ $f }->{subfield}->{$sf} = "unknown";
188                                                            }
189                                                  }                                                  }
190    
191                                                  # is repeatable?                                          }
192                                                  if ( ref($v->{$sf}) eq 'ARRAY' ) {                                          if (my @r_sf = sort keys( %$sf_repeatable )) {
193                                                          push @errors, "$f subfield $sf is repeatable: " .  
194                                                                  join('', _pack_subfields_hash( dclone($h), 1) );                                                  foreach my $sf (@r_sf) {
195                                                          ### FIXME                                                          $errors->{field}->{$f}->{subfield}->{$sf} = "repeatable";
196                                                            $errors->{dump}->{field}->{$f} =
197                                                                    join('', _pack_subfields_hash( $h, 1 ) );
198                                                  }                                                  }
199    
200                                          }                                          }
201    
202                                            if ( defined( $self->{must_exist_sf}->{$f} ) ) {
203                                                    foreach my $sf (sort keys %{ $self->{must_exist_sf}->{$f} }) {
204    #warn "====> $f $sf must exist\n";
205                                                            $errors->{field}->{$f}->{subfield}->{$sf} = "missing"
206                                                                    unless defined( $subfields->{$sf} );
207                                                    }
208                                            }
209    
210                                  }                                  }
211                          } elsif (ref($v) eq 'HASH') {                          } elsif (ref($v) eq 'HASH') {
212                                  push @errors, "$f has subfields which is not valid";                                  $errors->{field}->{$f}->{unexpected_subfields}++;
213                                    $errors->{dump}->{field}->{$f} =
214                                            join('', _pack_subfields_hash( $v, 1 ) );
215                          }                          }
216                  }                  }
217          }          }
218    
219          #$log->logcluck("return from this function is ARRAY") unless wantarray;          foreach my $must (sort keys %{ $self->{must_exist} }) {
220                    next if ($fields->{$must});
221                    $errors->{field}->{$must}->{missing}++;
222                    $errors->{dump}->{record}++;
223            }
224    
225            if ($errors) {
226                    $log->debug("errors: ", sub { dump( $errors ) } );
227    
228          $log->debug("errors: ", join(", ", @errors)) if (@errors);                  my $mfn = $rec->{'000'}->[0];
229                    $self->{errors}->{$mfn} = $errors;
230            }
231    
232            #$log->logcluck("return from this function is ARRAY") unless wantarray;
233    
234          return @errors;          return $errors;
235  }  }
236    
237  =head1 AUTHOR  =head1 AUTHOR

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

  ViewVC Help
Powered by ViewVC 1.1.26