1 package WebPAC::Validate;
8 use base 'WebPAC::Common';
10 use List::Util qw/first/;
12 use WebPAC::Normalize qw/_pack_subfields_hash/;
13 use Storable qw/dclone/;
17 WebPAC::Validate - provide simple validation for records
25 our $VERSION = '0.04';
29 This module provide a simple way to validate your file against a simple
30 configuration file in following format:
32 # field 10 doesn't have any subfields
36 # field 200 have valid subfields a-g
37 # and field e is repeatable
39 # field 205 can have only subfield a
42 # while 210 can have a c or d
49 Create new validation object
51 my $validate = new WebPAC::Validate(
52 path => 'conf/validate/file',
62 my $log = $self->_get_logger();
64 foreach my $p (qw/path/) {
65 $log->logconfess("need $p") unless ($self->{$p});
68 my $v_file = read_file( $self->{path} ) ||
69 $log->logdie("can't open validate path $self->{path}: $!");
74 foreach my $l (split(/[\n\r]+/, $v_file)) {
77 # skip comments and whitespaces
78 next if ($l =~ /^#/ || $l =~ /^\s*$/);
83 my @d = split(/\s+/, $l);
88 $self->{must_exist}->{$fld}++;
91 $log->logdie("need field name in line $curr_line: $l") unless (defined($fld));
96 if ( $sf =~ s/!(\*)?$/$1/ ) {
97 $self->{must_exist_sf}->{ $fld }->{ $sf }++;
107 $log->debug("current validation rules: ", Dumper($v));
111 $log->info("validation uses rules from $self->{path}");
113 $self ? return $self : return undef;
116 =head2 validate_errors
118 Validate record and return errors
120 my @errors = $validate->validate_errors( $rec );
124 sub validate_errors {
127 my $log = $self->_get_logger();
129 my $rec = shift || $log->logdie("validate_errors need record");
131 $log->logdie("rec isn't HASH") unless (ref($rec) eq 'HASH');
132 $log->logdie("can't find validation rules") unless (my $r = $self->{rules});
136 $log->debug("rec = ", sub { Dumper($rec) }, "keys = ", keys %{ $rec });
140 foreach my $f (keys %{ $rec }) {
142 next if (!defined($f) || $f eq '' || $f eq '000');
146 if ( ! defined($r->{$f}) ) {
147 push @errors, "field '$f' shouldn't exists";
152 if (ref($rec->{$f}) ne 'ARRAY') {
153 push @errors, "field '$f' isn't repetable, probably bug in parsing input data";
157 foreach my $v (@{ $rec->{$f} }) {
158 # can we have subfields?
159 if (ref($r->{$f}) eq 'ARRAY') {
160 # are values hashes? (has subfields)
161 if (ref($v) ne 'HASH') {
162 push @errors, "$f has value without subfields: $v";
166 my $h = dclone( $v );
170 delete($v->{subfields}) if (defined($v->{subfields}));
174 foreach my $sf (keys %{ $v }) {
176 $subfields->{ $sf }++;
178 # 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}++;
183 if (! first { $_ eq $sf } @{ $r->{$f} }) {
184 push @errors, "$f has unknown subfield: $sf";
189 if (my @r_sf = sort keys( %$sf_repeatable )) {
190 my $plural = $#r_sf > 0 ? 1 : 0;
192 push @errors, "$f subfield" .
193 ( $plural ? 's ' : ' ' ) .
195 ( $plural ? ' are ' : ' is ' ) .
197 join('', _pack_subfields_hash( $h, 1) );
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"
205 defined( $subfields->{$sf} )
211 } elsif (ref($v) eq 'HASH') {
212 push @errors, "$f has subfields which is not valid";
217 foreach my $must (sort keys %{ $self->{must_exist} }) {
218 next if ($fields->{$must});
220 "field $must should exist, but it doesn't";
223 #$log->logcluck("return from this function is ARRAY") unless wantarray;
225 $log->debug("errors: ", join(", ", @errors)) if (@errors);
232 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
234 =head1 COPYRIGHT & LICENSE
236 Copyright 2006 Dobrica Pavlinusic, All Rights Reserved.
238 This program is free software; you can redistribute it and/or modify it
239 under the same terms as Perl itself.
243 1; # End of WebPAC::Validate