项目作者: webtoolsnz

项目描述 :
Laravel's Form Request Validation, but for json-schema documents
高级语言: PHP
项目地址: git://github.com/webtoolsnz/laravel-json-schema-request.git
创建时间: 2020-06-18T09:35:59Z
项目社区:https://github.com/webtoolsnz/laravel-json-schema-request

开源协议:MIT License

下载


JSON Schema Request

CI Action
Code Coverage
Scrutinizer Code Quality

Laravels Form Request Validation for JSON Schema documents

Installation

  1. composer require webtoolsnz/laravel-json-schema-request

Usage

The development experience is identical to Laravel’s Form Request Validation, except instead of writing Laravel validation rules, you write a JSON Schema.

You can create a new request using the make:json-request command

  1. artisan make:json-request MyJsonRequest

You will now have new request class App\Http\Requests\MyJsonRequest, Below you can see a basic example schema.

  1. <?php
  2. namespace App\Http\Requests;
  3. use Webtools\JsonSchemaRequest\JsonSchemaRequest;
  4. class MyJsonRequest extends JsonSchemaRequest
  5. {
  6. public function schema(): array
  7. {
  8. return [
  9. 'type' => 'object',
  10. 'properties' => [
  11. 'first_name' => ['type' => 'string'],
  12. 'last_name' => ['type' => 'string'],
  13. 'email' => ['type' => 'string', 'format' => 'email'],
  14. ],
  15. 'required' => ['first_name', 'last_name', 'email'],
  16. 'additionalProperties' => false,
  17. ];
  18. }
  19. }

Once you have a JsonSchemaRequest object, all you need to do is type-hint the request on your controller method.
The incoming form request is validated before the controller method is called.

  1. public function store(MyJsonRequest $request)
  2. {
  3. // The incoming request is valid...
  4. // Retrieve the validated input data...
  5. $validated = $request->validated();
  6. }

License

The MIT License (MIT). Please see License File for more information.