6

I have a API endpoint where it will do input validation using rest_framework's serializer.is_valid() where it will return custom error message and response.

serializer = FormSerializer(data=data)
if not serializer.is_valid(raise_exception=False):
    return Response({"Failure": "Error"}, status=status.HTTP_400_BAD_REQUEST)

Is it possible to populate validation errors without using the generic response provided by raise_exception=True? I am trying to avoid using the generic response as it will display all the validation errors if there are more than one error.

The response will be something like

return Response(
     {
          "Failure": "Error", 
          "Error_list": {"field1": "This field is required"}
     },
     status=status.HTTP_400_BAD_REQUEST
) 
Clueless_Coder
  • 515
  • 2
  • 7
  • 16

3 Answers3

13

Create a Custom Exception class as,

from rest_framework.exceptions import PermissionDenied
from rest_framework import status


class MyCustomExcpetion(PermissionDenied):
    status_code = status.HTTP_400_BAD_REQUEST
    default_detail = "Custom Exception Message"
    default_code = 'invalid'

    def __init__(self, detail, status_code=None):
        self.detail = detail
        if status_code is not None:
            self.status_code = status_code


Why I'm inherrited from PermissionDenied exception class ??
see this SO post -- Why DRF ValidationError always returns 400

Then in your serializer, raise exceptions as,

class SampleSerializer(serializers.ModelSerializer):
    class Meta:
        fields = '__all__'
        model = SampleModel

    def validate_age(self, age):  # field level validation
        if age > 10:
            raise MyCustomExcpetion(detail={"Failure": "error"}, status_code=status.HTTP_400_BAD_REQUEST)
        return age

    def validate(self, attrs): # object level validation
        if some_condition:
            raise MyCustomExcpetion(detail={"your": "exception", "some_other": "key"}, status_code=status.HTTP_410_GONE)
        return attrs


age and name are two fields of SampleModel class


Response will be like this

enter image description here
By using this method,
1. You can customize the JSON Response
2. You can return any status codes
3. You don't need to pass True in serializer.is_valid() method (This is not reccomended)

JPG
  • 82,442
  • 19
  • 127
  • 206
  • Is it possible to catch field validation error in the object level validation? I have more than 20 fields and wish to just capture the first field validation error in the object level validation. – Clueless_Coder Aug 03 '18 at 05:15
  • According to my example, you will get the value of your field by `attrs['your_field']`. Then check specific validation by yourself. If validation fails, raise the CustomException – JPG Aug 03 '18 at 05:17
  • So it is not possible if I were using `name = serializers.CharField(max_length=10)` as my field validation and catch it in the object level validation? – Clueless_Coder Aug 03 '18 at 05:27
  • In that case, it will raise an **`HTTP 400 Bad Request`** only if you are set `raise_exception=True` in **`is_valid()`** ->> (`is_valid(True)`) – JPG Aug 03 '18 at 05:38
  • @Clueless_Coder Is there any problem? – JPG Aug 03 '18 at 06:04
2

You can write custom error handler:

from rest_framework.views import exception_handler

def custom_exception_handler(exc, context):
    response = exception_handler(exc, context)
    if response is not None:
        response.data['Failure'] = 'Error'

    return response
neverwalkaloner
  • 46,181
  • 7
  • 92
  • 100
2

A simple way is to use one of the exception messages, eg NotFound. See docs

# views.py
from rest_framework.exceptions import NotFound

class myview(viewsets.ModelViewSet):
    def perform_create(self, serializer):
        raise NotFound("My text here")

That will return a 404 and change the response to your text

HTTP 404 Not Found
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept
{
    "detail": "my text here"
}
Unicorn Tears
  • 346
  • 2
  • 5