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 new() -> ValidationErrors
pub fn new() -> ValidationErrors
Create empty validation errors.
Sourcepub fn single(error: ValidationError) -> ValidationErrors
pub fn single(error: ValidationError) -> ValidationErrors
Create from a single error.
Sourcepub fn from_errors(errors: Vec<ValidationError>) -> ValidationErrors
pub fn from_errors(errors: Vec<ValidationError>) -> ValidationErrors
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_body(self, body: Value) -> ValidationErrors
pub fn with_body(self, body: Value) -> ValidationErrors
Set the original body that failed validation.
Sourcepub fn with_debug_info(self, debug_info: DebugInfo) -> ValidationErrors
pub fn with_debug_info(self, debug_info: DebugInfo) -> ValidationErrors
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>) -> ValidationErrors
pub fn with_loc_prefix(self, prefix: Vec<LocItem>) -> ValidationErrors
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
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
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
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)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
Source§impl Extend<ValidationError> for ValidationErrors
impl Extend<ValidationError> for ValidationErrors
Source§fn extend<T>(&mut self, iter: T)where
T: IntoIterator<Item = ValidationError>,
fn extend<T>(&mut self, iter: T)where
T: IntoIterator<Item = ValidationError>,
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)