NAME

Validator::Custom - Custamizable validator

VERSION

Version 0.0902

STATE

This module is not stable. APIs will be changed for a while.

SYNOPSYS

use Validator::Custom;

# New
my $vc = Validator::Custom->new;

# Add Constraint
$vc->add_constraint(
    int => sub {
        my $value    = shift;
        my $is_valid = $value =~ /^\d+$/;
        return $is_valid;
    },
    ascii => sub {
        my $value    = shift;
        my $is_valid = $value =~ /^[\x21-\x7E]+$/;
        return $is_valid;
    },
    not_blank => sub {
        my $value = shift;
        my $is_valid = $value ne '';
        return $is_valid;
    },
    length => sub {
        my ($value, $args) = @_;
        my ($min, $max) = @$args;
        my $length = length $value;
        my $is_valid = $length >= $min && $length <= $max;
        return $is_valid;
    }
);

# Data
my $data = { 
    age => 19, 
    names => ['abcoooo', 'def']
};

# Validation rule
$vc->rule([
    age => [
        'int'
    ],
    names => [
        ['@not_blank',          "name must exist"],
        ['@ascii',              "name must be ascii"],
        [{'@length' => [1, 5]}, "name must be 1 to 5"]
    ]
]);

# Validation
my $result = $vc->validate($data);

# Get all error messages
my @errors = $result->errors;

# Get a error message
my $error = $result->error('age');

# Get invalid keys
my @invalid_keys = $result->invalid_keys;

# Get producted value
my $products = $result->products;

# Is all data valid?
my $ret = $result->is_valid;

# Is a data valid
$ret = $result->is_valid('age');

# Corelative validation
my $rule = [
    {password_check => [qw/password1 password2/]} => [
        ['duplicate', 'Passwor is not same']
    ]
];

# "or" validation
$rule = [
    email => [
        'blank'
    ],
    # or
    email => [
        'not_blank',
        'email'
    ]
];
    

ATTRIBUTES

constraints

Constraint functions

$vc          = $vc->constraints($constraints);
$constraints = $vc->constraints;

Example

$vc->constraints(
    int    => sub { ... },
    string => sub { ... }
);

error_stock

Are errors stocked?

$vc          = $vc->error_stock(0);
$error_stock = $vc->error_stcok;

If you set 0, validation errors is not stocked. Validation is finished when one error is occured. This is faster than stocking all errors.

Default is 1. All errors are stocked.

rule

Validation rule

$vc   = $vc->rule($rule);
$rule = $vc->rule;

Validation rule has the following syntax.

### Syntax of validation rule         
my $rule = [                          # 1. Validation rule is array ref
    key1 => [                         # 2. Constraints is array ref
        'constraint1_1',              # 3. Constraint is string
        ['constraint1_2', 'error1_2'],#      or arrya ref (error message)
        {'constraint1_3' => 'string'} #      or hash ref (arguments)
          
    ],
    key2 => [
        {'constraint2_1'              # 4. Argument is string
          => 'string'},               #
        {'constraint2_2'              #     or array ref
          => ['arg1', 'arg2']},       #
        {'constraint1_3'              #     or hash ref
          => {k1 => 'v1', k2 => 'v2'}}#
    ],
    key3 => [                           
        [{constraint3_1 => 'string'}, # 5. Combination argument
         'error3_1' ]                 #     and error message
    ],
    { key4 => ['key4_1', 'key4_2'] }  # 6. Multi key validation
        => [
            'constraint4_1'
           ],
    key5 => [
        '@constraint5_1'              # 7. array's items validation
    ]
];

'validation_rule' is deprecated. It is renamed to 'rule'

syntax

Syntax of validation rule

$vc     = $vc->syntax($syntax);
$syntax = $vc->syntax;

MEHTODS

new

Constructor

$vc = Validator::Costom->new;
$vc = Validator::Costom->new(rule => [ .. ]);

add_constraint

Add constraint function

$vc->add_constraint(%constraint);
$vc->add_constraint(\%constraint);

Example

$vc->add_constraint(
    int => sub {
        my $value    = shift;
        my $is_valid = $value =~ /^\-?[\d]+$/;
        return $is_valid;
    },
    ascii => sub {
        my $value    = shift;
        my $is_valid = $value =~ /^[\x21-\x7E]+$/;
        return $is_valid;
    }
);

validate

Validation

$result = $vc->validate($data, $rule);
$result = $vc->validate($data);

If you omit $rule, $vc->rule is used. Return value is Validator::Custom::Result object.

Validator::Custom::Result

'validate' method return Validator::Custom::Result object.

See Validator::Custom::Result.

The following is Validator::Custom::Result sample

# Restlt
$result = $vc->validate($data, $rule);

# Error message
@errors = $result->errors;

# Invalid keys
@invalid_keys = $result->invalid_keys;

# Producted values
$products = $result->products;
$product  = $products->{key1};

# Is it valid?
$is_valid = $result->is_valid;

CONSTRAINT FUNCTION

You can resist your constraint function using 'add_constraint' method.

canstrant function can receive two argument.

1. value in validating data
2. argument passed in validation rule

I explain using sample. You can pass argument in validation rule. and you can receive the argument in constraint function

my $data = {key => 'value'}; # 1. value
my $rule => {
    key => [
        {'name' => $args} # 2. arguments
    ],
}

$vc->add_constraint(name => sub {
    my ($value, $args) = @_;
    
    # ...
    
    return $is_valid;
});

constraint function also can return producted value.

$vc->add_constraint(name => sub {
    my ($value, $args) = @_;
    
    # ...
    
    return ($is_valid, $product);
});

Validator::Custom::HTML::Form is good sample.

CUSTOM CLASS

You can create your custom class extending Validator::Custom.

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

__PACKAGE__->add_constraint(
    int => sub {
        my $value    = shift;
        my $is_valid = $value =~ /^\-?[\d]+$/;
        return $is_valid;
    },
    ascii => sub {
        my $value    = shift;
        my $is_valid = $value =~ /^[\x21-\x7E]+$/;
        return $is_valid;
    }
);

This class is avalilable same way as Validator::Custom

$vc = Validator::Custom::Yours->new;

Validator::Custom::Trim, Validator::Custom::HTMLForm is good sample.

OR VALIDATION

This module also provide 'or' validation. You write key constaraint in a rule repeateadly. one of the constraint is valid, the key is valid.

$validator->rule( key1 => ['constraint'], key1 => ['constraint2'] );

Example

"email" is valid, if 'email' is blank or mail address, To understand "or validation" easily, it is good practice to add "# or" comment to your code.

$validator->rule([
    email => [
        'blank'
    ],
    # or
    email => [
        'not_blank',
        'email'
    ]
]);

AUTHOR

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

Development http://github.com/yuki-kimoto/Validator-Custom

COPYRIGHT & LICENCE

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.