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
impl StringLengthRules
Sourcepub fn check(
&self,
messages: &mut ValidateErrorCollector,
subject: &StringValidator<'_>,
)
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 aValidateErrorCollectorfor storing validation error messages if any constraints are violated.subject- A reference to aStringValidatorthat provides the string to validate against the defined length rules.
§Behavior
- If a minimum length (
min_length) is specified viaselfand thesubjectstring’s grapheme count is less than the minimum; an error message is added to themessagescollector indicating that the string must be at least the specified number of characters. - If a maximum length (
max_length) is specified viaselfand thesubjectstring’s grapheme count exceeds the maximum, an error message is added to themessagescollector 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.