Candy-Coated Data Validation; Codename: ShuhgaBear

2-CENTS

This week, in the intertubes, I came across a Node.js validation library which seemed to be popular amongst "those people". At-a-glance, as far as validation libraries go, I didn't think it was that well done, but I must admit to noticing a certain appeal in the expressiveness of the design which I suppose is due largely to that fact that its a JavaScript library.

Anyway, I decided that it would be cool to try and mimic the expressiveness while using Validation::Class as the foundation. It started as a feature-branch on Github but I was amazed at how quickly the concept came to fruition. The results is a module called Validation::Class::Simple::Streamer which has been added to the master-branch on Github as an optional experimental addition to the distribution.

Why? Validation::Class is design for a codebase that desires to be optimized and take advantage of compartmentalization and reusability, etc., though it requires some forethought and configuration. Validation::Class::Simple was added to provide a "container-class" to allow developers to use Validation::Class they way they would a traditional data validation library, in an ad-hoc fashion not interested in the advanced setup, etc. Validation::Class::Simple::Streamer was created because its a simple, maybe even fun, way of validating data and offers a low barrier-to-entry for people that don't care to configure the full framework.

SYNOPSIS

use Validation::Class::Simple::Streamer;

my $params = Validation::Class::Simple::Streamer->new($parameters);

unless ($params->check('email_address')->length(3)->email) {
    # validated login, password and email_address
}

unless ($params->check('home_phone')->telephone) {
    # validated login, password, email_address and home_phone
}

$params->check('password');

# be as expressive as you like
ok() if
    # validates login, password, email_address and home_phone
    $params->max_length(15) &&
    $params->min_symbols(1) &&
    $params->matches('password2')
;

# using your own workflow
$params->check($_)->filters('trim, strip') for qw(login password);

# are you of legal age?
if ($params->check('user_age')->between('18-75')) {
    # access to explicit content approved
    # validated login, password, email_address, home_phone and user_age
}

# validate like a boss
# THE END

DESCRIPTION

Validation::Class::Simple::Streamer is a simple streaming validation module that makes data validation fun. It is built around the powerful Validation::Class data validation framework via Validation::Class::Simple.

This module is/was inspired by the simplicity and expressiveness of the Node.js validator library, https://github.com/chriso/node-validator, but built on top of the ever-awesome Validation::Class framework, which is designed to be modular and extensible, i.e. whatever custom directives you create and install will become methods on this class which you can then use to enforce policies.

GITHUB LINK

http://goo.gl/2sZvu

MISC BANTER

  • [19:55] * Now talking to ironcamel
  • [20:49] anewkirk: speaking of Validation::Class, I just added a new feature called simple-streamer
  • [20:58] ironcamel: can you give me a summary of what it does
  • [20:59] anewkirk: sure, it allows you to define rules as-you-go and in boolean-context it calls validate for you automatically
  • [21:00] anewkirk: imagine a dancer plugin
  • [21:00] anewkirk: that exports a method named check()
  • [21:00] anewkirk: ... and then in your route handler you simply say
  • [21:01] anewkirk: return '/dashboard' if check('login')->required->min_length(5)
  • [21:01] anewkirk: you're not pre-declaring rules at this point
  • [21:01] anewkirk: and that code actually works
  • [21:01] ironcamel: unless ($params->check('home_phone')->telephone) {
  • [21:02] ironcamel: what is ->telephone() and who defines it?
  • [21:02] anewkirk: telephone is the directive
  • [21:02] anewkirk: as in ....
  • [21:02] ironcamel: it is a built in thing?
  • [21:03] anewkirk: field home_phone => { telephone => 1 };
  • [21:03] ironcamel: what?
  • [21:03] anewkirk: check out https://github.com/chriso/node-validator
  • [21:04] anewkirk: it node.js validation library
  • [21:05] anewkirk: I was inspired by HTML::Zoom also -- https://metacpan.org/module/HTML::Zoom
  • [21:06] anewkirk: which mst calls a streaming template engine because you're not really manipulating the template until its rendered
  • [21:06] anewkirk: basically the functions build-up instructions which all get executed once you call render
  • [21:09] anewkirk: its a play on the literal definition of streaming
  • [21:09] anewkirk: ok, so check('foobar')->required->min_symbols doesn't do any validation, those functions simply queue-up instructions
  • [21:09] anewkirk: so its kinda streaming the validation event
  • [21:09] ironcamel: maybe Validation::Class::Simple::Sugar would be better
  • [21:10] ironcamel: i don't get it
  • [21:10] anewkirk: but its not really sugar
  • [21:10] ironcamel: are you thinking it is streaming because you are chaining method calls?
  • [21:10] anewkirk: the chainable nature of the functions makes it stream-like, yes
  • [21:10] ironcamel: it kind of is sugar
  • [21:10] anewkirk: well ...
  • [21:10] anewkirk: maybe
  • [21:11] ironcamel: because ->length(3) is sugar for (length($foo) eq 3)
  • [21:11] ironcamel: *==
  • [21:11] anewkirk: yes and no
  • [21:11] ironcamel: chaining functions has nothing to do with streaming
  • [21:12] anewkirk: i know, its a play on the concept of streaming
  • [21:23] ironcamel: i would probably use Validation::Class::Simple for most of my own personal projects
  • [21:24] ironcamel: so it almost seams like the streamer thing should be Validation::Nodeish
  • [21:24] ironcamel: Validator::Node
  • [21:24] ironcamel: yes
  • [21:24] anewkirk: oh god no hahaha

Leave a comment

About Al Newkirk

user-pic ... proud Perl hacker, ask me anything!