7 |
|
|
8 |
use base 'WebPAC::Common'; |
use base 'WebPAC::Common'; |
9 |
use File::Slurp; |
use File::Slurp; |
10 |
|
use List::Util qw/first/; |
11 |
use Data::Dumper; |
use Data::Dumper; |
12 |
|
use WebPAC::Normalize qw/_pack_subfields_hash/; |
13 |
|
use Storable qw/dclone/; |
14 |
|
|
15 |
=head1 NAME |
=head1 NAME |
16 |
|
|
18 |
|
|
19 |
=head1 VERSION |
=head1 VERSION |
20 |
|
|
21 |
Version 0.01 |
Version 0.03 |
22 |
|
|
23 |
=cut |
=cut |
24 |
|
|
25 |
our $VERSION = '0.01'; |
our $VERSION = '0.03'; |
26 |
|
|
27 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
28 |
|
|
34 |
# same with 101 |
# same with 101 |
35 |
101 |
101 |
36 |
# field 200 have valid subfields a-g |
# field 200 have valid subfields a-g |
37 |
200 a b c d e f g |
# and field e is repeatable |
38 |
|
200 a b c d e* f g |
39 |
# field 205 can have only subfield a |
# field 205 can have only subfield a |
40 |
205 a |
# and must exists |
41 |
|
205! a |
42 |
# while 210 can have a c or d |
# while 210 can have a c or d |
43 |
210 a c d |
210 a c d |
44 |
|
|
49 |
Create new validation object |
Create new validation object |
50 |
|
|
51 |
my $validate = new WebPAC::Validate( |
my $validate = new WebPAC::Validate( |
52 |
path => '/path/to/input/validate_file', |
path => 'conf/validate/file', |
53 |
); |
); |
54 |
|
|
55 |
=cut |
=cut |
73 |
|
|
74 |
foreach my $l (split(/[\n\r]+/, $v_file)) { |
foreach my $l (split(/[\n\r]+/, $v_file)) { |
75 |
$curr_line++; |
$curr_line++; |
76 |
# skip comments |
|
77 |
next if ($l =~ m/^#/); |
# skip comments and whitespaces |
78 |
|
next if ($l =~ /^#/ || $l =~ /^\s*$/); |
79 |
|
|
80 |
$l =~ s/^\s+//; |
$l =~ s/^\s+//; |
81 |
$l =~ s/\s+$//; |
$l =~ s/\s+$//; |
82 |
|
|
83 |
my @d = split(/\s+/, $l); |
my @d = split(/\s+/, $l); |
84 |
|
|
85 |
my $fld = shift @d || $log->logdie("need field name in line $curr_line: $l"); |
my $fld = shift @d; |
86 |
|
|
87 |
|
if ($fld =~ s/!$//) { |
88 |
|
$self->{must_exist}->{$fld}++; |
89 |
|
} |
90 |
|
|
91 |
|
$log->logdie("need field name in line $curr_line: $l") unless (defined($fld)); |
92 |
|
|
93 |
if (@d) { |
if (@d) { |
94 |
$v->{$fld}->{ref} = 'ARRAY'; |
$v->{$fld} = \@d; |
|
$v->{$fld}->{sf} = \@d; |
|
95 |
} else { |
} else { |
96 |
$v->{$fld}->{ref} = ''; |
$v->{$fld} = 1; |
97 |
} |
} |
98 |
|
|
99 |
} |
} |
102 |
|
|
103 |
$self->{rules} = $v; |
$self->{rules} = $v; |
104 |
|
|
105 |
|
$log->info("validation uses rules from $self->{path}"); |
106 |
|
|
107 |
$self ? return $self : return undef; |
$self ? return $self : return undef; |
108 |
} |
} |
109 |
|
|
110 |
|
=head2 validate_errors |
111 |
|
|
112 |
|
Validate record and return errors |
113 |
|
|
114 |
|
my @errors = $validate->validate_errors( $rec ); |
115 |
|
|
116 |
|
=cut |
117 |
|
|
118 |
|
sub validate_errors { |
119 |
|
my $self = shift; |
120 |
|
|
121 |
|
my $log = $self->_get_logger(); |
122 |
|
|
123 |
|
my $rec = shift || $log->logdie("validate_errors need record"); |
124 |
|
|
125 |
|
$log->logdie("rec isn't HASH") unless (ref($rec) eq 'HASH'); |
126 |
|
$log->logdie("can't find validation rules") unless (my $r = $self->{rules}); |
127 |
|
|
128 |
|
my @errors; |
129 |
|
|
130 |
|
$log->debug("rec = ", sub { Dumper($rec) }, "keys = ", keys %{ $rec }); |
131 |
|
|
132 |
|
my $fields; |
133 |
|
|
134 |
|
foreach my $f (keys %{ $rec }) { |
135 |
|
|
136 |
|
next if (!defined($f) || $f eq '' || $f eq '000'); |
137 |
|
|
138 |
|
$fields->{$f}++; |
139 |
|
|
140 |
|
if ( ! defined($r->{$f}) ) { |
141 |
|
push @errors, "field '$f' shouldn't exists"; |
142 |
|
next; |
143 |
|
} |
144 |
|
|
145 |
|
|
146 |
|
if (ref($rec->{$f}) ne 'ARRAY') { |
147 |
|
push @errors, "field '$f' isn't repetable, probably bug in parsing input data"; |
148 |
|
next; |
149 |
|
} |
150 |
|
|
151 |
|
foreach my $v (@{ $rec->{$f} }) { |
152 |
|
# can we have subfields? |
153 |
|
if (ref($r->{$f}) eq 'ARRAY') { |
154 |
|
# are values hashes? (has subfields) |
155 |
|
if (ref($v) ne 'HASH') { |
156 |
|
push @errors, "$f has value without subfields: $v"; |
157 |
|
next; |
158 |
|
} else { |
159 |
|
|
160 |
|
my $h = dclone( $v ); |
161 |
|
|
162 |
|
my $sf_repeatable; |
163 |
|
|
164 |
|
delete($v->{subfields}) if (defined($v->{subfields})); |
165 |
|
|
166 |
|
foreach my $sf (keys %{ $v }) { |
167 |
|
|
168 |
|
# is non-repeatable but with multiple values? |
169 |
|
if ( ! first { $_ eq $sf.'*' } @{$r->{$f}} ) { |
170 |
|
if ( ref($v->{$sf}) eq 'ARRAY' ) { |
171 |
|
$sf_repeatable->{$sf}++; |
172 |
|
}; |
173 |
|
if (! first { $_ eq $sf } @{ $r->{$f} }) { |
174 |
|
push @errors, "$f has unknown subfield: $sf"; |
175 |
|
} |
176 |
|
} |
177 |
|
|
178 |
|
} |
179 |
|
if (my @r_sf = sort keys( %$sf_repeatable )) { |
180 |
|
my $plural = $#r_sf > 0 ? 1 : 0; |
181 |
|
|
182 |
|
push @errors, "$f subfield" . |
183 |
|
( $plural ? 's ' : ' ' ) . |
184 |
|
join(', ', @r_sf) . |
185 |
|
( $plural ? ' are ' : ' is ' ) . |
186 |
|
'repeatable in: ' . |
187 |
|
join('', _pack_subfields_hash( $h, 1) ); |
188 |
|
} |
189 |
|
} |
190 |
|
} elsif (ref($v) eq 'HASH') { |
191 |
|
push @errors, "$f has subfields which is not valid"; |
192 |
|
} |
193 |
|
} |
194 |
|
} |
195 |
|
|
196 |
|
foreach my $must (sort keys %{ $self->{must_exist} }) { |
197 |
|
next if ($fields->{$must}); |
198 |
|
push @errors, |
199 |
|
"field $must should exist, but it doesn't"; |
200 |
|
} |
201 |
|
|
202 |
|
#$log->logcluck("return from this function is ARRAY") unless wantarray; |
203 |
|
|
204 |
|
$log->debug("errors: ", join(", ", @errors)) if (@errors); |
205 |
|
|
206 |
|
return @errors; |
207 |
|
} |
208 |
|
|
209 |
=head1 AUTHOR |
=head1 AUTHOR |
210 |
|
|
211 |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |