Skip to content

Commit

Permalink
refactor: improve validation trait
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielberthier committed May 17, 2023
1 parent a8a8edf commit 7e8485b
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
6 changes: 1 addition & 5 deletions src/Presentation/Actions/Protocols/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ public function __invoke(Request $request, Response $response, array $args): Res
$this->args = $args;

try {
$body = $request->getBody()->__toString();

$parsedBody = json_decode($body, true);

$this->validate($parsedBody);
$this->validate($request);

return $this->action($request);
} catch (HttpSpecializedAdapter $e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Presentation\Actions\Protocols\HttpErrors\UnprocessableEntityException;
use App\Presentation\Helpers\Validation\Validators\Facade\ValidationFacade;
use Psr\Http\Message\ServerRequestInterface;

trait ValidationTrait
{
Expand All @@ -20,8 +21,11 @@ public function messages()
/**
* @throws UnprocessableEntityException
*/
protected function validate(null|array|object $body)
protected function validate(ServerRequestInterface $request)
{
$rawBody = $request->getBody()->__toString();
$body = json_decode($rawBody, true);

$rules = $this->rules() ?? [];
$messages = $this->messages() ?? [];
$body = $body ?? [];
Expand Down
2 changes: 1 addition & 1 deletion src/Presentation/Actions/User/ViewUserAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ViewUserAction extends UserAction
/**
* {@inheritdoc}
*/
protected function action(Request $request): Response
public function action(Request $request): Response
{
$userId = (int) $this->resolveArg('id');
$user = $this->userService->findUserOfId($userId);
Expand Down

0 comments on commit 7e8485b

Please sign in to comment.