ricfr / json-structure
This Package validates Laravel Requests against the new JSON Structure Specification which provides easier verification, typing, modularity, and determinism. Please also see json-structure.org
Package info
github.com/Ric16Fr/laravel-json-structure
Type:libary
pkg:composer/ricfr/json-structure
Requires
- php: ^8.5
- illuminate/support: ^12.0||^13.0
- json-structure/sdk: ^0.8.0
Requires (Dev)
- larastan/larastan: ^3.9
- laravel/pao: ^1.0
- laravel/pint: ^1.29
- orchestra/testbench: ^10.0||^11.0
- pestphp/pest: ^4.6
- pestphp/pest-plugin-laravel: ^4.1
- pestphp/pest-plugin-type-coverage: ^4.0
- phpstan/extension-installer: ^1.4
README
Laravel Json Structure
This Package validates Laravel Requests against the new JSON Structure Specification which provides easier verification, typing, modularity, and determinism. Please also see json-structure.org for more information and also https://json-structure.org/codegen.html for how to turn classes into a structure and vies verta.
It aims to replace Laravels own ->verify() method with this emerging standard for several progamming languages.
Installation
You can install the package via Composer:
composer require ricfr/json-structure
Usage
Creating the structure document
To verify a JSON from a request against a structure, you'll first need to create one. Use
\LaravelJsonStructure\LaravelJsonStructure\Logic\JSONStructure::createFromObject($struct)
to create a structure document from an PHP array. Thats usually the case when you either have the structure document defined by yourself (see the second link above to automate this) or you are reccieving the structure from an endpoint as well-
Or you can use
\LaravelJsonStructure\LaravelJsonStructure\Logic\JSONStructure::createFromFile($pathToFile)
when you got the structure in form of a JSON file somewhere. Which happens usually when a colleague (from backend or so) creates it and sends it over.
Verifying a Request
You can then validate a Laravel Request object against a created JSONStructure Object by calling
$request->validateWithJSONStructure($structure)
This will return an array with all validation errors, meaning that if its empty the validation was succesfull.
Alternatively you can also skip the first step and call
$request->validateWithUncheckedJSONStructure($structureAsArray)
to directly insert the Structure definiton as an array and not create a structure object out of it. This ist not reccomended though, since it skips the validation of the structure itself. But it may be usefull for structure definitions not totally complying to the standard (yet).
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Thank you for considering contributing to Laravel Json Structure! Please review our contributing guide to get started.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
Laravel Json Structure is open-sourced software licensed under the GPL 3.0 license.