aegisora/string-length-rule

String Length Rule provides a simple, rule-based string length validation implementation for the Aegisora ecosystem

Maintainers

Package info

github.com/Aegisora/string-length-rule

pkg:composer/aegisora/string-length-rule

Transparency log

Statistics

Installs: 58

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-08-22 13:16 UTC

This package is auto-updated.

Last update: 2026-08-22 13:21:22 UTC


README

Latest Version Total Downloads Code Coverage Badge Software License PHPStan Badge

String Length Rule provides a simple, rule-based string length 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 user input, form fields, usernames, passwords, API request parameters, database column constraints, and any other string that must satisfy a length boundary.

๐Ÿ“‘ Table of Contents

โœจ Features

  • ๐Ÿ”น Lightweight and dependency-free except aegisora/rule-contract
  • ๐Ÿ”น Validates a string length against a lower bound, an upper bound, or a range
  • ๐Ÿ”น Supports strict (>, <) and inclusive (>=, <=) comparisons
  • ๐Ÿ”น Counts characters (not bytes) via native mb_strlen(), so multibyte strings are measured correctly
  • ๐Ÿ”น 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
  • ๐Ÿ”น Expressive factory API for every boundary variation
  • ๐Ÿ”น Ready to use out of the box

๐Ÿ“ฆ Installation

composer require aegisora/string-length-rule

๐Ÿš€ Core Concept

This package implements a single validation rule with several factory variations:

  • accepts a string value via Context
  • checks whether the string length satisfies the configured boundary
  • returns a standardized Result

Under the hood it wraps the common boilerplate:

$length = mb_strlen($value);

if ($length < $min || $length > $max) {
    // length is out of the allowed boundary
}

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

$result = StringLengthRule::createGreaterThanOrEqualTo(8)->validate(Context::create('super-secret'));

if ($result->isValid()) {
    // length satisfies the boundary
} else {
    // length is out of the allowed boundary
}

โœ… Valid vs Invalid

The rule passes when the string length satisfies the configured boundary and fails otherwise. The length is measured in characters via mb_strlen().

Lower bound

StringLengthRule::createGreaterThan(3)->validate(Context::create('abcd'));         // valid   โ€” 4 > 3
StringLengthRule::createGreaterThan(3)->validate(Context::create('abc'));          // invalid โ€” 3 is not > 3

StringLengthRule::createGreaterThanOrEqualTo(3)->validate(Context::create('abc')); // valid   โ€” 3 >= 3
StringLengthRule::createGreaterThanOrEqualTo(3)->validate(Context::create('ab'));  // invalid โ€” 2 < 3

Upper bound

StringLengthRule::createLessThan(3)->validate(Context::create('ab'));              // valid   โ€” 2 < 3
StringLengthRule::createLessThan(3)->validate(Context::create('abc'));             // invalid โ€” 3 is not < 3

StringLengthRule::createLessThanOrEqualTo(3)->validate(Context::create('abc'));    // valid   โ€” 3 <= 3
StringLengthRule::createLessThanOrEqualTo(3)->validate(Context::create('abcd'));   // invalid โ€” 4 > 3

Range

StringLengthRule::createBetween(2, 4)->validate(Context::create('abc'));           // valid   โ€” 2 <= 3 <= 4
StringLengthRule::createBetween(2, 4)->validate(Context::create('a'));             // invalid โ€” 1 < 2

StringLengthRule::createBetweenExclusive(2, 4)->validate(Context::create('abc'));  // valid   โ€” 2 < 3 < 4
StringLengthRule::createBetweenExclusive(2, 4)->validate(Context::create('ab'));   // invalid โ€” 2 is not > 2

StringLengthRule::createBetweenMinExclusive(2, 4)->validate(Context::create('abcd')); // valid    โ€” 2 < 4 <= 4
StringLengthRule::createBetweenMinExclusive(2, 4)->validate(Context::create('ab'));   // invalid  โ€” 2 is not > 2

StringLengthRule::createBetweenMaxExclusive(2, 4)->validate(Context::create('ab'));   // valid    โ€” 2 <= 2 < 4
StringLengthRule::createBetweenMaxExclusive(2, 4)->validate(Context::create('abcd')); // invalid  โ€” 4 is not < 4

๐Ÿงช Validation Result

If the length satisfies the boundary, the rule returns a valid result.

$result->isValid(); // true

If the length is out of the boundary, the rule returns an invalid result.

$result->isValid(); // false
$result->getFailedRuleCode(); // string_length_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\StringLengthRule;
use App\Exceptions\InvalidUsernameException;

$guardian = new Guardian();

$guardian
    ->that($username)
    ->must(StringLengthRule::createBetween(3, 32), new InvalidUsernameException())
    ->validate();

If the length is out of the allowed boundary, Guardian throws the provided domain exception.

๐Ÿงญ Real-World Examples

String Length Rule is useful for enforcing length constraints before values are persisted or processed.

Examples

User Registration:

require a username between 3 and 32 characters
Security:

require a password of at least 8 characters
Database:

ensure a value fits a VARCHAR column limit
API:

reject request parameters that exceed a maximum length

๐Ÿงฉ Factory Methods

StringLengthRule::createGreaterThan($length);

  • passes when the string length is strictly greater than $length

StringLengthRule::createGreaterThanOrEqualTo($length);

  • passes when the string length is greater than or equal to $length

StringLengthRule::createLessThan($length);

  • passes when the string length is strictly less than $length

StringLengthRule::createLessThanOrEqualTo($length);

  • passes when the string length is less than or equal to $length

StringLengthRule::createBetween($min, $max);

  • passes when the string length is between $min and $max, both boundaries inclusive ($min <= length <= $max)

StringLengthRule::createBetweenExclusive($min, $max);

  • passes when the string length is between $min and $max, both boundaries exclusive ($min < length < $max)

StringLengthRule::createBetweenMinExclusive($min, $max);

  • passes when the string length is between $min (exclusive) and $max (inclusive) ($min < length <= $max)

StringLengthRule::createBetweenMaxExclusive($min, $max);

  • passes when the string length is between $min (inclusive) and $max (exclusive) ($min <= length < $max)

StringLengthRule::createGreaterThanOrEqualTo($length)->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 length is measured with mb_strlen()
  5. The length is compared against the configured boundary
  6. Result is returned โ€” valid on success, invalid with the string_length_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.