check for fields which must exist or subfields which can be repeatable
authorDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 6 Sep 2006 22:07:21 +0000 (22:07 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 6 Sep 2006 22:07:21 +0000 (22:07 +0000)
git-svn-id: svn+ssh://mjesec/home/dpavlin/svn/webpac2/trunk@645 07558da8-63fa-0310-ba24-9fe276d99e06

conf/validate/fflo
lib/WebPAC/Validate.pm

index 2fb7bc4..5ebdaa7 100644 (file)
@@ -1,7 +1,7 @@
 0 
 10 
 101 
-200 a b c d e f g k
+200! a b c d e* f g k
 205 a f 
 210 a c d 
 215 a c d e
index 9b04357..b87dfe9 100644 (file)
@@ -18,11 +18,11 @@ WebPAC::Validate - provide simple validation for records
 
 =head1 VERSION
 
-Version 0.02
+Version 0.03
 
 =cut
 
-our $VERSION = '0.02';
+our $VERSION = '0.03';
 
 =head1 SYNOPSIS
 
@@ -34,9 +34,11 @@ configuration file in following format:
   # 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 @@ sub new {
 
                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 @@ sub validate_errors {
 
        $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 @@ sub validate_errors {
 
                                        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 @@ sub validate_errors {
                }
        }
 
+       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);