pub struct LengthRule {
pub min: Option<usize>,
pub max: Option<usize>,
pub equal: Option<usize>,
pub mode: LengthMode,
pub message: Option<String>,
}Expand description
String/collection length validation rule.
§Example
use skp_validator_rules::string::length::LengthRule;
use skp_validator_core::{Rule, ValidationContext};
let rule = LengthRule::new().min(3).max(50);
let ctx = ValidationContext::default();
assert!(rule.validate("hello", &ctx).is_ok());
assert!(rule.validate("ab", &ctx).is_err()); // Too shortFields§
§min: Option<usize>Minimum length (inclusive)
max: Option<usize>Maximum length (inclusive)
equal: Option<usize>Exact length (if set, min/max are ignored)
mode: LengthModeLength calculation mode
message: Option<String>Custom error message
Implementations§
Source§impl LengthRule
impl LengthRule
Sourcepub fn mode(self, mode: LengthMode) -> Self
pub fn mode(self, mode: LengthMode) -> Self
Set length calculation mode.
Trait Implementations§
Source§impl Clone for LengthRule
impl Clone for LengthRule
Source§fn clone(&self) -> LengthRule
fn clone(&self) -> LengthRule
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for LengthRule
impl Debug for LengthRule
Source§impl Default for LengthRule
impl Default for LengthRule
Source§impl Rule<String> for LengthRule
impl Rule<String> for LengthRule
Source§fn validate(
&self,
value: &String,
ctx: &ValidationContext,
) -> ValidationResult<()>
fn validate( &self, value: &String, ctx: &ValidationContext, ) -> ValidationResult<()>
Validate the value.
Source§fn default_message(&self) -> String
fn default_message(&self) -> String
Get the default error message.
Source§fn validate_at(
&self,
value: &T,
path: &FieldPath,
ctx: &ValidationContext,
) -> Result<(), ValidationErrors>
fn validate_at( &self, value: &T, path: &FieldPath, ctx: &ValidationContext, ) -> Result<(), ValidationErrors>
Validate the value with a field path for error reporting.
Source§fn error_code(&self) -> String
fn error_code(&self) -> String
Get the error code.
Source§fn is_transform(&self) -> bool
fn is_transform(&self) -> bool
Check if this rule is a transformation (not validation).
Source§impl<T> Rule<Vec<T>> for LengthRule
impl<T> Rule<Vec<T>> for LengthRule
Source§fn validate(
&self,
value: &Vec<T>,
_ctx: &ValidationContext,
) -> ValidationResult<()>
fn validate( &self, value: &Vec<T>, _ctx: &ValidationContext, ) -> ValidationResult<()>
Validate the value.
Source§fn default_message(&self) -> String
fn default_message(&self) -> String
Get the default error message.
Source§fn validate_at(
&self,
value: &T,
path: &FieldPath,
ctx: &ValidationContext,
) -> Result<(), ValidationErrors>
fn validate_at( &self, value: &T, path: &FieldPath, ctx: &ValidationContext, ) -> Result<(), ValidationErrors>
Validate the value with a field path for error reporting.
Source§fn error_code(&self) -> String
fn error_code(&self) -> String
Get the error code.
Source§fn is_transform(&self) -> bool
fn is_transform(&self) -> bool
Check if this rule is a transformation (not validation).
Source§impl Rule<str> for LengthRule
impl Rule<str> for LengthRule
Source§fn validate(
&self,
value: &str,
_ctx: &ValidationContext,
) -> ValidationResult<()>
fn validate( &self, value: &str, _ctx: &ValidationContext, ) -> ValidationResult<()>
Validate the value.
Source§fn default_message(&self) -> String
fn default_message(&self) -> String
Get the default error message.
Source§fn validate_at(
&self,
value: &T,
path: &FieldPath,
ctx: &ValidationContext,
) -> Result<(), ValidationErrors>
fn validate_at( &self, value: &T, path: &FieldPath, ctx: &ValidationContext, ) -> Result<(), ValidationErrors>
Validate the value with a field path for error reporting.
Source§fn error_code(&self) -> String
fn error_code(&self) -> String
Get the error code.
Source§fn is_transform(&self) -> bool
fn is_transform(&self) -> bool
Check if this rule is a transformation (not validation).
Auto Trait Implementations§
impl Freeze for LengthRule
impl RefUnwindSafe for LengthRule
impl Send for LengthRule
impl Sync for LengthRule
impl Unpin for LengthRule
impl UnwindSafe for LengthRule
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