-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from reworkd/date-and-datetime-types
Date validation
- Loading branch information
Showing
5 changed files
with
529 additions
and
280 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from pydantic.functional_validators import AfterValidator | ||
from typing_extensions import Annotated | ||
|
||
import dateparser | ||
|
||
|
||
class ParserTypeDate: | ||
def __new__(self): | ||
return Annotated[str, AfterValidator(self.validate)] | ||
|
||
def validate(date: str): | ||
if not isinstance(date, str): | ||
raise ValueError("Wrong input type") | ||
|
||
# Trim whitespaces | ||
date = date.strip() | ||
|
||
# Make sure it's not empty string | ||
if len(date) == 0: | ||
raise ValueError("Empty input") | ||
|
||
# Attempt to parse date string | ||
try: | ||
dateparser.parse(date) | ||
return date | ||
except ValueError: | ||
pass | ||
|
||
raise ValueError(f"Unable to parse input as date: {date}") |
Oops, something went wrong.