You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to set a format field in the JSON schema on generic elements of array. I think the best way to describe it is with an example; I have a generic model Samples[T], which is currently implemented as:
I would like to add a format field into the items element of this schema (giving "items": {"type": "string", "format": "my-format"}) in a generic way. So far I've tried:
Customising the schema by implementing a config on Samples with a schema_extra static function (as per the docs) which targets a specific case (schema["properties"]["samples"]["items"]["format"] = "my-format"), but this doesn't generalise for any T (T may be a nested array of strings which also require specifying format on their elements).
Using a BaseModel with a __root__ element for T and implementing a config with a schema_extra on that. This would be the ideal solution, but it promotes the type of items to object, rather than string (this causes issues with FastAPI either which is my target in this case).
I could create a Pydantic model factory and dynamically create schema_extra functions which do the appropriate changes to each specific model (with the aid of some arguments to the factory), this seems like an ugly solution though. Is there anything else I can try?
The text was updated successfully, but these errors were encountered:
I'm trying to set a
format
field in the JSON schema on generic elements of array. I think the best way to describe it is with an example; I have a generic modelSamples[T]
, which is currently implemented as:I can produce the following JSON schema from a concrete type of this (say
Samples[str]
) which gives:I would like to add a
format
field into theitems
element of this schema (giving"items": {"type": "string", "format": "my-format"}
) in a generic way. So far I've tried:Samples
with aschema_extra
static function (as per the docs) which targets a specific case (schema["properties"]["samples"]["items"]["format"] = "my-format"
), but this doesn't generalise for anyT
(T
may be a nested array of strings which also require specifyingformat
on their elements).BaseModel
with a__root__
element forT
and implementing a config with aschema_extra
on that. This would be the ideal solution, but it promotes the type ofitems
toobject
, rather thanstring
(this causes issues with FastAPI either which is my target in this case).I could create a Pydantic model factory and dynamically create
schema_extra
functions which do the appropriate changes to each specific model (with the aid of some arguments to the factory), this seems like an ugly solution though. Is there anything else I can try?The text was updated successfully, but these errors were encountered: