carsdotcom / laravel-json-schema
Json Schema validation for Laravel projects
Installs: 11 274
Dependents: 2
Suggesters: 0
Security: 0
Stars: 4
Watchers: 19
Forks: 1
Open Issues: 0
Requires
- php: ^8.1
- laravel/framework: ^9.19 || ^10.0 || ^11.0
- opis/json-schema: ^2.3
Requires (Dev)
- fakerphp/faker: ^1.9.1
- mockery/mockery: ^1.4.4
- nunomaduro/collision: ^6.1|7.*|8.*
- orchestra/testbench: 7.*|8.*|9.*
- phpstan/phpstan: ^1.8
- phpunit/phpunit: ~8.0|~9.0|^10.5
- squizlabs/php_codesniffer: ^3.7
This package is auto-updated.
Last update: 2024-11-13 14:33:32 UTC
README
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
SchemaValidator
facade that can be used to instantiate the validator with appropriate loaders. - Adds new PhpUnit assertions in the
Carsdotcom\JsonSchemaValidation\Traits\JsonSchemaAssertions
trait, 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 v9+
.
Installation
composer require carsdotcom/laravel-json-schema
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
Schemas
folder under your application root folder, such asapp/Schemas
. - Create a new storage disk under the
disks
key within your application'sconfig/filesystem.php
file:
'disks' => [
'schemas' => [
'driver' => 'local',
'root' => app_path('app/Schemas'), // must match the 'config.json-schema.local_base_prefix' value
]
]
- Add your schema files to the
app/Schemas
folder. You may create subfolders to keep things organized.
Generate Enum Schemas
This is an optional step, but can be super helpful.
Note: Enums must be created either as a built-in PHP enum
object or a MyCLabs\Enum\Enum
class.
- Add
use Carsdotcom\JsonSchemaValidation\Traits\GeneratesSchemaTrait;
to the declarations in the Enum. - Add a
SCHEMA
constant to the enum. It's value will be the relative path to your schema file, such as:const SCHEMA = '/Acme/Enums/item_type.json';
- Run the
schema:generate
Artisan command.
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
.
{
"$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" ]
}
JSON Data to be Validated
{
"productId": 1,
"productName": "An ice sculpture",
"price": 12.50
}
Application Code for Validation
use Carsdotcom\JsonSchemaValidation\SchemaValidator;
SchemaValidator::validateOrThrow($json, 'Product.json');
Additional Functionality
Getting the Content of a Schema File
use Carsdotcom\JsonSchemaValidation\SchemaValidator;
SchemaValidator::getSchemaContents('Product.json');
Storing a Schema File at a Specific Location
use Carsdotcom\JsonSchemaValidation\SchemaValidator;
SchemaValidator::getSchemaContents('Customer.json', $jsonSchemaForCustomer);
Adding an In-Memory Schema File
$schemaKey = (new SchemaValidatorService)->registerRawSchema($jsonSchema);