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.

settings (admin/org): Show error for same choices in "list of options".

NerdyLucifer6a5d646739ed
diff --git a/zerver/lib/validator.py b/zerver/lib/validator.py
index 01ea93adf88..f50d1fe4f59 100644
--- a/zerver/lib/validator.py
+++ b/zerver/lib/validator.py
@@ -384,6 +384,9 @@ def validate_select_field_data(field_data: ProfileFieldData) -> Dict[str, Dict[s
]
)
+ # To create an array of texts of each option
+ distinct_field_names: Set[str] = set()
+
for key, value in field_data.items():
if not key.strip():
raise ValidationError(_("'{item}' cannot be blank.").format(item="value"))
@@ -391,6 +394,12 @@ def validate_select_field_data(field_data: ProfileFieldData) -> Dict[str, Dict[s
valid_value = validator("field_data", value)
assert value is valid_value # To justify the unchecked cast below
+ distinct_field_names.add(valid_value["text"])
+
+ # To show error if the options are duplicate
+ if len(field_data) != len(distinct_field_names):
+ raise ValidationError(_("Field must not have duplicate choices."))
+
return cast(Dict[str, Dict[str, str]], field_data)
diff --git a/zerver/tests/test_custom_profile_data.py b/zerver/tests/test_custom_profile_data.py
index 205e68733e0..1071a0f28ad 100644
--- a/zerver/tests/test_custom_profile_data.py
+++ b/zerver/tests/test_custom_profile_data.py
@@ -143,6 +143,15 @@ class CreateCustomProfileFieldTest(CustomProfileFieldTestCase):
result = self.client_post("/json/realm/profile_fields", info=data)
self.assert_json_error(result, "Field must have at least one choice.")
+ data["field_data"] = orjson.dumps(
+ {
+ "python": {"text": "Duplicate", "order": "1"},
+ "java": {"text": "Duplicate", "order": "2"},
+ }
+ ).decode()
+ result = self.client_post("/json/realm/profile_fields", info=data)
+ self.assert_json_error(result, "Field must not have duplicate choices.")
+
data["field_data"] = orjson.dumps(
{
"python": {"text": "Python", "order": "1"},