Skip to content

Commit

Permalink
add time validation so we support time fields because time is time
Browse files Browse the repository at this point in the history
  • Loading branch information
regiscamimura committed Apr 24, 2023
1 parent 22d5618 commit 0fbd99b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
2 changes: 2 additions & 0 deletions tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def profile_view_fixture(db, now, profile_factory, rf):
recovery_phone=phone,
last_active=now().date().isoformat(),
created_at=now().isoformat(),
start_time=now().time().isoformat(),
)
)
view.request = rf.patch(f"/{uuid}/")
Expand Down Expand Up @@ -61,6 +62,7 @@ def test_validate_bundle(profile_view):
profile_view.validate_bundle("small_integer")
profile_view.validate_bundle("recovery_email")
profile_view.validate_bundle("last_active")
profile_view.validate_bundle("start_time")
profile_view.validate_bundle("created_at")


Expand Down
18 changes: 16 additions & 2 deletions worf/validators.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from datetime import datetime
from datetime import datetime, time
from uuid import UUID

from django.core.exceptions import ValidationError
from django.core.validators import validate_email
from django.db import models
from django.utils.dateparse import parse_datetime
from django.utils.dateparse import parse_datetime, parse_time

from worf.conf import settings

Expand Down Expand Up @@ -60,6 +60,20 @@ def _validate_datetime(self, key):
)

return coerced

def _validate_time(self, key):
value = self.bundle[key]
coerced = None

if isinstance(value, str):
coerced = parse_time(value)

if not isinstance(coerced, time):
raise ValidationError(
f"Field {self.keymap[key]} accepts a iso time string, got {value}, coerced to {coerced}"
)

return coerced

def _validate_many_to_many(self, key):
value = self.bundle[key]
Expand Down

0 comments on commit 0fbd99b

Please sign in to comment.