Код: Выделить всё
>>> Animal(species="antelope")
>>> Animal(species="AnTeLoPe")
>>> Animal(species="frog") # ValidationError: `species must be antelope or zebra`
Код: Выделить всё
import pydantic
from typing import Literal
class Animal(pydantic.BaseModel):
species: Literal["antelope", "zebra"]
Код: Выделить всё
class Animal(pydantic.BaseModel):
species: pydantic.constr(to_lower=True)
p>
Код: Выделить всё
class Animal(pydantic.BaseModel):
species: Literal["antelope", "Antelope"] # ...etc
Код: Выделить всё
class Animal(pydantic.BaseModel):
model_config = pydantic.ConfigDict(str_to_lower=True)
species: Literal["antelope", "zebra"]
Animal(species="AnTeLope") # ValidationError
Подробнее здесь: https://stackoverflow.com/questions/793 ... lower-case