aegisora / json-rule-guardian
Json Rule Guardian provides a simple shortcut for JSON string validation using aegisora/guardian and aegisora/json-rule.
Requires
- php: >=7.4
- aegisora/guardian: ^1.0
- aegisora/json-rule: ^1.0
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^9.6
- squizlabs/php_codesniffer: ^4.0
README
Json Rule Guardian provides a simple shortcut for JSON string validation using aegisora/guardian and aegisora/json-rule.
It is designed for cases where you want to quickly check whether a value is a valid JSON string without manually creating validation pipelines.
This package is built on top of:
โจ Features
- ๐น Simple shortcut API for
JsonRule - ๐น Validates whether a value is a valid JSON string
- ๐น Uses
aegisora/guardianinternally - ๐น Uses
aegisora/json-ruleinternally - ๐น Supports custom validation exceptions
- ๐น Fully compatible with the Aegisora ecosystem
- ๐น Ready to use out of the box
๐ฆ Installation
composer require aegisora/json-rule-guardian
๐ Core Concept
This package wraps the common validation flow:
$guardian->check($value, JsonRule::create(), new InvalidJsonException());
into a dedicated shortcut class:
$jsonRuleGuardian->check($value, new InvalidJsonException());
Instead of manually creating JsonRule and passing it to Guardian, you can use JsonRuleGuardian directly.
๐๏ธ Basic Usage
use Aegisora\Guardian\Exceptions\GuardianValidationException; use Aegisora\Guardian\Guardian; use Aegisora\RuleGuardians\JsonRule\JsonRuleGuardian; $guardian = new Guardian(); $jsonRuleGuardian = new JsonRuleGuardian($guardian); try { $jsonRuleGuardian->check('{"key":"value"}'); // value is a valid JSON string } catch (GuardianValidationException $exception) { // value is not a valid JSON string }
๐งฉ Usage with Custom Exception
You may provide your own exception for validation failure.
use Aegisora\Guardian\Guardian; use Aegisora\RuleGuardians\JsonRule\JsonRuleGuardian; use App\Exceptions\InvalidJsonException; $guardian = new Guardian(); $jsonRuleGuardian = new JsonRuleGuardian($guardian); $jsonRuleGuardian->check('{invalid json}', new InvalidJsonException());
If the value is not a valid JSON string, the provided exception will be thrown.
This is useful when validation errors should have domain-specific meaning.
๐งช Example in Application Service
use Aegisora\RuleGuardians\JsonRule\JsonRuleGuardian; use App\Exceptions\InvalidJsonException; final class PayloadService { private JsonRuleGuardian $jsonRuleGuardian; public function __construct( JsonRuleGuardian $jsonRuleGuardian ) { $this->jsonRuleGuardian = $jsonRuleGuardian; } /** * @param mixed $value */ public function process($value): void { $this->jsonRuleGuardian->check($value, new InvalidJsonException()); // business logic for valid JSON string } }
๐จ Exceptions
This package does not define its own exception types. All errors are raised by the underlying aegisora/guardian package.
Both exceptions extend the abstract base class
Aegisora\Guardian\Exceptions\GuardianException,
so you can catch every validation error with a single catch:
use Aegisora\Guardian\Exceptions\GuardianException; try { $jsonRuleGuardian->check($value); } catch (GuardianException $exception) { // handles GuardianValidationException and GuardianExecutingRuleException }
GuardianValidationException
Thrown when validation fails and no custom exception is provided.
use Aegisora\Guardian\Exceptions\GuardianValidationException; try { $jsonRuleGuardian->check('{invalid json}'); } catch (GuardianValidationException $exception) { echo $exception->getRuleCode(); // "json_rule" }
GuardianExecutingRuleException
Thrown when the underlying rule execution fails, for example when the value is not a string.
Aegisora\Guardian\Exceptions\GuardianExecutingRuleException
๐งฉ API
JsonRuleGuardian::check()
/** * @param mixed $value */ public function check( $value, ?\Throwable $exception = null ): void
Parameters:
$value(mixed) โ value to validate as a JSON string$exception(?\Throwable, defaultnull) โ optional custom exception thrown on validation failure
Returns void. The method communicates results through exceptions only โ it returns nothing on success and throws on failure:
GuardianValidationExceptionโ validation failed and no custom exception was providedGuardianExecutingRuleExceptionโ the underlying rule failed to execute (e.g. the value is not a string)- the provided custom exception โ validation failed and a custom exception was passed
Example:
$jsonRuleGuardian->check('{"key":"value"}');
With custom exception:
$jsonRuleGuardian->check('{invalid json}', new InvalidJsonException());
๐๏ธ Architecture
This package is a small shortcut layer over the Aegisora validation pipeline.
Flow:
JsonRuleGuardian::check()is calledJsonRule::create()is createdGuardianexecutes the rule- If validation succeeds, execution continues normally
- If validation fails, custom exception or
GuardianValidationExceptionis thrown - If rule execution fails,
GuardianExecutingRuleExceptionis thrown
Internal flow:
Value โ JsonRuleGuardian โ Guardian โ JsonRule โ Result โ Exception
๐ Related Packages
- aegisora/guardian โ validation execution orchestrator
- aegisora/json-rule โ rule-based JSON string validation
- aegisora/rule-contract โ base rule contract and validation result architecture
โ๏ธ License
This package is open-source and licensed under the MIT License. See the LICENSE for details.
๐ฑ Contributing
Contributions are welcome and greatly appreciated!. See the CONTRIBUTING for details.
๐ Support
If you find this project useful, please consider giving it a star on GitHub!
It helps the project grow and motivates further development.