aegisora/json-rule

JSON Rule provides a simple, rule-based JSON validation implementation for the Aegisora ecosystem.

Maintainers

Package info

github.com/Aegisora/json-rule

pkg:composer/aegisora/json-rule

Transparency log

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-08-08 15:05 UTC

This package is auto-updated.

Last update: 2026-08-08 15:08:10 UTC


README

Latest Version Total Downloads Code Coverage Badge Software License PHPStan Badge

JSON Rule provides a simple, rule-based JSON validation implementation for the Aegisora ecosystem.

It is built on top of aegisora/rule-contract and follows its strict validation architecture, ensuring consistent and predictable behavior across applications.

This rule is useful for validating webhook payloads, API request bodies, configuration strings, message queue messages, and any other string that must contain well-formed JSON.

๐Ÿ“‘ Table of Contents

โœจ Features

  • ๐Ÿ”น Lightweight and dependency-free except aegisora/rule-contract
  • ๐Ÿ”น Validates whether a string contains well-formed JSON
  • ๐Ÿ”น Backed by native json_decode() and json_last_error()
  • ๐Ÿ”น Accepts any valid JSON value (objects, arrays, strings, numbers, booleans, null)
  • ๐Ÿ”น Rejects non-string input as an invalid context
  • ๐Ÿ”น Fully compatible with Aegisora validation pipeline
  • ๐Ÿ”น Strict Context โ†’ Result validation flow
  • ๐Ÿ”น No raw booleans โ€” only structured results
  • ๐Ÿ”น Safe execution via base Rule abstraction
  • ๐Ÿ”น Simple factory API (create)
  • ๐Ÿ”น Ready to use out of the box

๐Ÿ“ฆ Installation

composer require aegisora/json-rule

๐Ÿš€ Core Concept

This package implements a single validation rule:

  • accepts a string value via Context
  • checks whether the value is well-formed JSON
  • returns a standardized Result

Under the hood it wraps the common boilerplate:

json_decode($value);

if (json_last_error() !== JSON_ERROR_NONE) {
    // value is not valid JSON
}

into a reusable rule that reports its outcome through a Result object instead of a raw boolean.

๐Ÿ—๏ธ Basic Usage

use Aegisora\RuleContract\Models\Context;
use Aegisora\Rules\JsonRule;

$result = JsonRule::create()->validate(Context::create('{"name": "Aegisora"}'));

if ($result->isValid()) {
    // value is valid JSON
} else {
    // value is not valid JSON
}

โœ… Valid vs Invalid

The rule passes for any well-formed JSON value and fails for malformed input. Validation relies on the native JSON parser, so only strictly well-formed JSON is accepted.

Valid JSON

$rule = JsonRule::create();

$rule->validate(Context::create('{}'));                  // valid
$rule->validate(Context::create('[]'));                  // valid
$rule->validate(Context::create('[1, 2, "foo", null]'));// valid
$rule->validate(Context::create('"Hello"'));             // valid
$rule->validate(Context::create('100'));                 // valid
$rule->validate(Context::create('-5.67'));               // valid
$rule->validate(Context::create('true'));                // valid
$rule->validate(Context::create('false'));               // valid
$rule->validate(Context::create('null'));                // valid

Invalid JSON

$rule = JsonRule::create();

$rule->validate(Context::create(''));                    // invalid โ€” empty string
$rule->validate(Context::create("{'key': 'value'}"));    // invalid โ€” single quotes
$rule->validate(Context::create('{key: "value"}'));      // invalid โ€” unquoted key
$rule->validate(Context::create('{"a": 1, "b": 2,}'));   // invalid โ€” trailing comma
$rule->validate(Context::create('[1, 2, 3,]'));          // invalid โ€” trailing comma
$rule->validate(Context::create('TRUE'));                // invalid โ€” wrong case

๐Ÿงช Validation Result

If the value is valid JSON, the rule returns a valid result.

$result->isValid(); // true

If the value is not valid JSON, the rule returns an invalid result.

$result->isValid(); // false
$result->getFailedRuleCode(); // json_rule

If the context value is not a string, the rule throws:

Aegisora\RuleContract\Exceptions\InvalidRuleContextException

๐Ÿ”— Guardian Usage

This rule can be used together with aegisora/guardian to build fluent validation pipelines.

use Aegisora\Guardian\Guardian;
use Aegisora\Rules\JsonRule;
use App\Exceptions\InvalidPayloadException;

$guardian = new Guardian();

$guardian
    ->that($rawPayload)
    ->must(JsonRule::create(), new InvalidPayloadException())
    ->validate();

If the value is not valid JSON, Guardian throws the provided domain exception.

๐Ÿงญ Real-World Examples

JSON Rule is useful for validating string payloads before they are decoded or persisted.

Examples

Webhook:

validate incoming payload body is well-formed JSON
API Gateway:

reject requests whose body is not valid JSON
Configuration:

ensure a JSON config string can be parsed
Message Queue:

validate message payloads before processing

๐Ÿงฉ Factory Methods

JsonRule::create();

  • no arguments โ€” creates a new rule instance

JsonRule::create()->validate($context);

  • $context โ€” Context wrapping the string value to validate

๐Ÿ›๏ธ Architecture

This package relies on aegisora/rule-contract.

Flow:

  1. validate() is called
  2. Context is passed in
  3. The string value is extracted from context (non-strings raise InvalidRuleContextException)
  4. The value is decoded with json_decode()
  5. json_last_error() is checked
  6. Result is returned โ€” valid on success, invalid with the json_rule code on failure

All logic is safely handled by Rule contract.

โš–๏ธ 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.