--- trunk/lib/WebPAC/Validate.pm 2006/09/06 21:10:30 643 +++ trunk/lib/WebPAC/Validate.pm 2006/09/06 22:07:21 645 @@ -18,11 +18,11 @@ =head1 VERSION -Version 0.02 +Version 0.03 =cut -our $VERSION = '0.02'; +our $VERSION = '0.03'; =head1 SYNOPSIS @@ -34,9 +34,11 @@ # same with 101 101 # field 200 have valid subfields a-g - 200 a b c d e f g + # and field e is repeatable + 200 a b c d e* f g # field 205 can have only subfield a - 205 a + # and must exists + 205! a # while 210 can have a c or d 210 a c d @@ -82,6 +84,10 @@ my $fld = shift @d; + if ($fld =~ s/!$//) { + $self->{must_exist}->{$fld}++; + } + $log->logdie("need field name in line $curr_line: $l") unless (defined($fld)); if (@d) { @@ -123,15 +129,20 @@ $log->debug("rec = ", sub { Dumper($rec) }, "keys = ", keys %{ $rec }); + my $fields; + foreach my $f (keys %{ $rec }) { next if (!defined($f) || $f eq '' || $f eq '000'); - if (! defined($r->{$f})) { + $fields->{$f}++; + + if ( ! defined($r->{$f}) ) { push @errors, "field '$f' shouldn't exists"; next; } + if (ref($rec->{$f}) ne 'ARRAY') { push @errors, "field '$f' isn't repetable, probably bug in parsing input data"; next; @@ -148,21 +159,32 @@ my $h = dclone( $v ); + my $sf_repeatable; + delete($v->{subfields}) if (defined($v->{subfields})); foreach my $sf (keys %{ $v }) { - # permited subfield? - if (! first { $_ eq $sf } @{ $r->{$f} }) { - push @errors, "$f has unknown subfield: $sf"; + # is non-repeatable but with multiple values? + if ( ! first { $_ eq $sf.'*' } @{$r->{$f}} ) { + if ( ref($v->{$sf}) eq 'ARRAY' ) { + $sf_repeatable->{$sf}++; + }; + if (! first { $_ eq $sf } @{ $r->{$f} }) { + push @errors, "$f has unknown subfield: $sf"; + } } - # is repeatable? - if ( ref($v->{$sf}) eq 'ARRAY' ) { - push @errors, "$f subfield $sf is repeatable: " . - join('', _pack_subfields_hash( dclone($h), 1) ); - ### FIXME - } + } + if (my @r_sf = sort keys( %$sf_repeatable )) { + my $plural = $#r_sf > 0 ? 1 : 0; + + push @errors, "$f subfield" . + ( $plural ? 's ' : ' ' ) . + join(', ', @r_sf) . + ( $plural ? ' are ' : ' is ' ) . + 'repeatable in: ' . + join('', _pack_subfields_hash( $h, 1) ); } } } elsif (ref($v) eq 'HASH') { @@ -171,6 +193,12 @@ } } + foreach my $must (sort keys %{ $self->{must_exist} }) { + next if ($fields->{$must}); + push @errors, + "field $must should exist, but it doesn't"; + } + #$log->logcluck("return from this function is ARRAY") unless wantarray; $log->debug("errors: ", join(", ", @errors)) if (@errors);