pub struct CustomFnRule<T: ?Sized, F>{
pub rule_name: String,
pub validator: Arc<F>,
pub message: Option<String>,
/* private fields */
}Expand description
Custom function validation rule - validates using a user-provided function.
§Example
use skp_validator_rules::custom::custom_fn::CustomFnRule;
use skp_validator_core::{Rule, ValidationContext};
let rule = CustomFnRule::new("is_even", |value: &i32, _ctx| {
value % 2 == 0
}).message("Value must be even");
let ctx = ValidationContext::default();
assert!(rule.validate(&4, &ctx).is_ok());
assert!(rule.validate(&3, &ctx).is_err());Fields§
§rule_name: StringRule name for error reporting
validator: Arc<F>The validation function
message: Option<String>Custom error message
Implementations§
Trait Implementations§
Source§impl<T: ?Sized, F> Clone for CustomFnRule<T, F>
impl<T: ?Sized, F> Clone for CustomFnRule<T, F>
Source§impl<T: ?Sized, F> Debug for CustomFnRule<T, F>
impl<T: ?Sized, F> Debug for CustomFnRule<T, F>
Source§impl<T: ?Sized, F> Rule<T> for CustomFnRule<T, F>
impl<T: ?Sized, F> Rule<T> for CustomFnRule<T, F>
Source§fn validate(&self, value: &T, ctx: &ValidationContext) -> ValidationResult<()>
fn validate(&self, value: &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).
Auto Trait Implementations§
impl<T, F> Freeze for CustomFnRule<T, F>where
T: ?Sized,
impl<T, F> RefUnwindSafe for CustomFnRule<T, F>where
F: RefUnwindSafe,
T: ?Sized,
impl<T, F> Send for CustomFnRule<T, F>where
T: ?Sized,
impl<T, F> Sync for CustomFnRule<T, F>where
T: ?Sized,
impl<T, F> Unpin for CustomFnRule<T, F>where
T: ?Sized,
impl<T, F> UnwindSafe for CustomFnRule<T, F>where
F: RefUnwindSafe,
T: ?Sized,
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