This is the code I wrote
from typing import Any
from fastapi import APIRouter
from pydantic import BaseModel, ConfigDict, ValidationError
class State(BaseModel):
mode: str | None = None
alarm: int = 0
class StateLoose(State):
model_config = ConfigDict(extra='allow')
class StateExact(State):
model_config = ConfigDict(extra='forbid')
def validated_state(state: dict) -> State:
try:
return StateExact(**state)
except ValidationError as e:
logger.warning("Sanitized input state that caused a validation error. Error: %s", e)
return State(**state)
@router.put("/{client_id}", response_model=State)
def update_state(client_id: str, state: StateLoose) -> Any:
v_state = validated_state(state.dict()).dict()
return update_resource(client_id=client_id, state=v_state)
# Example State inputs
a = {"mode": "MANUAL", "alarm": 1}
b = {"mode": "MANUAL", "alarm": 1, "dog": "bau"}
normal = State(**a)
loose = StateLoose(**a)
exact = StateExact(**a)
From my understanding/tests with/of pydantic
- State "adapts" the input dict to fit the model and trows an exception only when something is very wrong (non-convertable type, missing required field). However, extra fields are lost.
- StateLoose, accepts extra fields and shows them by default (or with pydantic_extra)
- StateExact trows a ValidationError whenever something extra is provided
What I wanted to achieve is:
- Show the "State scheme as input" in the FastAPI generated Docs (this means having a State-like input in the "put function".
- Accept States that have extra elements but ignoring the extra elements (this means using State to remove extra args)
- Log a warning when extra elements are detected so that I can trace this since probably it means something went not as planned
To achieve this I was forced to create 3 different State classes and play with those. Since I plan to have lots of Models, I don't like the idea of having 3 versions of each and it feels like I am doing something quite wrong if it's so weird to accomplish.
Is there a less redundant way to:
- accept extra elements in a Model;
- use the Model as an input to FastAPI router.put;
- generate a warning;
- ignore extra elements and continue with the right ones?
You can use a
model_validatorwithmode="before"together with the field names from model - i.e. in your validator you see which fields got submitted and log those that you didn't expect.Setting
extra='ignore'as themodel_configwill also make sure that the extra fields gets removed automagically.Example requests:
As you can see,
bargets ignored when the Foo object is created.The second request will output
Log these fields: {'bar'}on the server side, while the first will keep quiet.