NAME

Validator::Custom - Custom validator

VERSION

Version 0.0203

CAUTION

Validator::Custom is yew experimental stage.

SYNOPSIS

### How to use Validator::Custom

# data
my $hash = { title => 'aaa', content => 'bbb' };

# validator functions
my $validator = [
    title => [
        [sub{$_[0]},              "Specify title"],
        [sub{length $_[0] < 128}, "Too long title"]
    ],
    content => [
        [sub{$_[0]},               "Specify content"],
        [sub{length $_[0] < 1024}, "Too long content"]
    ]
];

# validate
my $vc = Validator::Custom->new;
my $errors = $vc->validate($hash,$validator)->errors;

# process in error case
if($errors){
    foreach my $error (@$errors) {
        # process all errors
    }
}

### How to costomize Validator::Custom
package Validator::Custom::Yours;
use base 'Validator::Custom';

# regist custom type
__PACKAGE__->add_constraint(
    {
        Int => sub {$_[0] =~ /^\d+$/},
        Num => sub {
            require Scalar::Util;
            Scalar::Util::looks_like_number($_[0]);
        },
        Str => sub {!ref $_[0]}
    }
);

### How to use customized validator class
use Validator::Custom::Yours;
my $hash = { age => 'aaa', weight => 'bbb', favarite => [qw/sport food/};

my $validator = [
    title => [
        ['Int', "Must be integer"],
    ],
    content => [
        ['Num', "Must be number"],
    ],
    favorite => [
        ['@Str', "Must be string"]
    ]
];

my $vc = Validator::Custom::Yours->new;
my $errors = $vc->validate($hash,$validator)->errors;

CLASS METHOD

constraints

get constraints

# get
my $constraints = Validator::Custom::Your->constraints;

add_constraint

You can use this method in custom class. New validator functions is added.

package Validator::Custom::Yours;
use base 'Validator::Custom';

__PACKAGE__->add_constraint(
    {
        Int => sub {$_[0] =~ /^\d+$/},
    }
);

You can merge multiple custom class

package Validator::Custom::YoursNew;
use base 'Validator::Custom';

use Validator::Custum::Yours1;
use Validatro::Cumtum::Yours2;

__PACAKGE__->add_constraint
    Validator::Custom::Yours1->constraints,
    Validator::Custom::Yours2->constraints
);

METHOD

new

create instance

my $vc = Validator::Costom->new;

validate

validate

$vc->validate($hash,$validator);

validator format is like the following.

my $validator = [
    # Function
    key1 => [
        [ \&validator_function1, "Error message1-1"],
        [ \&validator_function2, "Error message1-2"] 
    ],
    
    # Custom Type
    key2 => [
        [ 'CustomType' ,         "Error message2-1"],
    ],
    
    # Array of Custom Type
    key3 => [
        [ '@CustomType',         "Error message3-1"]
    ]
];

this method retrun self.

errors

You can get validator errors

my $errors = $vc->errors;

You can use this method after calling validate

my $errors = $vc->validate($hash,$validator)->errors;

AUTHOR

Yuki Kimoto, <kimoto.yuki at gmail.com>

BUGS

Please report any bugs or feature requests to bug-validator-custom at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Validator-Custom. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc Validator::Custom

You can also look for information at:

ACKNOWLEDGEMENTS

COPYRIGHT & LICENSE

Copyright 2009 Yuki Kimoto, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.