From 8b0eafd530dcdc8ef2f9a22d977323262f788063 Mon Sep 17 00:00:00 2001 From: amickan Date: Mon, 25 Mar 2024 11:21:50 +0100 Subject: [PATCH 1/2] Simplify json error reporting --- app/grandchallenge/components/tasks.py | 8 ++++---- app/grandchallenge/core/validators.py | 4 +++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/app/grandchallenge/components/tasks.py b/app/grandchallenge/components/tasks.py index 4fb50a43f2..174f1b2133 100644 --- a/app/grandchallenge/components/tasks.py +++ b/app/grandchallenge/components/tasks.py @@ -1049,7 +1049,7 @@ def add_file_to_component_interface_value( civ.full_clean() except ValidationError as e: civ.delete() - error = str(e) + error = str(e.message) else: user_upload.copy_object(to_field=civ.file) civ.save() @@ -1200,7 +1200,7 @@ def add_file_to_object( civ = ComponentInterfaceValue.objects.get(pk=civ_pk) object.values.remove(civ) except ValidationError as e: - error = str(e) + error = str(e.message) if error is not None: Notification.send( @@ -1209,7 +1209,7 @@ def add_file_to_object( message=f"File for interface {interface.title} failed validation.", target=object.base_object, description=( - f"File for interface {interface.title} added to {object_pk} " - f"in {object.base_object.title} failed validation:\n{error}." + f"File for interface {interface.title} " + f"failed validation:\n{error}." ), ) diff --git a/app/grandchallenge/core/validators.py b/app/grandchallenge/core/validators.py index e0638a618c..5528dce99c 100644 --- a/app/grandchallenge/core/validators.py +++ b/app/grandchallenge/core/validators.py @@ -161,7 +161,9 @@ def __call__(self, value): try: validate(value, self.schema, registry=self.registry) except JSONValidationError as e: - raise ValidationError(f"JSON does not fulfill schema: {e}") + raise ValidationError( + f"JSON does not fulfill schema: instance {e.message.replace(str(e.instance), '')}" + ) def __eq__(self, other): return isinstance(other, JSONValidator) and self.schema == other.schema From c6049aaac560612a3bfcea11f90ac6a34fb2d0a1 Mon Sep 17 00:00:00 2001 From: amickan Date: Mon, 25 Mar 2024 13:46:32 +0100 Subject: [PATCH 2/2] Fix tests and remove extra whitespace --- app/grandchallenge/core/validators.py | 2 +- app/tests/components_tests/test_serializers.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/grandchallenge/core/validators.py b/app/grandchallenge/core/validators.py index 5528dce99c..f362dd1d8a 100644 --- a/app/grandchallenge/core/validators.py +++ b/app/grandchallenge/core/validators.py @@ -162,7 +162,7 @@ def __call__(self, value): validate(value, self.schema, registry=self.registry) except JSONValidationError as e: raise ValidationError( - f"JSON does not fulfill schema: instance {e.message.replace(str(e.instance), '')}" + f"JSON does not fulfill schema: instance {e.message.replace(str(e.instance) + ' ', '')}" ) def __eq__(self, other): diff --git a/app/tests/components_tests/test_serializers.py b/app/tests/components_tests/test_serializers.py index 99a703d687..e7023a279f 100644 --- a/app/tests/components_tests/test_serializers.py +++ b/app/tests/components_tests/test_serializers.py @@ -346,7 +346,7 @@ def test_civ_post_value_required(kind): # verify assert not serializer.is_valid() - assert "JSON does not fulfill schema: None is not of type" in str( + assert "JSON does not fulfill schema: instance is not of type" in str( serializer.errors["__all__"][0] )