MooseX::Role::Validatable - Role to add validation to a class
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(); ...; }
MooseX::Role::Validatable is a Moo/Moose role which provides a standard way to add validation to a class.
no error when init the object (no add_errors is called)
$self->add_errors(...)
add errors on those lazy attributes or sub BUILD
run all those _validate_* messages and returns true if no error found.
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 errors on init
all errors on validation
order by severity
the first error of all_errors_by_severity
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.
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');
Binary.com <fayland@binary.com>
Copyright 2014- Binary.com
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install MooseX::Role::Validatable, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MooseX::Role::Validatable
CPAN shell
perl -MCPAN -e shell install MooseX::Role::Validatable
For more information on module installation, please visit the detailed CPAN module installation guide.