pub struct ValidationErrors {
pub errors: Vec<ValidationError>,
pub body: Option<Value>,
pub debug_info: Option<DebugInfo>,
}Expand description
Collection of validation errors (422 Unprocessable Entity response).
This collects multiple validation errors from extractors and validators, producing a FastAPI-compatible error response format.
§Examples
use fastapi_core::error::{ValidationErrors, ValidationError, loc, error_types};
use serde_json::json;
let mut errors = ValidationErrors::new();
// Add multiple errors
errors.push(ValidationError::missing(loc::query("q")));
errors.push(ValidationError::string_too_short(loc::body_field("name"), 3)
.with_input(json!("ab")));
// Check and convert
if !errors.is_empty() {
let json = errors.to_json();
assert!(json.contains("missing"));
}Fields§
§errors: Vec<ValidationError>The collected errors.
body: Option<Value>The original input body (if available).
debug_info: Option<DebugInfo>Debug information (only included in response when debug mode is enabled).
Implementations§
Source§impl ValidationErrors
impl ValidationErrors
Sourcepub fn single(error: ValidationError) -> Self
pub fn single(error: ValidationError) -> Self
Create from a single error.
Sourcepub fn from_errors(errors: Vec<ValidationError>) -> Self
pub fn from_errors(errors: Vec<ValidationError>) -> Self
Create from multiple errors.
Sourcepub fn push(&mut self, error: ValidationError)
pub fn push(&mut self, error: ValidationError)
Add an error.
Sourcepub fn extend(&mut self, errors: impl IntoIterator<Item = ValidationError>)
pub fn extend(&mut self, errors: impl IntoIterator<Item = ValidationError>)
Add multiple errors.
Sourcepub fn with_debug_info(self, debug_info: DebugInfo) -> Self
pub fn with_debug_info(self, debug_info: DebugInfo) -> Self
Add debug information.
This information will only be included in the response when debug mode
is enabled (via enable_debug_mode()).
Sourcepub fn iter(&self) -> impl Iterator<Item = &ValidationError>
pub fn iter(&self) -> impl Iterator<Item = &ValidationError>
Get an iterator over the errors.
Sourcepub fn to_json_bytes(&self) -> Vec<u8> ⓘ
pub fn to_json_bytes(&self) -> Vec<u8> ⓘ
Convert to JSON bytes.
Sourcepub fn merge(&mut self, other: ValidationErrors)
pub fn merge(&mut self, other: ValidationErrors)
Merge another ValidationErrors into this one.
Sourcepub fn with_loc_prefix(self, prefix: Vec<LocItem>) -> Self
pub fn with_loc_prefix(self, prefix: Vec<LocItem>) -> Self
Add a prefix to all error locations.
Trait Implementations§
Source§impl Clone for ValidationErrors
impl Clone for ValidationErrors
Source§fn clone(&self) -> ValidationErrors
fn clone(&self) -> ValidationErrors
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for ValidationErrors
impl Debug for ValidationErrors
Source§impl Default for ValidationErrors
impl Default for ValidationErrors
Source§fn default() -> ValidationErrors
fn default() -> ValidationErrors
Returns the “default value” for a type. Read more
Source§impl Display for ValidationErrors
impl Display for ValidationErrors
Source§impl Error for ValidationErrors
impl Error for ValidationErrors
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
👎Deprecated since 1.42.0:
use the Display impl or to_string()
Source§impl Extend<ValidationError> for ValidationErrors
impl Extend<ValidationError> for ValidationErrors
Source§fn extend<T: IntoIterator<Item = ValidationError>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = ValidationError>>(&mut self, iter: T)
Extends a collection with the contents of an iterator. Read more
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
🔬This is a nightly-only experimental API. (
extend_one)Extends a collection with exactly one element.
Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
🔬This is a nightly-only experimental API. (
extend_one)Reserves capacity in a collection for the given number of additional elements. Read more
Source§impl FromIterator<ValidationError> for ValidationErrors
impl FromIterator<ValidationError> for ValidationErrors
Source§fn from_iter<T: IntoIterator<Item = ValidationError>>(iter: T) -> Self
fn from_iter<T: IntoIterator<Item = ValidationError>>(iter: T) -> Self
Creates a value from an iterator. Read more
Source§impl<'a> IntoIterator for &'a ValidationErrors
impl<'a> IntoIterator for &'a ValidationErrors
Source§impl IntoIterator for ValidationErrors
impl IntoIterator for ValidationErrors
Source§impl IntoResponse for ValidationErrors
impl IntoResponse for ValidationErrors
Source§fn into_response(self) -> Response
fn into_response(self) -> Response
Convert into a response.
Auto Trait Implementations§
impl Freeze for ValidationErrors
impl RefUnwindSafe for ValidationErrors
impl Send for ValidationErrors
impl Sync for ValidationErrors
impl Unpin for ValidationErrors
impl UnsafeUnpin for ValidationErrors
impl UnwindSafe for ValidationErrors
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, _span: NoopSpan) -> Self
fn instrument(self, _span: NoopSpan) -> Self
Instruments this future with a span (no-op when disabled).
Source§fn in_current_span(self) -> Self
fn in_current_span(self) -> Self
Instruments this future with the current span (no-op when disabled).