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 |
|
|
18 |
|
|
19 |
=head1 VERSION |
=head1 VERSION |
20 |
|
|
21 |
Version 0.04 |
Version 0.06 |
22 |
|
|
23 |
=cut |
=cut |
24 |
|
|
25 |
our $VERSION = '0.04'; |
our $VERSION = '0.06'; |
26 |
|
|
27 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
28 |
|
|
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 |
|
|
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 { Dumper($rec) }, "keys = ", keys %{ $rec }); |
$log->debug("rec = ", sub { dump($rec) }, "keys = ", keys %{ $rec }); |
137 |
|
|
138 |
my $fields; |
my $fields; |
139 |
|
|
144 |
$fields->{$f}++; |
$fields->{$f}++; |
145 |
|
|
146 |
if ( ! defined($r->{$f}) ) { |
if ( ! defined($r->{$f}) ) { |
147 |
push @errors, "field '$f' shouldn't exists"; |
$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 |
|
|
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 |
|
|
184 |
$sf_repeatable->{$sf}++; |
$sf_repeatable->{$sf}++; |
185 |
}; |
}; |
186 |
if (! first { $_ eq $sf } @{ $r->{$f} }) { |
if (! first { $_ eq $sf } @{ $r->{$f} }) { |
187 |
push @errors, "$f has unknown subfield: $sf"; |
$errors->{field}->{ $f }->{subfield}->{$sf} = "unknown"; |
188 |
} |
} |
189 |
} |
} |
190 |
|
|
191 |
} |
} |
192 |
if (my @r_sf = sort keys( %$sf_repeatable )) { |
if (my @r_sf = sort keys( %$sf_repeatable )) { |
|
my $plural = $#r_sf > 0 ? 1 : 0; |
|
193 |
|
|
194 |
push @errors, "$f subfield" . |
foreach my $sf (@r_sf) { |
195 |
( $plural ? 's ' : ' ' ) . |
$errors->{field}->{$f}->{subfield}->{$sf} = "repeatable"; |
196 |
join(', ', @r_sf) . |
$errors->{dump}->{field}->{$f} = |
197 |
( $plural ? ' are ' : ' is ' ) . |
join('', _pack_subfields_hash( $h, 1 ) ); |
198 |
'repeatable in: ' . |
} |
199 |
join('', _pack_subfields_hash( $h, 1) ); |
|
200 |
} |
} |
201 |
|
|
202 |
if ( defined( $self->{must_exist_sf}->{$f} ) ) { |
if ( defined( $self->{must_exist_sf}->{$f} ) ) { |
203 |
foreach my $sf (sort keys %{ $self->{must_exist_sf}->{$f} }) { |
foreach my $sf (sort keys %{ $self->{must_exist_sf}->{$f} }) { |
204 |
#warn "====> $f $sf must exist\n"; |
#warn "====> $f $sf must exist\n"; |
205 |
push @errors, "$f missing required subfield $sf" |
$errors->{field}->{$f}->{subfield}->{$sf} = "missing" |
206 |
unless ( |
unless defined( $subfields->{$sf} ); |
|
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 |
foreach my $must (sort keys %{ $self->{must_exist} }) { |
foreach my $must (sort keys %{ $self->{must_exist} }) { |
220 |
next if ($fields->{$must}); |
next if ($fields->{$must}); |
221 |
push @errors, |
$errors->{field}->{$must}->{missing}++; |
222 |
"field $must should exist, but it doesn't"; |
$errors->{dump}->{record}++; |
223 |
} |
} |
224 |
|
|
225 |
#$log->logcluck("return from this function is ARRAY") unless wantarray; |
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 |