pub struct StringSpecialCharRules {
pub must_have_uppercase: bool,
pub must_have_lowercase: bool,
pub must_have_special_chars: bool,
pub must_have_digit: bool,
}Expand description
A structure that defines rules for validating the presence of characters in a string. This can be used to enforce certain validation criteria for strings containing uppercase letters, lowercase letters, special characters, and numeric digits.
§Fields
-
must_have_uppercase- A boolean flag indicating whether the string must contain at least one uppercase letter (trueif required,falseotherwise). -
must_have_lowercase- A boolean flag indicating whether the string must contain at least one lowercase letter (trueif required,falseotherwise). -
must_have_special_chars- A boolean flag indicating whether the string must contain at least one special character (e.g.,!,@,#, etc.) (trueif required,falseotherwise). -
must_have_digit- A boolean flag indicating whether the string must contain at least one numeric digit (trueif required,falseotherwise).
§Default Implementation
By default, all fields are set to false, meaning no specific character requirements
will be enforced unless explicitly configured.
This structure can be used in validation logic where customizable character rules are required, such as password or input string checks.
Fields§
§must_have_uppercase: bool§must_have_lowercase: bool§must_have_special_chars: bool§must_have_digit: boolImplementations§
Source§impl StringSpecialCharRules
impl StringSpecialCharRules
Sourcepub fn check(
&self,
messages: &mut ValidateErrorCollector,
subject: &StringValidator<'_>,
)
pub fn check( &self, messages: &mut ValidateErrorCollector, subject: &StringValidator<'_>, )
Validates a string based on multiple constraints such as the presence of special characters,
uppercase and lowercase letters, and digits. If any constraint is not met, an error
message along with the corresponding error locale is added to the provided ValidateErrorCollector.
§Parameters
messages: A mutable reference to aValidateErrorCollector, which collects validation errors encountered during the checks.subject: A reference to aStringValidatorobject, which provides methods to check various string properties based on constraints.
§Behavior
- If
must_have_special_charsis true, the method checks whether the subject contains at least one special character. If not, an error is added tomessages. - If both
must_have_uppercaseandmust_have_lowercaseare true, the method verifies that the subject has at least one uppercase and one lowercase letter. An error is added if this condition is not met. - If only
must_have_uppercaseis true, the method ensures the presence of at least one uppercase letter in the subject, adding an error if the condition fails. - If only
must_have_lowercaseis true, the method ensures the presence of at least one lowercase letter in the subject, adding an error if the condition fails. - If
must_have_digitis true, the method checks that the subject contains at least one numeric digit. If not, an error is added tomessages.
§Error Handling
Each validation failure results in an entry being added to the ValidateErrorCollector,
consisting of an error message string and a corresponding locale represented by StringSpecialCharLocale.
§Example
use cjtoolkit_structured_validator::common::locale::ValidateErrorCollector;
use cjtoolkit_structured_validator::common::string_validator::StrValidationExtension;
use cjtoolkit_structured_validator::base::string_rules::StringSpecialCharRules;
let mut errors = ValidateErrorCollector::new();
let validator = "Password123!".as_string_validator();
let rules = StringSpecialCharRules {
must_have_special_chars: true,
must_have_uppercase: true,
must_have_lowercase: true,
must_have_digit: true,
};
rules.check(&mut errors, &validator);
if errors.is_empty() {
println!("Validation passed!");
} else {
println!("Validation failed with errors");
}