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

ActiveLibrary

javeh/class-validator
=====================

Attribut based validation of Objects for PHP 8+.

00PHP

Since Nov 19Pushed 6mo agoCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

Class Validator
===============

[](#class-validator)

PHP 8 attribute-based validator for value objects and DTOs. Annotate public properties with reusable attributes (e.g. `#[Text(min: 3)]`, `#[Number(max: 10)]`) and let the `Validation` orchestrator return a field-to-error map with localized messages.

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

[](#installation)

Requires PHP &gt;= 8.0. Install with Composer:

```
composer require javeh/class-validator
```

Unit tests use PHPUnit 11; to run them locally the dev dependency is already declared in `composer.json`.

### Optional Dependencies

[](#optional-dependencies)

- **PCOV or Xdebug** if you want code coverage (`vendor/bin/phpunit --coverage-text`).

Getting Started
---------------

[](#getting-started)

```
use Javeh\ClassValidator\Validation;
use Javeh\ClassValidator\Attributes as Assert;

class UserInput
{
    #[Assert\NotEmpty]
    #[Assert\Text(min: 3, max: 50)]
    public string $name;

    #[Assert\Email]
    public string $email;

    #[Assert\Number(min: 1, max: 5, integer: true)]
    public int $priority;
}

$input = new UserInput();
$input->name = '';
$input->email = 'nope';
$input->priority = 10;

$validation = new Validation();
$errors = $validation->validate($input);
```

`$errors` returns an array like:

```
[
    'name' => ['The value may not be empty.'],
    'email' => ['The value must be a valid email address.'],
    'priority' => ['The number must be less than or equal to 5.'],
]
```

### Attribute Catalog

[](#attribute-catalog)

AttributeDescription`NotEmpty`Rejects empty values (falsy minus `0` constraints, uses `empty()`).`Text`Enforces string type, min/max length, exact length or regex pattern. Delegates to `Length` for long/short checks.`Length`Counts strings, arrays or countables (`length`, `min`, `max`).`Number`Numeric validation (min/max, integer-only, positive/negative flags, step multiplicity).`PositiveNumber`Alias of `Number` enforcing values greater than zero (integers or floats).`Id`Alias of `Number` with positive integer defaults (integer + positive).`Range`Numeric range check (int/float).`Email`, `Url`, `Regex`Specialized validators using PHP filters/regex.`Choice`Single or multiple selection against a list (strict or loose comparison).`Instance`Ensures the value is an object of a given class/interface.`Date`Parses `DateTime` according to a format with optional min/max boundaries.Add new attributes by implementing `Contracts\ValidationAttribute`.

Localization
------------

[](#localization)

Messages are translated through the `Translation` contract. By default `Validation` bootstraps an `ArrayTranslation` with German output and English fallback plus 23 other European languages (`resources/lang/*.php`). To change the language:

```
use Javeh\ClassValidator\Support\ArrayTranslation;
use Javeh\ClassValidator\Validation;

$translator = ArrayTranslation::withDefaults('en');
$translator->extend('en', ['validation.email' => 'Please enter a valid email.']);

$validation = new Validation($translator); // use translator explicitly
```

See `docs/translation.md` for custom parser/locale examples.

Advanced Usage
--------------

[](#advanced-usage)

- **Custom translations**: update locale files or call `$translator->extend()` / `$translator->addLocale()` to adjust copy without touching attribute constructors.
- **Optional fields**: validators treat `null` as "not set" (returns `true`) except `NotEmpty`. Combine `#[NotEmpty]` with other attributes for mandatory fields.
- **Multiple validators per property**: errors are collected in the order attributes are declared.
- **Integration tests/examples**: check `tests/Integration/ValidationIntegrationTest.php` for realistic DTO flows.

Running Tests
-------------

[](#running-tests)

```
vendor/bin/phpunit
vendor/bin/phpunit --coverage-text # coverage (requires pcov/xdebug)
```

CI/deployment is not bundled, but the suite is fast (dozens of tests) and covers ~84% of the codebase.

License
-------

[](#license)

MIT (see `LICENSE` if present). Pull requests welcome!

###  Health Score

17

—

LowBetter than 6% of packages

Maintenance47

Moderate activity, may be stable

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity13

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://avatars.githubusercontent.com/u/242641905?v=4)[javeh](/maintainers/javeh)[@javeh](https://github.com/javeh)

---

Top Contributors

[![janvehlow](https://avatars.githubusercontent.com/u/128021830?v=4)](https://github.com/janvehlow "janvehlow (29 commits)")

### Embed Badge

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

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

PHPackages © 2026

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