Regex Rule Guardian provides a simple shortcut for string validation against a regular expression using aegisora/guardian and aegisora/regex-rule.
It is designed for cases where you want to quickly check whether a value matches a given regex pattern without manually creating validation pipelines.
This package is built on top of:
- πΉ Simple shortcut API for
RegexRule - πΉ Validates whether a value matches a regular expression
- πΉ Uses
aegisora/guardianinternally - πΉ Uses
aegisora/regex-ruleinternally - πΉ Supports custom validation exceptions
- πΉ Fully compatible with the Aegisora ecosystem
- πΉ Ready to use out of the box
composer require aegisora/regex-rule-guardianThis package wraps the common validation flow:
$guardian->check($value, RegexRule::create($pattern), new InvalidValueException());into a dedicated shortcut class:
$regexRuleGuardian->check($value, $pattern, new InvalidValueException());Instead of manually creating RegexRule and passing it to Guardian, you can use RegexRuleGuardian directly.
use Aegisora\Guardian\Exceptions\GuardianValidationException;
use Aegisora\Guardian\Guardian;
use Aegisora\RuleGuardians\RegexRule\RegexRuleGuardian;
$guardian = new Guardian();
$regexRuleGuardian = new RegexRuleGuardian($guardian);
try {
$regexRuleGuardian->check('abc123', '/^[a-z0-9]+$/');
// value matches the pattern
} catch (GuardianValidationException $exception) {
// value does not match the pattern
}You may provide your own exception for validation failure.
use Aegisora\Guardian\Guardian;
use Aegisora\RuleGuardians\RegexRule\RegexRuleGuardian;
use App\Exceptions\InvalidValueException;
$guardian = new Guardian();
$regexRuleGuardian = new RegexRuleGuardian($guardian);
$regexRuleGuardian->check('abc 123', '/^[a-z0-9]+$/', new InvalidValueException());If the value does not match the pattern, the provided exception will be thrown.
This is useful when validation errors should have domain-specific meaning.
use Aegisora\RuleGuardians\RegexRule\RegexRuleGuardian;
use App\Exceptions\InvalidValueException;
final class SlugService
{
private const SLUG_PATTERN = '/^[a-z0-9-]+$/';
private RegexRuleGuardian $regexRuleGuardian;
public function __construct(
RegexRuleGuardian $regexRuleGuardian
) {
$this->regexRuleGuardian = $regexRuleGuardian;
}
/**
* @param mixed $value
*/
public function process($value): void
{
$this->regexRuleGuardian->check($value, self::SLUG_PATTERN, new InvalidValueException());
// business logic for a value matching the pattern
}
}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 {
$regexRuleGuardian->check($value, $pattern);
} catch (GuardianException $exception) {
// handles GuardianValidationException and GuardianExecutingRuleException
}Thrown when validation fails and no custom exception is provided.
use Aegisora\Guardian\Exceptions\GuardianValidationException;
try {
$regexRuleGuardian->check('abc 123', '/^[a-z0-9]+$/');
} catch (GuardianValidationException $exception) {
echo $exception->getRuleCode(); // "regex_rule"
}Thrown when the underlying rule execution fails, for example when the value is not a string or the pattern is not a valid regular expression.
Aegisora\Guardian\Exceptions\GuardianExecutingRuleException
/**
* @param mixed $value
*/
public function check(
$value,
string $pattern,
?\Throwable $exception = null
): voidParameters:
$value(mixed) β value to validate against the pattern$pattern(string) β regular expression (including delimiters and flags) the value must match$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 or the pattern is invalid)- the provided custom exception β validation failed and a custom exception was passed
Example:
$regexRuleGuardian->check('abc123', '/^[a-z0-9]+$/');With custom exception:
$regexRuleGuardian->check('abc 123', '/^[a-z0-9]+$/', new InvalidValueException());This package is a small shortcut layer over the Aegisora validation pipeline.
Flow:
RegexRuleGuardian::check()is calledRegexRule::create($pattern)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 β RegexRuleGuardian β Guardian β RegexRule β Result β Exception
- aegisora/guardian β validation execution orchestrator
- aegisora/regex-rule β rule-based regular expression validation
- aegisora/rule-contract β base rule contract and validation result architecture
This package is open-source and licensed under the MIT License. See the LICENSE for details.
Contributions are welcome and greatly appreciated!. See the CONTRIBUTING for details.
If you find this project useful, please consider giving it a star on GitHub!
It helps the project grow and motivates further development.