Validation::Class::Simple::Streamer - Simple Streaming Data Validation
version 7.900048
use Validation::Class::Simple::Streamer; my $parameters = { credit_card => '0000000000000000', email_address => 'root@localhost', }; my $rules = Validation::Class::Simple::Streamer->new($parameters); # the point here is expressiveness # directive methods auto-validate in boolean context !!! if (not $rules->check('credit_card')->creditcard(['visa', 'mastercard'])) { # credit card is invalid visa/mastercard warn $rules->messages; } if (not $rules->check('email_address')->min_length(3)->email) { # email address is invalid warn $rules->messages; } # prepare password for validation $rules->check('password'); die "Password is not valid" unless $rules->min_symbols(1) && $rules->matches('password2'); # are you of legal age? if ($rules->check('member_years_of_age')->between('18-75')) { # access to explicit content approved } # get all fields with errors my $fields = $rules->validator->error_fields; # warn with errors if any warn $rules->messages unless $rules->is_valid; # validate like a boss # THE END
Validation::Class::Simple::Streamer is a simple streaming validation module that makes data validation fun. Target parameters and attach matching fields and directives to them by chaining together methods which represent Validation::Class directives. This module is built around the powerful Validation::Class data validation framework via Validation::Class::Simple. This module was inspired by the simplicity and expressiveness of the Node.js validator library, 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.
If you are new to Validation::Class, or would like more information on the underpinnings of this library and how it views and approaches data validation, please review Validation::Class::Whitepaper. Please review the "GUIDED-TOUR" in Validation::Class::Simple for a detailed step-by-step look into how Validation::Class works.
The check method specifies the parameter to be affected by directive methods if/when called.
$self = $self->check('email_address'); # focus on email_address $self->required; # apply the Required directive to email_address $self->min_symbols(1); # apply the MinSymbols directive to email_address $self->min_length(5); # apply the MinLength directive to email_address
The clear method resets the validation queue and declared fields but leaves the declared parameters in-tact, almost like the object state post-instantiation.
$self->clear;
The is_valid method returns a boolean value which is true if the last validation attempt was successful, and false if it was not (which is determined by looking for errors at the class and field levels).
$self->is_valid;
The messages method returns any registered errors as a concatenated string using the "errors_to_string" in Validation::Class::Prototype method and accepts the same parameters.
print $self->messages("\n");
The params method gives you access to the validator's params list which is a Validation::Class::Mapping object.
$params = $self->params($parameters);
The validate method uses the validator to perform data validation based on the series and sequence of commands issued previously. This method is called implicitly whenever the object is used in boolean context, e.g. in a conditional.
$true = $self->validate;
The validator method gives you access to the object's validation class which is a Validation::Class::Simple object by default.
$validator = $self->validator;
Al Newkirk <anewkirk@ana.io>
This software is copyright (c) 2011 by Al Newkirk.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Validation::Class, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Validation::Class
CPAN shell
perl -MCPAN -e shell install Validation::Class
For more information on module installation, please visit the detailed CPAN module installation guide.