PHPackages                             earc/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. earc/validator

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

earc/validator
==============

eArc - the explicit architecture framework - validation component

00PHP

Since Jul 5Pushed 4y agoCompare

[ Source](https://github.com/Koudela/eArc-validator)[ Packagist](https://packagist.org/packages/earc/validator)[ RSS](/packages/earc-validator/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

eArc-validator
==============

[](#earc-validator)

Lightweight dependency free validator component of the [earc framework](https://github.com/Koudela/eArc-core) for an [SOLID](https://en.wikipedia.org/wiki/SOLID) validation approach.

table of Contents
-----------------

[](#table-of-contents)

- [Install](#install)
- [bootstrap](#bootstrap)
- [configure](#configure)
- [basic usage](#basic-usage)
    - [check](#check)
    - [validate](#validate)
    - [chaining validation logic](#chaining-validation-logic)
        - [conjunction AND (AllOf)](#conjunction-and-allof)
        - [conjunction OR (OneOf)](#conjunction-or-oneof)
        - [conjunction XOR (NoneOf)](#conjunction-xor-noneof)
        - [logical inversion NOT](#logical-inversion-not)
        - [conditional validation WHEN](#conditional-validation-when)
    - [mixing validators](#mixing-validators)
    - [complete validation (assert)](#complete-validation-assert)
    - [locally individualized messages](#locally-individualized-messages)
        - [with key](#with-key)
        - [with](#with)
    - [object property validation (using attributes)](#object-property-validation-using-attributes)
- [advanced usage](#advanced-usage)
    - [globally individualized messages (localization)](#globally-individualized-messages-localization)
    - [extending the atomic validation logic](#extending-the-atomic-validation-logic)
- [releases](#releases)
    - [release 0.0](#release-00)

install
-------

[](#install)

```
$ composer require earc/validator
```

bootstrap
---------

[](#bootstrap)

The earc/validator does not require any bootstrapping.

configure
---------

[](#configure)

The earc/validator does not require any configuration.

basic usage
-----------

[](#basic-usage)

### check

[](#check)

Validation will be done in two steps.

1. Define the validation logic to validate against.
2. Check if the validation logic holds true against a target.

After setting the validation logic, checking can be repeated against different targets unlimited times.

```
use eArc\Validator\ValidatorFactory;

$validator = (new ValidatorFactory())->build();
$validator->email();

$validator->check('validator@example.com'); // returns true
$validator->check('no-email-address'); // returns false
```

By passing a `true` value as second parameter instead of returning true or false, an `AssertException` is thrown.

```
use eArc\Validator\Exceptions\AssertException;
use eArc\Validator\ValidatorFactory;

$validator = (new ValidatorFactory())->build();
$validator->email();

$throwOnNotValid = true;
try {
    $validator->check('no-email-address', $throwOnNotValid);
} catch (AssertException) {
    // email not valid
}
```

### validate

[](#validate)

To receive a message explaining why the value is not valid use `validate()` instead of `check()`. It returns a result object instead of a bool.

```
use eArc\Validator\Exceptions\AssertException;
use eArc\Validator\ValidatorFactory;

$validator = (new ValidatorFactory())->build();
$validator->email();

$result = $validator->validate('no-email-address');
if (!$result->isValid()) {
    echo $result->getFirstErrorMessage(); // echos 'has to be a valid email address'
}

$throwOnError = true;
try {
    $validator->validate('no-email-address', $throwOnError);
} catch (AssertException $e) {
    echo $e->getResult()->getFirstErrorMessage(); // echos 'has to be a valid email address'
}
```

You can [use your own messages](#locally-individualized-messages).

### chaining validation logic

[](#chaining-validation-logic)

#### conjunction AND (AllOf)

[](#conjunction-and-allof)

The simplest chain is build by the `AND` conjunction. You can do this in three different ways:

1. Subsequent calls to atomic validator methods.
2. Method chaining of atomic validator methods.
3. Use of the `AND()` or the `AllOf()` method.

The validation holds true, if all arguments validate to true.

```
use eArc\Validator\ValidatorFactory;

// 1. Subsequent calls to atomic validator methods:

$validator = (new ValidatorFactory())->build();
$validator->email();
$validator->regex('/@example\.com$/');

$validator->check('validator@example.com'); // returns true
$validator->check('validator@example.de'); // returns false
$validator->check('valid\\tor@example.com'); // returns false

// 2. Method chaining of atomic validator methods:

$validator = (new ValidatorFactory())
    ->build()
    ->email()
    ->regex('/@example\.com$/');

// 3.1 Use of the `AND()` method.

$validator = (new ValidatorFactory())->build();
$validator = $validator->AND(
    $validator->email(),
    $validator->regex('/@example\.com$/')
);

// 3.1 Use of the `AllOf()` method.

$validator = (new ValidatorFactory())->build();
$validator = $validator->AllOf(
    $validator->email(),
    $validator->regex('/@example\.com$/')
);
```

Choosing between or mixing them is just a case of syntactic preference.

#### conjunction OR (OneOf)

[](#conjunction-or-oneof)

The logical `OR` is represented by two methods `OR()` and `OneOf()`. Both take `Validators` as arguments.

The validation holds true, if one argument validates to true.

```
use eArc\Validator\ValidatorFactory;

$validator = (new ValidatorFactory())->build();
$validator = $validator->email()->OR(
    $validator->regex('/@example\.com$/'),
    $validator->regex('/@coding-crimes\.com$/'),
);

$validator->check('validator@example.com'); // returns true
$validator->check('validator@coding-crimes.com'); // returns true
$validator->check('validtor@example.de'); // returns false

// The above validation logic is equivalent to:

$validator = (new ValidatorFactory())->build();
$validator = $validator->email()->OneOf(
    $validator->regex('/@example\.com$/'),
    $validator->regex('/@coding-crimes\.com$/')
);
```

#### conjunction XOR (NoneOf)

[](#conjunction-xor-noneof)

The logical `XOR` is represented by two methods `XOR()` and `NoneOf()`. Both take `Validators` as arguments.

The validation holds true, if no argument validates to true.

```
use eArc\Validator\ValidatorFactory;

$validator = (new ValidatorFactory())->build();
$validator = $validator->email()->XOR(
    $validator->regex('/@blacklisted\.com$/'),
    $validator->regex('/@coding-crimes\.com$/'),
);

$validator->check('validator@example.com'); // returns true
$validator->check('validator@blacklisted.com'); // returns false
$validator->check('validator@coding-crimes.com'); // returns false

// The above validation logic is equivalent to:

$validator = (new ValidatorFactory())->build();
$validator = $validator->email()->NoneOf(
    $validator->regex('/@blacklisted\.com$/'),
    $validator->regex('/@coding-crimes\.com$/')
);
```

#### logical inversion NOT

[](#logical-inversion-not)

Logical inversion can be realized via the `NOT()` method.

`NOT()` holds true, if all arguments and the validation chain on the right-hand side validates to false.

#### conditional validation WHEN

[](#conditional-validation-when)

To realize conditional validation there exists the `WHEN()` method.

If the first argument is true, the second argument will be validated. Otherwise, the third argument will be used for validation, which defaults to true.

### mixing validators

[](#mixing-validators)

### complete validation (assert)

[](#complete-validation-assert)

### locally individualized messages

[](#locally-individualized-messages)

There are two methods that influence the validation error messages. `withKey()`sets the message key for the validation error message. `with()` sets the validation error message itself.

Messages can be [individualized on a global scope](#globally-individualized-messages-localization), too.

#### with key

[](#with-key)

#### with

[](#with)

### object property validation (using attributes)

[](#object-property-validation-using-attributes)

advanced usage
--------------

[](#advanced-usage)

### globally individualized messages (localization)

[](#globally-individualized-messages-localization)

### extending the atomic validation logic

[](#extending-the-atomic-validation-logic)

releases
--------

[](#releases)

### release 0.0

[](#release-00)

- first official release
- PHP ^8.0
- is coming soon (code base &lt;= release candidate status)
- TODO:
    - Documentation
    - Tests

###  Health Score

14

—

LowBetter than 3% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity28

Early-stage or recently created project

 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/b7308f2797252cace014cfec12c1b5978c1bf5608be78d7a188ff690192959f3?d=identicon)[Thomas Koudela](/maintainers/Thomas%20Koudela)

---

Top Contributors

[![Koudela](https://avatars.githubusercontent.com/u/21366492?v=4)](https://github.com/Koudela "Koudela (16 commits)")

### Embed Badge

![Health badge](/badges/earc-validator/health.svg)

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

###  Alternatives

[webmozart/assert

Assertions to validate method input/output with nice error messages.

7.7k882.5M1.1k](/packages/webmozart-assert)[composer/semver

Version comparison library that offers utilities, version constraint parsing and validation.

3.3k479.5M573](/packages/composer-semver)[laminas/laminas-escaper

Securely and safely escape HTML, HTML attributes, JavaScript, CSS, and URLs

21986.9M121](/packages/laminas-laminas-escaper)[swaggest/json-schema

High definition PHP structures with JSON-schema based validation

48612.2M70](/packages/swaggest-json-schema)[stevebauman/purify

An HTML Purifier / Sanitizer for Laravel

5245.4M14](/packages/stevebauman-purify)[ddeboer/vatin

Validate VAT identification numbers

1422.9M8](/packages/ddeboer-vatin)

PHPackages © 2026

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