Skip to content

Commit c62d19f

Browse files
committed
Modify ValidationError api
`ValidationErrorMessage` is now abstracted in `ValidationError`'s constructor
1 parent aba387f commit c62d19f

File tree

5 files changed

+18
-23
lines changed

5 files changed

+18
-23
lines changed

rest_framework/authtoken/serializers.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from django.utils.translation import ugettext_lazy as _
33

44
from rest_framework import serializers
5-
from rest_framework.exceptions import ValidationErrorMessage
65

76

87
class AuthTokenSerializer(serializers.Serializer):
@@ -20,24 +19,19 @@ def validate(self, attrs):
2019
if not user.is_active:
2120
msg = _('User account is disabled.')
2221
raise serializers.ValidationError(
23-
ValidationErrorMessage(
24-
msg,
25-
code='authorization')
26-
)
22+
msg,
23+
code='authorization')
2724
else:
2825
msg = _('Unable to log in with provided credentials.')
2926
raise serializers.ValidationError(
30-
ValidationErrorMessage(
31-
msg,
32-
code='authorization')
33-
)
27+
msg,
28+
code='authorization')
29+
3430
else:
3531
msg = _('Must include "username" and "password".')
3632
raise serializers.ValidationError(
37-
ValidationErrorMessage(
38-
msg,
39-
code='authorization')
40-
)
33+
msg,
34+
code='authorization')
4135

4236
attrs['user'] = user
4337
return attrs

rest_framework/exceptions.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,11 @@ def __new__(cls, string, code=None, *args, **kwargs):
8787
class ValidationError(APIException):
8888
status_code = status.HTTP_400_BAD_REQUEST
8989

90-
def __init__(self, detail):
90+
def __init__(self, detail, code=None):
91+
# If code is there, this means we are dealing with a message.
92+
if code and not isinstance(detail, ValidationErrorMessage):
93+
detail = ValidationErrorMessage(detail, code=code)
94+
9195
# For validation errors the 'detail' key is always required.
9296
# The details should always be coerced to a list if not already.
9397
if not isinstance(detail, dict) and not isinstance(detail, list):

rest_framework/fields.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@
3232
from rest_framework import ISO_8601
3333
from rest_framework.compat import unicode_repr, unicode_to_repr
3434
from rest_framework.exceptions import (
35-
ValidationError, ValidationErrorMessage,
36-
build_error_from_django_validation_error
35+
ValidationError, build_error_from_django_validation_error
3736
)
3837
from rest_framework.settings import api_settings
3938
from rest_framework.utils import html, humanize_datetime, representation
@@ -548,7 +547,7 @@ def fail(self, key, **kwargs):
548547
msg = MISSING_ERROR_MESSAGE.format(class_name=class_name, key=key)
549548
raise AssertionError(msg)
550549
message_string = msg.format(**kwargs)
551-
raise ValidationError(ValidationErrorMessage(message_string, code=key))
550+
raise ValidationError(message_string, code=key)
552551

553552
@cached_property
554553
def root(self):

rest_framework/serializers.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from rest_framework import exceptions
2525
from rest_framework.compat import JSONField as ModelJSONField
2626
from rest_framework.compat import postgres_fields, unicode_to_repr
27+
from rest_framework.exceptions import ValidationErrorMessage
2728
from rest_framework.utils import model_meta
2829
from rest_framework.utils.field_mapping import (
2930
ClassLookupDict, get_field_kwargs, get_nested_relation_kwargs,

rest_framework/validators.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ def __call__(self, value):
6060
queryset = self.filter_queryset(value, queryset)
6161
queryset = self.exclude_current_instance(queryset)
6262
if queryset.exists():
63-
raise ValidationError(ValidationErrorMessage(self.message,
64-
code='unique'))
63+
raise ValidationError(self.message, code='unique')
6564

6665
def __repr__(self):
6766
return unicode_to_repr('<%s(queryset=%s)>' % (
@@ -152,10 +151,8 @@ def __call__(self, attrs):
152151
if None not in checked_values and queryset.exists():
153152
field_names = ', '.join(self.fields)
154153
raise ValidationError(
155-
ValidationErrorMessage(
156-
self.message.format(field_names=field_names),
157-
code='unique')
158-
)
154+
self.message.format(field_names=field_names),
155+
code='unique')
159156

160157
def __repr__(self):
161158
return unicode_to_repr('<%s(queryset=%s, fields=%s)>' % (

0 commit comments

Comments
 (0)