Skip to content

Commit

Permalink
Modify ValidationError api
Browse files Browse the repository at this point in the history
`ValidationErrorMessage` is now abstracted in `ValidationError`'s
constructor
  • Loading branch information
johnraz committed Apr 7, 2016
1 parent 2de95ce commit 2668611
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 23 deletions.
20 changes: 7 additions & 13 deletions rest_framework/authtoken/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from django.utils.translation import ugettext_lazy as _

from rest_framework import serializers
from rest_framework.exceptions import ValidationErrorMessage


class AuthTokenSerializer(serializers.Serializer):
Expand All @@ -20,24 +19,19 @@ def validate(self, attrs):
if not user.is_active:
msg = _('User account is disabled.')
raise serializers.ValidationError(
ValidationErrorMessage(
msg,
code='authorization')
)
msg,
code='authorization')
else:
msg = _('Unable to log in with provided credentials.')
raise serializers.ValidationError(
ValidationErrorMessage(
msg,
code='authorization')
)
msg,
code='authorization')

else:
msg = _('Must include "username" and "password".')
raise serializers.ValidationError(
ValidationErrorMessage(
msg,
code='authorization')
)
msg,
code='authorization')

attrs['user'] = user
return attrs
6 changes: 5 additions & 1 deletion rest_framework/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ def __new__(cls, string, code=None, *args, **kwargs):
class ValidationError(APIException):
status_code = status.HTTP_400_BAD_REQUEST

def __init__(self, detail):
def __init__(self, detail, code=None):
# If code is there, this means we are dealing with a message.
if code and not isinstance(detail, ValidationErrorMessage):
detail = ValidationErrorMessage(detail, code=code)

# For validation errors the 'detail' key is always required.
# The details should always be coerced to a list if not already.
if not isinstance(detail, dict) and not isinstance(detail, list):
Expand Down
5 changes: 2 additions & 3 deletions rest_framework/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
from rest_framework import ISO_8601
from rest_framework.compat import unicode_repr, unicode_to_repr
from rest_framework.exceptions import (
ValidationError, ValidationErrorMessage,
build_error_from_django_validation_error
ValidationError, build_error_from_django_validation_error
)
from rest_framework.settings import api_settings
from rest_framework.utils import html, humanize_datetime, representation
Expand Down Expand Up @@ -548,7 +547,7 @@ def fail(self, key, **kwargs):
msg = MISSING_ERROR_MESSAGE.format(class_name=class_name, key=key)
raise AssertionError(msg)
message_string = msg.format(**kwargs)
raise ValidationError(ValidationErrorMessage(message_string, code=key))
raise ValidationError(message_string, code=key)

@cached_property
def root(self):
Expand Down
1 change: 1 addition & 0 deletions rest_framework/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from rest_framework import exceptions
from rest_framework.compat import JSONField as ModelJSONField
from rest_framework.compat import postgres_fields, unicode_to_repr
from rest_framework.exceptions import ValidationErrorMessage
from rest_framework.utils import model_meta
from rest_framework.utils.field_mapping import (
ClassLookupDict, get_field_kwargs, get_nested_relation_kwargs,
Expand Down
9 changes: 3 additions & 6 deletions rest_framework/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ def __call__(self, value):
queryset = self.filter_queryset(value, queryset)
queryset = self.exclude_current_instance(queryset)
if queryset.exists():
raise ValidationError(ValidationErrorMessage(self.message,
code='unique'))
raise ValidationError(self.message, code='unique')

def __repr__(self):
return unicode_to_repr('<%s(queryset=%s)>' % (
Expand Down Expand Up @@ -152,10 +151,8 @@ def __call__(self, attrs):
if None not in checked_values and queryset.exists():
field_names = ', '.join(self.fields)
raise ValidationError(
ValidationErrorMessage(
self.message.format(field_names=field_names),
code='unique')
)
self.message.format(field_names=field_names),
code='unique')

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

0 comments on commit 2668611

Please sign in to comment.