Given a FastAPI Pydantic model MessageWrapper *untested:
class MessageWrapper(BaseModel):
id: str = ids.get_time_sequential_uuid(datetime.datetime.utcnow())
body: Union[DataMin, DataMax] = None
created_by_app: Optional[AnyStr]
created_by_app_version: Optional[AnyStr]
created_at_utc: datetime = datetime.datetime.utcnow()
The MessageWrapper.body property expects a DataMin or DataMax model. How can I change this to take either an unvalidated standard Dict or ANY model (not just DataMin or DataMax).
My initial attempts ran into quite a few scenarios where Pydantic threw warnings for not properly defining the type of the body property. I expect to either pass standard dicts or models through this wrapper prior to the data being supplied through an API endpoint.
Ultimately the compiled nested dict would be received by the requestor and they could always expect to access the top level fields, while the body field could be any nested dictionary.
I am interested in functionality similar to Maxwell's Daemon where json data is inserted into a standard dictionary that includes metadata about the message itself.
It seems there would be times when it is OK to designate a property with Type (from typing import Type) to avoid validation, and other times where we'd always validate on all properties.
How can I create a Model that will take any other model or standard dict as a property using Pydantic's BaseModel?