pub struct NumberMandatoryRules {
pub is_mandatory: bool,
}
Expand description
Represents a set of rules determining whether a number field or value is mandatory.
§Fields
is_mandatory
(bool
): Specifies whether the associated number is mandatory or not.true
: The number is required (mandatory).false
: The number is optional.
Fields§
§is_mandatory: bool
Implementations§
Source§impl NumberMandatoryRules
impl NumberMandatoryRules
Sourcepub fn check<T: Into<LocaleValue>>(
&self,
messages: &mut ValidateErrorCollector,
subject: Option<T>,
)
pub fn check<T: Into<LocaleValue>>( &self, messages: &mut ValidateErrorCollector, subject: Option<T>, )
Checks whether a given subject is valid based on the rules of the current instance
and collects any validation errors in the provided ValidateErrorCollector
.
§Type Parameters
T
: A type that implements theInto<LocaleValue>
trait, representing the value to be checked.
§Parameters
&self
: Immutable reference to the instance containing validation settings (is_mandatory
in this case).messages
: A mutable reference to aValidateErrorCollector
, used to collect error messages if the validation fails.subject
: An optional input value of typeT
to be validated. IfNone
and the ruleis_mandatory
istrue
, it triggers a validation error.
§Behavior
- If
is_mandatory
istrue
and thesubject
isNone
(i.e., no value is provided):- The method appends an error message to
messages
, with the description"Cannot be empty"
and a boxedNumberMandatoryLocale
as additional context.
- The method appends an error message to
§Example
use cjtoolkit_structured_validator::base::number_rules::NumberMandatoryRules;
use cjtoolkit_structured_validator::common::locale::ValidateErrorCollector;
let validator = NumberMandatoryRules { is_mandatory: true };
let mut errors = ValidateErrorCollector::new();
// Example with a subject as None (will cause validation error)
validator.check::<f64>(&mut errors, None);
assert_eq!(errors.len(), 1);
// Example with a valid subject (no validation error)
validator.check::<f64>(&mut errors, Some(1.0));
assert_eq!(errors.len(), 1); // No additional errors added.
§Note
- Ensure that
ValidateErrorCollector
is properly initialized and passed by mutable reference to capture errors. - The subject, when provided, must implement the
Into<LocaleValue>
trait for compatibility.
Auto Trait Implementations§
impl Freeze for NumberMandatoryRules
impl RefUnwindSafe for NumberMandatoryRules
impl Send for NumberMandatoryRules
impl Sync for NumberMandatoryRules
impl Unpin for NumberMandatoryRules
impl UnwindSafe for NumberMandatoryRules
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