From 845d7bf76948ca90619ab05f7726fb880ea54047 Mon Sep 17 00:00:00 2001 From: Anton Zubariev Date: Tue, 24 Dec 2024 12:49:16 +0100 Subject: [PATCH] ACP-4499: Adjusted body structure validator --- .../Validator/BodyStructureValidator.php | 74 ++++++++----------- 1 file changed, 29 insertions(+), 45 deletions(-) diff --git a/src/Spryker/Glue/AppKernel/Validator/BodyStructureValidator.php b/src/Spryker/Glue/AppKernel/Validator/BodyStructureValidator.php index 2ebf517..caa90ee 100644 --- a/src/Spryker/Glue/AppKernel/Validator/BodyStructureValidator.php +++ b/src/Spryker/Glue/AppKernel/Validator/BodyStructureValidator.php @@ -13,12 +13,11 @@ use Spryker\Glue\AppKernel\AppKernelConfig; use Spryker\Glue\AppKernel\Dependency\Service\AppKernelToUtilEncodingServiceInterface; use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\Validator\Constraint; -use Symfony\Component\Validator\Constraints\All; use Symfony\Component\Validator\Constraints\Collection; use Symfony\Component\Validator\Constraints\EqualTo; use Symfony\Component\Validator\Constraints\Json; use Symfony\Component\Validator\Constraints\NotBlank; +use Symfony\Component\Validator\Constraints\NotNull; use Symfony\Component\Validator\Constraints\Required; use Symfony\Component\Validator\Constraints\Type; use Symfony\Component\Validator\Validator\ValidatorInterface; @@ -37,60 +36,45 @@ public function validate(GlueRequestTransfer $glueRequestTransfer): GlueRequestV ->setIsValid(true) ->setStatus(Response::HTTP_OK); - $constraintViolationList = $this->validator->validate($glueRequestTransfer->getContent(), $this->getConstrainForRequestContent()); - - if ($constraintViolationList->count() > 0) { - return $this->getFailedGlueRequestValidationTransfer($glueRequestValidationTransfer); - } - $content = $this->appKernelToUtilEncodingService->decodeJson((string)$glueRequestTransfer->getContent(), true); - $constraintViolationList = $this->validator->validate($content, $this->getConstraintForRequestStructure()); + $constraintViolationList = $this->validator->validate(['content' => $content], $this->getConstraintForRequestStructure()); if ($constraintViolationList->count() > 0) { - return $this->getFailedGlueRequestValidationTransfer($glueRequestValidationTransfer); + $glueRequestValidationTransfer + ->setIsValid(false) + ->setStatus(Response::HTTP_UNPROCESSABLE_ENTITY); + + $glueRequestValidationTransfer->addError( + (new GlueErrorTransfer()) + ->setCode((string)Response::HTTP_UNPROCESSABLE_ENTITY) + ->setMessage(AppKernelConfig::RESPONSE_MESSAGE_VALIDATION_FORMAT_ERROR_MESSAGE), + ); } return $glueRequestValidationTransfer; } - protected function getConstrainForRequestContent(): Constraint - { - return new All([ - new Required(), - new NotBlank(), - new Type(['type' => 'string']), - new Json(), - ]); - } - - protected function getConstraintForRequestStructure(): Constraint + protected function getConstraintForRequestStructure(): Collection { return new Collection([ - 'data' => new Collection([ - 'type' => new EqualTo(AppKernelConfig::REQUEST_DATA_TYPE), - 'attributes' => new Collection([ - 'configuration' => [ - new Required(), - new NotBlank(), - new Type(['type' => 'string']), - new Json(), - ], + 'content' => [ + new Required(), + new NotBlank(), + new NotNull(), + new Type(['type' => 'string']), + new Json(), + 'data' => new Collection([ + 'type' => new EqualTo(AppKernelConfig::REQUEST_DATA_TYPE), + 'attributes' => new Collection([ + 'configuration' => [ + new Required(), + new NotBlank(), + new Type(['type' => 'string']), + new Json(), + ], + ]), ]), - ]), + ], ]); } - - protected function getFailedGlueRequestValidationTransfer( - GlueRequestValidationTransfer $glueRequestValidationTransfer - ): GlueRequestValidationTransfer { - $glueRequestValidationTransfer - ->setIsValid(false) - ->setStatus(Response::HTTP_UNPROCESSABLE_ENTITY); - - return $glueRequestValidationTransfer->addError( - (new GlueErrorTransfer()) - ->setCode((string)Response::HTTP_UNPROCESSABLE_ENTITY) - ->setMessage(AppKernelConfig::RESPONSE_MESSAGE_VALIDATION_FORMAT_ERROR_MESSAGE), - ); - } }