aegisora/required-rule

Required Rule provides a simple, rule-based presence validation implementation for the Aegisora ecosystem

Maintainers

Package info

github.com/Aegisora/required-rule

Language:Shell

pkg:composer/aegisora/required-rule

Transparency log

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-08-27 16:37 UTC

This package is auto-updated.

Last update: 2026-08-27 16:39:28 UTC


README

Latest Version Total Downloads Code Coverage Badge Software License PHPStan Badge

Required Rule provides a simple, rule-based presence 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 asserting that a required field is present before it is processed โ€” form fields, API request parameters, configuration values, message queue payloads, and any other value that must not be null.

๐Ÿ“‘ Table of Contents

โœจ Features

  • ๐Ÿ”น Lightweight and dependency-free except aegisora/rule-contract
  • ๐Ÿ”น Validates whether a value is present (not null)
  • ๐Ÿ”น Accepts any non-null value (strings, numbers, booleans, arrays, objects, callables)
  • ๐Ÿ”น Treats 0, false, '' and [] as present โ€” only null is rejected
  • ๐Ÿ”น 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/required-rule

๐Ÿš€ Core Concept

This package implements a single validation rule:

  • accepts a value via Context
  • checks whether the value is present (not null)
  • returns a standardized Result

Under the hood it wraps the common boilerplate:

if ($value === null) {
    // value is missing
}

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\RequiredRule;

$result = RequiredRule::create()->validate(Context::create('Aegisora'));

if ($result->isValid()) {
    // value is present
} else {
    // value is missing
}

โœ… Valid vs Invalid

The rule passes for any non-null value and fails only when the value is null. Falsy values such as 0, false, '' and [] are still considered present.

Valid values

$rule = RequiredRule::create();

$rule->validate(Context::create('foo'));        // valid
$rule->validate(Context::create(''));           // valid โ€” empty string is present
$rule->validate(Context::create(0));            // valid โ€” zero is present
$rule->validate(Context::create(false));        // valid โ€” false is present
$rule->validate(Context::create([]));           // valid โ€” empty array is present
$rule->validate(Context::create([1, 2, 3]));    // valid
$rule->validate(Context::create(new stdClass()));// valid

Invalid values

$rule = RequiredRule::create();

$rule->validate(Context::create(null));         // invalid โ€” value is missing

๐Ÿงช Validation Result

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

$result->isValid(); // true

If the value is null, the rule returns an invalid result.

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

๐Ÿ”— Guardian Usage

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

use Aegisora\Guardian\Guardian;
use Aegisora\Rules\RequiredRule;
use App\Exceptions\MissingFieldException;

$guardian = new Guardian();

$guardian
    ->that($fieldValue)
    ->must(RequiredRule::create(), new MissingFieldException())
    ->validate();

If the value is missing, Guardian throws the provided domain exception.

๐Ÿงญ Real-World Examples

Required Rule is useful for asserting that a value is present before it is processed or persisted.

Examples

Form:

validate that a required field was submitted
API Gateway:

reject requests missing a mandatory parameter
Configuration:

ensure a required config value is provided
Message Queue:

validate that a required payload field is present

๐Ÿงฉ Factory Methods

RequiredRule::create();

  • no arguments โ€” creates a new rule instance

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

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

๐Ÿ›๏ธ Architecture

This package relies on aegisora/rule-contract.

Flow:

  1. validate() is called
  2. Context is passed in
  3. The value is extracted from context
  4. The value is checked for presence (!== null)
  5. Result is returned โ€” valid when present, invalid with the required_rule code when null

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.