DependencyRule

Struct DependencyRule 

Source
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: String

The 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: bool

Whether field is required when condition is met

§message: Option<String>

Custom error message

Implementations§

Source§

impl<T: ?Sized> DependencyRule<T>

Source

pub fn new(field_name: impl Into<String>) -> Self

Create a new dependency rule.

Source

pub fn depends_on( self, field: impl Into<String>, condition: DependencyCondition, ) -> Self

Set the field this depends on and condition.

Source

pub fn then_required(self) -> Self

Make field required when condition is met.

Source

pub fn then_validate<F>(self, validator: F) -> Self
where F: Fn(&T, &ValidationContext) -> bool + Send + Sync + 'static,

Set custom validation when condition is met.

Source

pub fn message(self, msg: impl Into<String>) -> Self

Set custom error message.

Trait Implementations§

Source§

impl<T: ?Sized> Debug for DependencyRule<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Rule<String> for DependencyRule<String>

Source§

fn validate( &self, value: &String, ctx: &ValidationContext, ) -> ValidationResult<()>

Validate the value.
Source§

fn name(&self) -> &'static str

Get the rule name for error reporting.
Source§

fn default_message(&self) -> String

Get the default error message.
Source§

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

Get the error code.
Source§

fn is_transform(&self) -> bool

Check if this rule is a transformation (not validation).
Source§

impl Rule<str> for DependencyRule<str>

Source§

fn validate(&self, value: &str, ctx: &ValidationContext) -> ValidationResult<()>

Validate the value.
Source§

fn name(&self) -> &'static str

Get the rule name for error reporting.
Source§

fn default_message(&self) -> String

Get the default error message.
Source§

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

Get the error code.
Source§

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> 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.