/[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 646 by dpavlin, Wed Sep 6 22:42:37 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.02  Version 0.04
22    
23  =cut  =cut
24    
25  our $VERSION = '0.02';  our $VERSION = '0.04';
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                  }                  }
# Line 123  sub validate_errors { Line 135  sub validate_errors {
135    
136          $log->debug("rec = ", sub { Dumper($rec) }, "keys = ", keys %{ $rec });          $log->debug("rec = ", sub { Dumper($rec) }, "keys = ", keys %{ $rec });
137    
138            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    
146                    if ( ! defined($r->{$f}) ) {
147                          push @errors, "field '$f' shouldn't exists";                          push @errors, "field '$f' shouldn't exists";
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";                          push @errors, "field '$f' isn't repetable, probably bug in parsing input data";
154                          next;                          next;
# Line 148  sub validate_errors { Line 165  sub validate_errors {
165    
166                                          my $h = dclone( $v );                                          my $h = dclone( $v );
167    
168                                            my $sf_repeatable;
169    
170                                          delete($v->{subfields}) if (defined($v->{subfields}));                                          delete($v->{subfields}) if (defined($v->{subfields}));
171    
172                                            my $subfields;
173    
174                                          foreach my $sf (keys %{ $v }) {                                          foreach my $sf (keys %{ $v }) {
175    
176                                                  # permited subfield?                                                  $subfields->{ $sf }++;
177                                                  if (! first { $_ eq $sf } @{ $r->{$f} }) {  
178                                                          push @errors, "$f has unknown subfield: $sf";                                                  # is non-repeatable but with multiple values?
179                                                    if ( ! first { $_ eq $sf.'*' } @{$r->{$f}} ) {
180                                                            if ( ref($v->{$sf}) eq 'ARRAY' ) {
181                                                                    $sf_repeatable->{$sf}++;
182                                                            };
183                                                            if (! first { $_ eq $sf } @{ $r->{$f} }) {
184                                                                    push @errors, "$f has unknown subfield: $sf";
185                                                            }
186                                                  }                                                  }
187    
188                                                  # is repeatable?                                          }
189                                                  if ( ref($v->{$sf}) eq 'ARRAY' ) {                                          if (my @r_sf = sort keys( %$sf_repeatable )) {
190                                                          push @errors, "$f subfield $sf is repeatable: " .                                                  my $plural = $#r_sf > 0 ? 1 : 0;
191                                                                  join('', _pack_subfields_hash( dclone($h), 1) );  
192                                                          ### FIXME                                                  push @errors, "$f subfield" .
193                                                    ( $plural ? 's ' : ' ' ) .
194                                                    join(', ', @r_sf) .
195                                                    ( $plural ? ' are ' : ' is ' ) .
196                                                    'repeatable in: ' .
197                                                    join('', _pack_subfields_hash( $h, 1) );
198                                            }
199    
200                                            if ( defined( $self->{must_exist_sf}->{$f} ) ) {
201                                                    foreach my $sf (sort keys %{ $self->{must_exist_sf}->{$f} }) {
202    warn "====> $f $sf must exist\n";
203                                                            push @errors, "$f missing required subfield $sf"
204                                                                    unless (
205                                                                            defined( $subfields->{$f} ) &&
206                                                                            defined( $subfields->{$f}->{$sf} )
207                                                                    )
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";                                  push @errors, "$f has subfields which is not valid";
# Line 171  sub validate_errors { Line 215  sub validate_errors {
215                  }                  }
216          }          }
217    
218            foreach my $must (sort keys %{ $self->{must_exist} }) {
219                    next if ($fields->{$must});
220                    push @errors,
221                            "field $must should exist, but it doesn't";
222            }
223    
224          #$log->logcluck("return from this function is ARRAY") unless wantarray;          #$log->logcluck("return from this function is ARRAY") unless wantarray;
225    
226          $log->debug("errors: ", join(", ", @errors)) if (@errors);          $log->debug("errors: ", join(", ", @errors)) if (@errors);

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

  ViewVC Help
Powered by ViewVC 1.1.26