PHPackages                             githusband/validation - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. githusband/validation

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

githusband/validation
=====================

Parameters Validation

v2.6.1(1y ago)2351.4k3MITPHPPHP &gt;=5.3.0

Since May 8Pushed 1y ago2 watchersCompare

[ Source](https://github.com/gitHusband/Validation)[ Packagist](https://packagist.org/packages/githusband/validation)[ Docs](https://github.com/gitHusband/Validation)[ RSS](/packages/githusband-validation/feed)WikiDiscussions main Synced 6d ago

READMEChangelog (10)Dependencies (2)Versions (25)Used By (0)

[English](README-EN.md) | 中文

 **Validation**

**简单，直观，客制化**

 [ ![](https://camo.githubusercontent.com/e7b062500b969b9215a2273d78b685346e7d0ad0c0f8c4eb9878f6aecea24d9a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f67697448757362616e642f56616c69646174696f6e) ](https://hits.seeyoufarm.com) [ ![](https://camo.githubusercontent.com/12800bd4144a8dbcb705327ce8c638daab5428a9197bda28535516901e0f244b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c616e6775616765732f746f702f67697448757362616e642f56616c69646174696f6e) ](https://hits.seeyoufarm.com) [ ![](https://camo.githubusercontent.com/35cccbaeec617dcff41daf7ac4eba2ddc62423ecd3ce4737c274f6bc2b438d51/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f67697448757362616e642f56616c69646174696f6e2f756e69742d74657374732e796d6c3f6c6162656c3d756e69742d7465737473) ](https://hits.seeyoufarm.com) [ ![](https://camo.githubusercontent.com/24ada154266cb432c7671404ba9a96dbb7f6b0bdd279315d98b558f8b0998b1d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f67697448757362616e642f56616c69646174696f6e) ](https://hits.seeyoufarm.com)

目录
==

[](#目录)

- [Validation —— 一款 PHP 直观的数据验证器](#validation--%E4%B8%80%E6%AC%BE-php-%E7%9B%B4%E8%A7%82%E7%9A%84%E6%95%B0%E6%8D%AE%E9%AA%8C%E8%AF%81%E5%99%A8)
    - [1. 简介](#1-%E7%AE%80%E4%BB%8B)
        - [1.1 特点](#11-%E7%89%B9%E7%82%B9)
        - [1.2 一个例子](#12-%E4%B8%80%E4%B8%AA%E4%BE%8B%E5%AD%90)
    - [2. 安装](#2-%E5%AE%89%E8%A3%85)
    - [3. 开发](#3-%E5%BC%80%E5%8F%91)
    - [4. 功能介绍](#4-%E5%8A%9F%E8%83%BD%E4%BB%8B%E7%BB%8D)
        - [4.1 方法及其标志](#41-%E6%96%B9%E6%B3%95%E5%8F%8A%E5%85%B6%E6%A0%87%E5%BF%97)
        - [4.2 正则表达式](#42-%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F)
        - [4.3 方法传参](#43-%E6%96%B9%E6%B3%95%E4%BC%A0%E5%8F%82)
            - [传参的几种方式](#%E4%BC%A0%E5%8F%82%E7%9A%84%E5%87%A0%E7%A7%8D%E6%96%B9%E5%BC%8F)
            - [参数类型](#%E5%8F%82%E6%95%B0%E7%B1%BB%E5%9E%8B)
        - [4.4 自定义方法](#44-%E8%87%AA%E5%AE%9A%E4%B9%89%E6%96%B9%E6%B3%95)
            - [自定义方法的几种方式](#%E8%87%AA%E5%AE%9A%E4%B9%89%E6%96%B9%E6%B3%95%E7%9A%84%E5%87%A0%E7%A7%8D%E6%96%B9%E5%BC%8F)
            - [设置方法标志](#%E8%AE%BE%E7%BD%AE%E6%96%B9%E6%B3%95%E6%A0%87%E5%BF%97)
        - [4.5 串联并联规则](#45-%E4%B8%B2%E8%81%94%E5%B9%B6%E8%81%94%E8%A7%84%E5%88%99)
        - [4.6 条件规则](#46-%E6%9D%A1%E4%BB%B6%E8%A7%84%E5%88%99)
            - [When 条件规则](#when-%E6%9D%A1%E4%BB%B6%E8%A7%84%E5%88%99)
            - [IF 条件规则](#if-%E6%9D%A1%E4%BB%B6%E8%A7%84%E5%88%99)
        - [4.7 无限嵌套的数据结构](#47-%E6%97%A0%E9%99%90%E5%B5%8C%E5%A5%97%E7%9A%84%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84)
            - [数组自身的规则](#%E6%95%B0%E7%BB%84%E8%87%AA%E8%BA%AB%E7%9A%84%E8%A7%84%E5%88%99)
        - [4.8 可选字段](#48-%E5%8F%AF%E9%80%89%E5%AD%97%E6%AE%B5)
        - [4.9 特殊的验证规则](#49-%E7%89%B9%E6%AE%8A%E7%9A%84%E9%AA%8C%E8%AF%81%E8%A7%84%E5%88%99)
        - [4.10 客制化配置](#410-%E5%AE%A2%E5%88%B6%E5%8C%96%E9%85%8D%E7%BD%AE)
            - [启用实体（重点）](#%E5%90%AF%E7%94%A8%E5%AE%9E%E4%BD%93%E9%87%8D%E7%82%B9)
        - [4.11 国际化](#411-%E5%9B%BD%E9%99%85%E5%8C%96)
        - [4.12 验证全部数据](#412-%E9%AA%8C%E8%AF%81%E5%85%A8%E9%83%A8%E6%95%B0%E6%8D%AE)
        - [4.13 错误信息模板](#413-%E9%94%99%E8%AF%AF%E4%BF%A1%E6%81%AF%E6%A8%A1%E6%9D%BF)
        - [4.14 错误信息格式](#414-%E9%94%99%E8%AF%AF%E4%BF%A1%E6%81%AF%E6%A0%BC%E5%BC%8F)
    - [附录 1 - 方法标志及其含义](#%E9%99%84%E5%BD%95-1---%E6%96%B9%E6%B3%95%E6%A0%87%E5%BF%97%E5%8F%8A%E5%85%B6%E5%90%AB%E4%B9%89)
    - [附录 2 - 验证完整示例](#%E9%99%84%E5%BD%95-2---%E9%AA%8C%E8%AF%81%E5%AE%8C%E6%95%B4%E7%A4%BA%E4%BE%8B)
    - [附录 3 - 错误信息格式](#%E9%99%84%E5%BD%95-3---%E9%94%99%E8%AF%AF%E4%BF%A1%E6%81%AF%E6%A0%BC%E5%BC%8F)
        - [一维字符型结构](#%E4%B8%80%E7%BB%B4%E5%AD%97%E7%AC%A6%E5%9E%8B%E7%BB%93%E6%9E%84)
        - [一维关联型结构](#%E4%B8%80%E7%BB%B4%E5%85%B3%E8%81%94%E5%9E%8B%E7%BB%93%E6%9E%84)
        - [无限嵌套字符型结构](#%E6%97%A0%E9%99%90%E5%B5%8C%E5%A5%97%E5%AD%97%E7%AC%A6%E5%9E%8B%E7%BB%93%E6%9E%84)
        - [无限嵌套关联型结构](#%E6%97%A0%E9%99%90%E5%B5%8C%E5%A5%97%E5%85%B3%E8%81%94%E5%9E%8B%E7%BB%93%E6%9E%84)

Validation —— 一款 PHP 直观的数据验证器
=============================

[](#validation--一款-php-直观的数据验证器)

Validation 用于对数据合法性的检查。 目标只有 10 个字 - **规则结构即是数据结构**。

> [github 仓库](https://github.com/gitHusband/Validation)

**期待任何形式的贡献，让我们一起开发或优化 Validation！谢谢！**

 🙋 **为什么写这个工具？**1. 对于API的参数，理论上对每个参数都应该进行合法性检查，尤其是那些需要转发给其他API接口或者需要存储到数据库的参数。 *比如，数据库基本上对数据长度类型等有限制，对于长度的验证可谓是简单繁琐，使用该工具可以大大简化代码。*
2. 如果API参数过多，验证的代码量势必很大，无法直观通过代码明白参数格式。
3. 只需定制一个验证规则数组，规则数组长啥样，请求参数就长啥样。
4. 方便地多样化地设置验证方法返回的错误信息
5. 暂时想不到，想到了再给你们编。 🐶

1. 简介
-----

[](#1-简介)

### 1.1 特点

[](#11-特点)

- 一个字段对应一个验证规则集，一个规则集由多个验证规则和方法（函数）组成。
- 验证方法支持用标志代替，易于理解，简化规则。采用`*`, `>`, `` 等方法标志，比如 `*` 表示必要的
- 支持正则表达式

 👇 点击查看更多特性...- 支持方法传参。如 `@this` 代表当前字段值
- 支持拓展方法
- 支持串联验证：一个参数多个方法必须全部满足
- 支持并联验证：一个参数多个规则满足其一即可
- 支持条件验证：条件满足则继续验证后续方法，不满足则表明该字段是可选的
- 支持无限嵌套的数据结构，包括关联数组，索引数组
- 支持特殊的验证规则
- 支持客制化配置。比如多个方法的分隔符号默认 `|`，可以改为其他字符，如 `;`
- 支持国际化。默认英语，支持自定义方法返回错误信息
- 支持一次性验证全部数据(默认)，也可设置参数验证失败后立即结束验证
- 支持自定义错误信息，支持多种格式的错误信息，无限嵌套或者一维数组的错误信息格式
- 暂时想不到，想到了再给你们编。 🐶

### 1.2 一个例子

[](#12-一个例子)

```
use githusband\Validation;

// 待验证参数的简单示例。实际上无论参数多么复杂，都支持一个验证规则数组完成验证
$data = [
    "id" => 1,
    "name" => "Devin",
    "age" => 18,
    "favorite_animation" => [
        "name" => "A Record of A Mortal's Journey to Immortality",
        "release_date" => "July 25, 2020 (China)"
    ]
];

// 验证规则数组。规则数组的格式与待验证参数的格式相同。
$rule = [
    "id" => "required|/^\d+$/",         // 必要的，且只能是数字
    "name" => "required|length>=1',
    ];

    protected function grater_than_or_equal_to_1($data)
    {
        return $data >= 1;
    }
}
```

规则就这么写

```
$rule = [
    // id 必要的，且必须大于等于 1
    "id" => "required|>=1",
    // parent_id 可选的，且必须等于 1
    "parent_id" => "optional|euqal_to_1",
];
```

##### 4. 全局函数

[](#4-全局函数)

包括系统自带的函数和用户自定义的全局函数。

**几种方法的优先级**`新增方法` &gt; `新增方法类` &gt; `拓展 Validation 类` &gt; `内置方法` &gt; `全局函数`

若方法都不存在，则报错：未定义

#### 设置方法标志

[](#设置方法标志)

允许设置方法的标志，更加直观。例如，`greater_than` 的标志是 `>`。参考 [附录 1 - 方法标志及其含义](#%E9%99%84%E5%BD%95-1---%E6%96%B9%E6%B3%95%E6%A0%87%E5%BF%97%E5%8F%8A%E5%85%B6%E5%90%AB%E4%B9%89)从上一节中，你应该已经注意到方法标志的使用了。那也是最通用的设置标志的方式。例如，

```
public static $method_symbols = [
    'is_custom_string' => 'cus_str',
];
```

方法标志 `$method_symbols` 可能还支持其他属性：

- 如果值为字符串，例如 'cus\_str'，则表示标志。
- 如果值为数组，则支持以下字段:
    - `symbols`: 表示标志，例如 'cus\_str'。
    - `is_variable_length_argument`: 默认为 false。表示方法第二个参数为可变长度参数，规则集 中的第一个参数之后的所有参数都会被第二个参数的子元素。参考 `githusband\Rule\RuleClassDefault::$method_symbols['in_number_array']`。
    - `default_arguments`: 默认为 无。设置方法的默认参数。参考 `githusband\Rule\RuleClassArray::$method_symbols['is_unique']`。
        - `default_arguments` 数组的键必须是整形数字，表示第几个默认参数. 例如，`2` 表示第二个参数。
        - `default_arguments` 数组的值可以是任意值。对于类似 "@parent" (表示当前字段的父数据)，参考 [4.3 方法传参](#43-%E6%96%B9%E6%B3%95%E4%BC%A0%E5%8F%82)

对于 [2. 新增方法类](#2-%E6%96%B0%E5%A2%9E%E6%96%B9%E6%B3%95%E7%B1%BBadd_rule_class)，如果支持方法标志的全部属性，例子如下，

 👇 **点击查看代码**```
class RuleClassTest
{
    /**
     * 方法标志：
     * - 如果值为字符串，则表示标志。
     * - 如果值为数组，则支持以下字段:
     *   - `symbols`: 表示标志
     *   - `is_variable_length_argument`: 默认为 false。表示方法第二个参数为可变长度参数，规则集 中的第一个参数之后的所有参数都会被第二个参数的子元素。参考 `githusband\Rule\RuleClassDefault::$method_symbols['in_number_array']`。
     *   - `default_arguments`: 默认为 无。设置方法的默认参数。参考 `githusband\Rule\RuleClassArray::$method_symbols['is_unique']`。
     *     - `default_arguments` 数组的键必须是整形数字，表示第几个默认参数. 例如，`2` 表示第二个参数。
     *     - `default_arguments` 数组的值可以是任意值。对于类似 "@parent" (表示当前字段的父数据)，参考 https://github.com/gitHusband/Validation?tab=readme-ov-file#43-%E6%96%B9%E6%B3%95%E4%BC%A0%E5%8F%82
     *
     * @example `in_number_array[1,2,3]` 第二个参数是一个数组 `[1,2,3]`
     * @see githusband\Rule\RuleClassDefault::$method_symbols
     * @see githusband\Rule\RuleClassArray::$method_symbols
     * @var array
     */
    public static $method_symbols = [
        'is_custom_string' => 'cus_str',
        'is_in_custom_list' => [
            'symbols' => '',
            'is_variable_length_argument' => true,  // 第一个参数之后的所有参数，都被认作第二个参数数组的子元素
        ],
        'is_equal_to_password' => [
            'symbols' => '=pwd',
            'default_arguments' => [
                2 => '@password'    // 第二个参数默认为 password 字段的值
            ]
        ]
    ];

    /**
     * 测试方法 1 - 测试当前字段的格式是否满足要求
     *
     * 用法：
     * - 'id' => 'is_custom_string'
     * - 'id' => 'cus_str'
     *
     * @param string $data
     * @return bool
     */
    public static function is_custom_string($data)
    {
        return preg_match('/^[\w\d -]{8,32}$/', $data) ? true : false;
    }

    /**
     * 测试方法 2 - 测试当前字段是否存在于列表内
     *
     * 用法：
     * - 'sequence' => 'is_in_custom_list[1st, First, 2nd, Second]'
     * - 'sequence' => '[1st, First, 2nd, Second]'
     *
     * 这是一个第二个参数为可变长度参数的例子。如果不设置可变长度参数，那么它的写法如下，注意第二个参数必须是合法的 JSON Encoded 字符串。例如：
     * - 'sequence' => 'is_in_custom_list[["1st", "First", "2nd", "Second"]]'
     * - 'sequence' => '[["1st", "First", "2nd", "Second"]]'
     *
     * @param mixed $data
     * @param array $list
     * @return bool
     */
    public static function is_in_custom_list($data, $list)
    {
        return in_array($data, $list);
    }

    /**
     * 测试方法 3 - 验证当前字段是否与 password 字段相等
     *
     * 用法：
     * - 'confirm_password' => 'is_equal_to_password'
     * - 'confirm_password' => '=pwd'
     *
     * 这是一个默认参数的例子。用 `euqal` 方法来写效果也一样, 它相当于给 equal 方法加了默认参数 '@password'。例如：
     * - 'confirm_password' => `equal[@password]`，
     * - 'confirm_password' => `=[@password]`，
     *
     * @param string $data
     * @param string $password
     * @return bool
     */
    public static function is_equal_to_password($data, $password)
    {
        return $data == $password;
    }
}
```

### 4.5 串联并联规则

[](#45-串联并联规则)

- 串联：一个参数的多个方法必须全部满足，标志是 `|`

```
"age" => "required|equal[20]"
```

- 并联：一个参数的多个规则满足其一即可。 两种方法：
    - A. `{字段名}` + `[or]`
    - B. 在当前字段下增加唯一子字段 `[or]`

`[or]` 的标志是 `[||]` , 标志支持自定义，使用方法同 `[or]`

```
// 串联，身高单位是必须的，且必须是 cm 或者 m
"height_unit" => "required|[cm,m]",
// A. 并联，规则可以这么写，[or] 可以替换成标志 [||]
"height[or]" => [
    // 若身高单位是厘米 cm, 则身高必须大于等于100，小于等于200
    "required|=(@height_unit,cm)|>====[180]",
];
```

- **2.2 否条件的必要规则**：`required:when_not()`

1. 如果条件不成立，则验证 `required` 方法
2. 如果条件成立，说明该字段是可选的： 2.1. 若该字段为空，立刻返回验证成功； 2.2. 若该字段不为空，则继续验证后续方法

```
$rule = [
    // 特征是必要的，且只能是 height(身高) 或 weight(体重)
    "attribute" => "required|[height,weight]",
    // 若属性不是 weight, 则 centimeter 是必要的，若是 weight，则是可选的。
    // 无论如何，若该值非空，则必须大于 180
    "centimeter" => "required:when_not(=(@attribute,weight))|>[180]",
];
```

#### IF 条件规则

[](#if-条件规则)

IF 条件规则的写法跟 `PHP` 的 `if 结构` 语法差不多, 例如：

- `if ( expr ) { statement }`
- `if ( !expr ) { statement }`
- `if ( expr1 || !expr2 ) { statement1 } else { statement2 }`

**支持的逻辑操作符：**

- `!`：逻辑操作符否。
    - 条件中不包含 `!`: 表示当条件结果与布尔值 `true` 严格相同(`===`)，则条件成立。
    - 条件中包含 `!`: 表示当条件结果与布尔值 `true` 不严格相同(`!==`)，则条件成立。
- `||`：逻辑操作符或。表示其中一个条件成立即可。

**执行逻辑是：**

1. 如果条件成立，则继续验证后续方法。
2. 如果条件不成立，则不继续验证后续方法，立即返回成功。

**例子 1：**

```
$rule = [
    // 特征是必要的，且只能是 height(身高) 或 weight(体重)
    "attribute" => "required|[height,weight]",
    // 若属性是 height, 则 centimeter 是必要的，且必须大于 180
    // 若不是 height，则不继续验证后续规则，即 centimeter 为任何值都可以。
    "centimeter" => "if(=(@attribute,height)){required|>[180]}",
];
```

**例子 2：**

```
$rule = [
    // 特征是必要的，且只能是 height(身高) 或 weight(体重)
    "attribute" => "required|[height,weight]",
    // 若属性不是 weight, 则 centimeter 是必要的，且必须大于 180
    // 若是 weight，则不继续验证后续规则，即 centimeter 为任何值都可以。
    "centimeter" => "if ( !=(@attribute,weight) ) { required|>[180] }",
];
```

**例子 3：**

```
$rule = [
    "id" => "required|>[3]",
    "favourite_fruit" => [
        "name" => "required|length>[3]",
        "color" => "required|length>[3]",
        "shape" => "required|length>[3]"
    ]
];
```

**2. 无限嵌套的索引数组**索引数组字段的名称后面加上标志 `.*`，或者给索引数组字段加上唯一子元素 `*`

 👇 **点击查看代码**```
$data = [
    "id" => 1,
    "name" => "Johnny",
    "favourite_color" => [
        "white",
        "red"
    ],
    "favourite_fruits" => [
        [
            "name" => "apple",
            "color" => "red",
            "shape" => "circular"
        ],
        [
            "name" => "banana",
            "color" => "yellow",
            "shape" => "long strip"
        ],
    ]
];

// 若要验证上述 $data，规则可以这么写
$rule = [
    "id" => "required|/^\d+$/",
    "name" => "required|length>[3]",
    "favourite_color.*" => "required|length>[3]",
    "favourite_fruits.*" => [
        "name" => "required|length>[3]",
        "color" => "required|length>[3]",
        "shape" => "required|length>[3]"
    ]
];

// 也可以这么写
$rule = [
    "id" => "required|/^\d+$/",
    "name" => "required|length>[3]",
    "favourite_color" => [
        "*" => "required|length>[3]"
    ],
    "favourite_fruits" => [
        "*" => [
            "name" => "required|length>[3]",
            "color" => "required|length>[3]",
            "shape" => "required|length>[3]"
        ]
    ]
];
```

#### 数组自身的规则

[](#数组自身的规则)

上述的例子，一个规则数组便可完成对复杂结构的数据的验证。但只能对叶子字段进行验证，如果要对父数组本身进行验证，还无法实现。 那么，我们设计通过一个 `__self__` 叶子字段，表示父数组本身的规则。

- `__self__` 字段允许自定义，见 [4.10 客制化配置](#410-%E5%AE%A2%E5%88%B6%E5%8C%96%E9%85%8D%E7%BD%AE)
- 特殊的，如果数组本身规则只有可选的 `optional`，有简便的写法，见 [4.8 可选字段](#48-%E5%8F%AF%E9%80%89%E5%AD%97%E6%AE%B5)

**1. 关联数组的自身规则**

```
$rule = [
    // 表示根数据是可以为空的，如果不为空，要求其字段有且只有包含 id, name, favourite_fruit
    "__self__" => "optional|require_array_keys[id, name, favourite_fruit]",
    "id" => "required|/^\d+$/",
    "name" => "required|length>[3]",
    "favourite_fruit" => [
        // 表示 favourite_fruit 是可以为空的，如果不为空，要求其字段有且只有包含 name, color, shape
        "__self__" => "optional|require_array_keys[name, color, shape]",
        "name" => "required|length>[3]",
        "color" => "required|length>[3]",
        "shape" => "required|length>[3]"
    ]
];
```

**2. 索引数组的自身规则**

```
$rule = [
    "id" => "required|/^\d+$/",
    "name" => "required|length>[3]",
    "favourite_color" => [
        // 表示 favourite_color 是可以为空的，如果不为空，要求其字段有且只有包含 0, 1，也就是只有两个子数据。
        "__self__" => "optional|require_array_keys[0,1]",
        "*" => "required|length>[3]"
    ],
    "favourite_fruits" => [
        // 表示 favourite_fruits 是可以为空的，如果不为空，要求其字段有且只有包含 0, 1，也就是只有两个子数据。
        "__self__" => "optional|require_array_keys[0,1]",
        "*" => [
            "name" => "required|length>[3]",
            "color" => "required|length>[3]",
            "shape" => "required|length>[3]"
        ]
    ]
];
```

### 4.8 可选字段

[](#48-可选字段)

1. 一般的，对于一个叶子字段（无任何子字段），可以直接使用 `optional` 方法，表示该字段是可选的。
2. 有时候，数组也是可选的，但是一旦设置，其中的子元素必须按规则验证。对于这种情况，只需要在数组字段名后面加上 `[optional]`，表示该数组是可选的。
3. 与在字段名后面加上 `[optional]` 一样的效果，给字段增加唯一子元素 `[optional]`，也表示该字段是可选的。
4. `[optional]` 的标志是 `[O]`，两者可以互相替换。

例如：

```
$rule = [
    // 1. 叶子字段，直接使用 optional 方法，表示该字段是可选的
    "name" => "optional|string",
    // 2. 任意字段，在字段名后面添加 [optional]，表示该字段是可选的
    "favourite_fruit[optional]" => [
        "name" => "required|string",
        "color" => "required|string"
    ],
    // 3. 任意字段，增加唯一子元素 [optional]，表示该字段是可选的
    "gender" => [ "[optional]" => "string" ],
    "favourite_food" => [
        "[optional]" => [
            "name" => "required|string",
            "taste" => "required|string"
        ]
    ],
];
```

### 4.9 特殊的验证规则

[](#49-特殊的验证规则)

特殊规则列表：

全称标志含义\[optional\]\[O\]表明字段是可选的，支持数组。见 [4.8 可选字段](#48-%E5%8F%AF%E9%80%89%E5%AD%97%E6%AE%B5)\[or\]\[||\]表明单个字段是或规则，多个规则满足其一即可。见 [4.5 串联并联规则](#45-%E4%B8%B2%E8%81%94%E5%B9%B6%E8%81%94%E8%A7%84%E5%88%99)(无全称).\*表明字段是索引数组。见 [4.7 无限嵌套的数据结构](#47-%E6%97%A0%E9%99%90%E5%B5%8C%E5%A5%97%E7%9A%84%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84)注意：标志使用方法和全称一样，且标志支持 [客制化](#410-%E5%AE%A2%E5%88%B6%E5%8C%96%E9%85%8D%E7%BD%AE)。

### 4.10 客制化配置

[](#410-客制化配置)

支持客制化的配置有：

 👇 **点击查看配置**```
$config = [
    'language' => 'en-us',                                      // Language, Default en-us
    'lang_path' => '',                                          // Customer Language file path
    'enable_entity' => false,                                   // Pre-parse ruleset into ruleset entity to reuse the ruleset without re-parse
    'validation_global' => true,                                // 1. true - validate all rules even though previous rule had been failed; 2. false - stop validating when any rule is failed
    'auto_field' => "data",                                     // If root data is string or numberic array, add the auto_field as the root data field name
    'reg_msg' => '/ >> (.*)$/sm',                               // Set the error message format for all the methods after a rule string
    'reg_preg' => '/^(\/.+\/.*)$/',                             // If a rule match reg_preg, indicates it's a regular expression instead of method
    'reg_preg_strict' => '/^(\/.+\/[imsxADSUXJun]*)$/',         // Verify if a regular expression is valid
    'symbol_if' => 'if',                                        // The start of IF construct. e.g. `if ( expr ) { statement }`
    'symbol_else' => 'else',                                    // The else part of IF construct. e.g. `else { statement }`. Then the elseif part is `else if ( expr ) { statement }`
    'symbol_logical_operator_not' => '!',                       // The logical operator not. e.g. `if ( !expr ) { statement }`
    'symbol_logical_operator_or' => '||',                       // The logical operator or. e.g. `if ( expr || expr ) { statement }`
    'symbol_rule_separator' => '|',                             // Serial rules seqarator to split a rule into multiple methods
    'symbol_parallel_rule' => '[||]',                           // Symbol of the parallel rule, Same as "[or]"
    'symbol_method_standard' => '/^([^\\(]*)\\((.*)\\)$/',      // Standard method format, e.g. equal(@this,1)
    'symbol_method_omit_this' => '/^([^\\[]*)\\[(.*)\\]$/',     // @this omitted method format, will add a @this parameter at first. e.g. equal[1]
    'symbol_parameter_separator' => ',',                        // Parameters separator to split the parameter string of a method into multiple parameters, e.g. equal(@this,1)
    'symbol_field_name_separator' => '.',                       // Field name separator of error message, e.g. "fruit.apple"
    'symbol_required' => '*',                                   // Symbol of required field, Same as the rule "required"
    'symbol_optional' => 'O',                                   // Symbol of optional field, can be not set or empty, Same as the rule "optional"
    'symbol_optional_unset' => 'O!',                            // Symbol of optional field, can be not set only, Same as the rule "optional_unset"
    'symbol_array_optional' => '[O]',                           // Symbol of array optional rule, Same as "[optional]"
    'symbol_index_array' => '.*',                               // Symbol of index array rule
    'reg_whens' => '/^(.+):(!)?\?\((.*)\)/',                    // A regular expression to match both reg_when and reg_when_not. Most of the methods are allowed to append a if rule, e.g. required:when, optional:when_not
    'reg_when' => '/^(.+):\?\((.*)\)/',                         // A regular expression to match a field which must be validated by method($1) only when the condition($3) is true
    'symbol_when' => ':?',                                      // We don't use the symbol to match a When Rule, it's used to generate the symbols in README
    'reg_when_not' => '/^(.+):!\?\((.*)\)/',                    // A regular expression to match a field which must be validated by method($1) only when the condition($3) is not true
    'symbol_when_not' => ':!?',                                 // We don't use the symbol to match a When Rule, it's used to generate the symbols in README
    'self_ruleset_key' => '__self__',                           // If an array has such a subfield with the same name as {self_ruleset_key}, then the ruleset of this subfield is the ruleset of the array.
];
```

例如，你觉得我设计的规则太丑了，一点都不好理解。😡于是你做了如下的定制:

```
$custom_config = [
    'reg_preg' => '/^Reg:(\/.+\/.*)$/',                         // If a rule match reg_preg, indicates it's a regular expression instead of method
    'symbol_rule_separator' => '&&',                            // Serial rules seqarator to split a rule into multiple methods
    'symbol_method_standard' => '/^(.*)#(.*)$/',                // Standard method format, e.g. equal(@this,1)
    'symbol_method_omit_this' => '/^(.*)~(.*)$/',               // @this omitted method format, will add a @this parameter at first. e.g. equal[1]
    'symbol_parameter_separator' => '+',                        // Parameters separator to split the parameter string of a method into multiple parameters, e.g. equal(@this,1)
    'symbol_field_name_separator' => '->',                      // Field name separator of error message, e.g. "fruit.apple"
    'symbol_required' => '!*',                                  // Symbol of required field, Same as the rule "required"
    'symbol_optional' => 'o?',                                  // Symbol of optional field, can be not set or empty, Same as the rule "optional"
];

$validation = new Validation($custom_config);
```

那么，[1.2 一个例子](#12-%E4%B8%80%E4%B8%AA%E4%BE%8B%E5%AD%90) 中的规则可以这么写：

```
$rule = [
    "id" => "!*&&Reg:/^\d+$/",          // 必要的，且只能是数字
    "name" => "!*&&length>
