kentjerone/laravel-chain-rule

A chainable rule builder for Laravel validation.

Installs: 207

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/kentjerone/laravel-chain-rule

v1.2.6 2025-10-20 02:07 UTC

This package is auto-updated.

Last update: 2025-12-20 02:27:27 UTC


README

A chainable validation rule builder for Laravel.
Simplifies writing complex validation rules by chaining methods together in a fluent, readable API .

Features

  • Chain simple rules : required(), nullable(), email(), string(), integer(), etc.
  • Chain parameterized rules : between(), after(), etc.
  • Conditional rules :
    • addIfTrue($condition, $rule) – adds the rule if the condition is true
    • addIfFalse($condition, $rule) – adds the rule if the condition is false
    • addIfNull($value, $rule) – adds the rule if the value is null
    • addIfNotNull($value, $rule) – adds the rule if the value is not null
    • addIfEmpty($value, $rule) – adds the rule if the value is empty (null, [], '', 0, false, or empty object)
    • addIfNotEmpty($value, $rule) – adds the rule if the value is not empty
    • addWhen(callable $callback, $rule) – adds the rule if the callback returns true
  • Security helpers:
    • stripTags(array $allowedHtmlTags) – validates that the field contains only the given safe tags (all others are rejected).
    • sanitizeXss(array $allowedHtmlTags) – blocks malicious content like <script>, onerror=, javascript: links, and alert() while allowing safe tags.
  • Automatic deduplication of rules to prevent duplicates when chaining or merging.
  • Returns rules as array (toArray()) or string (toString()).

Installation

Install via Composer:

composer require kentjerone/laravel-chain-rule

Usage in Form Requests

use KentJerone\ChainRule\ChainRule;
use Illuminate\Validation\Rule;

$request->validate([
    'email' => ChainRule::make()
        ->required()
        ->string()
        ->email(),

    'user_id' => ChainRule::make()
        ->required()
        ->integer()
        ->merge([Rule::unique('users', 'id')]),
]);

Conditional Example

$age = 20;

$rules = ChainRule::make()
    ->string()
    ->addIfTrue($age >= 18, 'required')
    ->addIfEmpty([], 'nullable')
    ->addWhen(fn() => $age < 18, 'min:18')

// Result: ['string', 'required', 'nullable']

Using ChainRule with Arrays and ChainRule Objects

$rules = ChainRule::make()
    ->string()
    ->addIfNotEmpty(['foo'], 'required')
    ->addIfNotNull('not null',chainRule()->max(255))

// Result: ['string', 'required', 'max:255']

Preventing XSS / Unsafe HTML

You can allow only safe HTML while rejecting scripts and malicious attributes:

$rules = ChainRule::make()
    ->sanitizeXss(['b','i','u','p','a'])

// ✅ passes: <p>Hello <b>World</b></p>
// ❌ fails: <script>alert(1)</script>
// ❌ fails: <a href="javascript:alert(1)">Click</a>

Or allow only specific tags:

$rules = ChainRule::make()
    ->stripTags(['b','i','u'])

// ✅ passes: <b>bold</b>
// ❌ fails: <div>not allowed</div>

Helper Method

For convenience, you can use the global chainRule() helper instead of ChainRule::make():

//example 1 helper
$rules1= chainRule()
    ->string()
    ->required()
    ->email()

//example 2 helper
$rules2 = cr()
    ->string()
    ->required()
    ->email()

// Result: ['string', 'required', 'email']

Mass Method

//example 1
$rules1 = chainRule()
    ->nullable_string_min_max(1, 255)

// Result: ['string', 'nullable', 'min:1', 'max:255']

//example 2
$rules2 = cr()
    ->nullable_string()

// Result: ['string', 'nullable']

LICENSE

The MIT License (MIT). Please see License File for more information.