From bd3295b420418b13d902b4c14f0eae57e1d7bbc4 Mon Sep 17 00:00:00 2001 From: PascalEgn Date: Mon, 23 Sep 2024 16:30:57 +0200 Subject: [PATCH] authors: decouple validation func from endpoint --- backoffice/backoffice/workflows/api/views.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/backoffice/backoffice/workflows/api/views.py b/backoffice/backoffice/workflows/api/views.py index 57f684bd..d3045dc3 100644 --- a/backoffice/backoffice/workflows/api/views.py +++ b/backoffice/backoffice/workflows/api/views.py @@ -120,17 +120,15 @@ def create(self, request): serializer = self.serializer_class(data=request.data) if serializer.is_valid(raise_exception=True): logger.info("Validating data against given schema: %s", request.data) - # Create request object to pass to schema validation - validation_request = request._request - validation_request.data = request.data.get("data") - validation_response = self.validate(validation_request) - if validation_response.status_code != status.HTTP_200_OK: - return validation_response - logger.info("Data passed schema validation, creating workflow.") - workflow = Workflow.objects.create( - data=serializer.validated_data["data"], - workflow_type=serializer.validated_data["workflow_type"], - ) + validation_errors = list(get_validation_errors(request.data.get("data"))) + if validation_errors: + raise ValidationError(validation_errors) + else: + logger.info("Data passed schema validation, creating workflow.") + workflow = Workflow.objects.create( + data=serializer.validated_data["data"], + workflow_type=serializer.validated_data["workflow_type"], + ) logger.info( "Trigger Airflow DAG: %s for %s", WORKFLOW_DAGS[workflow.workflow_type].initialize,