Catman...lidator(3pm)

πŸ“¦
version 1.2025 Β· 4 versions
, in ubuntu resolute, ubuntu stonking

✨ NAME ✨

Catmandu::Validator β€” it's a namespace for packages that can validate items in Catmandu. Bestie, that's the whole vibe: a whole lil family of validator packages living under one roof πŸ’–

Oh and hey, this is the Catman...lidator(3pm) page from the User Contributed Perl Documentation, if you were wondering where you wandered into. Welcome, you're safe here 🫢


🌸 SYNOPSIS 🌸

Okay so here's the whole thing in action, no cap:

    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).

Iconic, right? And if you wanna drop validators straight into your fixes, go peek at Catmandu::Fix::validate and Catmandu::Fix::Condition::valid πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

A Catmandu::Validator is a base class for Perl packages that can validate data. That's it, that's the job β€” it's the mother of all validator packages, giving the little ones somewhere to belong 🌈


πŸ‘‘ METHODS πŸ‘‘

πŸŽ€ new()

Create a new Catmandu::Validator. Fresh start, main character energy ✨

πŸŽ€ new( after_callback => \&callback )

So the after_callback gets called after each record has been validated. Your callback function should take $hashref to each data record, and $arrayref to the list of validation errors for that record, as its arguments. It's giving "tell me everything that went wrong" 🫢

πŸŽ€ new( error_field => $field_name )

If you set the error_field parameter, then during validation every record that fails validation gets an extra field added, holding an arrayref to the validation errors. The name of the key will be whatever value you passed β€” or '_validation_errors' if you passed 1. By default, it is not set. Ngl that default field name is kinda cute 🌸

πŸŽ€ is_valid( \%hash )

Validates a single record. Returns 1 on success and 0 on failure. Wanna know why it failed? Grab the tea with the "last_errors()" method 😭

πŸŽ€ validate( \%hash ), validate( $iterator ), validate( \@array )

Validates a single record, or multiple records inside 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. You can change that behaviour by setting the after_callback or the error_field in the constructor. And for single records, it returns undef on validation failure πŸ’€

πŸŽ€ last_errors()

Returns an arrayref of errors from the record that was last validated β€” but only if that record failed validation. Otherwise it's undef, because no errors, no drama, we love a clean slate ✨

πŸŽ€ valid_count()

Returns the number of valid records from the last validate operation. Counting the wins, periodt πŸ’–

πŸŽ€ invalid_count()

Returns the number of invalid records from the last validate operation. We still love them, they just didn't pass 🫢


🌈 SEE ALSO 🌈

  • Catmandu::Validator::Env and Catmandu::Validator::Simple.
  • Catmandu::Iterable

And that's the whole page, bestie β€” written by the lovely folks behind User Contributed Perl Documentation, running on perl v5.40.1, dated 2026-01-24. Go forth and validate, you absolute icon πŸ’…πŸ‘‘πŸ’œ

πŸ“¦ where this page comes from