Skip to content

Commit

Permalink
Empty cases of .validated_data and .errors as lists not dicts for Lis…
Browse files Browse the repository at this point in the history
…tSerializer (#4180)
  • Loading branch information
tomchristie committed Jun 8, 2016
1 parent 04e5b5b commit a5f822d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
22 changes: 22 additions & 0 deletions rest_framework/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,28 @@ def save(self, **kwargs):

return self.instance

def is_valid(self, raise_exception=False):
# This implementation is the same as the default,
# except that we use lists, rather than dicts, as the empty case.
assert hasattr(self, 'initial_data'), (
'Cannot call `.is_valid()` as no `data=` keyword argument was '
'passed when instantiating the serializer instance.'
)

if not hasattr(self, '_validated_data'):
try:
self._validated_data = self.run_validation(self.initial_data)
except ValidationError as exc:
self._validated_data = []
self._errors = exc.detail
else:
self._errors = []

if self._errors and raise_exception:
raise ValidationError(self.errors)

return not bool(self._errors)

def __repr__(self):
return unicode_to_repr(representation.list_repr(self, indent=1))

Expand Down
2 changes: 2 additions & 0 deletions tests/test_serializer_bulk_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def test_bulk_create_success(self):
serializer = self.BookSerializer(data=data, many=True)
self.assertEqual(serializer.is_valid(), True)
self.assertEqual(serializer.validated_data, data)
self.assertEqual(serializer.errors, [])

def test_bulk_create_errors(self):
"""
Expand Down Expand Up @@ -76,6 +77,7 @@ def test_bulk_create_errors(self):
serializer = self.BookSerializer(data=data, many=True)
self.assertEqual(serializer.is_valid(), False)
self.assertEqual(serializer.errors, expected_errors)
self.assertEqual(serializer.validated_data, [])

def test_invalid_list_datatype(self):
"""
Expand Down

0 comments on commit a5f822d

Please sign in to comment.