mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-08 14:24:48 +03:00
Modify ValidationError api
`ValidationErrorMessage` is now abstracted in `ValidationError`'s constructor
This commit is contained in:
parent
42f4c5549d
commit
7971343a64
|
@ -2,7 +2,6 @@ from django.contrib.auth import authenticate
|
|||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from rest_framework import serializers
|
||||
from rest_framework.exceptions import ValidationErrorMessage
|
||||
|
||||
|
||||
class AuthTokenSerializer(serializers.Serializer):
|
||||
|
@ -20,24 +19,19 @@ class AuthTokenSerializer(serializers.Serializer):
|
|||
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
|
||||
|
|
|
@ -87,7 +87,11 @@ class ValidationErrorMessage(six.text_type):
|
|||
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):
|
||||
|
|
|
@ -32,8 +32,7 @@ from rest_framework.compat import (
|
|||
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
|
||||
|
@ -546,7 +545,7 @@ class Field(object):
|
|||
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):
|
||||
|
|
|
@ -24,6 +24,7 @@ from rest_framework import exceptions
|
|||
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,
|
||||
|
|
|
@ -60,8 +60,7 @@ class UniqueValidator(object):
|
|||
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)>' % (
|
||||
|
@ -152,10 +151,8 @@ class UniqueTogetherValidator(object):
|
|||
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)>' % (
|
||||
|
|
Loading…
Reference in New Issue
Block a user