aegisora / required-rule
Required Rule provides a simple, rule-based presence validation implementation for the Aegisora ecosystem
Requires
- php: >=7.4
- aegisora/rule-contract: ^1.0
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^9.6
- squizlabs/php_codesniffer: ^4.0
README
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
- Installation
- Core Concept
- Basic Usage
- Valid vs Invalid
- Validation Result
- Guardian Usage
- Real-World Examples
- Factory Methods
- Architecture
- License
- Contributing
- Support
โจ 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 โ onlynullis rejected - ๐น Fully compatible with Aegisora validation pipeline
- ๐น Strict
ContextโResultvalidation flow - ๐น No raw booleans โ only structured results
- ๐น Safe execution via base
Ruleabstraction - ๐น 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โContextwrapping the value to validate
๐๏ธ Architecture
This package relies on aegisora/rule-contract.
Flow:
validate()is calledContextis passed in- The value is extracted from context
- The value is checked for presence (
!== null) Resultis returned โ valid when present, invalid with therequired_rulecode whennull
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.