StringLengthRules

Struct StringLengthRules 

Source
pub struct StringLengthRules {
    pub min_length: Option<usize>,
    pub max_length: Option<usize>,
}
Expand description

A structure representing rules for validating the length of a string.

This struct allows specifying optional minimum and maximum length constraints for a string. Both constraints are optional, meaning one or both of them can be unset depending on the requirements.

§Fields

  • min_length - An optional minimum length constraint for the string. If set, the string must have at least this many characters to pass validation.
  • max_length - An optional maximum length constraint for the string. If set, the string must not exceed this many characters to pass validation.

§Defaults

When derived using Default, both min_length and max_length will be set to None.

Fields§

§min_length: Option<usize>§max_length: Option<usize>

Implementations§

Source§

impl StringLengthRules

Source

pub fn check( &self, messages: &mut ValidateErrorCollector, subject: &StringValidator<'_>, )

Validates the length of a given string using the specified criteria for minimum and maximum lengths. If the string does not meet the specified length constraints, an error message is added to the validation error collector.

§Parameters
  • messages - A mutable reference to a ValidateErrorCollector for storing validation error messages if any constraints are violated.
  • subject - A reference to a StringValidator that provides the string to validate against the defined length rules.
§Behavior
  1. If a minimum length (min_length) is specified via self and the subject string’s grapheme count is less than the minimum; an error message is added to the messages collector indicating that the string must be at least the specified number of characters.
  2. If a maximum length (max_length) is specified via self and the subject string’s grapheme count exceeds the maximum, an error message is added to the messages collector indicating that the string must be at most the specified number of characters.
§Notes

This function assumes the count_graphemes method is available on the subject to properly count grapheme clusters, ensuring correctness when dealing with multibyte characters or special Unicode characters.

§Examples
use cjtoolkit_structured_validator::common::locale::ValidateErrorCollector;
use cjtoolkit_structured_validator::common::string_validator::StrValidationExtension;
use cjtoolkit_structured_validator::base::string_rules::StringLengthRules;
let mut messages = ValidateErrorCollector::new();
let validator = "example".as_string_validator();
let criteria = StringLengthRules { min_length: Some(5), max_length: Some(10) };

criteria.check(&mut messages, &validator);

assert!(messages.is_empty()); // The string "example" satisfies the length constraints.

Trait Implementations§

Source§

impl Default for StringLengthRules

Source§

fn default() -> StringLengthRules

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> ErasedDestructor for T
where T: 'static,