mirror of
https://github.com/graphql-python/graphene-django.git
synced 2025-06-22 22:43:12 +03:00
Add raise exception on serializer validation
This commit is contained in:
parent
8d95596ffb
commit
b440780c4f
|
@ -124,7 +124,7 @@ class SerializerMutation(ClientIDMutation):
|
||||||
kwargs = cls.get_serializer_kwargs(root, info, **input)
|
kwargs = cls.get_serializer_kwargs(root, info, **input)
|
||||||
serializer = cls._meta.serializer_class(**kwargs)
|
serializer = cls._meta.serializer_class(**kwargs)
|
||||||
|
|
||||||
if serializer.is_valid():
|
if serializer.is_valid(raise_exception=True):
|
||||||
return cls.perform_mutate(serializer, info)
|
return cls.perform_mutate(serializer, info)
|
||||||
else:
|
else:
|
||||||
errors = ErrorType.from_errors(serializer.errors)
|
errors = ErrorType.from_errors(serializer.errors)
|
||||||
|
|
|
@ -5,6 +5,7 @@ from rest_framework import serializers
|
||||||
|
|
||||||
from graphene import Field, ResolveInfo
|
from graphene import Field, ResolveInfo
|
||||||
from graphene.types.inputobjecttype import InputObjectType
|
from graphene.types.inputobjecttype import InputObjectType
|
||||||
|
from rest_framework.exceptions import ValidationError
|
||||||
|
|
||||||
from ...settings import graphene_settings
|
from ...settings import graphene_settings
|
||||||
from ...types import DjangoObjectType
|
from ...types import DjangoObjectType
|
||||||
|
@ -204,20 +205,23 @@ def test_mutate_and_get_payload_error():
|
||||||
serializer_class = MySerializer
|
serializer_class = MySerializer
|
||||||
|
|
||||||
# missing required fields
|
# missing required fields
|
||||||
result = MyMutation.mutate_and_get_payload(None, mock_info(), **{})
|
with raises(ValidationError):
|
||||||
assert len(result.errors) > 0
|
result = MyMutation.mutate_and_get_payload(None, mock_info(), **{})
|
||||||
|
assert len(result.errors) > 0
|
||||||
|
|
||||||
|
|
||||||
def test_model_mutate_and_get_payload_error():
|
def test_model_mutate_and_get_payload_error():
|
||||||
# missing required fields
|
# missing required fields
|
||||||
result = MyModelMutation.mutate_and_get_payload(None, mock_info(), **{})
|
with raises(ValidationError):
|
||||||
assert len(result.errors) > 0
|
result = MyModelMutation.mutate_and_get_payload(None, mock_info(), **{})
|
||||||
|
assert len(result.errors) > 0
|
||||||
|
|
||||||
|
|
||||||
def test_mutation_error_camelcased():
|
def test_mutation_error_camelcased():
|
||||||
graphene_settings.CAMELCASE_ERRORS = True
|
graphene_settings.CAMELCASE_ERRORS = True
|
||||||
result = MyModelMutation.mutate_and_get_payload(None, mock_info(), **{})
|
with raises(ValidationError):
|
||||||
assert result.errors[0].field == "coolName"
|
result = MyModelMutation.mutate_and_get_payload(None, mock_info(), **{})
|
||||||
|
assert result.errors[0].field == "coolName"
|
||||||
graphene_settings.CAMELCASE_ERRORS = False
|
graphene_settings.CAMELCASE_ERRORS = False
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user