r929@llin: dpavlin | 2006-09-11 13:56:02 +0200
[webpac2] / lib / WebPAC / Validate.pm
index f9f6fc5..b417e99 100644 (file)
@@ -8,7 +8,9 @@ use blib;
 use base 'WebPAC::Common';
 use File::Slurp;
 use List::Util qw/first/;
-use Data::Dumper;
+use Data::Dump qw/dump/;
+use WebPAC::Normalize qw/_pack_subfields_hash/;
+use Storable qw/dclone/;
 
 =head1 NAME
 
@@ -16,11 +18,11 @@ WebPAC::Validate - provide simple validation for records
 
 =head1 VERSION
 
-Version 0.01
+Version 0.07
 
 =cut
 
-our $VERSION = '0.01';
+our $VERSION = '0.07';
 
 =head1 SYNOPSIS
 
@@ -32,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
 
@@ -45,7 +49,7 @@ configuration file in following format:
 Create new validation object
 
   my $validate = new WebPAC::Validate(
-       path => '/path/to/input/validate_file',
+       path => 'conf/validate/file',
   );
 
 =cut
@@ -80,17 +84,27 @@ 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) {
-                       $v->{$fld} = \@d;
+                       $v->{$fld} = [ map {
+                               my $sf = $_;
+                               if ( $sf =~ s/!(\*)?$/$1/ ) {
+                                       $self->{must_exist_sf}->{ $fld }->{ $sf }++;
+                               };
+                               $sf;
+                       } @d ];
                } else {
                        $v->{$fld} = 1;
                }
 
        }
 
-       $log->debug("current validation rules: ", Dumper($v));
+       $log->debug("current validation rules: ", dump($v));
 
        $self->{rules} = $v;
 
@@ -103,7 +117,7 @@ sub new {
 
 Validate record and return errors
 
-  my @errors = $validate->validate_errors( $rec );
+  my @errors = $validate->validate_errors( $rec, $rec_dump );
 
 =cut
 
@@ -113,25 +127,31 @@ sub validate_errors {
        my $log = $self->_get_logger();
 
        my $rec = shift || $log->logdie("validate_errors need record");
+       my $rec_dump = shift;
 
        $log->logdie("rec isn't HASH") unless (ref($rec) eq 'HASH');
        $log->logdie("can't find validation rules") unless (my $r = $self->{rules});
 
-       my @errors;
+       my $errors;
+
+       $log->debug("rec = ", sub { dump($rec) }, "keys = ", keys %{ $rec });
 
-       $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})) {
-                       push @errors, "field '$f' shouldn't exists";
+               $fields->{$f}++;
+
+               if ( ! defined($r->{$f}) ) {
+                       $errors->{field}->{ $f }->{unexpected} = "this field is not expected";
                        next;
                }
 
+
                if (ref($rec->{$f}) ne 'ARRAY') {
-                       push @errors, "field '$f' isn't repetable, probably bug in parsing input data";
+                       $errors->{field}->{ $f }->{not_repeatable} = "probably bug in parsing input data";
                        next;
                }
 
@@ -139,28 +159,180 @@ sub validate_errors {
                        # can we have subfields?
                        if (ref($r->{$f}) eq 'ARRAY') {
                                # are values hashes? (has subfields)
-                               if (ref($v) ne 'HASH') {
-                                       push @errors, "$f has value without subfields: $v";
+                               if (! defined($v)) {
+#                                      $errors->{field}->{$f}->{empty} = undef;
+#                                      $errors->{dump} = $rec_dump if ($rec_dump);
+                               } elsif (ref($v) ne 'HASH') {
+                                       $errors->{field}->{$f}->{missing_subfield} = join(",", @{ $r->{$f} }) . " required";
                                        next;
                                } else {
+
+                                       my $h = dclone( $v );
+
+                                       my $sf_repeatable;
+
+                                       delete($v->{subfields}) if (defined($v->{subfields}));
+
+                                       my $subfields;
+
                                        foreach my $sf (keys %{ $v }) {
-                                               # permited subfield?
-                                               if (! first { $_ eq $sf } @{ $r->{$f} }) {
-                                                       push @errors, "$f has unknown subfield: $sf" if ($sf ne 'subfields');
+
+                                               $subfields->{ $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} }) {
+                                                               $errors->{field}->{ $f }->{subfield}->{extra}->{$sf}++;
+                                                       }
+                                               }
+
+                                       }
+                                       if (my @r_sf = sort keys( %$sf_repeatable )) {
+
+                                               foreach my $sf (@r_sf) {
+                                                       $errors->{field}->{$f}->{subfield}->{extra_repeatable}->{$sf}++;
+                                                       $errors->{field}->{$f}->{dump} =
+                                                               join('', _pack_subfields_hash( $h, 1 ) );
+                                               }
+
+                                       }
+
+                                       if ( defined( $self->{must_exist_sf}->{$f} ) ) {
+                                               foreach my $sf (sort keys %{ $self->{must_exist_sf}->{$f} }) {
+#warn "====> $f $sf must exist\n";
+                                                       $errors->{field}->{$f}->{subfield}->{missing}->{$sf}++
+                                                               unless defined( $subfields->{$sf} );
                                                }
                                        }
+
                                }
                        } elsif (ref($v) eq 'HASH') {
-                               push @errors, "$f has subfields which is not valid";
+                               $errors->{field}->{$f}->{unexpected_subfields}++;
+                               $errors->{field}->{$f}->{dump} =
+                                       join('', _pack_subfields_hash( $v, 1 ) );
                        }
                }
        }
 
