pub struct DependencyRule<T>where
T: ?Sized,{
pub field_name: String,
pub depends_on_field: Option<String>,
pub condition: Option<DependencyCondition>,
pub validator: Option<DependencyValidator<T>>,
pub required_when_met: bool,
pub message: Option<String>,
}Expand description
Field dependency validation rule - validates based on other field values.
§Example
use skp_validator_rules::custom::dependency::{DependencyRule, DependencyCondition};
use skp_validator_core::{Rule, ValidationContext};
// Shipping address is required when shipping_method is "delivery"
let rule = DependencyRule::<str>::new("shipping_address")
.depends_on("shipping_method", DependencyCondition::Equals("delivery".to_string()))
.then_required();Fields§
§field_name: StringThe field being validated
depends_on_field: Option<String>The field we depend on
condition: Option<DependencyCondition>The condition on the dependency field
validator: Option<DependencyValidator<T>>Validation to apply when condition is met
required_when_met: boolWhether field is required when condition is met
message: Option<String>Custom error message
Implementations§
Source§impl<T: ?Sized> DependencyRule<T>
impl<T: ?Sized> DependencyRule<T>
Sourcepub fn depends_on(
self,
field: impl Into<String>,
condition: DependencyCondition,
) -> Self
pub fn depends_on( self, field: impl Into<String>, condition: DependencyCondition, ) -> Self
Set the field this depends on and condition.
Sourcepub fn then_required(self) -> Self
pub fn then_required(self) -> Self
Make field required when condition is met.
Sourcepub fn then_validate<F>(self, validator: F) -> Self
pub fn then_validate<F>(self, validator: F) -> Self
Set custom validation when condition is met.
Trait Implementations§
Source§impl<T: ?Sized> Debug for DependencyRule<T>
impl<T: ?Sized> Debug for DependencyRule<T>
Source§impl Rule<String> for DependencyRule<String>
impl Rule<String> for DependencyRule<String>
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 Rule<str> for DependencyRule<str>
impl Rule<str> for DependencyRule<str>
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<T> Freeze for DependencyRule<T>where
T: ?Sized,
impl<T> !RefUnwindSafe for DependencyRule<T>
impl<T> Send for DependencyRule<T>where
T: ?Sized,
impl<T> Sync for DependencyRule<T>where
T: ?Sized,
impl<T> Unpin for DependencyRule<T>where
T: ?Sized,
impl<T> !UnwindSafe for DependencyRule<T>
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