--- trunk/lib/WebPAC/Validate.pm 2006/09/11 15:59:35 671 +++ trunk/lib/WebPAC/Validate.pm 2007/05/23 20:03:16 832 @@ -18,11 +18,11 @@ =head1 VERSION -Version 0.08 +Version 0.11 =cut -our $VERSION = '0.08'; +our $VERSION = '0.11'; =head1 SYNOPSIS @@ -41,6 +41,8 @@ 205! a # while 210 can have a c or d 210 a c d + # field which is ignored in validation + 999- =head1 FUNCTIONS @@ -50,8 +52,13 @@ my $validate = new WebPAC::Validate( path => 'conf/validate/file', + delimiters => [ ' : ', ' / ', ' ; ', ' , ' ], ); +Optional parametar C will turn on validating of delimiters. Be +careful here, those delimiters are just stuck into regex, so they can +contain L regexpes. + =cut sub new { @@ -86,6 +93,8 @@ if ($fld =~ s/!$//) { $self->{must_exist}->{$fld}++; + } elsif ($fld =~ s/-$//) { + $self->{dont_validate}->{$fld}++; } $log->logdie("need field name in line $curr_line: $l") unless (defined($fld)); @@ -110,23 +119,28 @@ $log->info("validation uses rules from $self->{path}"); + if ( $self->{delimiters} ) { + $self->{delimiters_regex} = '(\^[a-z0-9]|' . join('|', @{ $self->{delimiters} }) . ')'; + $log->info("validation check delimiters with regex $self->{delimiters_regex}"); + } + $self ? return $self : return undef; } -=head2 validate_errors +=head2 validate_rec Validate record and return errors - my @errors = $validate->validate_errors( $rec, $rec_dump ); + my @errors = $validate->validate_rec( $rec, $rec_dump ); =cut -sub validate_errors { +sub validate_rec { my $self = shift; my $log = $self->_get_logger(); - my $rec = shift || $log->logdie("validate_errors need record"); + my $rec = shift || $log->logdie("validate_rec need record"); my $rec_dump = shift; $log->logdie("rec isn't HASH") unless (ref($rec) eq 'HASH'); @@ -142,6 +156,22 @@ next if (!defined($f) || $f eq '' || $f eq '000'); + # first check delimiters + if ( my $regex = $self->{delimiters_regex} ) { + + foreach my $v (@{ $rec->{$f} }) { + my $l = _pack_subfields_hash( $v, 1 ); + my $template = ''; + $l =~ s/$regex/$template.=$1/eg; +# warn "## template: $template\n"; + $self->{_delimiters_templates}->{$f}->{$template}++ if ( $template ); + } + + } + + next if (defined( $self->{dont_validate}->{$f} )); + + # track field usage $fields->{$f}++; if ( ! defined($r->{$f}) ) { @@ -215,6 +245,8 @@ } } + $log->debug("_delimiters_templates = ", dump( $self->{_delimiters_templates} ) ); + foreach my $must (sort keys %{ $self->{must_exist} }) { next if ($fields->{$must}); $errors->{$must}->{missing}++; @@ -373,6 +405,35 @@ } +=head2 delimiters_templates + + +=cut + +sub delimiters_templates { + my $self = shift; + + my $t = $self->{_delimiters_templates}; + + my $log = $self->_get_logger; + + unless ($t) { + $log->warn("called without delimiters"); + return; + } + + my $out; + + foreach my $f (sort { $a <=> $b } keys %$t) { + $out .= "$f\n"; + foreach my $sft (sort { $a cmp $b } keys %{ $t->{$f} }) { + $out .= "\t" . $t->{$f}->{$sft} . "\t$sft\n"; + } + } + + return $out; +} + =head1 AUTHOR Dobrica Pavlinusic, C<< >>