henriqueramos / laravel_json_schema_validator
Laravel JSON Schema Validator it's a Composer package created to help us to validate JSON Schemas.
Installs: 15 181
Dependents: 0
Suggesters: 0
Security: 0
Stars: 6
Watchers: 2
Forks: 9
Open Issues: 4
Requires
- php: >=7.3
- laravel/framework: >=5.8
- swaggest/json-schema: ^0.12.29
Requires (Dev)
- phpunit/php-code-coverage: ^7.0.9
- phpunit/phpunit: 8.5.4
- slevomat/coding-standard: ~4.0
- squizlabs/php_codesniffer: *
This package is auto-updated.
Last update: 2024-10-29 23:09:57 UTC
README
JSON Schema Validator for Laravel it is a Composer
package created to validate JSON objects against JSON Schemas as an Illuminate\Validation\Validator
custom rule.
About
This package works only at Laravel versions >= 5.8
. And PHP version >= 7.3
.
We uses the incredible package swaggest/json-schema
as a dependency to make everything works like magic.
Installation
Add the following line to the require
section of composer.json
:
{ "require": { "henriqueramos/laravel_json_schema_validator": "^1.0.0" } }
Setup
- Run
php artisan vendor:publish --provider="RamosHenrique\JsonSchemaValidator"
. This will create on yourconfig
folder a file namedjson_schema_validator.php
. - In your
.env
file, add your JSON Schema files storage path with keyJSON_SCHEMA_VALIDATOR_STORAGE_PATH
(i.eJSON_SCHEMA_VALIDATOR_STORAGE_PATH=storage/jsonschemas/
). - Set up your JSON Schema file
What is a JSON Schema
We supported the following schemas:
Here's an example for JSON Schema and a valid payload for him:
$schemaJson = <<<'JSON' { "type": "object", "properties": { "uuid": { "type": "integer" }, "userId": { "type": "integer" }, "items": { "type": "array", "minimum": 1, "items": { "$ref": "#/definitions/items" } } }, "required":[ "uuid", "userId", "items", ], "definitions": { "items": { "type": "object", "properties": { "id": { "type": "integer" }, "price": { "type": "number" }, "updated": { "type": "string", "format": "date-time" } }, "required":[ "id", "price" ] } } } JSON; $payload = <<<'JSON' { "uuid": 8, "userId": 1, "items": [ { "id": 12, "price": 49.90, "updated": "2020-09-07T20:20:39-03:00" }, { "id": 15, "price": 99, "updated": "2020-06-22T16:48:12-03:00" } ] } JSON;
Usage
After save the JSON Schema file on your chosen storage path, you can use this as a Validation Rule on your FormRequest
extended class.
<?php declare(strict_types = 1); namespace RamosHenrique\Requests; use Illuminate\Foundation\Http\FormRequest; class ValidatingPayloadRequest extends FormRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize(): bool { return true; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules(): array { return [ 'jsonData' => [ 'bail', 'required', 'json', 'json_schema_validator:validJSONSchema.json', ], ]; } /** * Custom messages for the route validator. * * @return array */ public function messages(): array { return [ 'expectedData.required' => 'required.jsonData', 'expectedData.json' => 'expectedData.needs.needs.to.be.a.valid.json', ]; } }