pub struct StringMandatoryRules {
pub is_mandatory: bool,
}
Expand description
A struct representing rules for mandatory string fields.
This struct is designed to hold the configuration for whether a particular string field is mandatory or optional. It provides a simple boolean flag indicating the “mandatory” nature of the string.
§Fields
is_mandatory
- A boolean field that determines whether the string is mandatory.
- When set to
true
, the associated string must be provided. - When set to
false
, the associated string is optional.
§Traits
- The
Default
trait is implemented for this struct, allowing you to create a default instance whereis_mandatory
is set tofalse
.
Fields§
§is_mandatory: bool
Implementations§
Source§impl StringMandatoryRules
impl StringMandatoryRules
Sourcepub fn check(
&self,
messages: &mut ValidateErrorCollector,
subject: &StringValidator<'_>,
)
pub fn check( &self, messages: &mut ValidateErrorCollector, subject: &StringValidator<'_>, )
Validates a string based on the provided StringValidator
and collects validation errors.
§Parameters
messages
: A mutable reference to aValidateErrorCollector
that accumulates validation errors encountered during the check.subject
: A reference to aStringValidator
representing the string to be validated.
§Behavior
- If the
self.is_mandatory
field istrue
and thesubject
is empty, an error message with the text"Cannot be empty"
is pushed into themessages
collector along with a locale identifier (StringMandatoryLocale
).
§Example
use cjtoolkit_structured_validator::common::locale::ValidateErrorCollector;
use cjtoolkit_structured_validator::common::string_validator::StrValidationExtension;
use cjtoolkit_structured_validator::base::string_rules::StringMandatoryRules;
let mut messages = ValidateErrorCollector::new();
let validator = StringMandatoryRules { is_mandatory: true };
let subject = "".as_string_validator();
validator.check(&mut messages, &subject);
assert_eq!(messages.len(), 1); // If the subject is empty and is_mandatory is true, an error will be collected.
Trait Implementations§
Source§impl Default for StringMandatoryRules
impl Default for StringMandatoryRules
Source§fn default() -> StringMandatoryRules
fn default() -> StringMandatoryRules
Returns the “default value” for a type. Read more
Source§impl Into<StringMandatoryRules> for &EmailRules
Available on crate feature email
only.
impl Into<StringMandatoryRules> for &EmailRules
Available on crate feature
email
only.Source§fn into(self) -> StringMandatoryRules
fn into(self) -> StringMandatoryRules
Converts this type into the (usually inferred) input type.
Source§impl Into<StringMandatoryRules> for &UrlRules
Available on crate feature url
only.
impl Into<StringMandatoryRules> for &UrlRules
Available on crate feature
url
only.Source§fn into(self) -> StringMandatoryRules
fn into(self) -> StringMandatoryRules
Converts this type into the (usually inferred) input type.
Auto Trait Implementations§
impl Freeze for StringMandatoryRules
impl RefUnwindSafe for StringMandatoryRules
impl Send for StringMandatoryRules
impl Sync for StringMandatoryRules
impl Unpin for StringMandatoryRules
impl UnwindSafe for StringMandatoryRules
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more