[Questions] Support empty string with int, date validation
See original GitHub issueHi!
Is pydantic support empty values? I didn’t see anything in docs about empty or blank values. I have seen that I could write custom validator and return None when string is empty but maybe there is easier solution. Or if there is option to support empty values in future?
from datetime import date
from typing import Optional
from pydantic import BaseModel
class CustomModel(BaseModel):
custom_int: Optional[int] = None
custom_date: Optional[date] = None
CustomModel(**{'custom_int': '', 'custom_date': ''})
ValidationError: 4 validation errors
custom_int
value is not a valid integer (type=type_error.integer)
custom_int
value is not none (type=type_error.none.allowed)
custom_date
invalid date format (type=type_error.date)
custom_date
value is not none (type=type_error.none.allowed)
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Top Results From Across the Web
How to allow the empty string when validating an integer value
Use modulus by 1, to allow empty string when validating integers: var isNum = inputString % 1 === 0;. As said in the...
Read more >Assign empty string '' if datetime is null - TechNet - Microsoft
hi all,. may i know how to assign '' (empty string) for a varible is null which type is datetime? example: select EnterTime...
Read more >getting Transformation Error while converting empty string to ...
I have a Expression Transformation that converts input string to a date. Input is of format MM/DD/YYYY (from SQLServer source table). Input data...
Read more >Data conversion parameters - Amazon Redshift
Empty fields for other data types, such as INT, are always loaded with NULL. Empty fields occur when data contains two delimiters in...
Read more >Empty strings as None (revisited) · Issue #713 - GitHub
I'd like to revisit this suggestion - allowing empty strings to be interpreted as None: #543 I understand the sentiment of the response: ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Just as a note or “workaround” @krzysieqq:
Although it is not really “empty string values accepted as date”, you can define a field as a
Union
of other types (“union” means “any of these types”). And you can then use Pydantic’sconstr()
to create a typestr
with constraints, making it only accept empty strings.So, this code:
would print:
Note that the first two lines (with “valid data” for your use case) are taken as valid.
So, an empty string is actually accepted. And it shows an error only when the value passed is not of the first type (
int
,date
) or is a non-empty string.Thx for help and workaround 😃!