yuloh / illuminate-json-guard
Laravel/Lumen integration for league/json-guard
Installs: 4 748
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 0
Forks: 1
Open Issues: 0
Requires
- php: >=7.0
- illuminate/console: 5.3.*
- illuminate/support: 5.3.*
- league/json-guard: ^0.5.1
- yuloh/json-guard-cli: ^0.2.1
Requires (Dev)
- laravel/lumen-framework: ^5.3
- neomerx/json-api: ^0.8.10
- phpunit/phpunit: 4.*
- squizlabs/php_codesniffer: ~2.3
Suggests
- neomerx/json-api: To use the Neomerx response builder
This package is auto-updated.
Last update: 2022-02-01 13:03:54 UTC
README
Laravel/Lumen integration for league/json-guard.
This package is experimental and could break at any time.
Install
Via Composer
$ composer require yuloh/illuminate-json-guard
Register The Service Provider
Add the following line to app/boostrap.php
:
$app->register(Yuloh\JsonGuard\Illuminate\LumenServiceProvider::class);
Publish The Config File
As Lumen does not ship with a publish command, you will have to copy the config file manually.
cp ./vendor/league/illuminate-json-guard/config/json-guard.php ./config/json-guard.php
Usage
Controller Validation
You can use the provided ValidatesJsonRequests
trait to easily validate requests against a JSON Schema.
The trait provides a validateJson
method. Simply call the method with the current request and the name of the schema you would like to use, and the request body will be validated against the schema.
if validation fails, the method will throw a JsonSchemaValidationException
. The exception will be converted into a JSON response will all of the relevant error messages.
use Yuloh\JsonGuard\Illuminate\Http\ValidatesJsonRequests; class UsersController extends Controller { use ValidatesJsonRequests; public function show(Request $request, int $id) { $this->validateJson($request, 'user.json'); return User::find($id); } }
Route Middleware
A route middleware is also included. The route middleware takes the name of the schema you would like to use the as the only parameter. If validation fails the middleware will return a JSON response of the errors instead of passing through to your handler.
$app->post('/users', ['middleware' => 'json-schema:user.json', function () { // }]);
Loading Schemas
@todo
Localization
@todo
Customizing The Error Response
@todo
Testing
$ composer test
License
The MIT License (MIT). Please see License File for more information.