Validation error status code¶
Changing FastAPI's default 422 for request validation errors is not something
RouterVersioner does itself: it's a separate, general-purpose concern, handled by the
fastapi-validation-override package.
openapi_hook is the integration point: it lets you re-apply the same patch to every
per-version schema that RouterVersioner generates, so all of them, root schema included,
stay consistent.
from fastapi_validation_override import (
override_validation_error,
patch_422_responses,
)
# 1. Registers the runtime handler and patches the app's own root /openapi.json.
override_validation_error(app, status_code=400)
# 2. Re-applies the same patch to each version's own schema.
def versioning_openapi_hook(schema: dict, version: tuple[int, int]) -> dict:
return patch_422_responses(schema, "400")
RouterVersioner(
app=app,
routers=router,
version_format=VersionFormat.SEMVER,
openapi_hook=versioning_openapi_hook,
).versionize()
# Validation failures now return 400, both at runtime and in every schema
# (root, and every /vX_Y/openapi.json)