kentjerone/laravel-chain-rule
A chainable rule builder for Laravel validation.
1,013
| Install | |
|---|---|
composer require kentjerone/laravel-chain-rule |
|
| Latest Version: | v1.2.6 |
| PHP: | ^8.2 |
| License: | MIT |
| Last Updated: | Oct 20, 2025 |
| Links: | GitHub · Packagist |
Maintainer: ramosramosramos
Laravel ChainRule
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 istrueaddIfFalse($condition, $rule)– adds the rule if the condition isfalseaddIfNull($value, $rule)– adds the rule if the value isnulladdIfNotNull($value, $rule)– adds the rule if the value is notnulladdIfEmpty($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 emptyaddWhen(callable $callback, $rule)– adds the rule if the callback returnstrue
- 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, andalert()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.
Related Packages
resultsystems/validation
Inspired 'KennedyTedesco Validation' - The power of 'Respect Validation' on Lara...
33,105
28
freedfelipe/validation
Inspired 'KennedyTedesco Validation' - The power of 'Respect Validation' on Lara...
68
1