AssertionError: .validate() should return the validated data

Viewed 1763

I am able to get to the user_queryset of the validate() function of the PasswordChangeSerializer, however I am still receiving this error:

assert value is not None, '.validate() should return the validated data'.

serializers.py


class PasswordChangeSerializer(serializers.ModelSerializer):
    old_password = serializers.CharField(write_only=True, required=True, max_length=30)
    new_password = serializers.CharField(write_only=True, required=True, max_length=30)
    confirm_password = serializers.CharField(write_only=True, required=True, max_length=30)

    class Meta:
        model = User 
        fields = ['old_password', 'new_password', 'confirm_password']


    def validate(self, data):
        old_password = data['old_password']
        new_password = data['new_password']
        confirm_password = data['confirm_password']

        user_queryset = User.objects.all()

        if user_queryset.exists() and user_queryset.count() == 1:
            user_set = user_queryset.first()

            if not user_set.check_password(old_password):
                raise serializers.ValidationError({'old_password': 'Wrong password!'})

            if confirm_password != new_password:
                raise serializers.ValidationError({'password': 'Password must be confirmed correctly!'})

            return data

    def update(self, instance, validated_data):
        instance.set_password(validated_data['new_password'])
        instance.save()
        return instance

views.py


class PasswordChangeAPIView(APIView):
    permission_classes = [IsAdminUser]
    serializer_class = PasswordChangeSerializer

    def post(self, request, format=None):
        data = request.data
        password_ser = PasswordChangeSerializer(data=data)
        if password_ser.is_valid():
            password_ser.save()
            new_data = password_ser.data
            return Response(new_data, status=HTTP_201_CREATED)
        else:
            return Response({"msg":"invalid user!"}, status=HTTP_400_BAD_REQUEST)
            
1 Answers

You just need to return the data. You have kept the return statement inside if condition. Just take it outside of if condition.

def validate(self, data):
        old_password = data['old_password']
        new_password = data['new_password']
        confirm_password = data['confirm_password']

        user_queryset = User.objects.all()

        if user_queryset.exists() and user_queryset.count() == 1:
            self.user_set = user_queryset.first()

            if not self.user_set.check_password(old_password):
                raise serializers.ValidationError({'old_password': 'Wrong password!'})

            if confirm_password != new_password:
                raise serializers.ValidationError({'password': 'Password must be confirmed correctly!'})

        return data

And change your view as follows

class PasswordChangeAPIView(APIView):
    permission_classes = [IsAdminUser]
    serializer_class = PasswordChangeSerializer

    def post(self, request, format=None):
        data = request.data
        password_ser = PasswordChangeSerializer(data=data)
        if password_ser.is_valid():
            user = password_ser.user_set
            user.set_password(data['new_password'])
            user.save()
            return Response(new_data, status=HTTP_201_CREATED)
        else:
            return Response({"msg":"invalid user!"}, status=HTTP_400_BAD_REQUEST)
Related