StringSpecialCharRules

Struct StringSpecialCharRules 

Source
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 (true if required, false otherwise).

  • must_have_lowercase - A boolean flag indicating whether the string must contain at least one lowercase letter (true if required, false otherwise).

  • must_have_special_chars - A boolean flag indicating whether the string must contain at least one special character (e.g., !, @, #, etc.) (true if required, false otherwise).

  • must_have_digit - A boolean flag indicating whether the string must contain at least one numeric digit (true if required, false otherwise).

§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: bool

Implementations§

Source§

impl StringSpecialCharRules

Source

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 a ValidateErrorCollector, which collects validation errors encountered during the checks.
  • subject: A reference to a StringValidator object, which provides methods to check various string properties based on constraints.
§Behavior
  • If must_have_special_chars is true, the method checks whether the subject contains at least one special character. If not, an error is added to messages.
  • If both must_have_uppercase and must_have_lowercase are 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_uppercase is 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_lowercase is 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_digit is true, the method checks that the subject contains at least one numeric digit. If not, an error is added to messages.
§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");
}

Trait Implementations§

Source§

impl Default for StringSpecialCharRules

Source§

fn default() -> StringSpecialCharRules

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,