Module rocket::form::validate[][src]

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;

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};

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};

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"))?;




A helper macro for custom validation error messages.



Types for values that contain items.


Types for values that have a length.



Contains validator: succeeds when a value contains item.


Debug contains validator: like contains() but mentions item in the error message.


Debug equality validator: like eq() but mentions b in the error message.


Debug omits validator: like omits() but mentions item in the error message.


Equality validator: succeeds exactly when a == b, using PartialEq.


File type validator: succeeds when a TempFile has the Content-Type content_type.


Length validator: succeeds when the length of a value is within a range.


Negative equality validator: succeeds exactly when a != b, using PartialEq.


Omits validator: succeeds when a value does not contains item. error message.


Contains one of validator: succeeds when a value contains at least one item in an items iterator.


Integer range validator: succeeds when an integer value is within a range.


With validator: succeeds when an arbitrary function or closure does.