Skip to content

Commit

Permalink
Merge pull request #17 from sunrise-php/release/v1.5.1
Browse files Browse the repository at this point in the history
v1.5.1
  • Loading branch information
fenric authored Jun 9, 2020
2 parents 998ac32 + ed10f80 commit ed81e40
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/Middleware/RequestBodyValidationMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit ed81e40

Please sign in to comment.