NAME

Catmandu::Validator - Namespace for packages that can validate items in Catmandu

SYNOPSIS

use Catmandu::Validator::Simple;

my $validator = Catmandu::Validator::Simple->new(
    handler => sub {
        $data = shift;
        return "error" unless $data->{title} =~ m/good title/;
        return;
    }
);

if ( $validator->is_valid($hashref) ) {
    save_record_in_database($hashref);
} else {
    reject_form($validator->last_errors);
}

my $validator = Catmandu::Validator::Simple->new(
    handler => sub { ...},
    error_callback => sub {
        my ($data, $errors) = @_;
        print "Validation errors for record $data->{_id}:\n";
        print "$_\n" for @{$errors};
    }
};

my $validated_arrayref = $validator->validate($arrayref);

$validator->validate($iterator, {
    after_callback => sub {
        my ($record, $errors) = @_;
        if ($errors) {
            add_to_failure_report($rec, $errors);
            #omit the invalid record from the result
            return undef;
        }
        return $rec;
    }
})->each( sub {
    my $record = shift;
    publish_record($record);
});

See Catmandu::Fix::validate and Catmandu::Fix::Condition::valid to use validators in fixes (Catmandu::Fix).

DESCRIPTION

A Catmandu::Validator is a base class for Perl packages that can validate data.

METHODS

new()

Create a new Catmandu::Validator.

new( after_callback => \&callback )

The after_callback is called after each record has been validated. The callback function should take $hashref to each data record, and $arrayref to list of validation errors for the record as arguments.

new( error_field => $field_name )

If the error_field parameter is set, then during validation each record that fails validation will get an extra field added containing an arrayref to the validation errors. The name of the key will be the value passed or '_validation_errors' if 1 is passed. By default it is not set.

is_valid( \%hash )

Validates a single record. Returns 1 success and 0 on failure. Information about the validation errors can be retrieved with the "last_errors()" method.

validate( \%hash )

validate( $iterator )

validate( \@array )

Validates a single record or multiple records in an iterator or an array. Returns validated records in the same type of container for multiple records or the record itself for a single record. The default behaviour is to return the records that passed validation unchanged and omit the invalid records. This behaviour can be changed by setting the after_callback or the error_field in the constructor. Returns undef on validation failure for single records.

last_errors()

Returns arrayref of errors from the record that was last validated if that record failed validation or undef if there were no errors.

valid_count()

Returns the number of valid records from last validate operation.

invalid_count()

Returns the number of invalid records from the last validate operation.

SEE ALSO

Catmandu::Validator::Env and Catmandu::Validator::Simple.

Catmandu::Iterable