mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-09 06:44:47 +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 django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
from rest_framework.exceptions import ValidationErrorMessage
|
|
||||||
|
|
||||||
|
|
||||||
class AuthTokenSerializer(serializers.Serializer):
|
class AuthTokenSerializer(serializers.Serializer):
|
||||||
|
@ -20,24 +19,19 @@ class AuthTokenSerializer(serializers.Serializer):
|
||||||
if not user.is_active:
|
if not user.is_active:
|
||||||
msg = _('User account is disabled.')
|
msg = _('User account is disabled.')
|
||||||
raise serializers.ValidationError(
|
raise serializers.ValidationError(
|
||||||
ValidationErrorMessage(
|
|
||||||
msg,
|
msg,
|
||||||
code='authorization')
|
code='authorization')
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
msg = _('Unable to log in with provided credentials.')
|
msg = _('Unable to log in with provided credentials.')
|
||||||
raise serializers.ValidationError(
|
raise serializers.ValidationError(
|
||||||
ValidationErrorMessage(
|
|
||||||
msg,
|
msg,
|
||||||
code='authorization')
|
code='authorization')
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
msg = _('Must include "username" and "password".')
|
msg = _('Must include "username" and "password".')
|
||||||
raise serializers.ValidationError(
|
raise serializers.ValidationError(
|
||||||
ValidationErrorMessage(
|
|
||||||
msg,
|
msg,
|
||||||
code='authorization')
|
code='authorization')
|
||||||
)
|
|
||||||
|
|
||||||
attrs['user'] = user
|
attrs['user'] = user
|
||||||
return attrs
|
return attrs
|
||||||
|
|
|
@ -87,7 +87,11 @@ class ValidationErrorMessage(six.text_type):
|
||||||
class ValidationError(APIException):
|
class ValidationError(APIException):
|
||||||
status_code = status.HTTP_400_BAD_REQUEST
|
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.
|
# For validation errors the 'detail' key is always required.
|
||||||
# The details should always be coerced to a list if not already.
|
# The details should always be coerced to a list if not already.
|
||||||
if not isinstance(detail, dict) and not isinstance(detail, list):
|
if not isinstance(detail, dict) and not isinstance(detail, list):
|
||||||
|
|
|
@ -32,8 +32,7 @@ from rest_framework.compat import (
|
||||||
unicode_to_repr
|
unicode_to_repr
|
||||||
)
|
)
|
||||||
from rest_framework.exceptions import (
|
from rest_framework.exceptions import (
|
||||||
ValidationError, ValidationErrorMessage,
|
ValidationError, build_error_from_django_validation_error
|
||||||
build_error_from_django_validation_error
|
|
||||||
)
|
)
|
||||||
from rest_framework.settings import api_settings
|
from rest_framework.settings import api_settings
|
||||||
from rest_framework.utils import html, humanize_datetime, representation
|
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)
|
msg = MISSING_ERROR_MESSAGE.format(class_name=class_name, key=key)
|
||||||
raise AssertionError(msg)
|
raise AssertionError(msg)
|
||||||
message_string = msg.format(**kwargs)
|
message_string = msg.format(**kwargs)
|
||||||
raise ValidationError(ValidationErrorMessage(message_string, code=key))
|
raise ValidationError(message_string, code=key)
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def root(self):
|
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 DurationField as ModelDurationField
|
||||||
from rest_framework.compat import JSONField as ModelJSONField
|
from rest_framework.compat import JSONField as ModelJSONField
|
||||||
from rest_framework.compat import postgres_fields, unicode_to_repr
|
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 import model_meta
|
||||||
from rest_framework.utils.field_mapping import (
|
from rest_framework.utils.field_mapping import (
|
||||||
ClassLookupDict, get_field_kwargs, get_nested_relation_kwargs,
|
ClassLookupDict, get_field_kwargs, get_nested_relation_kwargs,
|
||||||
|
|
|
@ -60,8 +60,7 @@ class UniqueValidator(object):
|
||||||
queryset = self.filter_queryset(value, queryset)
|
queryset = self.filter_queryset(value, queryset)
|
||||||
queryset = self.exclude_current_instance(queryset)
|
queryset = self.exclude_current_instance(queryset)
|
||||||
if queryset.exists():
|
if queryset.exists():
|
||||||
raise ValidationError(ValidationErrorMessage(self.message,
|
raise ValidationError(self.message, code='unique')
|
||||||
code='unique'))
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return unicode_to_repr('<%s(queryset=%s)>' % (
|
return unicode_to_repr('<%s(queryset=%s)>' % (
|
||||||
|
@ -152,10 +151,8 @@ class UniqueTogetherValidator(object):
|
||||||
if None not in checked_values and queryset.exists():
|
if None not in checked_values and queryset.exists():
|
||||||
field_names = ', '.join(self.fields)
|
field_names = ', '.join(self.fields)
|
||||||
raise ValidationError(
|
raise ValidationError(
|
||||||
ValidationErrorMessage(
|
|
||||||
self.message.format(field_names=field_names),
|
self.message.format(field_names=field_names),
|
||||||
code='unique')
|
code='unique')
|
||||||
)
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return unicode_to_repr('<%s(queryset=%s, fields=%s)>' % (
|
return unicode_to_repr('<%s(queryset=%s, fields=%s)>' % (
|
||||||
|
|
Loading…
Reference in New Issue
Block a user