PHP code example of kinddylee / data-verification

1. Go to this page and download the library: Download kinddylee/data-verification library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kinddylee / data-verification example snippets


$api = [
    'username'=>'string|notEmpty',
    'contact'=>'= [
    'username'=>'john',
    'password'=>'123456',
    'contact'=>'0001-8976589765',
    'address'=>'Xi`An YanTa.',
];

// 结构校验通过
$validate->setDataCriterion( $api )->check( $requestData );

$api = [
    'username'=>'string|notEmpty',
    'contact'=>'= [
    'username'=>'john',
    'password'=>'123456',
    'contact'=>'0001-8976589765',
];

// 结构校验失败,缺少必须字段`address`
$validate->setDataCriterion( $api )->check( $requestData );

$api = [
    'username'=>'string|notEmpty',
    'contact'=>'= [
    'username'=>'john',
    'password'=>'123456',
    'contact'=>'0001-8976589765',
    'address'=>'

$api = [
    'actoin'=>'tring|notEmpty'], //定义多维数组需要添加`[`,`]`,只需定义第一位即可,后面已相同规则校验
    'jobs'=>[
        [
            'jobName'=>'[
            'jobName'=>'jobname1',
            'jobAction'=>'jobaction1',
        ],
        [
            'jobName'=>'jobname2',
            'jobAction'=>'jobaction2',
        ],
    ],
];

// 结构校验通过
$validate->setDataCriterion( $api )->check( $requestData );

$api = [
    'actoin'=>'tring|notEmpty'], //定义多维数组需要添加`[`,`]`,只需定义第一位即可,后面已相同规则校验
    'jobs'=>[
        [
            'jobName'=>'[
            'jobName'=>'jobname1',
            'jobAction'=>'jobaction1',
        ],
        [
            'jobName'=>'jobname2',
        ],
    ],
];

// 结构校验失败,jobs.1.jobAction缺失
$validate->setDataCriterion( $api )->check( $requestData );

必须性|数据类型|验证方法|参数


namespace YourNameSpace;

use DataVerification\VerificationAction\AbstractVerification;
use DataVerification\VerificationException\VerificationException;

class SomeDataValidate extends AbstractVerification
{
    //todo setKeyValidate
    private $keyTip;
    
    //form abstract
    protected function typeConvert($args)
    {
        global $requestKeyNameErrorTip;
        $this->keyTip = $requestKeyNameErrorTip;
                
        return $args;
    }
    
    //form abstract
    public function __call($method, $args) {
        $args = $this->typeConvert($args);
        //如果用户没有传指定校验方式,则只使用默认转换校验类型
        if ($method == 'typeConvert')
            return ;
        
        return call_user_func_array([$this, $method], $args);
    }
    
    //your validate action
    protected function yourCheckAction($value, $parameter)
    {
        throw new VerificationException('校验失败' . $parameter . $value);
    }
}



//通过静态工厂方法`addVerificationAction`将自定义验证实例注入至工厂中
\DataVerification\VerificationFactory::addVerificationAction('your_usage_label', new \YourNameSpace\SomeDataValidate());

// 实例化校验类
$validate = new DataVerification\Validate();

//设置自定义规则
$apiDefined = [
    "username" => "