|
NAMEData::Validator - Rule based validator on type constraint systemVERSIONThis document describes Data::Validator version 1.07.SYNOPSISuse 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('StrictSequenced'); my $args = $rule->validate(@_); # ... } seq( 'bar' ); # seq() will get { foo => 'bar' } seq({ foo => 'bar' }); # named style are NOT available! # using Method and StrictSequenced together sub seq_method { state $rule = Data::Validator->new( foo => 'Str', )->with( 'Method', 'StrictSequenced'); my($self, $args) = $rule->validate(@_); # ... } Foo->seq_method( 'bar' ); # seq_method() will get { foo => 'bar' } # using sequenced and named parameters sub smart_seq { my $rule = Data::Validator->new( r1 => 'Str', r2 => 'HashRef', # accept this o1 => { isa => 'Str', default => 'yes' }, o2 => { isa => 'Num', optional => 1 }, )->with('SmartSequenced'); my $args = $rule->validate(@_); # ... } # all will get { r1 => 'foo', r2 => { val => 'bar' }, o1 => 'yes' } # mixed style(recommend) smart_seq( 'foo', { val => 'bar' }, { o1 => 'yes' } ); smart_seq( 'foo', { val => 'bar' } ); # also accept sequenced style smart_seq( 'foo', { val => 'bar' }, 'yes' ); smart_seq( 'foo', { val => 'bar' } ); # also accept named style smart_seq( { r1 => 'foo', r2 => { val => 'bar' }, o1 => 'yes' } ); smart_seq( { r1 => 'foo', r2 => { val => 'bar' } } ); DESCRIPTIONThis is yet another validation library, based on "Smart::Args" but less smart.This is designed for general data validation. For example, it is useful for CSV, JSON, XML, and so on. Concepts
INTERFACE"Data::Validator->new( $arg_name => $rule [, ...]) :Validator"Creates a validation rule. You should cache the rules for performance.Attributes for $rule are as follows:
"$validator->find_rule($name :Str)"Finds the rule named $name. Provided for error handling."$validator->with(@extensions) :Validator"Applies @extensions to $validator and returns itself.See "EXTENSIONS" 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 its values. EXTENSIONSThere are extensions which changes behaviours of "validate()".MethodTakes the first argument as an invocant (i.e. class or object instance), and returns it as the first value:my($invocant, $args) = $rule->validate(@_); SmartSequencedDeals with arguments in mixing sequenced style and named style. The sequenced style should be passed by the order of argument rules, and the named style arguments should be the last argument as HASH ref.The typical usage is that the required arguments as sequenced style, and some optional arguments as named style. StrictSequencedDeals with arguments in sequenced style, where users should pass arguments by the order of argument rules, instead of by-name.Note that single HASH ref argument was dealt as named-style arguments, but this feature is NOT available since version 1.01. SequencedDeals 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. AllowExtraRegards unknown arguments as extra arguments, and returns them as a list of name-value pairs:my($args, %extra) = $rule->validate(@_); NoThrowDoes not throw errors. Instead, it provides validators with the "errors" attribute:my $args = $v->validate(@_); # it never throws errors if($v->has_errors) { my $errors = $v->clear_errors; foreach my $e(@{$errors}) { # $e has 'type', 'message' and 'name' print $e->{message}, "\n"; } } CroakDoes not report stack backtrace on errors, i.e. uses "croak()" instead of "confess()" to throw errors.NoRestrictedDoes not make the argument hash restricted.DEPENDENCIESPerl 5.8.1 or later.BUGSAll 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 ALSOSmart::ArgsParams::Validate Sub::Args MooseX::Params::Validate Mouse Hash::Util for a restricted hash. AUTHORFuji, Goro (gfx) <gfuji@cpan.org>LICENSE AND COPYRIGHTCopyright (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.
Visit the GSP FreeBSD Man Page Interface. |