Download the PHP package carsdotcom/laravel-json-schema without Composer
On this page you can find all versions of the php package carsdotcom/laravel-json-schema. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download carsdotcom/laravel-json-schema
More information about carsdotcom/laravel-json-schema
Files in carsdotcom/laravel-json-schema
Package laravel-json-schema
Short Description Json Schema validation for Laravel projects
License MIT
Informations about the package laravel-json-schema
laravel-json-schema
Use JsonSchema in Laravel apps
Purpose
This library builds on the outstanding JsonSchema validator opis/json-schema
The entire intent of this library is to make JsonSchema feel like a first class citizen in a Laravel project.
- Adds a new config file,
config/json-schema.php, to configure your root directory for self-hosted schema files. - Adds the
SchemaValidatorfacade that can be used to instantiate the validator with appropriate loaders. - Adds new PhpUnit assertions in the
Carsdotcom\JsonSchemaValidation\Traits\JsonSchemaAssertionstrait, such as validating that a mixed item validates for a specific schema. - Most interestingly, it lets you use JsonSchema to validate incoming Requests bodies, and/or validate your own outgoing response bodies, all using JsonSchema schemas that you can then export into OpenAPI documentation.
Laravel Version Compatibility
This package supports Laravel v10+ and PHP 8.1+.
Installation
Using Laravel JSON Schema
Setup
Config File
Copy the json-schema.php file from the vendor/carsdotcom/laravel-json-schema/config folder to your application's config folder.
Schema Storage
- Create a
Schemasfolder under your application root folder, such asapp/Schemas. -
Create a new storage disk under the
diskskey within your application'sconfig/filesystem.phpfile: - Add your schema files to the
app/Schemasfolder. You may create subfolders to keep things organized.
Generate Enum Schemas
This is an optional step, but can be super helpful.
For native PHP backed enums (BackedEnum) — recommended for all new code:
- Add
use Carsdotcom\JsonSchemaValidation\Traits\GeneratesSchemaTrait;to the enum. - Add a
SCHEMAconstant whose value is the relative path to your schema file:const SCHEMA = 'Acme/Enums/item_type.json'; - Run
php artisan schemas:generate.
For MyCLabs enums (MyCLabs\Enum\Enum subclasses) — legacy support only, not recommended for new code:
- Add
use Carsdotcom\JsonSchemaValidation\Traits\GeneratesSchemaMyCLabsTrait;to the class. - Add a
SCHEMAconstant as above. - Run
php artisan schemas:generate.
Validating JSON Data Against a Schema
For this example, we'll be using these objects:
Hosted JSON Schema File
This is assumed to be stored in your app/Schemas folder as Product.json.
JSON Data to be Validated
Application Code for Validation
Additional Functionality
Getting the Content of a Schema File
Storing a Schema File at a Specific Location
Adding an In-Memory Schema File
All versions of laravel-json-schema with dependencies
opis/json-schema Version ^2.3
php Version ^8.1