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 new(error_type: &'static str, loc: Vec<LocItem>) -> ValidationError
pub fn new(error_type: &'static str, loc: Vec<LocItem>) -> ValidationError
Create a new validation error.
Sourcepub fn missing(loc: Vec<LocItem>) -> ValidationError
pub fn missing(loc: Vec<LocItem>) -> ValidationError
Create a “missing” error for a required field.
Sourcepub fn string_too_short(loc: Vec<LocItem>, min_length: usize) -> ValidationError
pub fn string_too_short(loc: Vec<LocItem>, min_length: usize) -> ValidationError
Create a “string_too_short” error.
Sourcepub fn string_too_long(loc: Vec<LocItem>, max_length: usize) -> ValidationError
pub fn string_too_long(loc: Vec<LocItem>, max_length: usize) -> ValidationError
Create a “string_too_long” error.
Sourcepub fn type_error(
loc: Vec<LocItem>,
expected_type: &'static str,
) -> ValidationError
pub fn type_error( loc: Vec<LocItem>, expected_type: &'static str, ) -> ValidationError
Create a type error (e.g., expected int, got string).
Sourcepub fn json_invalid(
loc: Vec<LocItem>,
message: impl Into<String>,
) -> ValidationError
pub fn json_invalid( loc: Vec<LocItem>, message: impl Into<String>, ) -> ValidationError
Create a JSON parsing error.
Sourcepub fn with_msg(self, msg: impl Into<String>) -> ValidationError
pub fn with_msg(self, msg: impl Into<String>) -> ValidationError
Set the human-readable message.
Sourcepub fn with_input(self, input: Value) -> ValidationError
pub fn with_input(self, input: Value) -> ValidationError
Set the input value.
Sourcepub fn with_ctx_value(
self,
key: impl Into<String>,
value: Value,
) -> ValidationError
pub fn with_ctx_value( self, key: impl Into<String>, value: Value, ) -> ValidationError
Add a context key-value pair.
Sourcepub fn with_ctx(self, ctx: HashMap<String, Value>) -> ValidationError
pub fn with_ctx(self, ctx: HashMap<String, Value>) -> ValidationError
Set the full context map.
Sourcepub fn with_loc_prefix(self, prefix: Vec<LocItem>) -> ValidationError
pub fn with_loc_prefix(self, prefix: Vec<LocItem>) -> ValidationError
Add location items to the path.
Sourcepub fn with_loc_suffix(self, item: impl Into<LocItem>) -> ValidationError
pub fn with_loc_suffix(self, item: impl Into<LocItem>) -> ValidationError
Append a location item to the path.
Sourcepub fn greater_than_equal<T>(loc: Vec<LocItem>, min: T) -> ValidationErrorwhere
T: Display,
pub fn greater_than_equal<T>(loc: Vec<LocItem>, min: T) -> ValidationErrorwhere
T: Display,
Create a “greater_than_equal” error for minimum value constraint.
Sourcepub fn less_than_equal<T>(loc: Vec<LocItem>, max: T) -> ValidationErrorwhere
T: Display,
pub fn less_than_equal<T>(loc: Vec<LocItem>, max: T) -> ValidationErrorwhere
T: Display,
Create a “less_than_equal” error for maximum value constraint.
Sourcepub fn pattern_mismatch(loc: Vec<LocItem>, pattern: &str) -> ValidationError
pub fn pattern_mismatch(loc: Vec<LocItem>, pattern: &str) -> ValidationError
Create a “string_pattern_mismatch” error for regex pattern constraint.
Sourcepub fn invalid_email(loc: Vec<LocItem>) -> ValidationError
pub fn invalid_email(loc: Vec<LocItem>) -> ValidationError
Create a “value_error” for invalid email format.
Sourcepub fn invalid_url(loc: Vec<LocItem>) -> ValidationError
pub fn invalid_url(loc: Vec<LocItem>) -> ValidationError
Create a “value_error” for invalid URL format.
Sourcepub fn invalid_uuid(loc: Vec<LocItem>) -> ValidationError
pub fn invalid_uuid(loc: Vec<LocItem>) -> ValidationError
Create a “value_error” for invalid UUID format.
Sourcepub fn value_error(loc: Vec<LocItem>, msg: impl Into<String>) -> ValidationError
pub fn value_error(loc: Vec<LocItem>, msg: impl Into<String>) -> ValidationError
Create a generic “value_error” with custom message.
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>(&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)