basilicom / json-schema-request-validator-bundle
Easily validate Symfony request bodies via JSON schema and automatically reject invalid requests
Installs: 2 934
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 11
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- ext-json: *
- justinrainbow/json-schema: ^5.2
- symfony/config: ^5.3
- symfony/dependency-injection: ^5.3
- symfony/http-foundation: ^5.3
- symfony/http-kernel: ^5.3
This package is auto-updated.
Last update: 2024-10-17 22:48:47 UTC
README
Easily validate Symfony request bodies via JSON schema and automatically reject invalid requests
Version
Installation
via composer
composer require basilicom/json-schema-request-validator-bundle
add to \App\Kernel
$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 "400 Bad Request". If no JSON schema file can be found it will respond with "500 Internal Server Error".
Example Symfony controller
<?php namespace AppBundle\Controller; use Basilicom\JsonSchemaRequestValidator\Controller\JsonSchemaRequestValidationControllerInterface; 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 AbstractController implements JsonSchemaRequestValidationControllerInterface { /** * @Route("/testing", methods={"POST"}, name="testing_post") * * @param Request $request * * @return JsonResponse */ public function testingPost(Request $request): JsonResponse { return new JsonResponse(['success']); } /** * @Route("/testing", methods={"GET"}, name="testing_get") * * @param Request $request * * @return JsonResponse */ public function testingGet(Request $request): JsonResponse { return new JsonResponse(['success']); } public function setJsonSchemaFilePathsInFilePathProvider(FilePathProvider $filePathProvider) { $filePathProvider->setIgnoreRouteName('testing_get', true); $filePathProvider->setJsonSchemaFilePathForRouteName('testing_post', __DIR__ . '/../Resources/jsonschemas/test.json'); } }