我从 Typescript 中知道一个名为Discriminate unions的概念。这是您放置 2 个结构(类等)的地方,类型取决于结构的值。我试图通过Pydantic验证在FastAPI中实现类似的事情。我可以收到两种不同的请求有效负载。是一个还是另一个取决于变量。如果是,则应由 验证,如果是,则应由 验证。我如何实现这一目标?找不到任何其他解决方案。accountType
creative
RegistrationPayloadCreative
brand
RegistrationPayloadBrand
问题是它要么返回
unexpected value; permitted: 'creative' (type=value_error.const; given=brand; permitted=('creative',))
或者它根本不起作用。
class RegistrationPayloadBase(BaseModel):
first_name: str
last_name: str
email: str
password: str
class RegistrationPayloadCreative(RegistrationPayloadBase):
accountType: Literal['creative']
class RegistrationPayloadBrand(RegistrationPayloadBase):
company: str
phone: str
vat: str
accountType: Literal['brand']
class A(BaseModel):
b: Union[RegistrationPayloadBrand, RegistrationPayloadCreative]
def main():
A(b={'first_name': 'sdf', 'last_name': 'sdf', 'email': 'sdf', 'password': 'sdfds', 'accountType': 'brand'})
if __name__ == '__main__':
main()