1 |
dpavlin |
514 |
package WebPAC::Validate; |
2 |
|
|
|
3 |
|
|
use warnings; |
4 |
|
|
use strict; |
5 |
|
|
|
6 |
|
|
use blib; |
7 |
|
|
|
8 |
|
|
use base 'WebPAC::Common'; |
9 |
|
|
use File::Slurp; |
10 |
dpavlin |
515 |
use List::Util qw/first/; |
11 |
dpavlin |
514 |
use Data::Dumper; |
12 |
dpavlin |
643 |
use WebPAC::Normalize qw/_pack_subfields_hash/; |
13 |
|
|
use Storable qw/dclone/; |
14 |
dpavlin |
514 |
|
15 |
|
|
=head1 NAME |
16 |
|
|
|
17 |
|
|
WebPAC::Validate - provide simple validation for records |
18 |
|
|
|
19 |
|
|
=head1 VERSION |
20 |
|
|
|
21 |
dpavlin |
643 |
Version 0.02 |
22 |
dpavlin |
514 |
|
23 |
|
|
=cut |
24 |
|
|
|
25 |
dpavlin |
643 |
our $VERSION = '0.02'; |
26 |
dpavlin |
514 |
|
27 |
|
|
=head1 SYNOPSIS |
28 |
|
|
|
29 |
|
|
This module provide a simple way to validate your file against a simple |
30 |
|
|
configuration file in following format: |
31 |
|
|
|
32 |
|
|
# field 10 doesn't have any subfields |
33 |
|
|
10 |
34 |
|
|
# same with 101 |
35 |
|
|
101 |
36 |
|
|
# field 200 have valid subfields a-g |
37 |
|
|
200 a b c d e f g |
38 |
|
|
# field 205 can have only subfield a |
39 |
|
|
205 a |
40 |
|
|
# while 210 can have a c or d |
41 |
|
|
210 a c d |
42 |
|
|
|
43 |
|
|
=head1 FUNCTIONS |
44 |
|
|
|
45 |
|
|
=head2 new |
46 |
|
|
|
47 |
|
|
Create new validation object |
48 |
|
|
|
49 |
|
|
my $validate = new WebPAC::Validate( |
50 |
dpavlin |
643 |
path => 'conf/validate/file', |
51 |
dpavlin |
514 |
); |
52 |
|
|
|
53 |
|
|
=cut |
54 |
|
|
|
55 |
|
|
sub new { |
56 |
|
|
my $class = shift; |
57 |
|
|
my $self = {@_}; |
58 |
|
|
bless($self, $class); |
59 |
|
|
|
60 |
|
|
my $log = $self->_get_logger(); |
61 |
|
|
|
62 |
|
|
foreach my $p (qw/path/) { |
63 |
|
|
$log->logconfess("need $p") unless ($self->{$p}); |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
my $v_file = read_file( $self->{path} ) || |
67 |
|
|
$log->logdie("can't open validate path $self->{path}: $!"); |
68 |
|
|
|
69 |
|
|
my $v; |
70 |
|
|
my $curr_line = 1; |
71 |
|
|
|
72 |
|
|
foreach my $l (split(/[\n\r]+/, $v_file)) { |
73 |
|
|
$curr_line++; |
74 |
|
|
|
75 |
dpavlin |
516 |
# skip comments and whitespaces |
76 |
|
|
next if ($l =~ /^#/ || $l =~ /^\s*$/); |
77 |
|
|
|
78 |
dpavlin |
514 |
$l =~ s/^\s+//; |
79 |
|
|
$l =~ s/\s+$//; |
80 |
|
|
|
81 |
|
|
my @d = split(/\s+/, $l); |
82 |
|
|
|
83 |
dpavlin |
516 |
my $fld = shift @d; |
84 |
dpavlin |
514 |
|
85 |
dpavlin |
516 |
$log->logdie("need field name in line $curr_line: $l") unless (defined($fld)); |
86 |
|
|
|
87 |
dpavlin |
514 |
if (@d) { |
88 |
dpavlin |
515 |
$v->{$fld} = \@d; |
89 |
dpavlin |
514 |
} else { |
90 |
dpavlin |
515 |
$v->{$fld} = 1; |
91 |
dpavlin |
514 |
} |
92 |
|
|
|
93 |
|
|
} |
94 |
|
|
|
95 |
|
|
$log->debug("current validation rules: ", Dumper($v)); |
96 |
|
|
|
97 |
|
|
$self->{rules} = $v; |
98 |
|
|
|
99 |
dpavlin |
516 |
$log->info("validation uses rules from $self->{path}"); |
100 |
|
|
|
101 |
dpavlin |
514 |
$self ? return $self : return undef; |
102 |
|
|
} |
103 |
|
|
|
104 |
dpavlin |
515 |
=head2 validate_errors |
105 |
|
|
|
106 |
|
|
Validate record and return errors |
107 |
|
|
|
108 |
|
|
my @errors = $validate->validate_errors( $rec ); |
109 |
|
|
|
110 |
|
|
=cut |
111 |
|
|
|
112 |
|
|
sub validate_errors { |
113 |
|
|
my $self = shift; |
114 |
|
|
|
115 |
|
|
my $log = $self->_get_logger(); |
116 |
|
|
|
117 |
|
|
my $rec = shift || $log->logdie("validate_errors need record"); |
118 |
|
|
|
119 |
|
|
$log->logdie("rec isn't HASH") unless (ref($rec) eq 'HASH'); |
120 |
|
|
$log->logdie("can't find validation rules") unless (my $r = $self->{rules}); |
121 |
|
|
|
122 |
|
|
my @errors; |
123 |
|
|
|
124 |
|
|
$log->debug("rec = ", sub { Dumper($rec) }, "keys = ", keys %{ $rec }); |
125 |
|
|
|
126 |
|
|
foreach my $f (keys %{ $rec }) { |
127 |
|
|
|
128 |
dpavlin |
516 |
next if (!defined($f) || $f eq '' || $f eq '000'); |
129 |
dpavlin |
515 |
|
130 |
|
|
if (! defined($r->{$f})) { |
131 |
|
|
push @errors, "field '$f' shouldn't exists"; |
132 |
|
|
next; |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
if (ref($rec->{$f}) ne 'ARRAY') { |
136 |
|
|
push @errors, "field '$f' isn't repetable, probably bug in parsing input data"; |
137 |
|
|
next; |
138 |
|
|
} |
139 |
|
|
|
140 |
|
|
foreach my $v (@{ $rec->{$f} }) { |
141 |
|
|
# can we have subfields? |
142 |
|
|
if (ref($r->{$f}) eq 'ARRAY') { |
143 |
|
|
# are values hashes? (has subfields) |
144 |
|
|
if (ref($v) ne 'HASH') { |
145 |
|
|
push @errors, "$f has value without subfields: $v"; |
146 |
|
|
next; |
147 |
|
|
} else { |
148 |
dpavlin |
643 |
|
149 |
|
|
my $h = dclone( $v ); |
150 |
|
|
|
151 |
|
|
delete($v->{subfields}) if (defined($v->{subfields})); |
152 |
|
|
|
153 |
dpavlin |
515 |
foreach my $sf (keys %{ $v }) { |
154 |
dpavlin |
643 |
|
155 |
dpavlin |
515 |
# permited subfield? |
156 |
|
|
if (! first { $_ eq $sf } @{ $r->{$f} }) { |
157 |
dpavlin |
643 |
push @errors, "$f has unknown subfield: $sf"; |
158 |
dpavlin |
515 |
} |
159 |
dpavlin |
643 |
|
160 |
|
|
# is repeatable? |
161 |
|
|
if ( ref($v->{$sf}) eq 'ARRAY' ) { |
162 |
|
|
push @errors, "$f subfield $sf is repeatable: " . |
163 |
|
|
join('', _pack_subfields_hash( dclone($h), 1) ); |
164 |
|
|
### FIXME |
165 |
|
|
} |
166 |
dpavlin |
515 |
} |
167 |
|
|
} |
168 |
|
|
} elsif (ref($v) eq 'HASH') { |
169 |
|
|
push @errors, "$f has subfields which is not valid"; |
170 |
|
|
} |
171 |
|
|
} |
172 |
|
|
} |
173 |
|
|
|
174 |
|
|
#$log->logcluck("return from this function is ARRAY") unless wantarray; |
175 |
|
|
|
176 |
|
|
$log->debug("errors: ", join(", ", @errors)) if (@errors); |
177 |
|
|
|
178 |
|
|
return @errors; |
179 |
|
|
} |
180 |
|
|
|
181 |
dpavlin |
514 |
=head1 AUTHOR |
182 |
|
|
|
183 |
|
|
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
184 |
|
|
|
185 |
|
|
=head1 COPYRIGHT & LICENSE |
186 |
|
|
|
187 |
|
|
Copyright 2006 Dobrica Pavlinusic, All Rights Reserved. |
188 |
|
|
|
189 |
|
|
This program is free software; you can redistribute it and/or modify it |
190 |
|
|
under the same terms as Perl itself. |
191 |
|
|
|
192 |
|
|
=cut |
193 |
|
|
|
194 |
|
|
1; # End of WebPAC::Validate |