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: PathThe 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
impl RuleContext
Sourcepub fn root(field_name: impl Into<Arc<str>>) -> Self
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(), "");Sourcepub fn anonymous() -> Self
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);Sourcepub fn child(&self, field_name: impl Into<Arc<str>>) -> Self
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");Sourcepub fn with_value_debug(self, value: impl Into<String>) -> Self
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()));Sourcepub fn full_path(&self) -> Path
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
impl Clone for RuleContext
Source§fn clone(&self) -> RuleContext
fn clone(&self) -> RuleContext
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more