/[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 514 by dpavlin, Tue May 16 13:38:09 2006 UTC revision 643 by dpavlin, Wed Sep 6 21:10:30 2006 UTC
# Line 7  use blib; Line 7  use blib;
7    
8  use base 'WebPAC::Common';  use base 'WebPAC::Common';
9  use File::Slurp;  use File::Slurp;
10    use List::Util qw/first/;
11  use Data::Dumper;  use Data::Dumper;
12    use WebPAC::Normalize qw/_pack_subfields_hash/;
13    use Storable qw/dclone/;
14    
15  =head1 NAME  =head1 NAME
16    
# Line 15  WebPAC::Validate - provide simple valida Line 18  WebPAC::Validate - provide simple valida
18    
19  =head1 VERSION  =head1 VERSION
20    
21  Version 0.01  Version 0.02
22    
23  =cut  =cut
24    
25  our $VERSION = '0.01';  our $VERSION = '0.02';
26    
27  =head1 SYNOPSIS  =head1 SYNOPSIS
28    
# Line 44  configuration file in following format: Line 47  configuration file in following format:
47  Create new validation object  Create new validation object
48    
49    my $validate = new WebPAC::Validate(    my $validate = new WebPAC::Validate(
50          path => '/path/to/input/validate_file',          path => 'conf/validate/file',
51    );    );
52    
53  =cut  =cut
# Line 68  sub new { Line 71  sub new {
71    
72          foreach my $l (split(/[\n\r]+/, $v_file)) {          foreach my $l (split(/[\n\r]+/, $v_file)) {
73                  $curr_line++;                  $curr_line++;
74                  # skip comments  
75                  next if ($l =~ m/^#/);                  # skip comments and whitespaces
76                    next if ($l =~ /^#/ || $l =~ /^\s*$/);
77    
78                  $l =~ s/^\s+//;                  $l =~ s/^\s+//;
79                  $l =~ s/\s+$//;                  $l =~ s/\s+$//;
80    
81                  my @d = split(/\s+/, $l);                  my @d = split(/\s+/, $l);
82    
83                  my $fld = shift @d || $log->logdie("need field name in line $curr_line: $l");                  my $fld = shift @d;
84    
85                    $log->logdie("need field name in line $curr_line: $l") unless (defined($fld));
86    
87                  if (@d) {                  if (@d) {
88                          $v->{$fld}->{ref} = 'ARRAY';                          $v->{$fld} = \@d;
                         $v->{$fld}->{sf} = \@d;  
89                  } else {                  } else {
90                          $v->{$fld}->{ref} = '';                          $v->{$fld} = 1;
91                  }                  }
92    
93          }          }
# Line 91  sub new { Line 96  sub new {
96    
97          $self->{rules} = $v;          $self->{rules} = $v;
98    
99            $log->info("validation uses rules from $self->{path}");
100    
101          $self ? return $self : return undef;          $self ? return $self : return undef;
102  }  }
103    
104    =head2 validate_errors
105    
106    Validate record and return errors
107    
108      my @errors = $validate->validate_errors( $rec );
109    
110    =cut
111    
112    sub validate_errors {
113            my $self = shift;
114    
115            my $log = $self->_get_logger();
116    
117            my $rec = shift || $log->logdie("validate_errors need record");
118    
119            $log->logdie("rec isn't HASH") unless (ref($rec) eq 'HASH');
120            $log->logdie("can't find validation rules") unless (my $r = $self->{rules});
121    
122            my @errors;
123    
124            $log->debug("rec = ", sub { Dumper($rec) }, "keys = ", keys %{ $rec });
125    
126            foreach my $f (keys %{ $rec }) {
127    
128                    next if (!defined($f) || $f eq '' || $f eq '000');
129    
130                    if (! defined($r->{$f})) {
131                            push @errors, "field '$f' shouldn't exists";
132                            next;
133                    }
134    
135                    if (ref($rec->{$f}) ne 'ARRAY') {
136                            push @errors, "field '$f' isn't repetable, probably bug in parsing input data";
137                            next;
138                    }
139    
140                    foreach my $v (@{ $rec->{$f} }) {
141                            # can we have subfields?
142                            if (ref($r->{$f}) eq 'ARRAY') {
143                                    # are values hashes? (has subfields)
144                                    if (ref($v) ne 'HASH') {
145                                            push @errors, "$f has value without subfields: $v";
146                                            next;
147                                    } else {
148    
149                                            my $h = dclone( $v );
150    
151                                            delete($v->{subfields}) if (defined($v->{subfields}));
152    
153                                            foreach my $sf (keys %{ $v }) {
154    
155                                                    # permited subfield?
156                                                    if (! first { $_ eq $sf } @{ $r->{$f} }) {
157                                                            push @errors, "$f has unknown subfield: $sf";
158                                                    }
159    
160                                                    # is repeatable?
161                                                    if ( ref($v->{$sf}) eq 'ARRAY' ) {
162                                                            push @errors, "$f subfield $sf is repeatable: " .
163                                                                    join('', _pack_subfields_hash( dclone($h), 1) );
164                                                            ### FIXME
165                                                    }
166                                            }
167                                    }
168                            } elsif (ref($v) eq 'HASH') {
169                                    push @errors, "$f has subfields which is not valid";
170                            }
171                    }
172            }
173    
174            #$log->logcluck("return from this function is ARRAY") unless wantarray;
175    
176            $log->debug("errors: ", join(", ", @errors)) if (@errors);
177    
178            return @errors;
179    }
180    
181  =head1 AUTHOR  =head1 AUTHOR
182    
183  Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>  Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>

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

  ViewVC Help
Powered by ViewVC 1.1.26