pub struct Validate { /* private fields */ }Expand description
Implementations§
Source§impl Validate
impl Validate
Sourcepub fn rule(self, name: &str, rule: &str) -> Validate
pub fn rule(self, name: &str, rule: &str) -> Validate
添加字段验证规则
对齐 PHP rule($name, $rule = '')(第 286-298 行)
§参数
name:字段名(支持field|title格式指定字段描述)rule:验证规则(字符串形式,如"require|in:1,2,3")
Sourcepub fn message(self, messages: IndexMap<String, String>) -> Validate
pub fn message(self, messages: IndexMap<String, String>) -> Validate
设置提示信息
对齐 PHP message(array $message)(第 341-346 行)
Sourcepub fn field(self, fields: IndexMap<String, String>) -> Validate
pub fn field(self, fields: IndexMap<String, String>) -> Validate
设置字段描述
对齐 PHP rule() 方法中 $rule 为数组时合并到 $this->field
Sourcepub fn register_scene(self, name: &str, fields: Vec<String>) -> Validate
pub fn register_scene(self, name: &str, fields: Vec<String>) -> Validate
注册场景字段列表
对齐 PHP $this->scene[$name] = $fields 属性赋值
Sourcepub fn register_scene_callback(
self,
name: &str,
callback: Arc<dyn Fn(&mut Validate) + Send + Sync>,
) -> Validate
pub fn register_scene_callback( self, name: &str, callback: Arc<dyn Fn(&mut Validate) + Send + Sync>, ) -> Validate
注册场景回调 — 对齐 PHP protected function scene{Name}()
场景回调支持。回调签名 Fn(&mut Validate) + Send + Sync,
回调内部可调用 Validate::only_mut、Validate::append_mut、
Validate::remove_mut 修改场景状态。
§PHP 对齐
protected function sceneLogin()
{
return $this->only(['email']);
}Rust 等价:
use std::sync::Arc;
v.register_scene_callback("login", Arc::new(|v| {
v.only_mut(vec!["email".to_string()]);
}));§优先级
对齐 PHP getScene(第 1663-1668 行):回调优先于数组形式。
如果同一场景名同时注册了数组和回调,回调会被调用,数组被忽略。
Sourcepub fn has_scene(&self, name: &str) -> bool
pub fn has_scene(&self, name: &str) -> bool
判断是否存在某个验证场景
对齐 PHP hasScene(string $name)(第 368-371 行)
§PHP 行为
return isset($this->scene[$name]) || method_exists($this, 'scene' . $name);
Rust 实现:检查 scene 数组 或 scene_callbacks 映射
Sourcepub fn batch(self, batch: bool) -> Validate
pub fn batch(self, batch: bool) -> Validate
设置批量验证
对齐 PHP batch(bool $batch = true)(第 379-384 行)
Sourcepub fn only(self, fields: Vec<String>) -> Validate
pub fn only(self, fields: Vec<String>) -> Validate
指定需要验证的字段列表
对齐 PHP only(array $fields)(第 405-410 行)
Sourcepub fn remove(self, field: &str, rule: Option<Vec<String>>) -> Validate
pub fn remove(self, field: &str, rule: Option<Vec<String>>) -> Validate
移除某个字段的验证规则
对齐 PHP remove($field, $rule = null)(第 419-438 行)
§参数
field:字段名rule:要移除的规则列表(None表示移除所有规则)
Sourcepub fn append(self, field: &str, rule: Vec<String>) -> Validate
pub fn append(self, field: &str, rule: Vec<String>) -> Validate
追加某个字段的验证规则
对齐 PHP append($field, $rule = null)(第 447-462 行)
Sourcepub fn only_mut(&mut self, fields: Vec<String>)
pub fn only_mut(&mut self, fields: Vec<String>)
指定需要验证的字段列表(&mut self 版本)
对齐 PHP only(array $fields) 的 &mut self 语义,供 scene 回调使用。
对齐 PHP sceneXxx 方法内部调用 $this->only([...])。
Sourcepub fn remove_mut(&mut self, field: &str, rule: Option<Vec<String>>)
pub fn remove_mut(&mut self, field: &str, rule: Option<Vec<String>>)
移除某个字段的验证规则(&mut self 版本)
对齐 PHP remove($field, $rule = null) 的 &mut self 语义,供 scene 回调使用。
Sourcepub fn append_mut(&mut self, field: &str, rule: Vec<String>)
pub fn append_mut(&mut self, field: &str, rule: Vec<String>)
追加某个字段的验证规则(&mut self 版本)
对齐 PHP append($field, $rule = null) 的 &mut self 语义,供 scene 回调使用。
Sourcepub fn extend(
&mut self,
type_name: &str,
callback: Arc<dyn Fn(&Value, &str, &Value) -> bool + Send + Sync>,
) -> &mut Validate
pub fn extend( &mut self, type_name: &str, callback: Arc<dyn Fn(&Value, &str, &Value) -> bool + Send + Sync>, ) -> &mut Validate
注册验证类型(自定义规则回调)
对齐 PHP extend(string $type, callable $callback, string $message = null)(第 308-317 行)
Sourcepub fn set_lang(self, lang: Arc<dyn Lang>) -> Validate
pub fn set_lang(self, lang: Arc<dyn Lang>) -> Validate
设置多语言实例 — 对齐 PHP setLang(Lang $lang)
对齐 PHP Validate.php 第 252-255 行
§PHP 行为
public function setLang(Lang $lang)
{
$this->lang = $lang;
}§参数
lang:多语言实例(Arc<dyn Lang>)
§用法
use std::sync::Arc;
use sz_rust_infra_facade::validate::Validate;
use sz_rust_infra_facade::validate::message::{Lang, SimpleLang};
let lang: Arc<dyn Lang> = Arc::new(
SimpleLang::new().set("not conform to the rules", "不符合规则")
);
let v = Validate::new().set_lang(lang);Sourcepub fn get_data_value(data: &Value, key: &str) -> Value
pub fn get_data_value(data: &Value, key: &str) -> Value
获取数据值 — 对齐 PHP getDataValue
对齐 PHP Validate.php 第 1536-1554 行
§PHP 行为(R5-3)
- 数值型 key:返回 key 本身(PHP 怪异行为,复刻)
- 包含
.的 key:按多维数组访问 - 其他 key:返回
data[key]或 null
Sourcepub fn get_validate_type(
rule_type: &str,
rule_args: &str,
) -> (String, String, String)
pub fn get_validate_type( rule_type: &str, rule_args: &str, ) -> (String, String, String)
获取当前验证类型及规则 — 对齐 PHP getValidateType
对齐 PHP Validate.php 第 678-706 行
§PHP 行为(R5-4)
- 别名映射(
>→gt,>=→egt等) - 返回
(type, args, info):type:用于分发的回调名(考虑别名)args:规则参数info:原始规则名(用于 remove/append 匹配)
Sourcepub fn get_rule_msg(
&self,
field: &str,
title: &str,
type_name: &str,
rule: &str,
) -> String
pub fn get_rule_msg( &self, field: &str, title: &str, type_name: &str, rule: &str, ) -> String
获取验证规则的错误提示信息 — 对齐 PHP getRuleMsg
对齐 PHP Validate.php 第 1565-1586 行
§PHP 查找优先级(R5-1)
message[field.type]message[field]type_msg[type]- 如果 type 以
require开头,使用type_msg['require'] - 默认
$title . $this->lang->get('not conform to the rules')
§Lang 翻译
所有分支的 msg 在占位符替换前先经过 Self::parse_error_msg_with_lang
进行 Lang 翻译(对齐 PHP parseErrorMsg 第 1598-1602 行)。
默认分支直接调用 lang->get('not conform to the rules')(对齐 PHP
第 1578 行)。
Sourcepub fn parse_error_msg_with_lang(
&self,
msg: &str,
rule: &str,
title: &str,
) -> String
pub fn parse_error_msg_with_lang( &self, msg: &str, rule: &str, title: &str, ) -> String
解析错误提示(含 Lang 翻译) — 对齐 PHP parseErrorMsg
对齐 PHP Validate.php 第 1596-1633 行
§PHP 行为
- Lang 翻译(第 1598-1602 行,R5-7):
{%var}语法:lang->get(substr($msg, 2, -1))lang->has($msg):lang->get($msg)
- 占位符替换(第 1613-1630 行,R5-2):
:attribute→ title:1/:2/:3→ rule 按逗号分割后的前 3 个元素:rule→ rule 原值(仅当 msg 包含:rule时)
§无 Lang 实例时的行为
当 Validate::lang 为 None 时跳过翻译,直接执行占位符替换。
对齐 PHP 未注入 Lang 时的行为(PHP 中 $this->lang 必须存在,否则
parseErrorMsg 会致命错误;Rust 使用 Option 提供更安全的降级)。
Sourcepub fn parse_error_msg(msg: &str, rule: &str, title: &str) -> String
pub fn parse_error_msg(msg: &str, rule: &str, title: &str) -> String
解析错误提示 — 对齐 PHP parseErrorMsg 占位符替换部分
对齐 PHP Validate.php 第 1613-1630 行(不含 Lang 翻译)
§PHP 占位符替换(R5-2)
:attribute→ title:1/:2/:3→ rule 按逗号分割后的前 3 个元素:rule→ rule 原值(仅当 msg 包含:rule时)
§说明
本方法为静态方法,不包含 Lang 翻译。如需 Lang 翻译,请使用
Self::parse_error_msg_with_lang 实例方法。
Sourcepub fn get_error(&self) -> &ValidateError
pub fn get_error(&self) -> &ValidateError
获取错误信息 — 对齐 PHP getError()
Sourcepub fn require(value: &Value, _rule: &str) -> bool
pub fn require(value: &Value, _rule: &str) -> bool
必须验证 — 对齐 PHP require
对齐 PHP Validate.php 第 814-817 行(实际由 is 处理 require)
§行为
null→false- 空字符串
""→false - 字符串
"0"→true(PHP 特殊行为) - 其他非空值 →
true
Sourcepub fn must(value: &Value, _rule: &str) -> bool
pub fn must(value: &Value, _rule: &str) -> bool
必须验证(与 require 等价) — 对齐 PHP must
对齐 PHP Validate.php 第 814-817 行
Sourcepub fn is(value: &Value, rule: &str, _data: &Value) -> bool
pub fn is(value: &Value, rule: &str, _data: &Value) -> bool
验证字段值是否为有效格式 — 对齐 PHP is
对齐 PHP Validate.php 第 827-888 行
§支持的类型
require:必须accepted:接受(1/on/yes)date:有效日期boolean/bool:布尔值number:数字integer:整数float:浮点数alpha:字母alphaNum:字母数字alphaDash:字母数字下划线短横线chs:中文chsAlpha:中文或字母chsAlphaNum:中文或字母数字chsDash:中文字母数字下划线短横线mobile:手机号email:邮箱url:URLip:IP 地址macAddr:MAC 地址array:数组
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Validate
impl !UnwindSafe for Validate
impl Freeze for Validate
impl Send for Validate
impl Sync for Validate
impl Unpin for Validate
impl UnsafeUnpin for Validate
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.