Skip to content

Commit 5f290a6

Browse files
committed
validate plurals in suggestion API
1 parent 6ceb8a3 commit 5f290a6

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

weblate/api/serializers.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2293,6 +2293,16 @@ class Meta:
22932293
model = Suggestion
22942294
fields = ("id", "unit", "target", "user", "timestamp", "votes")
22952295

2296+
def validate_target(self, value: list[str]) -> list[str]:
2297+
unit = self.context.get("unit")
2298+
if unit is None:
2299+
return value
2300+
target_copy = value.copy()
2301+
if target_copy != unit.adjust_plurals(value.copy()):
2302+
msg = gettext_lazy("Number of plurals does not match")
2303+
raise serializers.ValidationError(msg)
2304+
return value
2305+
22962306
def create(self, validated_data):
22972307
request = self.context["request"]
22982308
unit = self.context["unit"]

weblate/api/tests.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11086,7 +11086,20 @@ def test_add_suggestion_plural(self) -> None:
1108611086
unit = Unit.objects.get(
1108711087
translation__language_code="cs", source__startswith="Orangutan has "
1108811088
)
11089-
target = ["Orangutan má %d banán.\n", "Orangutan má %d banány.\n"]
11089+
target = [
11090+
"Orangutan má %d banán.\n",
11091+
"Orangutan má %d banány.\n",
11092+
]
11093+
response = self._add_suggestion(unit, target, code=400)
11094+
self.assertEqual(
11095+
response.data["errors"][0]["detail"],
11096+
"Number of plurals does not match",
11097+
)
11098+
target = [
11099+
"Orangutan má %d banán.\n",
11100+
"Orangutan má %d banány.\n",
11101+
"Orangutan má %d banánů.\n",
11102+
]
1109011103
response = self._add_suggestion(unit, target)
1109111104
self.assertEqual(response.data["target"], target)
1109211105

0 commit comments

Comments
 (0)