PHPackages                             ohtyap/laravel-sane-validator - 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. ohtyap/laravel-sane-validator

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

ohtyap/laravel-sane-validator
=============================

sane validation rule handling for laravel

1.1.0(1y ago)08[1 PRs](https://github.com/ohtyap/laravel-sane-validator/pulls)MITPHPCI passing

Since Nov 1Pushed 1y ago1 watchersCompare

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

READMEChangelogDependencies (4)Versions (4)Used By (0)

laravel-sane-validator
======================

[](#laravel-sane-validator)

This is a strongly opinionated patch of laravel's validator. When using the validator outside of an HTTP POST scope (e.g., validating some data structures), you might notice some shortcomings and unexpected behaviors from the validator. `laravel-sane-validator` tries fixing them.

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

[](#installation)

Install the package via `composer`:

```
composer require ohtyap/laravel-sane-validator

```

After that, declare to use `laravel-sane-validator` by setting the `resolver` in the validation factory:

```
public function boot(): void
{
    $this->app->extend('validator', function (Factory $factory) {
        $factory->resolver(function (Translator $translator, array $data, array $rules, array $messages, array $attributes) {
            return new \Ohtyap\LaravelSaneValidator\Validator($translator, $data, $rules, $messages, $attributes);
        });

        return $factory;
    });
}
```

Shortcomings of laravel's validator
-----------------------------------

[](#shortcomings-of-laravels-validator)

The first shortcoming is how the validator is handling implicit rules in combination of empty strings. The following quote is taken from the laravel documentation:

> By default, when an attribute being validated is not present or contains an empty string, normal validation rules, including custom rules, are not run.

This has the effect that input data might bypass your validation - especially when it comes to optional fields.

```
$rules = ['email' => 'email'];
$data = ['email' => ' '];

Validator::make($data, $rules)->passes(); // true
```

In the above example, the expectation would be that the validator fails because the input (a `space`) isn't (obviously) not a valid email address.

It's possible to enforce the validation of empty strings by using the `required` or `filled` rule. This is not very intuitive for optional fields, and it is very easy to forget to add `filled` (or `sometimes|required`).

Moreover, this brings the second shortcoming: laravel is making assumptions about how a non-empty value should look. It is impossible to pass `null` or `[]` - even if both are perfectly valid values - when using `required`. Consequently, combinations like `required|nullable` aren't possible.

`laravel-sane-validator` behavior
---------------------------------

[](#laravel-sane-validator-behavior)

When using this validator, implicit rules are always executed when the field is present in the input. That way, it is no longer possible for empty strings to bypass validation.

`required` (including the other special cases of `required*` like `requiredIf` ) has the same behavior as `present`. It's only checking if the field is present in the input. More explicit evaluation of what is considered valid is up to the developer and what is defined in the rules.

### Special note about `filled`

[](#special-note-about-filled)

`filled` is using the same behavior as in the original validator. An empty string is still considered invalid (same for an empty array, `null`, etc.). But it is possible to declare rules like `filled|nullable`.

Strategies to start using `laravel-sane-validator`
--------------------------------------------------

[](#strategies-to-start-using-laravel-sane-validator)

By default you install and register `laravel-sane-validator` and you are good to go. But this might be troublesome for existing and bigger application. A step-by-step approach might be a better way of changing the behavior of the validation.

### Enable the `laravel-sane-validator` behavior by default, disable is occasionally

[](#enable-the-laravel-sane-validator-behavior-by-default-disable-is-occasionally)

```
# `laravel-sane-validator` behavior
$validator = Validator::make($data, $rules);
$validator->passes();

# laravel default behavior
$validator = Validator::make($data, $rules);
$validator->disableSaneValidation();
$validator->passes();
```

### Disable the `laravel-sane-validator` behavior by default, enable is occasionally

[](#disable-the-laravel-sane-validator-behavior-by-default-enable-is-occasionally)

```
public function boot(): void
{
    \Ohtyap\LaravelSaneValidator\Validator::defaultEnableSaneValidation(false);
}
```

```
# `laravel-sane-validator` behavior
$validator = Validator::make($data, $rules);
$validator->enableSaneValidation();
$validator->passes();

# laravel default behavior
$validator = Validator::make($data, $rules);
$validator->passes();
```

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance38

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 92% 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 ~0 days

Total

2

Last Release

556d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/64fa57f5be9c392ddc1c107879548d6016b90fc5154fb79eca9683530fa8cf74?d=identicon)[ohtyap](/maintainers/ohtyap)

---

Top Contributors

[![ohtyap](https://avatars.githubusercontent.com/u/1170097?v=4)](https://github.com/ohtyap "ohtyap (23 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (2 commits)")

---

Tags

laravelvalidationlaravelvalidation

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/ohtyap-laravel-sane-validator/health.svg)

```
[![Health](https://phpackages.com/badges/ohtyap-laravel-sane-validator/health.svg)](https://phpackages.com/packages/ohtyap-laravel-sane-validator)
```

###  Alternatives

[propaganistas/laravel-phone

Adds phone number functionality to Laravel based on Google's libphonenumber API.

3.0k35.7M106](/packages/propaganistas-laravel-phone)[intervention/validation

Additional validation rules for the Laravel framework

6826.7M8](/packages/intervention-validation)[proengsoft/laravel-jsvalidation

Validate forms transparently with Javascript reusing your Laravel Validation Rules, Messages, and FormRequest

1.1k2.3M49](/packages/proengsoft-laravel-jsvalidation)[axlon/laravel-postal-code-validation

Worldwide postal code validation for Laravel and Lumen

3853.3M1](/packages/axlon-laravel-postal-code-validation)[wendelladriel/laravel-validated-dto

Data Transfer Objects with validation for Laravel applications

759569.4k13](/packages/wendelladriel-laravel-validated-dto)[laravel-validation-rules/credit-card

Validate credit card number, expiration date, cvc

2412.2M5](/packages/laravel-validation-rules-credit-card)

PHPackages © 2026

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