RuleContext

Struct RuleContext 

Source
pub struct RuleContext {
    pub field_name: Option<Arc<str>>,
    pub parent_path: Path,
    pub value_debug: Option<String>,
}
Expand description

Context information available to validation rules.

RuleContext provides rules with information about the field being validated, enabling more helpful, context-aware error messages.

§Examples

use domainstack::{Rule, RuleContext, ValidationError, Path};

fn min_len_with_context(min: usize) -> Rule<str> {
    Rule::new(move |value: &str, ctx: &RuleContext| {
        if value.len() < min {
            ValidationError::single(
                ctx.parent_path.clone(),
                "min_length",
                format!(
                    "Field '{}' must be at least {} characters (got {})",
                    ctx.field_name.as_ref().map(|s| s.as_ref()).unwrap_or("unknown"),
                    min,
                    value.len()
                )
            )
        } else {
            ValidationError::default()
        }
    })
}

Fields§

§field_name: Option<Arc<str>>

The name of the field being validated, if known.

This is typically provided by the derive macro or when using the validate() helper. For ad-hoc validation, this may be None.

§parent_path: Path

The path from the root to the parent of the current field.

For example, when validating user.email, the parent_path would be "user". Rules should typically append their field_name to this path when creating errors.

§value_debug: Option<String>

Optional string representation of the current value for debugging.

This can be included in error messages to help users understand what value failed validation. For sensitive fields (passwords, tokens), this should be None.

Implementations§

Source§

impl RuleContext

Source

pub fn root(field_name: impl Into<Arc<str>>) -> Self

Creates a new RuleContext for a root-level field.

§Examples
use domainstack::RuleContext;

let ctx = RuleContext::root("email");
assert_eq!(ctx.field_name, Some("email".into()));
assert_eq!(ctx.parent_path.to_string(), "");
Source

pub fn anonymous() -> Self

Creates a new RuleContext without a field name.

Useful for ad-hoc validation where field information isn’t available.

§Examples
use domainstack::RuleContext;

let ctx = RuleContext::anonymous();
assert_eq!(ctx.field_name, None);
Source

pub fn child(&self, field_name: impl Into<Arc<str>>) -> Self

Creates a child context for a nested field.

§Examples
use domainstack::RuleContext;

let parent = RuleContext::root("user");
let child = parent.child("email");

assert_eq!(child.field_name, Some("email".into()));
assert_eq!(child.parent_path.to_string(), "user");
Source

pub fn with_value_debug(self, value: impl Into<String>) -> Self

Sets the debug representation of the value being validated.

§Examples
use domainstack::RuleContext;

let ctx = RuleContext::root("age")
    .with_value_debug("42");

assert_eq!(ctx.value_debug, Some("42".to_string()));
Source

pub fn full_path(&self) -> Path

Gets the full path to the current field.

Combines parent_path with field_name to produce the complete path.

§Examples
use domainstack::{RuleContext, Path};

let ctx = RuleContext::root("email");
assert_eq!(ctx.full_path(), Path::root().field("email"));

let parent = RuleContext::root("user");
let child = parent.child("email");
assert_eq!(child.full_path().to_string(), "user.email");

Trait Implementations§

Source§

impl Clone for RuleContext

Source§

fn clone(&self) -> RuleContext

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for RuleContext

Source§

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

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

impl Default for RuleContext

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.