Skip to main content

ValidationContext

Struct ValidationContext 

Source
pub struct ValidationContext { /* private fields */ }
Expand description

Mutable state threaded through prepare and validate.

Implementations§

Source§

impl ValidationContext

Source

pub fn new() -> Self

Fresh context for JSON input.

Source

pub fn for_text() -> Self

Context for text input (query strings, forms, path segments).

Source

pub fn with_strict(self, strict: bool) -> Self

Force strict (true) or lax (false) mode for every model, like Pydantic’s model_validate(strict=...).

Source

pub fn at_root(self, item: impl Into<LocationItem>) -> Self

Prefix every reported location with item (e.g. "body").

Source

pub fn with_data<T: Any + Send + Sync>(self, value: T) -> Self

Attach user data readable by validators (Pydantic validation context).

Source

pub fn data<T: Any + Send + Sync>(&self) -> Option<&T>

User data of type T, if attached.

Source

pub fn config(&self) -> &ModelConfig

Settings of the model currently being processed.

Source

pub fn allows_text_coercion(&self) -> bool

Whether lax string → scalar coercion is allowed right now.

Source

pub fn is_strict(&self) -> bool

Effective strictness (override beats model config).

Source

pub fn input_kind(&self) -> InputKind

Input kind.

Source

pub fn with_config<R>( &mut self, config: ModelConfig, f: impl FnOnce(&mut Self) -> R, ) -> R

Run f with config installed, restoring the previous one after.

Source

pub fn at<R>( &mut self, item: impl Into<LocationItem>, f: impl FnOnce(&mut Self) -> R, ) -> R

Run f with item appended to the current location.

Source

pub fn location(&self) -> &[LocationItem]

Current location.

Source

pub fn error( &mut self, code: impl Into<Cow<'static, str>>, message: impl Into<String>, )

Record an error at the current location.

Source

pub fn push(&mut self, error: FieldError)

Record error, prefixing its location with the current location.

Source

pub fn check(&mut self, result: Result<(), FieldError>)

Record the result of a rule check at the current location.

Source

pub fn has_errors(&self) -> bool

Whether any error has been recorded.

Source

pub fn error_count(&self) -> usize

Number of errors recorded so far (useful to detect new ones).

Source

pub fn take_errors(&mut self) -> ValidationError

Take the collected errors, leaving the context empty of errors.

Trait Implementations§

Source§

impl Debug for ValidationContext

Source§

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

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

impl Default for ValidationContext

Source§

fn default() -> ValidationContext

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