pub struct NumberRangeRules<T>{
pub min: Option<T>,
pub max: Option<T>,
}Expand description
A struct that represents rules for defining a range of numeric values with optional minimum and maximum bounds.
This struct is generic and can work with any type T that meets the following trait bounds:
Clone: The type can be cloned.Into<LocaleValue>: The type can be converted into aLocaleValue. This allows for localization support.Default: The type has a default value.PartialOrd: The type supports partial ordering, enabling comparisons like less than or greater than.Display: The type can be formatted as a string for display purposes.
§Fields
min(Option<T>): The optional lower bound of the range. IfNone, there is no restriction on the minimum value.max(Option<T>): The optional upper bound of the range. IfNone, there is no restriction on the maximum value.
Fields§
§min: Option<T>§max: Option<T>Implementations§
Source§impl<T> NumberRangeRules<T>
impl<T> NumberRangeRules<T>
Sourcepub fn check(&self, messages: &mut ValidateErrorCollector, subject: Option<T>)
pub fn check(&self, messages: &mut ValidateErrorCollector, subject: Option<T>)
Validates a given subject against optional minimum and maximum value constraints.
§Parameters
&self: A reference to the current instance of the object containing validation constraints.messages: A mutable reference to aValidateErrorCollector, where validation error messages will be stored if thesubjectdoes not meet the constraints.subject: An optional value of typeTto be validated against the constraints.
§Behavior
- If the
subjectisSome:- It checks whether the value is less than the optional minimum value (
self.min).- If the value is less, an error message is added to
messagesstating that the value must be at least the specified minimum.
- If the value is less, an error message is added to
- It checks whether the value is greater than the optional maximum value (
self.max).- If the value is greater, an error message is added to
messagesstating that the value must be at most the specified maximum.
- If the value is greater, an error message is added to
- It checks whether the value is less than the optional minimum value (
- If the
subjectisNone, the default value is used during validation (T::default()). - Does nothing if both
self.minandself.maxareNone.
§Usage
This function is intended to validate numerical ranges or similar constraints. The errors detected
during validation are collected into the ValidateErrorCollector provided in the messages parameter.
§Examples
use cjtoolkit_structured_validator::common::locale::ValidateErrorCollector;
use cjtoolkit_structured_validator::base::number_rules::NumberRangeRules;
let mut error_collector = ValidateErrorCollector::new();
let validator = NumberRangeRules::<usize> {
min: Some(10),
max: Some(100),
};
validator.check(&mut error_collector, Some(5)); // Value too small, error is added.
validator.check(&mut error_collector, Some(105)); // Value too large, error is added.
validator.check(&mut error_collector, Some(50)); // Valid value, no error.§Note
- It is assumed that
Timplements theDefault,PartialOrd, andClonetraits. - The
ValidateErrorCollectorandNumberRangeLocaletypes are expected to support the operations shown above.
Auto Trait Implementations§
impl<T> Freeze for NumberRangeRules<T>where
T: Freeze,
impl<T> RefUnwindSafe for NumberRangeRules<T>where
T: RefUnwindSafe,
impl<T> Send for NumberRangeRules<T>where
T: Send,
impl<T> Sync for NumberRangeRules<T>where
T: Sync,
impl<T> Unpin for NumberRangeRules<T>where
T: Unpin,
impl<T> UnsafeUnpin for NumberRangeRules<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for NumberRangeRules<T>where
T: UnwindSafe,
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