PHPackages                             kentjerone/laravel-chain-rule - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. kentjerone/laravel-chain-rule

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

kentjerone/laravel-chain-rule
=============================

A chainable rule builder for Laravel validation.

v1.2.6(8mo ago)0761↓90.5%MITPHPPHP ^8.2CI passing

Since Sep 22Pushed 8mo agoCompare

[ Source](https://github.com/ramosramosramos/laravel-chain-rule)[ Packagist](https://packagist.org/packages/kentjerone/laravel-chain-rule)[ RSS](/packages/kentjerone-laravel-chain-rule/feed)WikiDiscussions main Synced 2d ago

READMEChangelog (4)Dependencies (5)Versions (15)Used By (0)

Laravel ChainRule
=================

[](#laravel-chainrule)

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

Features
--------

[](#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 ``, `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
------------

[](#installation)

Install via Composer:

```
composer require kentjerone/laravel-chain-rule
```

Usage in Form Requests
----------------------

[](#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

[](#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

[](#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

[](#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: Hello World
// ❌ fails: alert(1)
// ❌ fails: Click
```

Or allow only specific tags:

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

// ✅ passes: bold
// ❌ fails: not allowed
```

Helper Method
-------------

[](#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
-----------

[](#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
-------

[](#license)

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

###  Health Score

38

—

LowBetter than 83% of packages

Maintenance59

Moderate activity, may be stable

Popularity17

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~2 days

Total

13

Last Release

257d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/167238174?v=4)[Kent Jerone Ramos](/maintainers/ramosramosramos)[@ramosramosramos](https://github.com/ramosramosramos)

---

Top Contributors

[![ramosramosramos](https://avatars.githubusercontent.com/u/167238174?v=4)](https://github.com/ramosramosramos "ramosramosramos (34 commits)")

---

Tags

laravelvalidationbuilderfluentruleschainableform-request

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/kentjerone-laravel-chain-rule/health.svg)

```
[![Health](https://phpackages.com/badges/kentjerone-laravel-chain-rule/health.svg)](https://phpackages.com/packages/kentjerone-laravel-chain-rule)
```

###  Alternatives

[axlon/laravel-postal-code-validation

Worldwide postal code validation for Laravel

3893.6M1](/packages/axlon-laravel-postal-code-validation)[laravel-validation-rules/credit-card

Validate credit card number, expiration date, cvc

2462.4M7](/packages/laravel-validation-rules-credit-card)[sandermuller/laravel-fluent-validation

Fluent validation rule builders for Laravel

20518.8k4](/packages/sandermuller-laravel-fluent-validation)[timacdonald/rule-builder

A fluent rule builder for Laravel validation rule generation.

1027.9k](/packages/timacdonald-rule-builder)[resultsystems/validation

Inspired 'KennedyTedesco Validation' - The power of 'Respect Validation' on Laravel.

2832.8k4](/packages/resultsystems-validation)[iutrace/laravel-cuit-validator

Argentinian CUIT and CUIL Validator

1013.4k](/packages/iutrace-laravel-cuit-validator)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
