117 |
|
|
118 |
Validate record and return errors |
Validate record and return errors |
119 |
|
|
120 |
my @errors = $validate->validate_errors( $rec ); |
my @errors = $validate->validate_errors( $rec, $rec_dump ); |
121 |
|
|
122 |
=cut |
=cut |
123 |
|
|
127 |
my $log = $self->_get_logger(); |
my $log = $self->_get_logger(); |
128 |
|
|
129 |
my $rec = shift || $log->logdie("validate_errors need record"); |
my $rec = shift || $log->logdie("validate_errors need record"); |
130 |
|
my $rec_dump = shift; |
131 |
|
|
132 |
$log->logdie("rec isn't HASH") unless (ref($rec) eq 'HASH'); |
$log->logdie("rec isn't HASH") unless (ref($rec) eq 'HASH'); |
133 |
$log->logdie("can't find validation rules") unless (my $r = $self->{rules}); |
$log->logdie("can't find validation rules") unless (my $r = $self->{rules}); |
161 |
# are values hashes? (has subfields) |
# are values hashes? (has subfields) |
162 |
if (! defined($v)) { |
if (! defined($v)) { |
163 |
$errors->{field}->{$f}->{empty} = undef; |
$errors->{field}->{$f}->{empty} = undef; |
164 |
$errors->{dump}->{record}++; |
$errors->{dump} = $rec_dump if ($rec_dump); |
165 |
} elsif (ref($v) ne 'HASH') { |
} elsif (ref($v) ne 'HASH') { |
166 |
$errors->{field}->{$f}->{missing_subfield} = "value without subfields: $v"; |
$errors->{field}->{$f}->{missing_subfield} = "subfields required for this field"; |
167 |
next; |
next; |
168 |
} else { |
} else { |
169 |
|
|
185 |
$sf_repeatable->{$sf}++; |
$sf_repeatable->{$sf}++; |
186 |
}; |
}; |
187 |
if (! first { $_ eq $sf } @{ $r->{$f} }) { |
if (! first { $_ eq $sf } @{ $r->{$f} }) { |
188 |
$errors->{field}->{ $f }->{subfield}->{$sf} = "unknown"; |
$errors->{field}->{ $f }->{subfield}->{$sf} = "found extra subfield $sf which shouldn't exist"; |
189 |
} |
} |
190 |
} |
} |
191 |
|
|
194 |
|
|
195 |
foreach my $sf (@r_sf) { |
foreach my $sf (@r_sf) { |
196 |
$errors->{field}->{$f}->{subfield}->{$sf} = "repeatable"; |
$errors->{field}->{$f}->{subfield}->{$sf} = "repeatable"; |
197 |
$errors->{dump}->{field}->{$f} = |
$errors->{field}->{$f}->{dump}->{$f} = |
198 |
join('', _pack_subfields_hash( $h, 1 ) ); |
join('', _pack_subfields_hash( $h, 1 ) ); |
199 |
} |
} |
200 |
|
|
211 |
} |
} |
212 |
} elsif (ref($v) eq 'HASH') { |
} elsif (ref($v) eq 'HASH') { |
213 |
$errors->{field}->{$f}->{unexpected_subfields}++; |
$errors->{field}->{$f}->{unexpected_subfields}++; |
214 |
$errors->{dump}->{field}->{$f} = |
$errors->{field}->{$f}->{dump} = |
215 |
join('', _pack_subfields_hash( $v, 1 ) ); |
join('', _pack_subfields_hash( $v, 1 ) ); |
216 |
} |
} |
217 |
} |
} |
220 |
foreach my $must (sort keys %{ $self->{must_exist} }) { |
foreach my $must (sort keys %{ $self->{must_exist} }) { |
221 |
next if ($fields->{$must}); |
next if ($fields->{$must}); |
222 |
$errors->{field}->{$must}->{missing}++; |
$errors->{field}->{$must}->{missing}++; |
223 |
$errors->{dump}->{record}++; |
$errors->{dump} = $rec_dump if ($rec_dump); |
224 |
} |
} |
225 |
|
|
226 |
if ($errors) { |
if ($errors) { |
227 |
$log->debug("errors: ", sub { dump( $errors ) } ); |
$log->debug("errors: ", sub { dump( $errors ) } ); |
228 |
|
|
229 |
my $mfn = $rec->{'000'}->[0]; |
my $mfn = $rec->{'000'}->[0] || $log->logconfess("record ", dump( $rec ), " doesn't have MFN"); |
230 |
$self->{errors}->{$mfn} = $errors; |
$self->{errors}->{$mfn} = $errors; |
231 |
} |
} |
232 |
|
|
235 |
return $errors; |
return $errors; |
236 |
} |
} |
237 |
|
|
238 |
|
=head2 reset_errors |
239 |
|
|
240 |
|
Clean all accumulated errors for this input |
241 |
|
|
242 |
|
$validate->reset_errors; |
243 |
|
|
244 |
|
=cut |
245 |
|
|
246 |
|
sub reset_errors { |
247 |
|
my $self = shift; |
248 |
|
delete ($self->{errors}); |
249 |
|
} |
250 |
|
|
251 |
|
=head2 all_errors |
252 |
|
|
253 |
|
Return hash with all errors |
254 |
|
|
255 |
|
print dump( $validate->all_errors ); |
256 |
|
|
257 |
|
=cut |
258 |
|
|
259 |
|
sub all_errors { |
260 |
|
my $self = shift; |
261 |
|
return $self->{errors}; |
262 |
|
} |
263 |
|
|
264 |
=head1 AUTHOR |
=head1 AUTHOR |
265 |
|
|
266 |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |