| 1 | package ETLp::File::Validate; | ||
| 2 | |||
| 3 | use MooseX::Declare; | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | ETLp::File::Validate - Validate file content | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | |||
| 12 | use ETLp::File::Validate; | ||
| 13 | |||
| 14 | my $val = ETLp::File::Validate->new( | ||
| 15 | data_directory => '/data/incoming', | ||
| 16 | file_config_directory => "$Bin/../conf", | ||
| 17 | file_definition => 'file_def.cfg', | ||
| 18 | localize => 1, | ||
| 19 | type => 'csv' | ||
| 20 | ); | ||
| 21 | |||
| 22 | my $ret = $val->validate('data.txt'); | ||
| 23 | |||
| 24 | unless($ret) { | ||
| 25 | my @errors = @{$val->get_errors}; | ||
| 26 | |||
| 27 | foreach my $error (@errors) { | ||
| 28 | print $error->{line_number} .": " . $error->{message}, "\n"; | ||
| 29 | } | ||
| 30 | } | ||
| 31 | ... | ||
| 32 | |||
| 33 | =head1 METHODS | ||
| 34 | |||
| 35 | =head2 new | ||
| 36 | |||
| 37 | Create a validation object. | ||
| 38 | |||
| 39 | Parameters | ||
| 40 | |||
| 41 | Hash or hashref consisting of | ||
| 42 | |||
| 43 | * type: Required. The type of file (csv), fixed width | ||
| 44 | * data_directory: Optional. The directory where the files to be | ||
| 45 | loaded are located. | ||
| 46 | * file_config_directory: Optional. The directory where the | ||
| 47 | file_defintion file can be found. | ||
| 48 | * file_definition: Required. A file that contains a defintion | ||
| 49 | of the validation rules. If the file_config_directory parameter | ||
| 50 | is not set, then this must be the full path to the defintion file | ||
| 51 | |||
| 52 | Returns | ||
| 53 | |||
| 54 | * a ETLp::File::Validate object | ||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | class ETLp::File::Validate { | ||
| 59 | use Time::Piece; | ||
| 60 | use ETLp::Exception; | ||
| 61 | use ETLp::File::Config; | ||
| 62 | |||
| 63 | has 'data_directory' => (is => 'ro', isa => 'Str'); | ||
| 64 | has 'file_config_directory' => (is => 'ro', isa => 'Str'); | ||
| 65 | has 'file_definition' => => (is => 'ro', isa => 'Str', required => 1); | ||
| 66 | has 'type' => (is => 'ro', isa => 'Str', required => 1); | ||
| 67 | has 'localize' => (is => 'ro', isa => 'Bool', default => 0); | ||
| 68 | has 'skip' => (is => 'ro', isa => 'Int', default => 0); | ||
| 69 | has 'csv_options' => (is => 'ro', isa => 'HashRef', required => 0, default => sub{{}}); | ||
| 70 | |||
| 71 | our @errors; | ||
| 72 | |||
| 73 | =head2 validate | ||
| 74 | |||
| 75 | Validate the file | ||
| 76 | |||
| 77 | Parameters | ||
| 78 | |||
| 79 | * The name of the file being validated, | ||
| 80 | |||
| 81 | Returns | ||
| 82 | |||
| 83 | * Success flag (0 = fail, success = 1) | ||
| 84 | |||
| 85 | =cut | ||
| 86 | |||
| 87 | method validate(Str $filename) { | ||
| 88 | |||
| 89 | @errors = (); | ||
| 90 | |||
| 91 | if ($self->data_directory) { | ||
| 92 | $filename = $self->data_directory . "/$filename"; | ||
| 93 | } | ||
| 94 | |||
| 95 | if ($self->type eq 'csv') { | ||
| 96 | return $self->_validate_csv($filename); | ||
| 97 | } else { | ||
| 98 | ETLpException->throw(error => "Unknown file type: " . $self->type); | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | # Reads the supplied CSV file and validates each line against the | ||
| 103 | # definition | ||
| 104 | method _validate_csv(Str $filename) { | ||
| 105 | |||
| 106 | require ETLp::File::Read::CSV; | ||
| 107 | |||
| 108 | my $csv = ETLp::File::Read::CSV->new( | ||
| 109 | filename => $filename, | ||
| 110 | fields => $self->config->fields, | ||
| 111 | csv_options => $self->csv_options,#{allow_whitespace => 1}, | ||
| 112 | localize => $self->localize, | ||
| 113 | skip => $self->skip | ||
| 114 | ); | ||
| 115 | |||
| 116 | # Loop through each record in the file, and validate it | ||
| 117 | # against the rules | ||
| 118 | my $line_counter = 1; | ||
| 119 | while (my $fields = $csv->get_fields) { | ||
| 120 | $self->_validate_record($line_counter++, $fields); | ||
| 121 | } | ||
| 122 | |||
| 123 | my $ret_status = (@errors > 0) ? 0 : 1; | ||
| 124 | return $ret_status; | ||
| 125 | } | ||
| 126 | |||
| 127 | # Validate a record's values against its definitions | ||
| 128 | |||
| 129 | method _validate_record(Int $line_counter, HashRef $fields) { | ||
| 130 | my $rules = $self->config->rules; | ||
| 131 | |||
| 132 | LOOP: | ||
| 133 | foreach my $field_name (@{$self->config->fields}) { | ||
| 134 | my $value = $fields->{$field_name}; | ||
| 135 | my $nullable = $rules->{$field_name}->{nullable}; | ||
| 136 | my @rules = | ||
| 137 | (ref $rules->{$field_name}->{rule} eq 'ARRAY') | ||
| 138 | ? @{$rules->{$field_name}->{rule}} | ||
| 139 | : ($rules->{$field_name}->{rule}); | ||
| 140 | |||
| 141 | # Make sure that a mandatory field has a value; | ||
| 142 | if ( (uc $rules->{$field_name}->{nullable} eq 'N') | ||
| 143 | && ($value =~ /^$/)) | ||
| 144 | { | ||
| 145 | _add_error( | ||
| 146 | line_number => $line_counter, | ||
| 147 | field_name => $field_name, | ||
| 148 | field_value => $value, | ||
| 149 | message => 'Mandatory field missing value', | ||
| 150 | ); | ||
| 151 | |||
| 152 | next LOOP; | ||
| 153 | } | ||
| 154 | |||
| 155 | # No other checks required unless we have a value | ||
| 156 | next LOOP unless $value; | ||
| 157 | |||
| 158 | for (my $j = 0 ; $j < @rules ; $j++) { | ||
| 159 | # Range check means that the value is a number | ||
| 160 | if ($rules[$j] =~ /^range\(.*\)$/i) { | ||
| 161 | |||
| 162 | unless (_is_number($value)) { | ||
| 163 | _add_error( | ||
| 164 | line_number => $line_counter, | ||
| 165 | field_name => $field_name, | ||
| 166 | field_value => $value, | ||
| 167 | message => "Value must be an a number", | ||
| 168 | ); | ||
| 169 | |||
| 170 | next LOOP; | ||
| 171 | } | ||
| 172 | |||
| 173 | unless ($value =~ /^[-+]?\d*\.?\d*$/) { | ||
| 174 | _add_error( | ||
| 175 | line_number => $line_counter, | ||
| 176 | field_name => $field_name, | ||
| 177 | field_value => $value, | ||
| 178 | message => 'Must be a number', | ||
| 179 | ); | ||
| 180 | |||
| 181 | next LOOP; | ||
| 182 | } | ||
| 183 | } | ||
| 184 | # Check minimum and maximum range values | ||
| 185 | if ($rules[$j] =~ /^range\(\s*([-+]?\d+),\s*([-+]?\d+)\s*\)$/i) | ||
| 186 | { | ||
| 187 | my $lower = $1; | ||
| 188 | my $upper = $2; | ||
| 189 | |||
| 190 | unless (_is_number($value)) { | ||
| 191 | _add_error( | ||
| 192 | line_number => $line_counter, | ||
| 193 | field_name => $field_name, | ||
| 194 | field_value => $value, | ||
| 195 | message => "Value must be an a number", | ||
| 196 | ); | ||
| 197 | |||
| 198 | next LOOP; | ||
| 199 | } | ||
| 200 | |||
| 201 | unless (($value >= $lower) && ($value <= $upper)) { | ||
| 202 | _add_error( | ||
| 203 | line_number => $line_counter, | ||
| 204 | field_name => $field_name, | ||
| 205 | field_value => $value, | ||
| 206 | message => 'Value outside of range: ' . $rules[$j], | ||
| 207 | ); | ||
| 208 | |||
| 209 | next LOOP; | ||
| 210 | } | ||
| 211 | } | ||
| 212 | # Upper range check | ||
| 213 | elsif ($rules[$j] =~ /^range\(\s*,\s*([-+]?\d+)\s*\)$/i) { | ||
| 214 | my $upper = $1; | ||
| 215 | |||
| 216 | unless (_is_number($value)) { | ||
| 217 | _add_error( | ||
| 218 | line_number => $line_counter, | ||
| 219 | field_name => $field_name, | ||
| 220 | field_value => $value, | ||
| 221 | message => "Value must be an a number", | ||
| 222 | ); | ||
| 223 | |||
| 224 | next LOOP; | ||
| 225 | } | ||
| 226 | |||
| 227 | unless ($value <= $upper) { | ||
| 228 | _add_error( | ||
| 229 | line_number => $line_counter, | ||
| 230 | field_name => $field_name, | ||
| 231 | field_value => $value, | ||
| 232 | message => "Value must be <= $upper", | ||
| 233 | ); | ||
| 234 | } | ||
| 235 | } | ||
| 236 | # Lower range check | ||
| 237 | elsif ($rules[$j] =~ /^range\(\s*([-+]?\d+)\s*,\s*\)$/i) { | ||
| 238 | my $lower = $1; | ||
| 239 | |||
| 240 | unless (_is_number($value)) { | ||
| 241 | _add_error( | ||
| 242 | line_number => $line_counter, | ||
| 243 | field_name => $field_name, | ||
| 244 | field_value => $value, | ||
| 245 | message => "Value must be an a number", | ||
| 246 | ); | ||
| 247 | |||
| 248 | next LOOP; | ||
| 249 | } | ||
| 250 | |||
| 251 | unless ($value >= $lower) { | ||
| 252 | _add_error( | ||
| 253 | line_number => $line_counter, | ||
| 254 | field_name => $field_name, | ||
| 255 | field_value => $value, | ||
| 256 | message => "Value must_be >= $lower", | ||
| 257 | ); | ||
| 258 | } | ||
| 259 | } | ||
| 260 | # varchar check | ||
| 261 | elsif ($rules[$j] =~ /^varchar\((\d+)\)$/i) { | ||
| 262 | my $max_length = $1; | ||
| 263 | my $value_length = length($value); | ||
| 264 | |||
| 265 | unless ($value_length <= $max_length) { | ||
| 266 | _add_error( | ||
| 267 | line_number => $line_counter, | ||
| 268 | field_name => $field_name, | ||
| 269 | field_value => $value, | ||
| 270 | message => "Length must be less than or equal to " | ||
| 271 | . $max_length | ||
| 272 | . " characters", | ||
| 273 | ); | ||
| 274 | } | ||
| 275 | } | ||
| 276 | # Integer check | ||
| 277 | elsif (uc $rules[$j] eq 'INTEGER') { | ||
| 278 | unless ($value =~ /^[-+]?\d+$/) { | ||
| 279 | _add_error( | ||
| 280 | line_number => $line_counter, | ||
| 281 | field_name => $field_name, | ||
| 282 | field_value => $value, | ||
| 283 | message => "Value must be an integer", | ||
| 284 | ); | ||
| 285 | } | ||
| 286 | } | ||
| 287 | # Float check | ||
| 288 | elsif (uc $rules[$j] eq 'FLOAT') { | ||
| 289 | unless (_is_number($value)) { | ||
| 290 | _add_error( | ||
| 291 | line_number => $line_counter, | ||
| 292 | field_name => $field_name, | ||
| 293 | field_value => $value, | ||
| 294 | message => "Value must be a floating number", | ||
| 295 | ); | ||
| 296 | } | ||
| 297 | } | ||
| 298 | # Date check | ||
| 299 | elsif ($rules[$j] =~ /^date\('?(.*)'?\)$/i) { | ||
| 300 | my $pattern = $1; | ||
| 301 | |||
| 302 | eval { Time::Piece->strptime($value, $pattern); }; | ||
| 303 | |||
| 304 | if ($@) { | ||
| 305 | _add_error( | ||
| 306 | line_number => $line_counter, | ||
| 307 | field_name => $field_name, | ||
| 308 | field_value => $value, | ||
| 309 | message => "Invalid date for pattern: $pattern", | ||
| 310 | ); | ||
| 311 | } | ||
| 312 | } | ||
| 313 | # Regex check | ||
| 314 | elsif ($rules[$j] =~ /^qr\/.*\//i) { | ||
| 315 | my $regex = eval($rules[$j]); | ||
| 316 | if ($value !~ $regex) { | ||
| 317 | _add_error( | ||
| 318 | line_number => $line_counter, | ||
| 319 | field_name => $field_name, | ||
| 320 | field_value => $value, | ||
| 321 | message => "Value does not match pattern " | ||
| 322 | . $rules[$j], | ||
| 323 | ); | ||
| 324 | } | ||
| 325 | } else { | ||
| 326 | _add_error( | ||
| 327 | line_number => $line_counter, | ||
| 328 | field_name => $field_name, | ||
| 329 | field_value => $fields->{field_name}, | ||
| 330 | message => "Unknown pattern " . $rules[$j], | ||
| 331 | ); | ||
| 332 | } | ||
| 333 | } | ||
| 334 | } | ||
| 335 | }; | ||
| 336 | |||
| 337 | # Add an error to the stack | ||
| 338 | sub _add_error { | ||
| 339 | my %args = @_; | ||
| 340 | push @errors, \%args; | ||
| 341 | } | ||
| 342 | |||
| 343 | # Is the value a number? Used for range checks | ||
| 344 | sub _is_number { | ||
| 345 | my $value = shift; | ||
| 346 | return $value =~ /^[-+]?\d*\.?\d*$/; | ||
| 347 | } | ||
| 348 | |||
| 349 | =head2 get_errors | ||
| 350 | |||
| 351 | Returns a list of error messages | ||
| 352 | |||
| 353 | Parameters | ||
| 354 | |||
| 355 | * None | ||
| 356 | |||
| 357 | Returns | ||
| 358 | |||
| 359 | * An arrayref of hashrefs. Each array elemnet is an error. Each | ||
| 360 | hashref contains the error details | ||
| 361 | |||
| 362 | o line_number: The line number where the error occurred | ||
| 363 | o field_name: The name of the field that failed validated | ||
| 364 | o field_value: The value in the field | ||
| 365 | o message: The error message | ||
| 366 | |||
| 367 | =cut | ||
| 368 | |||
| 369 | sub get_errors { | ||
| 370 | return \@errors; | ||
| 371 | } | ||
| 372 | |||
| 373 | =head2 config | ||
| 374 | |||
| 375 | Returns the parsed configuration used by the validation | ||
| 376 | |||
| 377 | Parameters | ||
| 378 | |||
| 379 | * None | ||
| 380 | |||
| 381 | Returns | ||
| 382 | |||
| 383 | * a ETLp::File::Config object | ||
| 384 | |||
| 385 | =cut | ||
| 386 | |||
| 387 | method config { | ||
| 388 | return $self->{_config}; | ||
| 389 | } | ||
| 390 | |||
| 391 | method BUILD { | ||
| 392 | $self->{_config} = ETLp::File::Config->new( | ||
| 393 | directory => $self->file_config_directory, | ||
| 394 | definition => $self->file_definition, | ||
| 395 | ); | ||
| 396 | } | ||
| 397 | } | ||
| 398 | |||
| 399 | =head1 LICENSE AND COPYRIGHT | ||
| 400 | |||
| 401 | Copyright 2010 Redbone Systems Ltd | ||
| 402 | |||
| 403 | This program is free software; you can redistribute it and/or modify it | ||
| 404 | under the terms of either: the GNU General Public License as published | ||
| 405 | by the Free Software Foundation; or the Artistic License. | ||
| 406 | |||
| 407 | The terms are in the LICENSE file that accompanies this application | ||
| 408 | |||
| 409 | =cut |