PHPackages                             henzeb/ruler-laravel - 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. henzeb/ruler-laravel

ActiveLibrary

henzeb/ruler-laravel
====================

extends Laravel's validator using their own Rule interface

v2.0(2mo ago)1157↓100%AGPL-3.0-onlyPHPPHP ^8.3CI passing

Since Feb 13Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/henzeb/ruler-laravel)[ Packagist](https://packagist.org/packages/henzeb/ruler-laravel)[ Docs](https://github.com/henzeb/ruler-laravel)[ RSS](/packages/henzeb-ruler-laravel/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (7)Dependencies (3)Versions (8)Used By (0)

Ruler for Laravel
=================

[](#ruler-for-laravel)

[![Build Status](https://github.com/henzeb/ruler-laravel/workflows/tests/badge.svg)](https://github.com/henzeb/ruler-laravel/actions)[![Latest Version on Packagist](https://camo.githubusercontent.com/d75f4394bfb3f7d1709a9c76b568a01f7f2d4773ffd0b384baaec10a1ea8f002/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f68656e7a65622f72756c65722d6c61726176656c2e737667)](https://packagist.org/packages/henzeb/ruler-laravel)[![Total Downloads](https://camo.githubusercontent.com/8763afa2689ec1f358dcf1742cfe52dbec415b96deb6db94790302f525ee7eb4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f68656e7a65622f72756c65722d6c61726176656c2e737667)](https://packagist.org/packages/henzeb/ruler-laravel)[![Test Coverage](https://camo.githubusercontent.com/5a46e0e282d4311ca90545f68787871ca4feb55b48839b27d7d360f310cae8e9/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f65393965393465653966613561396134303930312f746573745f636f766572616765)](https://codeclimate.com/github/henzeb/ruler-laravel/test_coverage)

This library enhances the way you work with Laravel's validation Rules. It allows you to use your Rule classes as string-based validators and group rules into reusable rulesets.

For example: Laravel has bundled a rule for enums. As the parameter you pass is a string, you should be able to use it like this:

```
 [
   'attribute'=>'required|enum:App\Enums\YourEnum'
 ]
```

right?

wrong!

In order to use it like this, you need to extend your Validator and use the `Illuminate\Validation\Rules\Enum` rule, or create your own version. This library simplifies that process.

I've gone ahead and registered `enum` for you, but you can just as easily add your own rules.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require henzeb/ruler-laravel
```

note: I only support PHP ^8.1 because of the enums.

Usage
-----

[](#usage)

Simply add the `Henzeb\Ruler\Concerns\Ruler` trait to your service provider and add your rules to the `$rules` property.

```
use Henzeb\Ruler\Concerns\Ruler;
use Illuminate\Support\ServiceProvider;
use App\Rules\YourRule;
use App\Rules\YourOtherRule;

class YourProvider extends ServiceProvider
{
    use Ruler;

    private array $rules = [
        'your_rule' => YourRule::class,
        YourOtherRule::class
    ];
}
```

You can either specify a name, or just let Ruler create a name for you.

For example: `YourOtherRule` will get the name `your_other_rule`.

If your provider needs to implement the boot method, just call the `bootRuler`method inside that `boot` method

```
use Henzeb\Ruler\Concerns\Ruler;
use Illuminate\Support\ServiceProvider;
use App\Rules\YourRule;
use App\Rules\YourOtherRule;

class YourProvider extends ServiceProvider
{
    use Ruler;

    private array $rules = [
        'your_rule' => YourRule::class,
        YourOtherRule::class
    ];

    public function boot() {
        $this->bootRuler();
        // your code
    }
}
```

It is also possible to do it yourself in case you need to do it conditionally.

```
use Henzeb\Ruler\Concerns\Ruler;
use Illuminate\Support\ServiceProvider;
use App\Rules\YourRule;
use App\Rules\YourOtherRule;

class YourProvider extends ServiceProvider
{
    use Ruler;

    public function boot() {
        if(/** some condition */) {
            $this->rule(YourRule::class, 'your_rule');
        }

        if(/** some condition */) {
            $this->rule(YourOtherRule::class);
        }
        // your code
    }
}
```

#### Registering rules via the Rule facade

[](#registering-rules-via-the-rule-facade)

You can also register rules directly using the `Rule` facade, without needing a service provider:

```
use Illuminate\Validation\Rule;
use App\Rules\YourRule;
use App\Rules\YourOtherRule;

Rule::register(YourRule::class, 'your_rule');
Rule::register(YourOtherRule::class);
```

You can also pass an array to register multiple rules at once:

```
Rule::register([
    'your_rule' => YourRule::class,
    YourOtherRule::class,
]);
```

### The Rule class

[](#the-rule-class)

The rules are implemented just like any other `Rule` you'd normally define. So you'll be familiar with the implementation.

```
use Illuminate\Contracts\Validation\Rule;

class YourRule implements Rule {

    public function passes($attribute, $value)
    {
        // your validation code
    }

    public function message()
    {
        return 'Message when fails';
    }
}
```

Note: You can also use invokable rules here.

#### Parameters

[](#parameters)

You can use parameters. Just add a constructor with the parameters in the order you'd like to use them. Optional parameters are supported.

Note: As for now, you'll receive them as strings, no casting to other scalars is done at this time.

```
public function __construct(private string $param1, private string $param2 = null){}
```

#### Implicit rules

[](#implicit-rules)

To add an implicit rule, all you have to do is implement the `Illuminate\Contracts\Validation\ImplicitRule` interface. Ruler will do the rest for you.

```
use Illuminate\Contracts\Validation\ImplicitRule;

class YourImplicitRule implements ImplicitRule {
    // the code
}
```

#### Dependent rules

[](#dependent-rules)

To add a dependent rule, just implement the `Illuminate\Contracts\Validation\DataAwareRule` interface. interface Ruler will do the rest for you.

```
use Illuminate\Contracts\Validation\DataAwareRule;

class DependentRule implements DataAwareRule {
    private array $data;

    public function setData(array $data) {
        $this->data = $data;
        return $this;
    }

    // your code
}
```

##### Validator aware rules

[](#validator-aware-rules)

Ruler also supports Validator aware rules. Just implement the required interface.

#### mixing up interfaces

[](#mixing-up-interfaces)

You can mix up the interfaces just like you would in vanilla Laravel. For instance: A rule that is `implicit` can also be `dependent`

### The error message

[](#the-error-message)

The error message should be placed in the `message` method as defined in `Illuminate\Contracts\Validation\Rule`, just as you normally would.

The message method is called dynamically, which means you can store the message in your `Rule` instance and return it in the `message` method.

```
use Henzeb\Ruler\Contracts\ReplacerAwareRule;
use Illuminate\Contracts\Validation\Rule;

class YourRule implements Rule
{
    return string $message = 'Something went wrong';

    public __construct($param_1, $paramTwo) {}

    public function passes($attribute, $value)
    {
        $this->message = 'Your error message';
        return false;
    }

    public function message()
    {
        return return $this->message;
    }
}
```

It also supports returning arrays. When an array is returned, the `MessageBag` contains the messages as if they were coming from different validation rules. This way your `Rule`can do grouped validations (for instance using another Validator instance).

#### replacers

[](#replacers)

Out of the box, you can use `:` to point to a parameter, but if you want them named, you can use the `Henzeb\Ruler\Contracts\ReplacerAwareRule` interface.

```
use Henzeb\Ruler\Contracts\ReplacerAwareRule;

class YourRule implements ReplacerAwareRule
{
    public __construct($param_1, $paramTwo) {}

    public function message()
    {
        return ':attribute :param_1 :paramTwo';
    }

    public function replacers(): array
    {
        return [
            'param_1',
            'paramTwo'
        ];
    }
}
```

The parameters are in order as specified. `param_1` will point to the value of `$param_1` and so on.

#### Closures

[](#closures)

You can add a `Closure` to a replacer if you have specific needs. A `Closure` always receives the value of the current parameter, the attributes name, the other parameters (named) and all the fields that are under validation.

```
class YourRule implements ReplacerAwareRule
{
    public function message()
    {
        return ':attribute :param_1 :paramTwo';
    }

    public function replacers(): array
    {
        return [
            'param_1'=> function(string $value, string $attribute, array $parameters, array $data) {
                return 'any string'.$parameters['paramTwo']
            },
            'paramTwo'
        ];
    }
}
```

### Ruleset

[](#ruleset)

The `Ruleset` class allows you to group multiple validation rules into a single reusable rule object. Instead of writing a custom `Rule` with manual validation logic, you define a set of existing Laravel rules that are applied together.

#### Basic usage

[](#basic-usage)

Extend the `Henzeb\Ruler\Ruleset` class and implement the `rules` method:

```
use Henzeb\Ruler\Ruleset;

class NameRuleset extends Ruleset
{
    protected function rules(): array
    {
        return ['required', 'string', 'min:3'];
    }
}
```

Then use it like any other rule object:

```
Validator::make($data, [
    'name' => [new NameRuleset()],
]);
```

You can also return rules as a string:

```
protected function rules(): string
{
    return 'required|string|min:3';
}
```

#### Implicit behavior

[](#implicit-behavior)

By default, a `Ruleset` is implicit, meaning it validates even when the value is empty. You can disable this by setting the `$implicit` property to `false`:

```
$ruleset = new NameRuleset();
$ruleset->implicit = false;
```

#### Custom messages and attributes

[](#custom-messages-and-attributes)

Override the `messages` and `attributes` methods to customize error messages and attribute names:

```
use Henzeb\Ruler\Ruleset;

class NameRuleset extends Ruleset
{
    protected function rules(): array
    {
        return ['required', 'string', 'min:3'];
    }

    protected function messages(): array
    {
        return ['min' => 'Too short!'];
    }

    protected function attributes(): array
    {
        return ['name' => 'username'];
    }
}
```

#### Configuring the inner validator

[](#configuring-the-inner-validator)

The `configure` method gives you access to the underlying `Validator` instance, allowing you to use features like `stopOnFirstFailure` or `sometimes`:

```
use Henzeb\Ruler\Ruleset;
use Illuminate\Validation\Validator;

class NameRuleset extends Ruleset
{
    protected function rules(): array
    {
        return ['required', 'string', 'min:3'];
    }

    protected function configure(Validator $validator): void
    {
        $validator->stopOnFirstFailure();
    }
}
```

#### Using with the Ruler trait

[](#using-with-the-ruler-trait)

A `Ruleset` can also be registered as a string-based rule via the `Ruler` trait, just like any other rule:

```
private array $rules = [
    'name_ruleset' => NameRuleset::class,
];
```

This allows you to use it as a string:

```
Validator::make($data, [
    'name' => 'name_ruleset',
]);
```

### Overriding the Validator resolver

[](#overriding-the-validator-resolver)

Because Ruler has a custom Validator instance set to resolve by Laravel, you need to extend the `Henzeb\Ruler\Validator\RulerValidator` class in case you want to change the resolver.

### Testing

[](#testing)

```
composer test
```

### Changelog

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

### Security

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Henze Berkheij](https://github.com/henzeb)

License
-------

[](#license)

The GNU AGPLv. Please see [License File](LICENSE.md) for more information.

###  Health Score

47

—

FairBetter than 94% of packages

Maintenance84

Actively maintained with recent releases

Popularity14

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity69

Established project with proven stability

 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 ~244 days

Recently: every ~361 days

Total

7

Last Release

83d ago

Major Versions

v1.4.0 → v2.02026-02-18

PHP version history (2 changes)v1.0.0PHP ^8.1

v2.0PHP ^8.3

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/15928532?v=4)[henzeb](/maintainers/henzeb)[@henzeb](https://github.com/henzeb)

---

Top Contributors

[![henzeb](https://avatars.githubusercontent.com/u/15928532?v=4)](https://github.com/henzeb "henzeb (11 commits)")

---

Tags

henzebruler-laravel

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/henzeb-ruler-laravel/health.svg)

```
[![Health](https://phpackages.com/badges/henzeb-ruler-laravel/health.svg)](https://phpackages.com/packages/henzeb-ruler-laravel)
```

###  Alternatives

[fumeapp/modeltyper

Generate TypeScript interfaces from Laravel Models

196277.9k](/packages/fumeapp-modeltyper)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

255.2k](/packages/aedart-athenaeum)[henzeb/laravel-cache-index

Flexible replacement for tags

1213.9k](/packages/henzeb-laravel-cache-index)

PHPackages © 2026

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