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

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

yuandian/validation
===================

Helpers for making PHP Attribute validation.

v1.2.1(5mo ago)119MITPHPPHP &gt;=8.1

Since Sep 14Pushed 5mo ago1 watchersCompare

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

READMEChangelog (2)Dependencies (1)Versions (6)Used By (0)

参数验证 validator
==============

[](#参数验证-validator)

使用PHP原生属性进行参数校验

安装
==

[](#安装)

`composer require yuandian/validation`

使用示例
====

[](#使用示例)

- 定义实体文件 `UserRequest.php`

```
use yuandian\rules\NotEmpty;
use yuandian\rules\Email;
use yuandian\rules\Scene;

// 用于配置场景验证
#[Scene("add",  ['name'])]
class UserRequest {
    #[NotEmpty(message: "Name cannot be empty.")]
    public string $name;

    #[Email(message: "Invalid email format.")]
    #[NotEmpty(message: "Email cannot be empty.")]
    public string $email;
}
```

- 赋值与验证

```
require_once 'vendor/autoload.php';

use yuandian\Validation\Validator
use yuandian\Validation\Exception\ValidateException;

$request = new UserRequest();
$request->name = '张三';
$request->email = 'zhangsan@Validator'
// 验证实体
try {
    $validator = new Validator();

    $validator->validate($userRequest);
    // 批量验证
    // $validator->batch(true)->validate($userRequest);
    // 场景验证
    // $validator->validate($userRequest, 'add');
} catch (ValidateException $e) {
    echo "Validation errors: " . $e->getMessage() . "\n";
}
```

自动赋值&amp;&amp;自动验证
==================

[](#自动赋值自动验证)

- 定义实体文件 `UserRequest.php`

```
use yuandian\rules\NotEmpty;
use yuandian\rules\Email;
use yuandian\BaseValidatorEntity;

class UserRequest extends BaseValidatorEntity {
    #[NotEmpty(message: "Name cannot be empty.")]
    public string $name;

    #[Email(message: "Invalid email format.")]
    #[NotEmpty(message: "Email cannot be empty.")]
    public string $email;
}
```

- 赋值与验证

```
require_once 'vendor/autoload.php';

use yuandian\Validation\Exception\ValidateException;

// 模拟请求数据
$requestData = [
    'name' => 'John Doe',
    'email' => 'invalid-email'
];

// 验证实体
try {
    $request = new UserRequest($requestData);
} catch (ValidateException $e) {
    echo "Validation errors: " . $e->getMessage() . "\n";
}
```

自定义验证规则
=======

[](#自定义验证规则)

- 实现 `yuandian\Validation\Rule`接口
- 在自定义的类上增加`#[Attribute(Attribute::TARGET_PROPERTY)]`注解

捐献
--

[](#捐献)

[![](./wechat.png)](./wechat.png)[![](./alipay.png)](./alipay.png)

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance71

Regular maintenance activity

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity52

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

Total

4

Last Release

162d ago

PHP version history (2 changes)v1.0.0PHP &gt;=8.0

v1.1.0PHP &gt;=8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/8101f03acc9ab7ed572b6b5c7a8b7ac3fdfc1d01060bda55bafe9ef0d7ebc5bf?d=identicon)[yuan-dian](/maintainers/yuan-dian)

---

Top Contributors

[![yuan-dian](https://avatars.githubusercontent.com/u/33216505?v=4)](https://github.com/yuan-dian "yuan-dian (51 commits)")

---

Tags

phpvalidationattribute

### Embed Badge

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

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

###  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)

PHPackages © 2026

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