/[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 644 by dpavlin, Wed Sep 6 21:10:30 2006 UTC revision 645 by dpavlin, Wed Sep 6 22:07:21 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.03
22    
23  =cut  =cut
24    
25  our $VERSION = '0.02';  our $VERSION = '0.03';
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) {
# Line 123  sub validate_errors { Line 129  sub validate_errors {
129    
130          $log->debug("rec = ", sub { Dumper($rec) }, "keys = ", keys %{ $rec });          $log->debug("rec = ", sub { Dumper($rec) }, "keys = ", keys %{ $rec });
131    
132            my $fields;
133    
134          foreach my $f (keys %{ $rec }) {          foreach my $f (keys %{ $rec }) {
135    
136                  next if (!defined($f) || $f eq '' || $f eq '000');                  next if (!defined($f) || $f eq '' || $f eq '000');
137    
138                  if (! defined($r->{$f})) {                  $fields->{$f}++;
139    
140                    if ( ! defined($r->{$f}) ) {
141                          push @errors, "field '$f' shouldn't exists";                          push @errors, "field '$f' shouldn't exists";
142                          next;                          next;
143                  }                  }
144    
145    
146                  if (ref($rec->{$f}) ne 'ARRAY') {                  if (ref($rec->{$f}) ne 'ARRAY') {
147                          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";
148                          next;                          next;
# Line 148  sub validate_errors { Line 159  sub validate_errors {
159    
160                                          my $h = dclone( $v );                                          my $h = dclone( $v );
161    
162                                            my $sf_repeatable;
163    
164                                          delete($v->{subfields}) if (defined($v->{subfields}));                                          delete($v->{subfields}) if (defined($v->{subfields}));
165    
166                                          foreach my $sf (keys %{ $v }) {                                          foreach my $sf (keys %{ $v }) {
167    
168                                                  # permited subfield?                                                  # is non-repeatable but with multiple values?
169                                                  if (! first { $_ eq $sf } @{ $r->{$f} }) {                                                  if ( ! first { $_ eq $sf.'*' } @{$r->{$f}} ) {
170                                                          push @errors, "$f has unknown subfield: $sf";                                                          if ( ref($v->{$sf}) eq 'ARRAY' ) {
171                                                                    $sf_repeatable->{$sf}++;
172                                                            };
173                                                            if (! first { $_ eq $sf } @{ $r->{$f} }) {
174                                                                    push @errors, "$f has unknown subfield: $sf";
175                                                            }
176                                                  }                                                  }
177    
178                                                  # is repeatable?                                          }
179                                                  if ( ref($v->{$sf}) eq 'ARRAY' ) {                                          if (my @r_sf = sort keys( %$sf_repeatable )) {
180                                                          push @errors, "$f subfield $sf is repeatable: " .                                                  my $plural = $#r_sf > 0 ? 1 : 0;
181                                                                  join('', _pack_subfields_hash( dclone($h), 1) );  
182                                                          ### FIXME                                                  push @errors, "$f subfield" .
183                                                  }                                                  ( $plural ? 's ' : ' ' ) .
184                                                    join(', ', @r_sf) .
185                                                    ( $plural ? ' are ' : ' is ' ) .
186                                                    'repeatable in: ' .
187                                                    join('', _pack_subfields_hash( $h, 1) );
188                                          }                                          }
189                                  }                                  }
190                          } elsif (ref($v) eq 'HASH') {                          } elsif (ref($v) eq 'HASH') {
# Line 171  sub validate_errors { Line 193  sub validate_errors {
193                  }                  }
194          }          }
195    
196            foreach my $must (sort keys %{ $self->{must_exist} }) {
197                    next if ($fields->{$must});
198                    push @errors,
199                            "field $must should exist, but it doesn't";
200            }
201    
202          #$log->logcluck("return from this function is ARRAY") unless wantarray;          #$log->logcluck("return from this function is ARRAY") unless wantarray;
203    
204          $log->debug("errors: ", join(", ", @errors)) if (@errors);          $log->debug("errors: ", join(", ", @errors)) if (@errors);

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

  ViewVC Help
Powered by ViewVC 1.1.26