Skip to content

Commit

Permalink
Update the Bundle to symfony 5
Browse files Browse the repository at this point in the history
  • Loading branch information
kerstinbasilicom committed Nov 15, 2021
1 parent 4836389 commit 2b677ff
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

Easily validate Symfony request bodies via JSON schema and automatically reject invalid requests

## Version
| Version | Symfony |
|---|---|
| 1.x | 3.4.x or 4.1.x |
| 2.x | 5.3.x |


## Usage
The controller needs to implement the `JsonSchemaRequestValidationControllerInterface`.
All request bodies of its actions then will be validated with JSON schema files set via the interface method `setJsonSchemaFilePathsInFilePathProvider(FilePathProvider $filePathProvider)`.
Expand Down Expand Up @@ -57,4 +64,4 @@ class TestingEndpointsController extends Controller implements JsonSchemaRequest
}
}

```
```
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
"minimum-stability": "dev",
"require": {
"ext-json": "*",
"symfony/symfony": "^3.4.26 || ^4.1.12",
"symfony/symfony": "^5.3",
"justinrainbow/json-schema": "^5.2"
},
"autoload": {
"psr-4": {
"Basilicom\\JsonSchemaRequestValidator\\": "src/"
}
}
}
}
24 changes: 18 additions & 6 deletions src/EventListener/JsonSchemaRequestValidatorListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\Event\ControllerEvent;

class JsonSchemaRequestValidatorListener
{
Expand All @@ -24,7 +24,7 @@ public function __construct(JsonSchemaValidator $jsonSchemaValidator)
$this->jsonSchemaValidator = $jsonSchemaValidator;
}

public function onKernelController(FilterControllerEvent $event)
public function onKernelController(ControllerEvent $event)
{
$controller = $event->getController();

Expand All @@ -47,7 +47,10 @@ public function onKernelController(FilterControllerEvent $event)
$jsonSchemaFilePath = $this->getJsonSchemaFilePath($event->getRequest(), $jsonSchemaFilePathProvider);
} catch (FileNotFoundException | NoFilePathProvidedException $exception) {
$event->setController(function () use ($exception) {
return JsonResponse::create(['message' => 'Could not get json schema to validate request body'], Response::HTTP_INTERNAL_SERVER_ERROR);
return new JsonResponse(
['message' => 'Could not get json schema to validate request body'],
Response::HTTP_INTERNAL_SERVER_ERROR
);
});

return;
Expand All @@ -56,7 +59,10 @@ public function onKernelController(FilterControllerEvent $event)
$content = $this->getRequestContent($event->getRequest());
if (empty($content)) {
$event->setController(function () {
return JsonResponse::create(['message' => 'Request did not contain any valid content'], Response::HTTP_FORBIDDEN);
return new JsonResponse(
['message' => 'Request did not contain any valid content'],
Response::HTTP_BAD_REQUEST
);
});

return;
Expand All @@ -66,12 +72,18 @@ public function onKernelController(FilterControllerEvent $event)
if (!$this->jsonSchemaValidator->isValid($content, $jsonSchemaFilePath)) {
$errors = $this->jsonSchemaValidator->getErrors($content, $jsonSchemaFilePath);
$event->setController(function () use ($errors) {
return JsonResponse::create(['message' => 'Request content validation failed', 'errors' => $errors], Response::HTTP_FORBIDDEN);
return new JsonResponse(
['message' => 'Request content validation failed', 'errors' => $errors],
Response::HTTP_BAD_REQUEST
);
});
}
} catch (GeneralJsonSchemaRequestValidatorException $exception) {
$event->setController(function () use ($exception) {
return JsonResponse::create(['message' => $exception->getMessage()], Response::HTTP_FORBIDDEN);
return new JsonResponse(
['message' => $exception->getMessage()],
Response::HTTP_BAD_REQUEST
);
});
}
}
Expand Down

0 comments on commit 2b677ff

Please sign in to comment.