Function rocket::form::validate::len

source ·
pub fn len<'v, V, L, R>(value: V, range: R) -> Result<'v, ()>
where V: Len<L>, L: Copy + PartialOrd, R: RangeBounds<L>,
Expand description

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

The value must implement Len. On error, returns an InvalidLength error. See Len for supported types and how their length is computed.

§Data Limits

All form types are constrained by a data limit. As such, the len() validator should be used only when a data limit is insufficiently specific. For example, prefer to use data Limits to validate the length of files as not doing so will result in writing more data to disk than necessary.


use rocket::http::ContentType;
use rocket::form::{FromForm, FromFormField};
use rocket::data::ToByteUnit;
use rocket::fs::TempFile;

struct Foo<'r> {
    #[field(validate = len(5..20))]
    name: &'r str,
    #[field(validate = len(..=200))]
    maybe_name: Option<&'r str>,
    #[field(validate = len(..=2.mebibytes()))]
    #[field(validate = ext(ContentType::Plain))]
    file: TempFile<'r>,