PHPackages                             aneterial/laravel-data-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. aneterial/laravel-data-validator

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

aneterial/laravel-data-validator
================================

Laravel package for convenient validation and hydration of a request into DTO structures

1.0.1(1y ago)018MITPHPPHP &gt;=8.2

Since Jun 2Pushed 1y ago1 watchersCompare

[ Source](https://github.com/aneterial/laravel-data-validator)[ Packagist](https://packagist.org/packages/aneterial/laravel-data-validator)[ Docs](https://github.com/aneterial/laravel-data-validator)[ RSS](/packages/aneterial-laravel-data-validator/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (4)Versions (6)Used By (0)

laravel-data-validator
======================

[](#laravel-data-validator)

 [![Build Status](https://github.com/aneterial/laravel-data-validator/actions/workflows/tests.yml/badge.svg)](https://github.com/aneterial/laravel-data-validator/actions) [![License](https://camo.githubusercontent.com/3dddbd1bff3627b23765722546383071d36cfa4929e1426f6b1bb6f6f87757d4/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d3865626231333f7374796c653d666c6174)](https://github.com/aneterial/laravel-data-validator?tab=MIT-1-ov-file) [![Packagist](https://camo.githubusercontent.com/a378aa858d14268af9f8e6d5af695eeb0c3fce4c0d2f8ae48e6f99e9ea173148/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7061636b61676973742d76312e302e312d626c75653f7374796c653d666c6174)](https://packagist.org/packages/aneterial/laravel-data-validator) [![PHP](https://camo.githubusercontent.com/156d7bbb72fa5f8c9988d4d2a5512b197534a4f80fd68f7d16be0e849fb0973f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253545382e322d3761383662383f7374796c653d666c6174266c6f676f3d706870)](https://www.php.net/releases/8.2/en.php) [![Laravel](https://camo.githubusercontent.com/a60f94358e0b4fa30ef633a506ace40df043473dc84437baed5efa52e14ac29f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c61726176656c2d25354531312d6639333332623f7374796c653d666c6174266c6f676f3d6c61726176656c)](https://laravel.com/)

Contents
--------

[](#contents)

- [Prerequisites](#prerequisites)
- [Installation](#installation)
- [Usage](#usage)
- [Examples](#examples)
- [Restrictions](#restrictions)

---

Prerequisites
-------------

[](#prerequisites)

- PHP 8.2 or higher
- Laravel 11

for dev

- PHPUnit &amp;11
- phpstan ^1.11
- php-cs-fixer ^3.58

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

[](#installation)

Install package via composer

```
composer require aneterial/laravel-data-validator
```

Usage
-----

[](#usage)

You now have a class attribute `DataValidator\Attributes\RequestProperty` at your disposal. Add it to the properties of your DTO and set the necessary configuration fields

```
use DataValidator\Attributes\RequestProperty;

final readonly class ExampleDTO {
  #[RequestProperty(property: 'id', rules: 'required|integer|min:0')]
  public int $id;

  #[RequestProperty(property: 'email', rules: 'required|string|email')]
  public string $email;
}
```

Description of fields:

- `property`: name of the request key that matches the property
- `rules`: validation rules based on component semantics [Laravel Validation](https://laravel.com/docs/11.x/validation), accepts only string value
- `requestDataType`: to indicate where a field is expected - in the request body (default) `const RequestProperty::BODY_TYPE` or in query string `const RequestProperty::QUERY_TYPE`
- `listRules`: if the value is an array (list) - set the validation rules for each element according to the semantics of [Laravel Validation](https://laravel.com/docs/11.x/validation)

Next, you need to get a `DataValidator\DataManager` instance in your controller from app DI container and pass the request essence to it, indicating the DTO class that you expect to receive after validation and filling with data.

```
$dataManager = app(\DataValidator\DataManager::class);
```

Next, the Laravel validator will check the request entity (`instanse of \Illuminate\Http\Request`), and if the data is incorrect, it will throw an `\Illuminate\Validation\ValidationException`. If the data is correct, the Manager will create and fill the DTO object with data, which you can use in your application

```
/** @var ExampleDTO $dto */
$dto = $dataManager->validateAndConvert(from: $request, to: ExampleDTO::class);
```

If your endpoint involves passing array of objects `[{...}, {...}, {...}]`, you can use a method that will validate the request and return an array of DTOs

```
/** @var ExampleDTO[] $dtos */
$dtos = $dataManager->validateAndConvertList(from: $request, to: ExampleDTO::class);
```

Examples
--------

[](#examples)

Here are some examples of using validation by attributes

1. DTO with lists

```
final readonly class ExampleDTO {
...

  /** @var string[] $emails */
  #[RequestProperty(property: 'emails', rules: 'required|list', listRules: 'string|email')]
  public array $emails;

  /** @var int[] $ids */
  #[RequestProperty(property: 'ids', rules: 'required|list', listRules: 'int|min:0')]
  public array $ids;

...
}
```

2. DTO with non required properties, if it not required - it should be nullable, except array - it can be empty array

```
final readonly class ExampleDTO {
  #[RequestProperty(property: 'id', rules: 'integer|min:0')]
  public ?int $id;

  #[RequestProperty(property: 'email', rules: 'string|email')]
  public ?string $email;

  /** @var int[] $ids */
  #[RequestProperty(property: 'ids', rules: 'list', listRules: 'int|min:0')]
  public array $ids;
}
```

3. DTO with nested object

```
final readonly class ExampleDTO {
...

  #[RequestProperty(property: 'child', rules: 'required|array')]
  public NestedDTO $child;

...
}

// NestedDTO should contain properties with attributes
final readonly class NestedDTO {
  #[RequestProperty(property: 'id', rules: 'required|integer|min:0')]
  public int $id;

  #[RequestProperty(property: 'email', rules: 'required|string|email')]
  public string $email;
}
```

4. DTO with list of nested object

```
final readonly class ExampleDTO {
...

  /** @var NestedDTO[] $children */
  #[RequestProperty(property: 'children', rules: 'required|list', listRules: NestedDTO::class)]
  public array $children;

...
}
```

5. DTO with enum of `BackedEnum` property, you should set enum type to property and no more rules are required except, if you want - indicate type of enum

```
final readonly class ExampleDTO {
...

  #[RequestProperty(property: 'enum', rules: 'required|string')]
  public AnApplicationEnum $enum;

...
}
```

6. DTO with enums of `BackedEnum` property

```
final readonly class ExampleDTO {
...
  /** @var AnApplicationEnum[] $enums */
  #[RequestProperty(property: 'enums', rules: 'required|list', listRules: AnApplicationEnum::class)]
  public array $enums;

...
}
```

Restrictions
------------

[](#restrictions)

```
- Important note: for different requestDataType
```

**if DTO has nested objects or arrays of nested objects, the `requestDataType` of these entities is ignored and taken from the parrent entity**

So if you use

```
final readonly class ExampleDTO {
...

  #[RequestProperty(property: 'child', rules: 'required|array', requestDataType: RequestProperty::BODY_TYPE)]
  public NestedDTO $child;

...
}

// NestedDTO should contain properties with attributes
final readonly class NestedDTO {
  #[RequestProperty(property: 'id', rules: 'required|integer|min:0', requestDataType: RequestProperty::QUERY_TYPE)]
  public int $id;

  #[RequestProperty(property: 'email', rules: 'required|string|email', requestDataType: RequestProperty::QUERY_TYPE)]
  public string $email;
}
```

In nested object `requestDataType` will not work and it be `RequestProperty::BODY_TYPE` like in parrent entity

```
- Another one: for list validation
```

**Method `DataManager::validateAndConvertList` can only work with data from request body, so all properties of entity will be force casted to type `RequestProperty::BODY_TYPE` in this usage**

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance33

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity56

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

Total

5

Last Release

703d ago

Major Versions

0.3 → 1.0.02024-06-04

### Community

Maintainers

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

---

Top Contributors

[![aneterial](https://avatars.githubusercontent.com/u/83873886?v=4)](https://github.com/aneterial "aneterial (12 commits)")

---

Tags

phplaravelvalidation

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/aneterial-laravel-data-validator/health.svg)

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

###  Alternatives

[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.

1062.8k1](/packages/yorcreative-laravel-argonaut-dto)[reducktion/socrates

A package to validate, and extract citizen information from, national identification numbers.

488.5k](/packages/reducktion-socrates)[carsdotcom/laravel-json-schema

Json Schema validation for Laravel projects

1036.7k3](/packages/carsdotcom-laravel-json-schema)

PHPackages © 2026

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