Json Schema validation for Laravel projects
carsdotcom/laravel-json-schema is a Laravel package for json schema validation for laravel projects.
It currently has 10 GitHub stars and 40.938 downloads on Packagist (latest version v3.0.0).
Install it with composer require carsdotcom/laravel-json-schema.
Discover more Laravel packages by carsdotcom
or browse all Laravel packages to compare alternatives.
Last updated
Use JsonSchema in Laravel apps
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.
config/json-schema.php, to configure your root directory for self-hosted schema files.SchemaValidator facade that can be used to instantiate the validator with appropriate loaders.Carsdotcom\JsonSchemaValidation\Traits\JsonSchemaAssertions trait, such as validating that a mixed item validates for a specific schema.This package supports Laravel v10+ and PHP 8.1+.
composer require carsdotcom/laravel-json-schema
Copy the json-schema.php file from the vendor/carsdotcom/laravel-json-schema/config folder to your application's config folder.
Schemas folder under your application root folder, such as app/Schemas.disks key within your application's config/filesystem.php file:'disks' => [
'schemas' => [
'driver' => 'local',
'root' => app_path('app/Schemas'), // must match the 'config.json-schema.local_base_prefix' value
]
]
app/Schemas folder. You may create subfolders to keep things organized.This is an optional step, but can be super helpful.
For native PHP backed enums (BackedEnum) — recommended for all new code:
use Carsdotcom\JsonSchemaValidation\Traits\GeneratesSchemaTrait; to the enum.SCHEMA constant whose value is the relative path to your schema file: const SCHEMA = 'Acme/Enums/item_type.json';php artisan schemas:generate.For MyCLabs enums (MyCLabs\Enum\Enum subclasses) — legacy support only, not recommended for new code:
use Carsdotcom\JsonSchemaValidation\Traits\GeneratesSchemaMyCLabsTrait; to the class.SCHEMA constant as above.php artisan schemas:generate.For this example, we'll be using these objects:
This is assumed to be stored in your app/Schemas folder as Product.json.
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/product.schema.json",
"title": "Product",
"description": "A product from Acme's catalog",
"type": "object",
"properties": {
"productId": {
"description": "The unique identifier for a product",
"type": "integer"
},
"productName": {
"description": "Name of the product",
"type": "string"
},
"price": {
"description": "The price of the product",
"type": "number",
"exclusiveMinimum": 0
}
},
"required": [ "productId", "productName", "price" ]
}
{
"productId": 1,
"productName": "An ice sculpture",
"price": 12.50
}
use Carsdotcom\JsonSchemaValidation\SchemaValidator;
SchemaValidator::validateOrThrow($json, 'Product.json');
use Carsdotcom\JsonSchemaValidation\SchemaValidator;
SchemaValidator::getSchemaContents('Product.json');
use Carsdotcom\JsonSchemaValidation\SchemaValidator;
SchemaValidator::getSchemaContents('Customer.json', $jsonSchemaForCustomer);
$schemaKey = (new SchemaValidatorService)->registerRawSchema($jsonSchema);