Loading…
Back To Schedule
Thursday, June 10 • 15:00 - 15:50
Valiant - Heroic validations for Moo and DBIC classes

Sign up or log in to save this to your schedule, view media, leave feedback and see who's attending!

Feedback form is now closed.
Valiant is a CPAN distribution that provides validations for your Moo and DBIx::Class result classes that is based on the popular and successful system integrated with Ruby on Rails.

# Example

package Local::Person;

use Moo;
use Valiant::Validations;
use Valiant::Filters;

has name => (is=>'ro');
has age => (is=>'ro');

filters_with => 'Trim';

validates name => (
length => {
maximum => 10,
minimum => 3,
}
);

validates age => (
numericality => {
is_integer => 1,
less_than => 200,
},
);

validates_with 'MySpecialValidator' => (arg1=>'foo', arg2=>'bar');

my $person = Local::Person->new(
name => 'Ja',
age => 300,
);

$person->validate;
$person->valid; # FALSE
$person->invalid; # TRUE

my %errors = $person->errors->to_hash(full_messages=>1);

# \%errors = +{
# age => [
# "Age must be less than 200",
# ],
# name => [
# "Name is too short (minimum is 3 characters)',
# ],
# };


# Description

Valiant (https://metacpan.org/pod/Valiant) is a set of Moo roles and DBIC components that integrate validations directly into your Moo classes or DBIx::Class result classes. Provides a domain specific language which allows you to defined for a given class what a valid state for an instance of that class would be and to gather reportable error messages. Used to defined constraints related to business logic or for validating user input (for example via CGI forms).

When we say domain level or business logic validation, what we mean is that invalid data is a possible and expected state that needs to be evaluated and reported to the end user for correction. For example when writing a web application you might have a form that requests user profile information (such as name, DOB, address, etc). It's an expected condition that the user might submit form data that is invalid in some way (such as a date of birth that is in the future) but is still 'well formed' and is able to be processed. In these cases your business logic would be to inform the user of the incorrect data and request fixes (rather than simply throw a 500 server error and giving up).

This differs from type constraints (such as Type::Tiny) that you might put on your Moo attributes which are used to express when attributes have values that are so unacceptable that no further work can be done and an exception must be thrown.

Valiant fits into a similar category as HTML::Formhander and FormFu although its not HTML form specific. Prior art for this would be the validations system for ActiveRecords in Ruby on Rails and the Javascript library class-validator.js, both of which the author reviewed extensively when writing this code.

# Outline of proposed talk

1) Quick overview of validation distributions on CPAN and why I chose to write yet another one
2) Basic Moo example
3) Discussion of built in validators
4) Creating a custom validator
5) Using roles and inheritance
6) Basic DBIx-Class integration
7) DBIC integration with nested related results
8) Example web application (written in Catalyst)


Thursday June 10, 2021 15:00 - 15:50 EDT
Zoom Room 2
  Track 2