PHPackages                             xtompie/validation - 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. xtompie/validation

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

xtompie/validation
==================

Validation component

4.2(2y ago)16.9k↓39.6%MITPHPPHP &gt;=8.0

Since Dec 2Pushed 2y ago1 watchersCompare

[ Source](https://github.com/xtompie/validation)[ Packagist](https://packagist.org/packages/xtompie/validation)[ RSS](/packages/xtompie-validation/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (18)Used By (0)

Validation
==========

[](#validation)

Validation component to validate models, input data. Handle any data types - arrays, objects, scalars, getter methods. Easy to extend. Type hinting / autocompletion. Fluent syntax.

```
use Xtompie\Validation\Validation;

$result = Validation::of($input)
    ->key('email')->required()->email()
    ->key('password')->required()->min(3)
    ->group()
    ->main('password')->callback(fn($input) => $input['email'] != $input['password'])
    ->group()
    ->key('email')->callback(fn($email) => !inUse($email))
    ->result();
```

`$result` is [`Xtompie\Result\Result`](https://github.com/xtompie/result)

Requiments
----------

[](#requiments)

PHP &gt;= 8.0

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

[](#installation)

Using [composer](https://getcomposer.org/)

```
composer require xtompie/validation
```

Docs
----

[](#docs)

### Subject

[](#subject)

Validation subject can be provided by

```
Validation::of($input);
Validation::new()->withSubject($input);
Validation::new()->validate($input);
```

### Groups

[](#groups)

```
Validation::of($input)
    /* Group 1 */
    ->group()
    /* Group 2 */
    ->group()
    /* Group 3 */
    ;
```

If an error occurs during group validation, subsequent groups will not be validated and validation will stop.

### Targets

[](#targets)

```
Validation::new()
    ->main() // validation will target main subject
    ->property($name) // when subject is an object, will target property named $name
    ->method($name) // when subject is an object, will target getter method named $name
    ->key($key) // when subject is an array, will target array value where key is $key
    ->take($callback) // custom target $callback, as first argument main subject will be given
;
```

### Nested Target

[](#nested-target)

Targets can be nested e.g.

```
    $validation = Validation::of(['person' => ['name' => 'John']])
        ->key('person')
        ->nested()->key('name')->required()->lengthMin(10)
    ;
    $validation->errors()->first()->space(); // person.name
```

After nested() function targets are related to last target. Nested can be reset by `unested()`, `group()` or `main()` target. Nested can be composed in multiple levels downwards Space in error is automaticly generated.

### Filters

[](#filters)

Filters are applied before validators

```
Validation::new()
    ->key('name')
        ->filter(fn($x) => ucfirst($x)) // custom callback filter
        ->trim()
;
```

### Required/Optional

[](#requiredoptional)

Targets are optional by default. If target is required use required method.

```
Validation::new()
    ->key('name')->required()
;
```

### Validators

[](#validators)

```
Validation::new()
    ->key('name')
    // raw validator, validator return Result
    ->validator(fn ($value) => strlen($value) !== 13 ? Result::ofSuccess() : Result::ofErrorMsg('Length can not be 13'))
    // custom callback
    ->callback(fn ($value) => strlen($value) !== 13, 'Length can not be 13')
    ->notBlank('Fill name!')
;
```

All list validator in source

### Scalars

[](#scalars)

```
$ok = Validation::of($email)->required()->email()->success();
```

If no target is provided, then the main target, validation subject, will be used.

### Validation feedback

[](#validation-feedback)

```
$v = Validation::new();
$v->result(); // Xtompie\Result\Result
$v->errors(); // Xtompie\Result\ErrorCollection
$v->error(); // ?Xtompie\Result\Error first error
$v->success(); // bool
$v->fail(); // bool
```

### Extending

[](#extending)

Component consists of 3 elements.

1. ValidationValidator - builder and validator.
2. ValidationCore - wrapper for the ValidationValidator. Gives fluent syntax, deals with validation subject.
3. Validation - extends ValidationCore by inheritance. Gives concrete validations, filters, messages, keys.

#### Inheritance

[](#inheritance)

Validation or ValidationCore can be extended by inheritance.

```
namespace App\Shared\Validation;

use App\Shared\Dao\Dao;
use Xtompie\Validation\Validation as BaseValidation;

class Validation extends BaseValidation
{
    public function __construct(
        protected Dao $dao,
    ) {}

    protected function msgs(): array
    {
        return array_merge(parent::msgs(), [
            'dao_not_exists' => 'Value {value} already exists',
        ]);
    }

    public function trim(): static
    {
        return $this->filter(fn($v) => trim($v));
    }

    public function digit($msg = 'Only digits allowed', $key = 'digit'): static
    {
        return $this->validator(fn($v) => ctype_digit($v) ? Result::ofSucces() : Result::ofErrorMsg($msg, $key));
    }

    public function daoNotExists(string $table, string $field, ?string $exceptId = null, ?string $msg = null)
    {
        return $this->validator(fn ($v) => $this->test(
            !$this->dao->exists($table, [$field => $v, 'id !=' => $exceptId]),
            'dao_not_exists',
            $msg,
            ['{value}' => $v]
        ));
    }
}

namespace App\User\Application\Service;
use App\Shared\Validation\Validation;

class CreateUserService
{
    public function __construct(
        protected Validation $validation,
    ) {}

    public function __invoke(string $email): Result
    {
        $result = $this->validation->withSubject($email)
            ->required()
            ->email()
            ->daoNotExists('user', 'email')
        ;
        if ($result->fail()) {
            return $result;
        }

        // create user

        return Result::ofSuccess();
    }
}
```

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity25

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity63

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

Recently: every ~79 days

Total

16

Last Release

877d ago

Major Versions

1.5 → 2.02022-08-28

2.2 → 3.02023-01-31

3.1 → 4.02023-03-12

### Community

Maintainers

![](https://www.gravatar.com/avatar/0c1316cdb671814b72350d75b23d7cafbde97ead0c21ac5613171a298bb15736?d=identicon)[xtompie](/maintainers/xtompie)

---

Top Contributors

[![xtompie](https://avatars.githubusercontent.com/u/69162230?v=4)](https://github.com/xtompie "xtompie (23 commits)")

---

Tags

validationinput

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/xtompie-validation/health.svg)

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

###  Alternatives

[composer/semver

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

3.3k489.6M672](/packages/composer-semver)[giggsey/libphonenumber-for-php

A library for parsing, formatting, storing and validating international phone numbers, a PHP Port of Google's libphonenumber.

5.0k148.7M416](/packages/giggsey-libphonenumber-for-php)[respect/validation

The most awesome validation engine ever created for PHP

5.9k37.4M383](/packages/respect-validation)[propaganistas/laravel-phone

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

3.0k35.7M107](/packages/propaganistas-laravel-phone)[opis/json-schema

Json Schema Validator for PHP

64736.9M186](/packages/opis-json-schema)[giggsey/libphonenumber-for-php-lite

A lite version of giggsey/libphonenumber-for-php, which is a PHP Port of Google's libphonenumber

8912.9M47](/packages/giggsey-libphonenumber-for-php-lite)

PHPackages © 2026

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