PHPackages                             aegisora/json-rule-guardian - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. aegisora/json-rule-guardian

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

aegisora/json-rule-guardian
===========================

Json Rule Guardian provides a simple shortcut for JSON string validation using aegisora/guardian and aegisora/json-rule.

v1.0.0(yesterday)00MITPHPPHP &gt;=7.4

Since Aug 16Pushed yesterdayCompare

[ Source](https://github.com/Aegisora/json-rule-guardian)[ Packagist](https://packagist.org/packages/aegisora/json-rule-guardian)[ Docs](https://github.com/Aegisora/json-rule-guardian)[ RSS](/packages/aegisora-json-rule-guardian/feed)WikiDiscussions main Synced today

READMEChangelog (1)Dependencies (5)Versions (2)Used By (0)

Aegisora Json Rule Guardian
===========================

[](#aegisora-json-rule-guardian)

[![Latest Version](https://camo.githubusercontent.com/1fa64a0d2bae86d84bf6976740a165190fd9c845867838bb728a63ed9e12c8fc/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f61656769736f72612f6a736f6e2d72756c652d677561726469616e3f7374796c653d666c61742d737175617265)](https://packagist.org/packages/aegisora/json-rule-guardian)[![Total Downloads](https://camo.githubusercontent.com/75fcb4361f43464cb03b3f7acefcb39ac12fee1b344870415e696701f77862d2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f61656769736f72612f6a736f6e2d72756c652d677561726469616e3f7374796c653d666c61742d737175617265)](https://packagist.org/packages/aegisora/json-rule-guardian)[![Code Coverage Badge](./badge.svg)](./badge.svg)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![PHPStan Badge](https://camo.githubusercontent.com/83dd3d35cebed0eab9ee97ff1a5849c1344cda6a8ee9cac2cda20f5aa55b67bd/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d6c6576656c253230392d627269676874677265656e2e7376673f7374796c653d666c6174)](https://camo.githubusercontent.com/83dd3d35cebed0eab9ee97ff1a5849c1344cda6a8ee9cac2cda20f5aa55b67bd/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d6c6576656c253230392d627269676874677265656e2e7376673f7374796c653d666c6174)

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:

- [aegisora/guardian](https://github.com/Aegisora/guardian)
- [aegisora/json-rule](https://github.com/Aegisora/json-rule)

---

✨ Features
----------

[](#-features)

- 🔹 Simple shortcut API for `JsonRule`
- 🔹 Validates whether a value is a valid JSON string
- 🔹 Uses `aegisora/guardian` internally
- 🔹 Uses `aegisora/json-rule` internally
- 🔹 Supports custom validation exceptions
- 🔹 Fully compatible with the Aegisora ecosystem
- 🔹 Ready to use out of the box

---

📦 Installation
--------------

[](#-installation)

```
composer require aegisora/json-rule-guardian
```

---

🚀 Core Concept
--------------

[](#-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
--------------

[](#️-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
-----------------------------

[](#-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
--------------------------------

[](#-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
------------

[](#-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`

[](#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`

[](#guardianexecutingruleexception)

Thrown when the underlying rule execution fails, for example when the value is not a string.

`Aegisora\Guardian\Exceptions\GuardianExecutingRuleException`

---

🧩 API
-----

[](#-api)

### `JsonRuleGuardian::check()`

[](#jsonruleguardiancheck)

```
/**
 * @param mixed $value
 */
public function check(
    $value,
    ?\Throwable $exception = null
): void
```

Parameters:

- `$value` *(mixed)* — value to validate as a JSON string
- `$exception` *(?\\Throwable, default `null`)* — 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 provided
- `GuardianExecutingRuleException` — 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
---------------

[](#️-architecture)

This package is a small shortcut layer over the Aegisora validation pipeline.

Flow:

1. `JsonRuleGuardian::check()` is called
2. `JsonRule::create()` is created
3. `Guardian` executes the rule
4. If validation succeeds, execution continues normally
5. If validation fails, custom exception or `GuardianValidationException` is thrown
6. If rule execution fails, `GuardianExecutingRuleException` is thrown

Internal flow:

```
Value → JsonRuleGuardian → Guardian → JsonRule → Result → Exception

```

---

🔗 Related Packages
------------------

[](#-related-packages)

- [aegisora/guardian](https://github.com/Aegisora/guardian) — validation execution orchestrator
- [aegisora/json-rule](https://github.com/Aegisora/json-rule) — rule-based JSON string validation
- [aegisora/rule-contract](https://github.com/Aegisora/rule-contract) — base rule contract and validation result architecture

---

⚖️ License
----------

[](#️-license)

This package is open-source and licensed under the MIT License. See the LICENSE for details.

---

🌱 Contributing
--------------

[](#-contributing)

Contributions are welcome and greatly appreciated!. See the CONTRIBUTING for details.

---

🌟 Support
---------

[](#-support)

If you find this project useful, please consider giving it a star on GitHub!

It helps the project grow and motivates further development.

###  Health Score

36

—

LowBetter than 79% of packages

Maintenance100

Actively maintained with recent releases

Popularity0

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity33

Early-stage or recently created project

 Bus Factor1

Top contributor holds 77.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/81402dcd0a07ad550b7f80f5871e7c302770b29d4c73a52fc35ba697f702d56e?d=identicon)[arslanim](/maintainers/arslanim)

---

Top Contributors

[![arslanim](https://avatars.githubusercontent.com/u/22678154?v=4)](https://github.com/arslanim "arslanim (7 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (2 commits)")

---

Tags

aegisoraaegisora-ecosystemguardianjsonjson-validationphprulerule-guardianvalidationphpjsonvalidationruleguardianjson-validationaegisoraaegisora-ecosystemrule-guardian

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/aegisora-json-rule-guardian/health.svg)

```
[![Health](https://phpackages.com/badges/aegisora-json-rule-guardian/health.svg)](https://phpackages.com/packages/aegisora-json-rule-guardian)
```

###  Alternatives

[codezero/laravel-unique-translation

Check if a translated value in a JSON column is unique in the database.

1901.1M8](/packages/codezero-laravel-unique-translation)[evaisse/php-json-schema-generator

A JSON Schema Generator.

18321.8k1](/packages/evaisse-php-json-schema-generator)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