+       foreach my $must (sort keys %{ $self->{must_exist} }) {
+               next if ($fields->{$must});
+               $errors->{field}->{$must}->{missing}++;
+               $errors->{dump} = $rec_dump if ($rec_dump);
+       }
+
+       if ($errors) {
+               $log->debug("errors: ", sub { dump( $errors ) } );
+
+               my $mfn = $rec->{'000'}->[0] || $log->logconfess("record ", dump( $rec ), " doesn't have MFN");
+               $self->{errors}->{$mfn} = $errors;
+       }
+
        #$log->logcluck("return from this function is ARRAY") unless wantarray;
 
-       $log->debug("errors: ", join(", ", @errors)) if (@errors);
+       return $errors;
+}
+
+=head2 reset_errors
+
+Clean all accumulated errors for this input
+
+  $validate->reset_errors;
+
+=cut
+
+sub reset_errors {
+       my $self = shift;
+       delete ($self->{errors});
+}
+
+=head2 all_errors
+
+Return hash with all errors
+
+  print dump( $validate->all_errors );
+
+=cut
+
+sub all_errors {
+       my $self = shift;
+       return $self->{errors};
+}
+
+=head2 report
+
+Produce nice humanly readable report of errors
+
+  print $validate->report;
+
+=cut
+
+sub report {
+       my $self = shift;
+
+       my $log = $self->_get_logger();
+
+       sub unroll {
+               my ($tree, $accumulated) = @_;
+
+               $log->debug("# ",
+                       ( $tree                 ? "tree: $tree "                                        : '' ),
+                       ( $accumulated  ? "accumulated: $accumulated "          : '' ),
+               );
+
+               my $results;
+
+               if (ref($tree) ne 'HASH') {
+                       return ("$accumulated\t($tree)", undef);
+               }
+
+               my $dump;
+
+               foreach my $k (sort keys %{ $tree }) {
+
+                       if ($k eq 'dump') {
+                               $dump = $tree->{dump};
+                               warn "## dump: $dump\n";
+                               next;
+                       }
+
+                       $log->debug("current: $k");
+
+                       my ($new_results, $new_dump) = unroll($tree->{$k},
+                               $accumulated ? "$accumulated\t$k" : $k
+                       );
+
+                       $log->debug(
+                               ( $new_results          ? "new_results: " . dump($new_results) ." "     : '' ),
+                       );
+
+                       push @$results, $new_results if ($new_results);
+                       $dump = $new_dump if ($new_dump);
+
+               }
+
+               $log->debug(
+                       ( $results              ? "results: " . dump($results) ." "     : '' ),
+               );
+
+               if ($#$results == 0) {
+                       return ($results->[0], $dump);
+               } else {
+                       return ($results, $dump);
+               }
+       }
+
+       my $out = '';
+       my $e = $self->{errors} || return;
+
+       foreach my $mfn (sort keys %$e) {
+               my ($r, $d) = unroll( $e->{$mfn} );
+               $out .= "MFN $mfn\n", dump($r), "\t$d\n\n";
+       }
 
-       return @errors;
+       return $out;
 }
 
 =head1 AUTHOR