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.

data_type: Add StringDict data type.

orientor91ca1afe982e
diff --git a/tools/check-node-fixtures b/tools/check-node-fixtures
index edf342df2c2..5e6c8180da0 100755
--- a/tools/check-node-fixtures
+++ b/tools/check-node-fixtures
@@ -47,6 +47,7 @@ from zerver.lib.data_types import (
EnumType,
ListType,
NumberType,
+ StringDictType,
UnionType,
make_checker,
schema,
@@ -62,9 +63,6 @@ SKIP_LIST = [
EXEMPT_OPENAPI_NAMES = [
- # bots
- "realm_bot_add_event",
- "realm_bot_update_event",
# users field missing
"update_display_settings_event",
"update_global_notifications_event",
@@ -132,6 +130,8 @@ def from_openapi(node: Dict[str, Any]) -> Any:
# this might be a glitch in our current spec? or
# maybe I just understand it yet
if isinstance(node["additionalProperties"], dict):
+ if node["additionalProperties"].get("type", "") == "string":
+ return StringDictType()
return from_openapi(node["additionalProperties"])
if "properties" not in node:
diff --git a/zerver/lib/data_types.py b/zerver/lib/data_types.py
index af190a92eb2..fa1d94825b9 100644
--- a/zerver/lib/data_types.py
+++ b/zerver/lib/data_types.py
@@ -121,6 +121,21 @@ class ListType:
return f"{var_name} (list):\n{indent(sub_schema)}"
+@dataclass
+class StringDictType:
+ def check_data(self, var_name: str, val: Dict[Any, Any]) -> None:
+ if not isinstance(val, dict):
+ raise AssertionError(f"{var_name} is not a dictionary")
+
+ for key, value in val.items():
+ if isinstance(key, str) and isinstance(value, str):
+ continue
+ raise AssertionError(f"{var_name} is not a string:string dictionary")
+
+ def schema(self, var_name: str) -> str:
+ return f"{var_name}: string_dict"
+
+
@dataclass
class OptionalType:
sub_type: Any
diff --git a/zerver/lib/event_schema.py b/zerver/lib/event_schema.py
index 46411ead885..456190211fc 100644
--- a/zerver/lib/event_schema.py
+++ b/zerver/lib/event_schema.py
@@ -14,6 +14,7 @@ from zerver.lib.data_types import (
ListType,
NumberType,
OptionalType,
+ StringDictType,
UnionType,
UrlType,
check_data,
@@ -313,16 +314,13 @@ bot_services_outgoing_type = DictType(
]
)
-# We use a strict check here, because our tests
-# don't specifically focus on seeing how
-# flexible we can make the types be for config_data.
-_ad_hoc_config_data_schema = Equals(dict(foo="bar"))
+config_data_schema = StringDictType()
bot_services_embedded_type = DictType(
required_keys=[
# force vertical
("service_name", str),
- ("config_data", _ad_hoc_config_data_schema),
+ ("config_data", config_data_schema),
]
)
diff --git a/zerver/tests/test_data_types.py b/zerver/tests/test_data_types.py
index 04765d74c9a..0cd5f5fb3a1 100644
--- a/zerver/tests/test_data_types.py
+++ b/zerver/tests/test_data_types.py
@@ -5,6 +5,7 @@ from zerver.lib.data_types import (
ListType,
NumberType,
OptionalType,
+ StringDictType,
UnionType,
UrlType,
schema,
@@ -27,12 +28,14 @@ class MiscTest(ZulipTestCase):
("flag", bool),
("level", EnumType([1, 2, 3])),
("lst", ListType(int)),
+ ("config", StringDictType()),
("value", UnionType([int, str])),
("url", UrlType()),
]
)
expected = """
test (dict):
+ config: string_dict
flag: bool
level in [1, 2, 3]
lst (list):