NAME
Data::Validator - Rule based validator on type constraint subsystem
VERSION
This document describes Data::Validator version 0.03.
SYNOPSIS
use 5.10.0;
use Data::Validator;
# for functions
sub get {
state $rule = Data::Validator->new(
uri => { isa => 'Str', xor => [qw(schema host path_query)] },
schema => { isa => 'Str', default => 'http' },
host => { isa => 'Str' },
path_query => { isa => 'Str', default => '/' },
method => { isa => 'Str', default => 'GET' },
);
my $args = $rule->validate(@_);
# ...
}
get( uri => 'http://example.com/' );
# for methods
sub method {
state $rule = Data::Validator->new(
foo => 'Str',
)->with('Method');
my($self, $args) = $rule->validate(@_);
# ...
}
Foo->method( foo => 'bar' );
# using sequenced parameters
sub seq {
state $rule = Data::Validator->new(
foo => 'Str',
)->with('Sequenced');
my $args = $rule->validate(@_);
# ...
}
seq( 'bar' ); # seq() will get { foo => 'bar' }
seq({ foo => 'bar' }); # named style are available!
# both Method and Sequenced
sub seq_method {
state $rule = Data::Validator->new(
foo => 'Str',
)->with( 'Method', 'Sequenced');
my($self, $args) = $rule->validate(@_);
# ...
}
Foo->seq_method( 'bar' ); # seq() will get { foo => 'bar' }
DESCRIPTION
This is yet another validation library, based on Smart::Args
but less smart.
This is under development. Any API will change without notice.
Concepts
- Natural as Perl code
-
I love
Smart::Args
because it is really stylish, but it does not seem Perl-ish.Thus, I have designed
Data::Validator
in more Perl-ish way with full ofSmart::Args
functionality. - Basics on Mouse's type constraint system
-
Moose's type constraint system is awesome, and so is Mouse's. In fact, Mouse's type constraints are much faster than Moose's so that you need not hesitate to use type validations.
Thus, I have made
Data::Validator
based on Mouse's type constraint system. - Pure Perl
-
Although I do not hesitate to depend on XS modules, some people think that XS modules are hard to install.
Thus, I have written
Data::Validator
in pure Perl and chosen required modules which work in pure Perl. - Performance
-
I think validators should be as fast as possible because they are only important for illegal inputs.
This is much faster than
Params::Validate
, which has an XS backend, though.
INTERFACE
Data::Validator->new( $arg_name => $rule [, ...]) :Validator
Creates a validation rule.
Attributes for $rule are as follows:
isa => $type : Str|Object
does => $role : Str|Object
optional => $value : Bool
xor => $exclusives : ArrayRef
documentation => $doc : Str
$validator->with(@roles) :Validator
Applies @roles to $validator and returns itself.
See "EXTENTIONS" for details.
$validator->validate(@args) :HashRef
Validates @args and returns a restricted HASH reference.
Restricted hashes are hashes which do not allow to access non-existing keys, so you must check a key exists
in the hash before fetching values.
EXTENTIONS
There are extentions which changes behaviours of validate()
.
Extentions are defined as Mouse::Role
.
Method
Takes the first argument as a invocants (i.e. class or object instance), and returns it as the first value:
my($invocant, $args) = $rule->validate(@_);
Sequenced
Deals with arguments in sequenced style, where users should pass arguments by the order of argument rules, instead of by-name.
Note that if the last argument is a HASH reference, it is regarded as named-style arguments.
AllowExtra
Regards unknown arguments as extra arguments, and returns them as a list of name-value pairs:
my($args, %extra) = $rule->validate(@_);
DEPENDENCIES
Perl 5.8.1 or later.
BUGS
All complex software has bugs lurking in it, and this module is no exception. If you find a bug please either email me, or add the bug to cpan-RT.
SEE ALSO
AUTHOR
Fuji, Goro (gfx) <gfuji@cpan.org>
LICENSE AND COPYRIGHT
Copyright (c) 2010, Fuji Goro (gfx). All rights reserved.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.