I'm trying to build a custom field in Fastapi-users pydantic schema as follows:
class UserRead(schemas.BaseUser[uuid.UUID]):
twitter_account: Optional['TwitterAccount']
On UserRead validation Pydantic returns
ValidationError: 1 validation error for UserRead
twitter_account
Field required [type=missing, input_value={}, input_type=dict]
on every field in 'TwitterAccount' schema.update_forward_refs() is called at the end.
TwitterAccount itself has required fields and making them optional isn't an acceptable workaround. I notices I could make Optional[List['TwitterAccount']] and it will work, but that's a bit silly.
Optional is a bit misleading here. What it means technically means is that twitter_account can be a TwitterAccount or None, but it is still a required argument. To make it truly optional (as in, it doesn't have to be provided), you must provide a default:
class UserRead(schemas.BaseUser[uuid.UUID]):
twitter_account: Optional['TwitterAccount'] = None
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With