Skip to content

Commit

Permalink
ACP-4499: Adjusted body structure validator
Browse files Browse the repository at this point in the history
  • Loading branch information
pushokwhite committed Dec 24, 2024
1 parent dabff10 commit 845d7bf
Showing 1 changed file with 29 additions and 45 deletions.
74 changes: 29 additions & 45 deletions src/Spryker/Glue/AppKernel/Validator/BodyStructureValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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),
);
}
}

0 comments on commit 845d7bf

Please sign in to comment.