Expand description
验证器模块 — 对齐 PHP think\Validate
本模块实现验证器框架,对齐 PHP think\Validate 类的
规则定义、批量验证、错误消息查找、场景管理等核心机制。
§PHP 对齐
§核心结构映射
| PHP 字段 | Rust 字段 | 说明 |
|---|---|---|
$rule | Validate::rule | 当前验证规则 |
$message | Validate::message | 验证提示信息 |
$field | Validate::field | 字段描述 |
$typeMsg | TYPE_MSG | 默认规则提示 |
$alias | ALIAS | 验证类型别名 |
$defaultRegex | DEFAULT_REGEX | 内置正则 |
$regex | Validate::regex | 自定义正则 |
$scene | Validate::scene | 验证场景定义 |
$currentScene | Validate::current_scene | 当前验证场景 |
$batch | Validate::batch | 是否批量验证 |
$only | Validate::only | 场景需要验证的字段 |
$remove | Validate::remove | 场景移除的规则 |
$append | Validate::append | 场景追加的规则 |
$error | Validate::error | 验证失败错误信息 |
$type | Validate::type_callbacks | 自定义验证类型 |
§核心方法映射
| PHP 方法 | Rust 方法 | 说明 |
|---|---|---|
rule($name, $rule) | Validate::rule | 添加字段验证规则 |
message(array $msg) | Validate::message | 设置提示信息 |
scene($name) | Validate::scene | 设置验证场景 |
hasScene($name) | Validate::has_scene | 判断场景是否存在 |
batch(bool) | Validate::batch | 设置批量验证 |
only(array) | Validate::only | 指定需要验证的字段 |
remove($field, $rule) | Validate::remove | 移除字段规则 |
append($field, $rule) | Validate::append | 追加字段规则 |
extend($type, $cb) | Validate::extend | 注册验证类型 |
check(array $data, array $rules) | Validate::check | 数据自动验证 |
checkRule($value, $rules) | Validate::check_rule | 根据规则验证数据 |
getError() | Validate::get_error | 获取错误信息 |
getRuleMsg(...) | Validate::get_rule_msg | 获取规则错误提示 |
parseErrorMsg(...) | Validate::parse_error_msg | 解析错误提示 |
getDataValue(...) | Validate::get_data_value | 获取数据值 |
getValidateType(...) | Validate::get_validate_type | 获取验证类型 |
is($value, $rule) | Validate::is | 验证字段值是否为有效格式 |
require($value) | Validate::require | 必须验证 |
must($value) | Validate::must | 必须验证(与 require 等价) |
§PHP 行为对齐(R5 硬约束)
本模块严格对齐以下 PHP 行为(包括 bug):
-
R5-1:
getRuleMsg查找优先级链(对齐 PHP 第 1565-1586 行):message[field.type]message[field]type_msg[type]- 如果 type 以
require开头,使用type_msg['require'] - 默认
$title . lang->get('not conform to the rules')(无前导空格,对齐 PHP 第 1578 行)
-
R5-2:
parseErrorMsg占位符替换(对齐 PHP 第 1596-1633 行)::attribute→ title:1/:2/:3→ rule 按逗号分割后的前 3 个元素:rule→ rule 原值(仅当 msg 包含:rule时)
-
R5-3:
getDataValue行为(对齐 PHP 第 1536-1554 行):- 数值型 key 返回 key 本身(PHP 怪异行为,复刻)
- 包含
.的 key 按多维数组访问 - 其他 key 返回
data[key]或 null
-
R5-4:
getValidateType类型推导(对齐 PHP 第 678-706 行):- 别名映射(
>→gt,>=→egt等) info字段用于remove/append匹配
- 别名映射(
-
R5-5:空值跳过验证行为(对齐 PHP 第 634-637 行):
- 如果 value 是 null 或空字符串,且 info 不是
must或不以require开头,则跳过验证
- 如果 value 是 null 或空字符串,且 info 不是
-
R5-6:场景重置行为(对齐 PHP
getScene第 1661 行):- 切换场景时,
only/append/remove全部重置
- 切换场景时,
§架构说明
框架层内置基础规则(require/must/is)。
完整内置规则集在 validate/rules.rs 中
(email/mobile/url/in/notIn/max/min/length 等)。
自定义规则通过 Validate::extend 注册,回调签名:
Fn(value: &Value, rule: &str, data: &Value) -> bool + Send + Sync
§PHP 源码参考
e:\vue\test\鲜视达\server\vendor\topthink\framework\src\think\Validate.php- 第 24-215 行:类属性定义
- 第 286-298 行:
rule()方法 - 第 354-360 行:
scene()方法 - 第 471-540 行:
check()方法 - 第 594-669 行:
checkItem()方法 - 第 678-706 行:
getValidateType()方法 - 第 827-888 行:
is()方法 - 第 1536-1554 行:
getDataValue()方法 - 第 1565-1586 行:
getRuleMsg()方法 - 第 1596-1633 行:
parseErrorMsg()方法
Modules§
- message
- 错误消息国际化 — 对齐 PHP
think\Lang和think\Validate::parseErrorMsg - rules
- 内置验证规则 — 对齐 PHP
think\Validate类的内置规则方法 - scene
- 验证场景 — 对齐 PHP
think\Validate的场景机制
Structs§
- Validate
- 验证器 — 对齐 PHP
think\Validate
Enums§
- Rule
- 验证规则表示
- Validate
Error - 验证错误
Statics§
- ALIAS
- PHP
think\Validate::$alias验证类型别名 - DEFAULT_
REGEX - PHP
think\Validate::$defaultRegex内置正则 - TYPE_
MSG - PHP
think\Validate::$typeMsg内置类型提示
Type Aliases§
- Rule
Callback - 自定义规则回调类型