From 0fbd99beed317f2d9e674eb4d2068ed6addaec59 Mon Sep 17 00:00:00 2001 From: Regis Camimura Date: Mon, 24 Apr 2023 16:43:42 -0300 Subject: [PATCH] add time validation so we support time fields because time is time --- tests/test_validators.py | 2 ++ worf/validators.py | 18 ++++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/tests/test_validators.py b/tests/test_validators.py index 0013e4a..76d36a6 100644 --- a/tests/test_validators.py +++ b/tests/test_validators.py @@ -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}/") @@ -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") diff --git a/worf/validators.py b/worf/validators.py index f4e8602..b71f541 100644 --- a/worf/validators.py +++ b/worf/validators.py @@ -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 @@ -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]