Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

License

NotificationsYou must be signed in to change notification settings

henriqueramos/laravel-json-schema-validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JSON Schema Validator for Laravel it is aComposer package created to validate JSON objects againstJSON Schemas as anIlluminate\Validation\Validator custom rule.

Build StatusScrutinizer Code QualityLatest Stable VersionLicense

About

This package works only at Laravel versions>= 5.8. And PHP version>= 7.3.

We uses the incredible packageswaggest/json-schema as a dependency to make everythingworks like magic.

Installation

Add the following line to therequire section ofcomposer.json:

{"require": {"henriqueramos/laravel_json_schema_validator":"^1.0.0"    }}

Setup

  1. Runphp artisan vendor:publish --provider="RamosHenrique\JsonSchemaValidator". This will create on yourconfig folder a file namedjson_schema_validator.php.
  2. 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/).
  3. Set up yourJSON 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 theJSON Schema file on your chosen storage path, you can use this as aValidation Rule on yourFormRequest extended class.

<?phpdeclare(strict_types =1);namespaceRamosHenrique\Requests;useIlluminate\Foundation\Http\FormRequest;class ValidatingPayloadRequestextends FormRequest{/**     * Determine if the user is authorized to make this request.     *     * @return bool     */publicfunctionauthorize():bool    {returntrue;    }/**     * Get the validation rules that apply to the request.     *     * @return array     */publicfunctionrules():array    {return ['jsonData' => ['bail','required','json','json_schema_validator:validJSONSchema.json',            ],        ];    }/**     * Custom messages for the route validator.     *     * @return array     */publicfunctionmessages():array    {return ['expectedData.required' =>'required.jsonData','expectedData.json' =>'expectedData.needs.needs.to.be.a.valid.json',        ];    }}

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp