PHPackages                             fatjon-lleshi/antares-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. fatjon-lleshi/antares-validation

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

fatjon-lleshi/antares-validation
================================

Lightweight DTO hydration and validation for PHP 8.2+

v0.1.4(1mo ago)0151MITPHPPHP &gt;=8.2

Since Apr 24Pushed 1mo agoCompare

[ Source](https://github.com/johnlesis/antares-validation)[ Packagist](https://packagist.org/packages/fatjon-lleshi/antares-validation)[ Docs](https://github.com/johnlesis/antares-validation)[ RSS](/packages/fatjon-lleshi-antares-validation/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependencies (3)Versions (6)Used By (1)

antares-validation
==================

[](#antares-validation)

Lightweight DTO hydration and validation for PHP 8.2+.

Part of the [Antares](https://github.com/johnlesis/antares-framework) framework — but usable standalone in any PHP project.

Requirements
------------

[](#requirements)

- PHP 8.2+
- PSR-7 HTTP Message

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

[](#installation)

```
composer require fatjon-lleshi/antares-validation
```

Overview
--------

[](#overview)

This package provides two things:

- **Hydrator** — takes raw input data and maps it into a typed readonly DTO
- **Validator** — validates DTO properties using PHP attributes

They work together: the Hydrator hydrates the DTO, the Validator validates it. You can also use the Validator independently on any class.

Defining a DTO
--------------

[](#defining-a-dto)

Mark your DTO class with `#[Dto]` and declare properties as readonly constructor parameters:

```
use Antares\Validation\Attributes\Dto;
use Antares\Validation\Attributes\NotBlank;
use Antares\Validation\Attributes\Email;
use Antares\Validation\Attributes\Min;

#[Dto]
readonly class CreateUserRequest
{
    public function __construct(
        #[NotBlank]
        public string $name,

        #[NotBlank, Email]
        public string $email,

        #[Min(18)]
        public int $age,
    ) {}
}
```

Hydration
---------

[](#hydration)

The `Hydrator` takes raw array data and produces a hydrated DTO instance:

```
use Antares\Hydration\Hydrator;

$hydrator = new Hydrator();

$data = json_decode((string) $request->getBody(), true);

$dto = $hydrator->hydrate(CreateUserRequest::class, $data);
```

If hydration fails (missing required field, wrong type), a `HydrationException` is thrown.

### Strict Mode

[](#strict-mode)

Apply `#[Strict]` to the DTO class to reject unknown input fields. Without it, extra fields are silently ignored:

```
use Antares\Validation\Attributes\Strict;

#[Dto, Strict]
readonly class CreateUserRequest
{
    public function __construct(
        public string $name,
        public int $age,
    ) {}
}
```

Passing `['name' => 'John', 'age' => 25, 'role' => 'admin']` will throw a `HydrationException` because `role` is not a known field.

```

### Nested DTOs

Nested readonly DTOs are hydrated recursively:

```php
#[Dto]
readonly class AddressRequest
{
    public function __construct(
        #[NotBlank]
        public string $city,
        #[NotBlank]
        public string $country,
    ) {}
}

#[Dto]
readonly class CreateUserRequest
{
    public function __construct(
        #[NotBlank]
        public string $name,
        public AddressRequest $address,
    ) {}
}

```

Validation
----------

[](#validation)

The `Validator` inspects a DTO instance and collects all validation errors:

```
use Antares\Validation\Validator;
use Antares\Validation\Exceptions\ValidationException;

$validator = new Validator();

try {
    $validator->validate($dto);
} catch (ValidationException $e) {
    $errors = $e->getErrors();
}
```

Validation collects all errors before throwing — you get the full list, not just the first failure.

Available Attributes
--------------------

[](#available-attributes)

### Strings

[](#strings)

AttributeDescription`#[NotBlank]`Value must not be empty`#[MinLength(n)]`Minimum string length`#[MaxLength(n)]`Maximum string length`#[Email]`Valid email address`#[Url]`Valid URL`#[Pattern('/regex/')]`Matches regular expression`#[Alpha]`Alphabetic characters only`#[AlphaNumeric]`Alphanumeric characters only`#[Numeric]`Numeric string`#[HexColor]`Valid hex color (`#fff` or `#ffffff`)`#[Uuid]`Valid UUID v4`#[Phone]`Valid phone number`#[Ip]`Valid IP address (v4 or v6)`#[Json]`Valid JSON string### Numbers

[](#numbers)

AttributeDescription`#[Min(n)]`Minimum numeric value`#[Max(n)]`Maximum numeric value`#[Between(min, max)]`Value within range (inclusive)`#[Positive]`Value must be positive`#[Negative]`Value must be negative### General

[](#general)

AttributeDescription`#[NotNull]`Value must not be null`#[In([...])]`Value must be in list`#[InEnum(MyEnum::class)]`Value must be a valid enum case`#[Size(n)]`Array must have exactly n elements`#[ArrayOf(type)]`Array elements must all be of given type`#[Date]`Valid date string (`Y-m-d`)`#[DateTime]`Valid datetime string (`Y-m-d H:i:s`)### DTO

[](#dto)

AttributeDescription`#[Dto]`Marks class as a hydratable DTO`#[Strict]`Enables strict type checking during hydrationCustom Validation Attributes
----------------------------

[](#custom-validation-attributes)

Implement `ValidationAttribute` to create your own:

```
use Antares\Validation\Attributes\ValidationAttribute;
use Attribute;

#[Attribute(Attribute::TARGET_PARAMETER)]
final class Slug implements ValidationAttribute
{
    public function validate(mixed $value): ?string
    {
        if (!preg_match('/^[a-z0-9]+(?:-[a-z0-9]+)*$/', (string) $value)) {
            return 'Must be a valid slug.';
        }

        return null;
    }
}
```

Return a string error message if validation fails, `null` if it passes.

Exceptions
----------

[](#exceptions)

ExceptionThrown when`HydrationException`Raw data cannot be mapped to the DTO`ValidationException`One or more validation rules fail```
use Antares\Hydration\Exceptions\HydrationException;
use Antares\Validation\Exceptions\ValidationException;

try {
    $dto = $hydrator->hydrate(CreateUserRequest::class, $data);
    $validator->validate($dto);
} catch (HydrationException $e) {
    // 400 Bad Request
} catch (ValidationException $e) {
    // 422 Unprocessable Entity
    $errors = $e->getErrors(); // ['email' => 'Must be a valid email address.']
}
```

Standalone Usage
----------------

[](#standalone-usage)

This package has no dependency on the Antares framework core. You can use it in any PHP 8.2+ project:

```
composer require fatjon-lleshi/antares-validation
```

License
-------

[](#license)

MIT — [Fatjon Lleshi](https://github.com/johnlesis)

###  Health Score

38

—

LowBetter than 83% of packages

Maintenance91

Actively maintained with recent releases

Popularity6

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity40

Maturing project, gaining track record

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

Total

5

Last Release

44d ago

### Community

Maintainers

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

---

Top Contributors

[![johnlesis](https://avatars.githubusercontent.com/u/127132076?v=4)](https://github.com/johnlesis "johnlesis (17 commits)")

---

Tags

phpvalidationdtohydration

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/fatjon-lleshi-antares-validation/health.svg)

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

###  Alternatives

[aporat/store-receipt-validator

PHP receipt validator for Apple App Store and Amazon Appstore

6503.9M11](/packages/aporat-store-receipt-validator)[telnyx/telnyx-php

Official Telnyx PHP SDK — APIs for Voice, SMS, MMS, WhatsApp, Fax, SIP Trunking, Wireless IoT, Call Control, and more. Build global communications on Telnyx's private carrier-grade network.

35729.6k2](/packages/telnyx-telnyx-php)[yorcreative/laravel-argonaut-dto

Argonaut is a lightweight Data Transfer Object (DTO) package for Laravel that supports nested casting, recursive serialization, and validation out of the box. Ideal for service layers, APIs, and clean architecture workflows.

1063.3k2](/packages/yorcreative-laravel-argonaut-dto)[fab2s/dt0

Immutable DTOs with bidirectional casting. No framework required. 8x faster than the alternative.

101.9k1](/packages/fab2s-dt0)

PHPackages © 2026

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