Expand description
Form field validation routines.
Each function in this module can be used as the target of the
field(validate)
field attribute of the FromForm
derive.
use rocket::form::FromForm;
#[derive(FromForm)]
struct MyForm<'r> {
#[field(validate = range(2..10))]
id: usize,
#[field(validate = omits("password"))]
password: &'r str,
}
The validate
parameter takes any expression that returns a
form::Result<()>
. If the expression is a function
call, a reference to the field is inserted as the first parameter. Thus,
functions calls to validate
must take a reference to some type,
typically a generic with some bounds, as their first argument.
§Custom Error Messages
To set a custom error messages, it is useful to chain results:
use rocket::form::{Errors, Error, FromForm};
#[derive(FromForm)]
struct MyForm<'r> {
// By defining another function...
#[field(validate = omits("password").map_err(pass_help))]
password: &'r str,
// or inline using the `msg` helper. `or_else` inverts the validator
#[field(validate = omits("password").or_else(msg!("please omit `password`")))]
password2: &'r str,
// You can even refer to the field in the message...
#[field(validate = range(1..).or_else(msg!("`{}` < 1", self.n)))]
n: isize,
// ..or other fields!
#[field(validate = range(..self.n).or_else(msg!("`{}` > `{}`", self.z, self.n)))]
z: isize,
}
fn pass_help<'a>(errors: Errors<'_>) -> Errors<'a> {
Error::validation("passwords can't contain the text \"password\"").into()
}
§Custom Validation
Custom validation routines can be defined as regular functions. Consider a routine that tries to validate a credit card number:
extern crate time;
use rocket::form::{self, FromForm, Error};
#[derive(FromForm)]
struct CreditCard {
#[field(validate = luhn(self.cvv, &self.expiration))]
number: u64,
cvv: u16,
expiration: time::Date,
}
// Implementation of Luhn validator.
fn luhn<'v>(number: &u64, cvv: u16, exp: &time::Date) -> form::Result<'v, ()> {
if !valid {
Err(Error::validation("invalid credit card number"))?;
}
Ok(())
}
Macros§
- msg
- A helper macro for custom validation error messages.
Traits§
Functions§
- contains
- Contains validator: succeeds when a value contains
item
. - dbg_
contains - Debug contains validator: like
contains()
but mentionsitem
in the error message. - dbg_eq
- Debug equality validator: like
eq()
but mentionsb
in the error message. - dbg_
omits - Debug omits validator: like
omits()
but mentionsitem
in the error message. - eq
- Equality validator: succeeds exactly when
a
==b
, usingPartialEq
. - ext
- File type validator: succeeds when a
TempFile
has the Content-Typecontent_type
. - len
- Length validator: succeeds when the length of a value is within a
range
. - neq
- Negative equality validator: succeeds exactly when
a
!=b
, usingPartialEq
. - omits
- Omits validator: succeeds when a value does not contains
item
. error message. - one_of
- Contains one of validator: succeeds when a value contains at least one item
in an
items
iterator. - range
- Integer range validator: succeeds when an integer value is within a range.
- try_
with - Try With validator: succeeds when an arbitrary function or closure does.
- with
- With validator: succeeds when an arbitrary function or closure does.