Json Schema Validation For Laravel And Lumen
Laravel/Lumen integration for league/json-guard., (*1)
This package is experimental and could break at any time., (*2)
Install
Via Composer, (*3)
``` bash
$ composer require yuloh/illuminate-json-guard, (*4)
### Register The Service Provider
Add the following line to `app/boostrap.php`:
```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., (*5)
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., (*6)
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., (*7)
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., (*8)
``` php
use Yuloh\JsonGuard\Illuminate\Http\ValidatesJsonRequests;, (*9)
class UsersController extends Controller
{
use ValidatesJsonRequests;, (*10)
public function show(Request $request, int $id)
{
$this->validateJson($request, 'user.json');
return User::find($id);
}
}, (*11)
### 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.
```php
$app->post('/users', ['middleware' => 'json-schema:user.json', function () {
//
}]);
Loading Schemas
@todo, (*12)
Localization
@todo, (*13)
Customizing The Error Response
@todo, (*14)
Testing
bash
$ composer test, (*15)
License
The MIT License (MIT). Please see License File for more information., (*16)