mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-10 19:56:59 +03:00
Use invalid_data key for error message. Closes #2002.
This commit is contained in:
parent
d27b8cc09b
commit
003c42b0f5
|
@ -15,6 +15,7 @@ from django.db import models
|
|||
from django.db.models.fields import FieldDoesNotExist
|
||||
from django.utils import six
|
||||
from django.utils.datastructures import SortedDict
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from rest_framework.exceptions import ValidationError
|
||||
from rest_framework.fields import empty, set_value, Field, SkipField
|
||||
from rest_framework.settings import api_settings
|
||||
|
@ -282,6 +283,10 @@ class SerializerMetaclass(type):
|
|||
|
||||
@six.add_metaclass(SerializerMetaclass)
|
||||
class Serializer(BaseSerializer):
|
||||
default_error_messages = {
|
||||
'invalid': _('Invalid data. Expected a dictionary, but got {datatype}.')
|
||||
}
|
||||
|
||||
@property
|
||||
def fields(self):
|
||||
if not hasattr(self, '_fields'):
|
||||
|
@ -339,8 +344,11 @@ class Serializer(BaseSerializer):
|
|||
return None
|
||||
|
||||
if not isinstance(data, dict):
|
||||
message = self.error_messages['invalid'].format(
|
||||
datatype=type(data).__name__
|
||||
)
|
||||
raise ValidationError({
|
||||
api_settings.NON_FIELD_ERRORS_KEY: ['Invalid data']
|
||||
api_settings.NON_FIELD_ERRORS_KEY: [message]
|
||||
})
|
||||
|
||||
value = self.to_internal_value(data)
|
||||
|
|
|
@ -87,8 +87,11 @@ class TestAvoidValidation(TestCase):
|
|||
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']})
|
||||
self.assertDictEqual(serializer.errors, {
|
||||
'non_field_errors': [
|
||||
'Invalid data. Expected a dictionary, but got unicode.'
|
||||
]
|
||||
})
|
||||
|
||||
|
||||
# regression tests for issue: 1493
|
||||
|
|
Loading…
Reference in New Issue
Block a user