NAME

Validator::Custom - Custom validator

VERSION

Version 0.0603

CAUTION

Validator::Custom is yew experimental stage.

SYNOPSIS

    ### How to use Validator::Custom
    
    # Data
    my $data = { k1 => 1, k2 => 2 };
    
    # Validate
    my $vc = Validator::Custom->new
    
    $vc->add_constraints(
        int => sub { my $value = shift; return $value =~ /^\d+$/; }
    );
    
    $vc->validation_rule([
        k1 => [
            'int'
        ],
        k2 => [
            'int'
        ]
    ]);
    
    my $result = $vc->validate($data,$validation_rule);
    
    # Get errors
    my @errors = $result->errors;
    
    # Handle errors
    foreach my $error (@$errors) {
        # ...
    }
    
    # Get invalid keys
    my @invalid_keys = $result->invalid_keys;
    
    # Get converted value
    my $products = $result->products;
    $product = $products->{key1};
    
    # Check valid or not
    if($result->is_valid) {
        # ...
    }
    
    ### 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 $data = { age => 'aaa', weight => 'bbb', favarite => [qw/sport food/};
    
    # Validation rule normal syntax
    my $validation_rule = [
        title => [
            ['Int', "Must be integer"],
        ],
        content => [
            ['Num', "Must be number"],
        ],
        favorite => [
            ['@Str', "Must be string"]
        ]
    ];
    
    # Validation rule light syntax
    my $validation_rule = [
        title => [
            'Int',
        ],
        content => [
            'Num',
        ],
        favorite => [
            '@Str'
        ]
    ];
    
    # Corelative check
    my $validation_rule => [
        [qw/password1 password2/] => [
            ['Same', 'passwor is not same']
        ]
    ]
    
    # Specify keys
    my $validation_rule => [
        {password_check => [qw/password1 password2/]} => [
            ['Same', 'passwor is not same']
        ]
    ]
    
    

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}
    );

ACCESSORS

constraints

You can also set constraint functions to object

    $vc->constraints($constraints);

add_constraint

You can add constraint to object

    $vc->add_constraint($constraint);

error_stock

If you stock error, set 1, or set 0.

Default is 1.

validation_rule

You can set validation_rule

    $vc->validation_rule($validation_rule);

Validation rule is the following

    ### Syntax of validation rule         
    my $validation_rule = [               # 1.Validation rule must be array ref
        key1 => [                         # 2.Constraints must be array ref
            'constraint1_1',              # 3.Constraint can be string
            ['constraint1_2', 'error1_2'],#     or arrya ref (error message)
            {'constraint1_3' => 'string'} #     or hash ref (arguments)
              
        ],
        key2 => [
            {'constraint2_1'              # 4.Argument can be 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 ref each value validation
        ]
    ];

METHOD

new

create instance

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

validate

validate

    my $result = $vc->validate($data,$validation_rule);
    my $result = $vc->validate($data) # $self->validation_rule is used

Validation rule is like the following.

    my $validation_rule = [
        # Custom Type
        key1 => [
            [ 'CustomType' ,         "Error message2-1"],
        ],
        
        # Array of Custom Type
        key2 => [
            [ '@CustomType',         "Error message3-1"]
        ]
    ];

$result is Validator::Custom::Result object. This have 'errors', 'invalid_keys', and 'products' methods.

Error messages is saved to 'errors' if some error occured. Invalid keys is saved to 'invalid_keys' if some error occured. Conversion products is saved to 'products' if convertion is excuted.

Validator::Custom::Result object

errors

You can get validation errors

    $errors = $result->errors
    @errors = $result->errors

invalid_keys

You can get invalid keys

    @invalid_keys = $result->invalid_keys
    $invalid_keys = $result->invalid_keys

products

You can get converted data.

    $products = $result->products

This is hash ref. You can get each product by specify key.

    $product = $products->{key}

is_valid

You can check data is valid or not

    my $is_valid = $result->is_valid;

AUTHOR

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

BUG REPORT

I develope this module http://github.com/yuki-kimoto/Validator-Custom

Please send message if you find bug.

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.