Skip to content

Commit

Permalink
Update the Bundle to symfony 5 - minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kerstinbasilicom committed Nov 15, 2021
1 parent 2b677ff commit f187d27
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
25 changes: 18 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,25 @@ Easily validate Symfony request bodies via JSON schema and automatically reject
| 1.x | 3.4.x or 4.1.x |
| 2.x | 5.3.x |

## Installation

via composer
```
composer require basilicom/json-schema-request-validator-bundle
```

add to \App\Kernel
```php
$collection->addBundle(new JsonSchemaRequestValidatorBundle());
```

## 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)`.

All actions of this controller must have a JSON schema file which must be mapped via the route name.

The system automatically rejects an invalid incoming requests with status code "403 Forbidden".
The system automatically rejects an invalid incoming requests with status code "400 Bad Request".
If no JSON schema file can be found it will respond with "500 Internal Server Error".

## Example Symfony controller
Expand All @@ -26,12 +37,12 @@ If no JSON schema file can be found it will respond with "500 Internal Server Er
namespace AppBundle\Controller;

use Basilicom\JsonSchemaRequestValidator\Controller\JsonSchemaRequestValidationControllerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

class TestingEndpointsController extends Controller implements JsonSchemaRequestValidationControllerInterface
class TestingEndpointsController extends AbstractController implements JsonSchemaRequestValidationControllerInterface
{
/**
* @Route("/testing", methods={"POST"}, name="testing_post")
Expand All @@ -40,9 +51,9 @@ class TestingEndpointsController extends Controller implements JsonSchemaRequest
*
* @return JsonResponse
*/
public function testingPost(Request $request)
public function testingPost(Request $request): JsonResponse
{
return JsonResponse::create(['success']);
return new JsonResponse(['success']);
}

/**
Expand All @@ -52,9 +63,9 @@ class TestingEndpointsController extends Controller implements JsonSchemaRequest
*
* @return JsonResponse
*/
public function testingGet(Request $request)
public function testingGet(Request $request): JsonResponse
{
return JsonResponse::create(['success']);
return new JsonResponse(['success']);
}

public function setJsonSchemaFilePathsInFilePathProvider(FilePathProvider $filePathProvider)
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
"minimum-stability": "dev",
"require": {
"ext-json": "*",
"symfony/symfony": "^5.3",
"symfony/http-kernel": "^5.3",
"symfony/http-foundation": "^5.3",
"symfony/config": "^5.3",
"symfony/dependency-injection": "^5.3",
"justinrainbow/json-schema": "^5.2"
},
"autoload": {
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ services:
Basilicom\JsonSchemaRequestValidator\Validator\JsonSchemaValidator: ~
Basilicom\JsonSchemaRequestValidator\EventListener\JsonSchemaRequestValidatorListener:
tags:
- { name: kernel.event_listener, event: kernel.controller, method: onKernelController}
- { name: kernel.event_listener, event: kernel.controller}

0 comments on commit f187d27

Please sign in to comment.