Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Modify ValidationError api
`ValidationErrorMessage` is now abstracted in `ValidationError`'s
constructor
  • Loading branch information
johnraz committed Dec 17, 2015
commit 7971343a6457c7c72bf48c9c0a3838f01ea67cb3
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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess these would be marginally cleaner reformatted as three lines...

msg = ...
code = ...
raise serializers.ValidationError(msg, code)

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 @@
unicode_to_repr
)
from rest_framework.exceptions import (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might as well keep that formatting as-is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed blame my IDE XD

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 @@ -546,7 +545,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.compat import DurationField as ModelDurationField
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