[go: up one dir, main page]

Module garde::rules::length

source ·
Expand description

Length validation.

#[derive(garde::Validate)]
struct Test {
    #[garde(length(min=1, max=100))]
    v: String,
}

The entrypoint is the Length trait. Implementing this trait for a type allows that type to be used with the #[garde(length(...))] rule.

The Length has a companion trait HasLength, which may be implemented for any container with a known length. Length is implemented for any T: HasLength.

In case of string types, HasLength::length should return the number of characters as opposed to the number of bytes. For validation of length counted in bytes, see the crate::rules::byte_length rule.

Here’s what implementing the trait for a custom string-like type might look like:

#[repr(transparent)]
struct MyString(String);

impl garde::rules::length::HasLength for MyString {
    fn length(&self) -> usize {
        self.0.chars().count()
    }
}

Enums

Traits

Functions