mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-16 19:41:06 +03:00
Merge pull request #900 from copitux/fix-validation-layer
Bugfix: Fix run validation layer with invalid data
This commit is contained in:
commit
62e59f8277
|
@ -315,7 +315,8 @@ class BaseSerializer(WritableField):
|
|||
self._errors = {}
|
||||
if data is not None or files is not None:
|
||||
attrs = self.restore_fields(data, files)
|
||||
attrs = self.perform_validation(attrs)
|
||||
if attrs is not None:
|
||||
attrs = self.perform_validation(attrs)
|
||||
else:
|
||||
self._errors['non_field_errors'] = ['No input provided']
|
||||
|
||||
|
|
|
@ -63,3 +63,25 @@ class TestPreSaveValidationExclusions(TestCase):
|
|||
# does not have `blank=True`, so this serializer should not validate.
|
||||
serializer = ShouldValidateModelSerializer(data={'renamed': ''})
|
||||
self.assertEqual(serializer.is_valid(), False)
|
||||
|
||||
|
||||
class ValidationSerializer(serializers.Serializer):
|
||||
foo = serializers.CharField()
|
||||
|
||||
def validate_foo(self, attrs, source):
|
||||
raise serializers.ValidationError("foo invalid")
|
||||
|
||||
def validate(self, attrs):
|
||||
raise serializers.ValidationError("serializer invalid")
|
||||
|
||||
|
||||
class TestAvoidValidation(TestCase):
|
||||
"""
|
||||
If serializer was initialized with invalid data (None or non dict-like), it
|
||||
should avoid validation layer (validate_<field> and validate methods)
|
||||
"""
|
||||
def test_serializer_errors_has_only_invalid_data_error(self):
|
||||
serializer = ValidationSerializer(data='invalid data')
|
||||
self.assertFalse(serializer.is_valid())
|
||||
self.assertDictEqual(serializer.errors,
|
||||
{'non_field_errors': ['Invalid data']})
|
||||
|
|
Loading…
Reference in New Issue
Block a user