NAME

MooseX::Role::Validatable - Role to add validation to a class

SYNOPSIS

package MyClass;

use Moose;
with 'MooseX::Role::Validatable';

has 'attr1' => (is => 'ro', lazy_build => 1);

sub _build_attr1 {
    my $self = shift;

    # Note initialization errors
    $self->add_errors( {
        message => 'Error: blabla',
        message_to_client => 'Something is wrong!'
    } ) if 'blabla';
}

sub _validate_some_other_errors { # _validate_*
    my $self = shift;

    my @errors;
    push @errors, {
        message => '...',
        message_to_client => '...',
    };

    return @errors;
}

## use
my $ex = MyClass->new();

if (not $ex->initialized_correctly) {
    my @errors = $ex->all_init_errors();
    ...;    # We didn't even start with good data.
}

if (not $ex->confirm_validity) { # does not pass those _validate_*
    my @errors = $ex->all_errors();
    ...;
}

DESCRIPTION

MooseX::Role::Validatable is a Moo/Moose role which provides a standard way to add validation to a class.

METHODS

initialized_correctly

no error when init the object (no add_errors is called)

add_errors

$self->add_errors(...)

add errors on those lazy attributes or sub BUILD

confirm_validity

run all those _validate_* messages and returns true if no error found.

all_errors

An array of the errors currently noted. combined with all_init_errors and all_validation_errors

all errors including below methods are instance of error_class, default to MooseX::Role::Validatable::Error

all_init_errors

all errors on init

all_validation_errors

all errors on validation

all_errors_by_severity

order by severity

primary_validation_error

the first error of all_errors_by_severity

validation_methods

A list of all validation methods available on this object. This can be auto-generated from all methods which begin with "_validate_" which is especially helpful in devleoping new validations.

You may wish to set this list directly on the object, if you create and validate a lot of static objects.

error_class

default to MooseX::Role::Validatable::Error, override by

has '+error_class' => (is => 'ro', default => sub { 'My::Validatable::Error' });

# or
->new(error_class => 'My::Validatable::Error');

passes_validation

should_alert

AUTHOR

Binary.com <fayland@binary.com>

COPYRIGHT

Copyright 2014- Binary.com

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO