I'm using pydantic to validate data in AWS Lambda, I have a field for date that can accept either a valid date or empty string.
When I pass an empty string to that field 'date', it does not work and I get the following error :
pydantic.error_wrappers.ValidationError: 1 validation error for EmployeeInput
date
invalid datetime format (type=value_error.datetime)
This is how I defined the model :
class EmployeeInput(BaseModel):
last_name: str = ''
first_name: str = ''
date: Optional[datetime] = get_date_now()
I have tried to use Union[datetime, str], it is now accepting empty string but does not validate date anymore.
How can I make my field accept both empty string '' and validate the content if its a proper date ?