ETLp
view release on metacpan or search on metacpan
lib/ETLp/File/Validate.pm view on Meta::CPAN
=head1 SYNOPSIS
use ETLp::File::Validate;
my $val = ETLp::File::Validate->new(
data_directory => '/data/incoming',
file_config_directory => "$Bin/../conf",
file_definition => 'file_def.cfg',
localize => 1,
type => 'csv'
);
my $ret = $val->validate('data.txt');
unless($ret) {
my @errors = @{$val->get_errors};
foreach my $error (@errors) {
print $error->{line_number} .": " . $error->{message}, "\n";
}
}
...
=head1 METHODS
=head2 new
Create a validation object.
Parameters
Hash or hashref consisting of
* type: Required. The type of file (csv), fixed width
* data_directory: Optional. The directory where the files to be
loaded are located.
* file_config_directory: Optional. The directory where the
file_defintion file can be found.
* file_definition: Required. A file that contains a defintion
of the validation rules. If the file_config_directory parameter
is not set, then this must be the full path to the defintion file
Returns
* a ETLp::File::Validate object
=cut
class ETLp::File::Validate {
use Time::Piece;
use ETLp::Exception;
use ETLp::File::Config;
has 'data_directory' => (is => 'ro', isa => 'Str');
has 'file_config_directory' => (is => 'ro', isa => 'Str');
has 'file_definition' => => (is => 'ro', isa => 'Str', required => 1);
has 'type' => (is => 'ro', isa => 'Str', required => 1);
has 'localize' => (is => 'ro', isa => 'Bool', default => 0);
has 'skip' => (is => 'ro', isa => 'Int', default => 0);
has 'csv_options' => (is => 'ro', isa => 'HashRef', required => 0, default => sub{{}});
our @errors;
=head2 validate
Validate the file
Parameters
* The name of the file being validated,
Returns
* Success flag (0 = fail, success = 1)
=cut
method validate(Str $filename) {
@errors = ();
if ($self->data_directory) {
$filename = $self->data_directory . "/$filename";
}
if ($self->type eq 'csv') {
return $self->_validate_csv($filename);
} else {
ETLpException->throw(error => "Unknown file type: " . $self->type);
}
}
# Reads the supplied CSV file and validates each line against the
# definition
method _validate_csv(Str $filename) {
require ETLp::File::Read::CSV;
my $csv = ETLp::File::Read::CSV->new(
filename => $filename,
fields => $self->config->fields,
csv_options => $self->csv_options,#{allow_whitespace => 1},
localize => $self->localize,
skip => $self->skip
);
# Loop through each record in the file, and validate it
# against the rules
my $line_counter = 1;
while (my $fields = $csv->get_fields) {
$self->_validate_record($line_counter++, $fields);
}
my $ret_status = (@errors > 0) ? 0 : 1;
return $ret_status;
}
# Validate a record's values against its definitions
method _validate_record(Int $line_counter, HashRef $fields) {
( run in 0.854 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )