Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions aiogoogle/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import datetime
import warnings
import re
import rfc3339
from functools import wraps

from .excs import ValidationError
Expand Down Expand Up @@ -235,6 +234,13 @@ def byte_validator(value, schema_name=None):
base64.urlsafe_b64decode(value)


def parse_rfc3339_datetime(value):
# datetime.fromisoformat() rejects the RFC 3339 'Z'/'z' UTC suffix before Python 3.11
return datetime.datetime.fromisoformat(
value.replace("Z", "+00:00").replace("z", "+00:00")
)


@handle_type_and_value_errors
def date_validator(value, schema_name=None):
msg = make_validation_error_msg(
Expand All @@ -243,8 +249,7 @@ def date_validator(value, schema_name=None):
schema_name,
)
try:
pvalue = rfc3339.parse_date(value)
# pvalue = datetime.date.fromisoformat(value)
pvalue = datetime.date.fromisoformat(value)
except Exception as e:
raise ValidationError(str(e) + msg)
if not isinstance(pvalue, datetime.date):
Expand All @@ -259,8 +264,7 @@ def datetime_validator(value, schema_name=None):
schema_name,
)
try:
pvalue = rfc3339.parse_datetime(value)
# pvalue = datetime.datetime.fromisoformat(value)
pvalue = parse_rfc3339_datetime(value)
except Exception as e:
raise ValidationError(str(e), msg)
if not isinstance(pvalue, datetime.datetime):
Expand Down
1 change: 0 additions & 1 deletion docs_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ aiohttp
aiofiles
google-auth
sphinxcontrib-asyncio
tonyg-rfc3339
async-timeout
asks
trio
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
aiohttp
aiofiles
google-auth
tonyg-rfc3339
async-timeout
1 change: 0 additions & 1 deletion test_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ aiohttp
aiofiles
google-auth
sphinxcontrib-asyncio
tonyg-rfc3339
async-timeout
asks
trio
Expand Down
27 changes: 27 additions & 0 deletions tests/test_units/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,3 +461,30 @@ def test_validates_repeated(create_api):
ranges=[132, 'valid'], # Only first item is invalid
validate=True
)


def test_date_validator():
from aiogoogle.validate import date_validator

date_validator("2024-01-31")

with pytest.raises(ValidationError):
date_validator("not-a-date")

with pytest.raises(ValidationError):
date_validator("2024-13-01")


def test_datetime_validator():
from aiogoogle.validate import datetime_validator

datetime_validator("2024-01-31T12:34:56")
datetime_validator("2024-01-31T12:34:56Z")
datetime_validator("2024-01-31T12:34:56.123456Z")
datetime_validator("2024-01-31T12:34:56+02:00")

with pytest.raises(ValidationError):
datetime_validator("not-a-datetime")

with pytest.raises(ValidationError):
datetime_validator("2024-01-31T25:00:00Z")
Loading