pub struct Validator(/* private fields */);Expand description
A boxed, state-capturing field validator.
Unlike the deprecated FormValidator function pointer, a Validator
wraps a closure, so it can capture surrounding state — a compiled matcher,
a min/max pulled from config, or a sibling field’s value. Built-in
constructors live in the validators module.
You rarely construct one directly: FormField::validate accepts a closure
and boxes it for you. Use Validator::new when you need to build a
Validator value yourself.
§Example
let min = 3usize; // captured state — impossible with a fn pointer
let v = Validator::new(move |s: &str| {
if s.len() >= min { Ok(()) } else { Err(format!("min {min} chars")) }
});
assert!(v.run("hello").is_ok());
assert!(v.run("hi").is_err());Implementations§
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Validator
impl !Send for Validator
impl !Sync for Validator
impl !UnwindSafe for Validator
impl Freeze 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