I have several string fields:
class Object(BaseModel):
color: str
shape: str
I would wish to validate the value of these fields, since they have a definite format, but the set of values is not finite, so an enum does not work.
In this example:
color
can be not only"red"
or"blue"
but also e.g. hex codes#ffc8a0
, …shape
can be not only"square"
,"cube"
,"hexagon"
but also"9-gon"
,"135-gon"
, …
So I would want to have
class Color(BaseModel):
...
class Shape(BaseModel):
...
class Object(BaseModel):
color: Color
shape: Shape
But if I have
class Color(BaseModel):
name: str | None
hex: str
I still want to be able to parse "red"
and get Color(name="red", hex="#ff0000")
instead of having to parse {"name": "red", "hex": "#ff0000"}
.