Module length

Module length 

Source
Expand description

Length validation.

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

The concept of “length” is somewhat complicated, especially for strings. Therefore, the length rule currently supports different modes:

The mode is configured on the length rule:

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

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

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

impl garde::rules::length::HasSimpleLength for MyString {
    fn length(&self) -> usize {
        self.0.len()
    }
}

See each trait for more information.

Re-exports§

pub use bytes::HasBytes;
pub use chars::HasChars;
pub use simple::HasSimpleLength;
pub use utf16::HasUtf16CodeUnits;

Modules§

bytes
Implemented by types for which we can retrieve the number of bytes.
chars
Implemented by string-like types for which we can retrieve the number of Unicode Scalar Values.
simple
Implemented by types which have a known length.
utf16
Implemented by string-like types for which we can retrieve length in UTF-16 code units.