From ed10f801ab245621660c6371c6da93263d85d822 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B0=D1=82=D0=BE=D0=BB=D0=B8=D0=B9=20=D0=9D?= =?UTF-8?q?=D0=B5=D1=85=D0=B0=D0=B9?= Date: Tue, 9 Jun 2020 17:31:26 +0300 Subject: [PATCH] validation logic was improved --- .../RequestBodyValidationMiddleware.php | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/Middleware/RequestBodyValidationMiddleware.php b/src/Middleware/RequestBodyValidationMiddleware.php index aa356d8..97af227 100644 --- a/src/Middleware/RequestBodyValidationMiddleware.php +++ b/src/Middleware/RequestBodyValidationMiddleware.php @@ -159,12 +159,36 @@ protected function validate(ServerRequestInterface $request) : void ], $e->getCode(), $e); } - if (null === $jsonSchema) { + if (false === isset($jsonSchema, $jsonSchema['type'])) { return; } - $payload = json_encode($request->getParsedBody()); - $payload = json_decode($payload); + $payload = null; + $parsedBody = $request->getParsedBody(); + + switch ($jsonSchema['type']) { + case 'array': + if ([] === $parsedBody) { + $payload = []; + } else { + $payload = json_encode($parsedBody); + $payload = (array) json_decode($payload); + } + break; + + case 'object': + if ([] === $parsedBody) { + $payload = new \stdClass(); + } else { + $payload = json_encode($parsedBody); + $payload = (object) json_decode($payload); + } + break; + + case 'string': + $payload = (string) $request->getBody(); + break; + } $validator = new Validator(); $validator->validate($payload, $jsonSchema);