pub enum Validator {
MinLength(usize),
MaxLength(usize),
Min(f64),
Max(f64),
Email,
EmailLoose,
Phone,
PhoneLoose,
Url,
Regex(String),
Custom(ValidatorFn),
}Expand description
字段验证器
Variants§
MinLength(usize)
最小长度验证
MaxLength(usize)
最大长度验证
Min(f64)
最小值验证
Max(f64)
最大值验证
邮箱格式验证(严格模式)
EmailLoose
邮箱格式验证(宽松模式,向后兼容)
Phone
手机号格式验证(严格模式,E.164 格式)
PhoneLoose
手机号格式验证(宽松模式,向后兼容)
Url
URL 格式验证(仅校验协议前缀)
安全警告: 此验证器仅检查字符串是否以 http:// 或 https:// 开头,
不解析主机名、不校验端口、不过滤内网/保留地址,不提供 SSRF 防护。
以下输入均能通过本验证器:
http://169.254.169.254/latest/meta-data/(云元数据端点)http://127.0.0.1:6379/(本机 Redis)http://[::1]:8080/(IPv6 loopback)http://localhost/(本机服务)
若用于构造出站 HTTP 请求,必须在调用侧额外实施 SSRF 防护 (如校验解析后的 IP 不属于保留/内网段)。
Regex(String)
正则表达式验证
Custom(ValidatorFn)
自定义验证函数
Implementations§
Source§impl Validator
impl Validator
Sourcepub fn display_name(&self) -> &str
pub fn display_name(&self) -> &str
获取验证器的显示名称
Sourcepub fn validate(&self, field_name: &str, value: &Value) -> Result<(), BaseError>
pub fn validate(&self, field_name: &str, value: &Value) -> Result<(), BaseError>
验证字段值是否符合验证规则(无缓存回退)。
保留此方法以兼容未持有 crate::tools::Tools 的公开调用方:Email/Phone/Regex
变体在此路径下一次性编译、不入缓存。运行期写路径请优先使用
Validator::validate_with 传入共享 RegexCache。
Sourcepub fn validate_with(
&self,
field_name: &str,
value: &Value,
cache: &RegexCache,
) -> Result<(), BaseError>
pub fn validate_with( &self, field_name: &str, value: &Value, cache: &RegexCache, ) -> Result<(), BaseError>
使用共享正则缓存验证字段值。
Email/Phone 走缓存内预编译常量正则;Regex 走有界动态缓存,避免重复编译与 进程级无界缓存。
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Validator
impl<'de> Deserialize<'de> for Validator
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations§
impl !RefUnwindSafe for Validator
impl !UnwindSafe for Validator
impl Forget for Validator
impl Freeze for Validator
impl Move for Validator
impl Send for Validator
impl Sync for Validator
impl Unpin for Validator
impl UnsafeUnpin for Validator
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
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
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> ⓘ
Converts
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> ⓘ
Converts
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 more