Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

validator: Add check_iso_datetime.

Shubham Padiacf3a3c6bcdea
diff --git a/zerver/lib/validator.py b/zerver/lib/validator.py
index 9768a1ae0cc..0d4eda6ffe5 100644
--- a/zerver/lib/validator.py
+++ b/zerver/lib/validator.py
@@ -30,7 +30,7 @@ for any particular type of object.
from collections.abc import Collection, Container, Iterator
from dataclasses import dataclass
-from datetime import date
+from datetime import date, datetime
from typing import Any, NoReturn, TypeVar, cast, overload
import orjson
@@ -124,6 +124,17 @@ def check_date(var_name: str, val: object) -> str:
return val
+def check_iso_datetime(var_name: str, val: object) -> datetime:
+ if not isinstance(val, str):
+ raise ValidationError(_("{var_name} is not a string").format(var_name=var_name))
+ try:
+ return datetime.fromisoformat(val)
+ except ValueError:
+ raise ValidationError(
+ _("{var_name} is not an ISO 8601 datetime string").format(var_name=var_name)
+ )
+
+
def check_int(var_name: str, val: object) -> int:
if not isinstance(val, int):
raise ValidationError(_("{var_name} is not an integer").format(var_name=var_name))
diff --git a/zerver/tests/test_validators.py b/zerver/tests/test_validators.py
index 19bb2922174..efcdb1a9c37 100644
--- a/zerver/tests/test_validators.py
+++ b/zerver/tests/test_validators.py
@@ -16,6 +16,7 @@ from zerver.lib.validator import (
check_float,
check_int,
check_int_in,
+ check_iso_datetime,
check_list,
check_none_or,
check_short_string,
@@ -399,3 +400,47 @@ class ValidatorTestCase(ZulipTestCase):
x = "2024-1-2"
with self.assertRaisesRegex(ValidationError, r"x is not a date"):
check_date("x", x)
+
+ def test_check_iso_datetime(self) -> None:
+ x: Any = "2024-01-02"
+ self.assertTrue(check_iso_datetime("x", x))
+
+ x = 123
+ with self.assertRaisesRegex(ValidationError, r"x is not a string"):
+ check_iso_datetime("x", x)
+
+ x = "2024-13-02"
+ with self.assertRaisesRegex(ValidationError, r"is not an ISO 8601 datetime string"):
+ check_iso_datetime("x", x)
+
+ x = "2024-1-2"
+ with self.assertRaisesRegex(ValidationError, r"is not an ISO 8601 datetime string"):
+ check_iso_datetime("x", x)
+
+ x = "2024-02-13T09:15:00"
+ self.assertTrue(check_iso_datetime("x", x))
+
+ x = "2024-02-13T09:15"
+ self.assertTrue(check_iso_datetime("x", x))
+
+ x = "2024-02-13T09:15:00+04:00"
+ self.assertTrue(check_iso_datetime("x", x))
+
+ x = "2024-02-13T09:15:00Z"
+ self.assertTrue(check_iso_datetime("x", x))
+
+ x = "09:15:00"
+ with self.assertRaisesRegex(ValidationError, r"is not an ISO 8601 datetime string"):
+ check_iso_datetime("x", x)
+
+ x = "2024-02-13T9:15:00"
+ with self.assertRaisesRegex(ValidationError, r"is not an ISO 8601 datetime string"):
+ check_iso_datetime("x", x)
+
+ x = "2024-14-13T09:15:00"
+ with self.assertRaisesRegex(ValidationError, r"is not an ISO 8601 datetime string"):
+ check_iso_datetime("x", x)
+
+ x = "2024-02-13T25:15:00Z"
+ with self.assertRaisesRegex(ValidationError, r"is not an ISO 8601 datetime string"):
+ check_iso_datetime("x", x)