NAME

Mojolicious::Plugin::KossyValidator - 根据 Kossy 中的 Validator 移植过来的模块

SYNOPSIS

use Mojolicious::Plugin::KossyValidator;

sub show {
    my $c = shift
    my $result = $c->validator([
        'name' => {
            rule => [
                ['NOT_NULL', '不能为空'],
                #['ALPHA', '需要是数字'],
                #[['LENGTH',(10, 20)], '长度为 10-20 之间'],
                #[['BETWEEN',(1, 2)], '参数只能为数字'],
            ],  
        },  
        'description' => {
            default => '无',
            rule    => [], 
        },  
    ]); 

    $c->render( json => {
        result => 'false',
        messages => $result->errors
    }) if $result->has_error;

    $result->has_error:Flag
    $result->messages:ArrayRef[`Str]
     
    my $val = $result->valid('name');  # 注意取请求过来的参数时原函数 param 替换为 valid 了
    my @val = $result->valid('description');
     
    my %hash = $result->to_hash;
    # ...
};

dancer;

DESCRIPTION

Kossy 是 Perl 中另一个迷你框架,这个模块根据 Kossy 中的 Validator 移植过来支持 Mojolicious 的模块。

VALIDATORS

NOT_NULL
CHOICE
['CHOICE',qw/dog cat/]
INT

int

UINT

unsigned int

NATURAL

natural number

ALPHA

English alphabet

BETWEEN

between two number

LENGTH

param length

@SELECTED_NUM
['@SELECTED_NUM',min,max]
@SELECTED_UNIQ

all selected values are unique

CODEref VALIDATOR

my $result = $c->validator([
    'q' => [
        [sub{
            my ($req,$val) = @_;
        },'invalid']
    ],
]);

my $result = $c->validator([
    'q' => [
        [[sub{
            my ($req,$val,@args) = @_;
        },0,1],'invalid']
    ],
]);

AUTHOR

原模块作者 Masahiro Nagano <kazeburo {at} gmail.com> 移植人: 扶凯 iakuf {at} 163.com

SEE ALSO

Kossy::Validator

LICENSE

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