PHPackages                             konsulting/laravel-rule-repository - 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. konsulting/laravel-rule-repository

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

konsulting/laravel-rule-repository
==================================

A package to allow rules (for example validation rules) to be attached to a model.

0.3.0(3y ago)1255MITPHPPHP ^7.0||^8.0

Since Jan 15Pushed 3y ago2 watchersCompare

[ Source](https://github.com/konsulting/laravel-rule-repository)[ Packagist](https://packagist.org/packages/konsulting/laravel-rule-repository)[ RSS](/packages/konsulting-laravel-rule-repository/feed)WikiDiscussions master Synced 4w ago

READMEChangelog (3)Dependencies (2)Versions (6)Used By (0)

Laravel Rule Repository
=======================

[](#laravel-rule-repository)

[![Build Status](https://camo.githubusercontent.com/b098493b802260282a2a9b69a84edb88eaffe609f5fc17d62599bc95b5f4eb51/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6b6f6e73756c74696e672f6c61726176656c2d72756c652d7265706f7369746f72792f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/konsulting/laravel-rule-repository/build-status/master)[![Code Coverage](https://camo.githubusercontent.com/c27cab624fb0819ad7efbed383f06da59b85168c818719610d1b0ea9a6e52d65/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6b6f6e73756c74696e672f6c61726176656c2d72756c652d7265706f7369746f72792f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/konsulting/laravel-rule-repository/?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/cde1ec283b2cec087bab3b9a305234577f3358c0b8f59b95f82070723fc17713/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6b6f6e73756c74696e672f6c61726176656c2d72756c652d7265706f7369746f72792f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/konsulting/laravel-rule-repository/?branch=master)

A package to allow rules (for example validation rules) to be attached to a model, whilst avoiding storing them on the model itself or in a controller.

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

[](#installation)

Install via Composer:

```
composer require konsulting/laravel-rule-repository

```

Usage
-----

[](#usage)

Each model under validation should have its own validation repository attached to it, containing default validation rules and, optionally, validation rules for different states. You may, for example, require different validation rules when updating the model as opposed to creating it.

### Creating the repository

[](#creating-the-repository)

The validation repository must implement `Contracts\RuleRepository`, and as such must contain a `default()` method which returns an array of default rules. It may also contain any number of 'state' methods which contain differing rules.

**Note:**

- State methods names should use camel case.
- State-specific rules will be merged (non-recursively) with the default rules when they are retrieved.

### Extending the `AbstractRepository` class

[](#extending-the-abstractrepository-class)

The `AbstractRepository` class is provided with some helper functions to make defining rules easier. This class may be extended instead of directly implementing the interface.

Sometimes it's useful to append or prepend a rule to an existing list of rules, e.g. making values required only on model creation. This is possible with the following methods:

```
AbstractRepository::prependRule(string $rule, array $baseRules);
AbstractRepository::prependRules(array $rules, array $baseRules);

AbstractRepository::appendRule(string $rule, array $baseRules);
AbstractRepository::appendRules(array $rules, array $baseRules);
```

Example repository:

```
use Konsulting\Laravel\RuleRepository\AbstractRepository;

class UserRuleRepository extends AbstractRepository
{
    public function default() : array
    {
        return [
            'name'          => 'string',
            'email'         => 'string|email',
            'date_of_birth' => 'date',
        ];
    }

    public function create() : array
    {
        return $this->prependRule('required', $this->default());
    }
}
```

### Attaching the repository to the model

[](#attaching-the-repository-to-the-model)

The model to attach the rules to should use the `RuleRepositoryTrait` trait. It is recommended (but not required) that the model implements the interface `Contracts\HasRuleRepositories`.

The static property `$ruleRepositories` should be initialised to an associative array of repository class paths, with the repository name as the key.

```
use Konsulting\Laravel\RuleRepository\Contracts\HasRuleRepository;
use Konsulting\Laravel\RuleRepository\RuleRepositoryTrait;

class User extends Model implements HasRuleRepository
{
    use RuleRepositoryTrait;

    protected static $ruleRepositories = [
        'validation' => UserValidationRepository::class;
    ];
}
```

### Retrieving validation rules

[](#retrieving-validation-rules)

The model's validation rules can be retrieved using the static method `getRules($name, $state = 'default')`.

To return the default rules for the validation repository:

```
User::getRules('validation');

// or

User::getRules('validation', 'default');
```

To get state-specific rules:

```
User::getRules('validation', 'update');

User::getRules('validation', $state);
```

States may be named in either camel-case or snake-case.

#### Using magic methods

[](#using-magic-methods)

The `RepositoryMagicMethods` trait may also be added to the model to allow rules to be retrieved with a more concise syntax. Rules may be retrieved using:

```
Model::{$repositoryName . 'Rules'}($state = 'default');
```

For example:

```
User::validationRules();

User::validationRules('update');
```

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 95.2% 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 ~577 days

Total

4

Last Release

1353d ago

PHP version history (2 changes)0.1.0PHP ^7.0

0.3.0PHP ^7.0||^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/bae802ba5a70c050135022ed88fc558d2ac76ace8e5b67ce176f683d1a0afc1b?d=identicon)[rdarcy1](/maintainers/rdarcy1)

![](https://www.gravatar.com/avatar/fd6c694a38f1c925be7f746ef1c4079940760775af834a89c28c04f1f35af0ab?d=identicon)[keoghan](/maintainers/keoghan)

---

Top Contributors

[![rdarcy1](https://avatars.githubusercontent.com/u/15962421?v=4)](https://github.com/rdarcy1 "rdarcy1 (40 commits)")[![Keoghan](https://avatars.githubusercontent.com/u/6714599?v=4)](https://github.com/Keoghan "Keoghan (1 commits)")[![php-shift](https://avatars.githubusercontent.com/u/18268760?v=4)](https://github.com/php-shift "php-shift (1 commits)")

---

Tags

laravellaravel-validationmodel-view-controllervalidationlaravelvalidatorvalidationrulesrepositorytransformer

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/konsulting-laravel-rule-repository/health.svg)

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

###  Alternatives

[illuminatech/validation-composite

Allows uniting several validation rules into a single one for easy re-usage

180517.4k](/packages/illuminatech-validation-composite)[iutrace/laravel-cuit-validator

Argentinian CUIT and CUIL Validator

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

A phone validator for Laravel using the free Twilio phone lookup service.

2861.7k](/packages/stuyam-laravel-phone-validator)[laravel-validation-rules/timezone

Validate that a given timezone is valid.

2119.0k](/packages/laravel-validation-rules-timezone)

PHPackages © 2026

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