Skip to content

Commit c20a3f0

Browse files
committed
Validate that at least one translation is provided when deserializing with TranslationsMixin. Fixes #256.
1 parent de47ea3 commit c20a3f0

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

hvad/contrib/restframework/utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class TranslationListSerializer(serializers.ListSerializer):
1111
many = True
1212
default_error_messages = {
1313
'not_a_dict': _l('Expected a dictionary of items, but got a {input_type}.'),
14+
'no_translation': _l('At least one translation must be provided.'),
1415
}
1516

1617
def to_internal_value(self, data):
@@ -21,6 +22,11 @@ def to_internal_value(self, data):
2122
raise ValidationError({
2223
api_settings.NON_FIELD_ERRORS_KEY: [message]
2324
})
25+
if not data:
26+
message = self.error_messages['no_translation']
27+
raise ValidationError({
28+
api_settings.NON_FIELD_ERRORS_KEY: [message]
29+
})
2430

2531
ret, errors = {}, {}
2632
for language, translation in data.items():

hvad/tests/contrib/restframework.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,14 @@ def test_serialize_custom(self):
288288

289289
def test_invalid(self):
290290
'Submit invalid data'
291+
# No translations
292+
data = {
293+
'shared_field': 'shared',
294+
'translations': {},
295+
}
296+
serializer = TranslationsSerializer(data=data)
297+
self.assertFalse(serializer.is_valid())
298+
self.assertTrue(serializer.errors['translations'])
291299

292300
# Invalid translations type
293301
data = {

0 commit comments

Comments
 (0)