Reference¶
override_validation_error
¶
override_validation_error(
app,
status_code=400,
handle_exceptions=True,
merge_target_response=True,
error_builder=None,
media_type="application/json",
)
Override FastAPI's default 422 validation error response with a custom status code, at runtime and in the OpenAPI schema.
The schema is patched on top of whatever the current app.openapi returns, so a custom
OpenAPI function assigned before this call is preserved, and its caching with it.
The first call configures the app. Subsequent calls on the same instance are ignored, even
with different arguments. Passing status_code=422 without error_builder and with the
default media_type is a no-op too: the app is left unpatched, as in FastAPI.
Raises TypeError if status_code is not an int (a bool is rejected too), or if
error_builder is async or its return annotation is not a Pydantic model. Raises
ValueError if status_code is outside 100-599 or can't carry a body, if media_type is
not a JSON content type, or if error_builder is passed with handle_exceptions=False.
Nothing is registered on the app when it raises.
| PARAMETER | DESCRIPTION |
|---|---|
app
|
The application to patch, in place.
TYPE:
|
status_code
|
The HTTP status code to use instead of 422. It must be a valid code (100-599) that carries a response body, so 1xx, 204 and 304 are rejected. An
TYPE:
|
handle_exceptions
|
If
TYPE:
|
merge_target_response
|
If
TYPE:
|
error_builder
|
A regular (not async) function that turns the
TYPE:
|
media_type
|
The content type of the validation error, e.g.
TYPE:
|
Source code in fastapi_validation_override/overrider.py
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 | |
patch_422_responses
¶
Move FastAPI's 422 validation error response to target_code, across paths, webhooks, and
callbacks (recursively, for callbacks declared on any operation). Returns the same schema,
mutated in place.
Designed for schemas produced by fastapi.openapi.utils.get_openapi() — the same generator
app.openapi() uses internally. It's exposed publicly so it can be patched outside
override_validation_error, e.g. from a custom app.openapi function or a tool that builds its
own per-version schema via get_openapi(). It is not meant as a general-purpose processor for
arbitrary or hand-authored OpenAPI documents: correctness on documents built by hand or by other
tools is best-effort, not a guarantee.
A parameter declared with include_in_schema=False still triggers validation at runtime, but
is invisible in the generated schema. If such a route also already occupies 422, 4XX, or
default with something else, FastAPI never emits its own 422 response for that operation —
the only signal this function relies on to detect the need for validation on such a parameter.
In that specific combination, target_code won't be documented.
| PARAMETER | DESCRIPTION |
|---|---|
schema
|
An OpenAPI schema produced by
TYPE:
|
target_code
|
The status code to document the validation error at, e.g.
TYPE:
|
merge_target_response
|
If
TYPE:
|