PHPackages                             lujihong/easy-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. lujihong/easy-validator

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

lujihong/easy-validator
=======================

php hyperf validator

v1.0.3(3y ago)0465MITPHPPHP &gt;=8.0

Since Oct 17Pushed 3y ago1 watchersCompare

[ Source](https://github.com/lujihong/hyperf-easy-validator)[ Packagist](https://packagist.org/packages/lujihong/easy-validator)[ RSS](/packages/lujihong-easy-validator/feed)WikiDiscussions main Synced today

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

hyperf 框架注解验证器
--------------

[](#hyperf-框架注解验证器)

基于 [Hyperf](https://github.com/hyperf/hyperf) EasyValidator

##### 优点

[](#优点)

- 根据注解自动生成Vo类，自动获取并转换request参数类型
- 支持框架数据验证器，自定义验证规则

注意
--

[](#注意)

> php &gt;= 8.0

安装
--

[](#安装)

```
composer require lujihong/easy-validator

```

使用示例
----

[](#使用示例)

### 控制器

[](#控制器)

```
declare(strict_types=1);

namespace Api\Controller;

use Hyperf\EasyValidator\Annotation\Body;
use Hyperf\EasyValidator\Annotation\FormData;
use Hyperf\EasyValidator\Annotation\Query;
use Hyperf\EasyValidator\Annotation\Header;
use Api\Vo\Demo\DemoEditVo; //自动生成的Vo类

#[Controller(prefix: '/demo')]
class DemoController extends AbstractController
{
    #[
        PostMapping(path: "test"),
        FormData(key: "phone|手机号", rule: "required|phone"),
        FormData(key: "code|验证码", rule: "required|max:6")
    ]
    public function index()
    {

    }

    #[
        PutMapping(path: 'edit'),
        Header(key: 'Token|token', rule: 'required'),
        Body(key: 'test|测试', rule: 'required', fieldType: 'int'),
        Body(key: 'test1|测试1', rule: 'required', fieldType: 'string'),
    ]
    public function edit()
    {
        $vo = new DemoEditVo();、

        //从request请求中获取请求参数
        $vo->getAttributes();//获取所有属性，包含ip
        $vo->getIp();//获取Ip
        $vo->getTest();//获取测试字段值，已自动转换成int类型
        $vo->getTest1();//获取测试1字段值，已自动转换成string类型

        //自定义request参数值
        $changeVal = 2;
        $vo->setTest($changeVal);
        $vo->getTest();//此时值已变为2

        //设置其他自定义参数
        $vo->setOther(['foo_field' => '测试值']);
        $vo->getOther();//获取设置的其他值
    }

    #[
        PostMapping(path: 'fromData'),
        FormData(key: "phone|手机号", rule: "required|phone"),
        FormData(key: "code|验证码", rule: "required|max:6")
    ]
    public function fromData(): bool
    {

    }

    #[
        GetMapping(path: 'find/{id}/and/{in}'),
        Query(key: "id|测试id", rule: "required|numeric"),
        Query(key: "name|测试名称", rule: "required|test") //test为自定义验证规格
    ]
    public function find(int $id, float $in): array
    {
        return ['$id' => $id, '$in' => $in];
    }

}
```

### Vo类

[](#vo类)

- fieldType，用于生成Vo类的类型限定，所有字段均不设置则不生成Vo类

> 支持的值为：string | array | bool | int | float | file

### 验证器

[](#验证器)

- key 格式：field|字段名称
- rule 验证规则

> rule 支持hyperf框架所有验证

> 内置规则：phone、telephone、identity\_card、crontab、class\_exist、comma\_separated

- 自定义验证规则

> 只需类名称上加上`CustomValidationRule`，并实现对应规则即可

```
use Hyperf\EasyValidator\Annotation\CustomValidationRule;

#[CustomValidationRule]
class DemoCustomValidationRule
{
    /**
     * 校验错误则返回错误信息, 正确则返回 true
     * @param $attribute
     * @param $value
     * @return bool|string
     */
    public function test($attribute, $value): bool|string
    {
        if (!preg_match('/^1[3456789]{1}\d{9}$/', trim((string)$value))) {
            return '格式不正确';
        }

        return true;
    }
}
```

###  Health Score

26

—

LowBetter than 41% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity55

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

Total

4

Last Release

1204d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/857a0c0ff93a510cb3a9e7a41229e4799a3c13c6f982c1a872f29cc4fa481957?d=identicon)[lujihong](/maintainers/lujihong)

---

Top Contributors

[![lujihong](https://avatars.githubusercontent.com/u/31462984?v=4)](https://github.com/lujihong "lujihong (9 commits)")

---

Tags

validatorvalidationhyperfhyperf validation

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/lujihong-easy-validator/health.svg)

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

###  Alternatives

[craftcms/cms

Craft CMS

3.6k3.6M3.0k](/packages/craftcms-cms)[web-auth/webauthn-lib

FIDO2/Webauthn Support For PHP

12310.5M132](/packages/web-auth-webauthn-lib)[web-auth/webauthn-framework

FIDO2/Webauthn library for PHP and Symfony Bundle.

515100.5k3](/packages/web-auth-webauthn-framework)[2lenet/crudit-bundle

The easy like Crud'it Bundle.

1616.4k13](/packages/2lenet-crudit-bundle)[tangwei/dto

php hyperf dto

18138.4k3](/packages/tangwei-dto)[api-platform/validator

API Platform validator component

274.6M27](/packages/api-platform-validator)

PHPackages © 2026

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