NumberMandatoryRules

Struct NumberMandatoryRules 

Source
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

Source

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 the Into<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 a ValidateErrorCollector, used to collect error messages if the validation fails.
  • subject: An optional input value of type T to be validated. If None and the rule is_mandatory is true, it triggers a validation error.
§Behavior
  • If is_mandatory is true and the subject is None (i.e., no value is provided):
    • The method appends an error message to messages, with the description "Cannot be empty" and a boxed NumberMandatoryLocale as additional context.
§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§

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.