Skip to main content

Module validate

Module validate 

Source
Expand description

验证器模块 — 对齐 PHP think\Validate

本模块实现验证器框架,对齐 PHP think\Validate 类的 规则定义、批量验证、错误消息查找、场景管理等核心机制。

§PHP 对齐

§核心结构映射

PHP 字段Rust 字段说明
$ruleValidate::rule当前验证规则
$messageValidate::message验证提示信息
$fieldValidate::field字段描述
$typeMsgTYPE_MSG默认规则提示
$aliasALIAS验证类型别名
$defaultRegexDEFAULT_REGEX内置正则
$regexValidate::regex自定义正则
$sceneValidate::scene验证场景定义
$currentSceneValidate::current_scene当前验证场景
$batchValidate::batch是否批量验证
$onlyValidate::only场景需要验证的字段
$removeValidate::remove场景移除的规则
$appendValidate::append场景追加的规则
$errorValidate::error验证失败错误信息
$typeValidate::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-1getRuleMsg 查找优先级链(对齐 PHP 第 1565-1586 行):

    1. message[field.type]
    2. message[field]
    3. type_msg[type]
    4. 如果 type 以 require 开头,使用 type_msg['require']
    5. 默认 $title . lang->get('not conform to the rules')(无前导空格,对齐 PHP 第 1578 行)
  • R5-2parseErrorMsg 占位符替换(对齐 PHP 第 1596-1633 行):

    1. :attribute → title
    2. :1 / :2 / :3 → rule 按逗号分割后的前 3 个元素
    3. :rule → rule 原值(仅当 msg 包含 :rule 时)
  • R5-3getDataValue 行为(对齐 PHP 第 1536-1554 行):

    • 数值型 key 返回 key 本身(PHP 怪异行为,复刻)
    • 包含 . 的 key 按多维数组访问
    • 其他 key 返回 data[key] 或 null
  • R5-4getValidateType 类型推导(对齐 PHP 第 678-706 行):

    • 别名映射(>gt>=egt 等)
    • info 字段用于 remove/append 匹配
  • R5-5:空值跳过验证行为(对齐 PHP 第 634-637 行):

    • 如果 value 是 null 或空字符串,且 info 不是 must 或不以 require 开头,则跳过验证
  • 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\Langthink\Validate::parseErrorMsg
rules
内置验证规则 — 对齐 PHP think\Validate 类的内置规则方法
scene
验证场景 — 对齐 PHP think\Validate 的场景机制

Structs§

Validate
验证器 — 对齐 PHP think\Validate

Enums§

Rule
验证规则表示
ValidateError
验证错误

Statics§

ALIAS
PHP think\Validate::$alias 验证类型别名
DEFAULT_REGEX
PHP think\Validate::$defaultRegex 内置正则
TYPE_MSG
PHP think\Validate::$typeMsg 内置类型提示

Type Aliases§

RuleCallback
自定义规则回调类型