pub struct ValidationError {
pub error_type: &'static str,
pub loc: Vec<LocItem>,
pub msg: String,
pub input: Option<Value>,
pub ctx: Option<HashMap<String, Value>>,
}Expand description
A single validation error item.
This structure matches FastAPI/Pydantic v2’s validation error format exactly, allowing for seamless compatibility with clients expecting FastAPI responses.
§Examples
use fastapi_core::error::{ValidationError, loc, error_types};
use serde_json::json;
// Missing required field
let error = ValidationError::missing(loc::query("q"));
assert_eq!(error.error_type, "missing");
// String too short with context
let error = ValidationError::new(error_types::STRING_TOO_SHORT, loc::body_field("name"))
.with_msg("String should have at least 3 characters")
.with_input(json!("ab"))
.with_ctx_value("min_length", json!(3));Fields§
§error_type: &'static strError type identifier (e.g., “missing”, “string_too_short”).
loc: Vec<LocItem>Location path as a tuple of strings and integers.
Examples:
["query", "q"]for query parameter["body", "user", "email"]for nested body field["body", "items", 0, "name"]for array item
msg: StringHuman-readable error message.
input: Option<Value>The input value that failed validation.
This is the actual value the user provided that caused the error.
ctx: Option<HashMap<String, Value>>Additional context about the validation constraint.
Examples:
{"min_length": 3}for string_too_short{"ge": 0}for greater_than_equal{"pattern": "^\\d+$"}for pattern mismatch
Implementations§
Source§impl ValidationError
impl ValidationError
Sourcepub fn string_too_short(loc: Vec<LocItem>, min_length: usize) -> Self
pub fn string_too_short(loc: Vec<LocItem>, min_length: usize) -> Self
Create a “string_too_short” error.
Sourcepub fn string_too_long(loc: Vec<LocItem>, max_length: usize) -> Self
pub fn string_too_long(loc: Vec<LocItem>, max_length: usize) -> Self
Create a “string_too_long” error.
Sourcepub fn type_error(loc: Vec<LocItem>, expected_type: &'static str) -> Self
pub fn type_error(loc: Vec<LocItem>, expected_type: &'static str) -> Self
Create a type error (e.g., expected int, got string).
Sourcepub fn json_invalid(loc: Vec<LocItem>, message: impl Into<String>) -> Self
pub fn json_invalid(loc: Vec<LocItem>, message: impl Into<String>) -> Self
Create a JSON parsing error.
Sourcepub fn with_input(self, input: Value) -> Self
pub fn with_input(self, input: Value) -> Self
Set the input value.
Sourcepub fn with_ctx_value(self, key: impl Into<String>, value: Value) -> Self
pub fn with_ctx_value(self, key: impl Into<String>, value: Value) -> Self
Add a context key-value pair.
Sourcepub fn with_loc_prefix(self, prefix: Vec<LocItem>) -> Self
pub fn with_loc_prefix(self, prefix: Vec<LocItem>) -> Self
Add location items to the path.
Sourcepub fn with_loc_suffix(self, item: impl Into<LocItem>) -> Self
pub fn with_loc_suffix(self, item: impl Into<LocItem>) -> Self
Append a location item to the path.
Trait Implementations§
Source§impl Clone for ValidationError
impl Clone for ValidationError
Source§fn clone(&self) -> ValidationError
fn clone(&self) -> ValidationError
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for ValidationError
impl Debug for ValidationError
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)
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)