PHPackages                             mzh/hyperf-validate - 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. [Framework](/categories/framework)
4. /
5. mzh/hyperf-validate

ActiveLibrary[Framework](/categories/framework)

mzh/hyperf-validate
===================

hyperf-validate 支持request 和方法场景验证，兼容tp5写法 支持控制器注解验证、方法注解验证

v1.0.6(5y ago)202.9k1Apache-2.0PHPPHP &gt;=7.2CI failing

Since Dec 21Pushed 5y ago2 watchersCompare

[ Source](https://github.com/lphkxd/hyperf-validate)[ Packagist](https://packagist.org/packages/mzh/hyperf-validate)[ RSS](/packages/mzh-hyperf-validate/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (6)Versions (12)Used By (1)

hyperf-validate
===============

[](#hyperf-validate)

支持request 和方法场景验证，兼容tp写法 性能比laravel验证器提高50%

 [![Stable Version](https://camo.githubusercontent.com/08db349afdd83e1ce7a67bd7010199fa4c3c03e92a159567a99ac5a8b3c4ad50/68747470733a2f2f706f7365722e707567782e6f72672f6d7a682f6879706572662d76616c69646174652f762f737461626c65)](https://github.com/lphkxd/hyperf-validate/releases) [![Build Status](https://camo.githubusercontent.com/1d770f0ecdd6486be5c6cfaf84465f3b54d52c761d326255690ef26721437e77/68747470733a2f2f7472617669732d63692e6f72672f6d7a682f6879706572662d76616c69646174652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/mzh/hyperf-validate) [![Total Downloads](https://camo.githubusercontent.com/7f3add9718d4ffa821c1328f6b987e05e3f30cbcbb9c74554e974b0b47928362/68747470733a2f2f706f7365722e707567782e6f72672f6d7a682f6879706572662d76616c69646174652f646f776e6c6f616473)](https://packagist.org/packages/mzh/hyperf-validate) [![Monthly Downloads](https://camo.githubusercontent.com/c361316baeb11bd54992c60d714e292be137a2bc59a8fe264f4723a39a37663b/68747470733a2f2f706f7365722e707567782e6f72672f6d7a682f6879706572662d76616c69646174652f642f6d6f6e74686c79)](https://packagist.org/packages/mzh/hyperf-validate) [![Php Version](https://camo.githubusercontent.com/c197ef1e57a32c886b2a54d02ead7dbc035f18b3f39a642703b9372eef7819f9/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d2533453d372e312d627269676874677265656e2e7376673f6d61784167653d32353932303030)](https://www.php.net) [![Swoole Version](https://camo.githubusercontent.com/0a5cfeaacb96598473ccdbddfedeaf286e434fa5c166ed3b382c92575af33dfa/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f73776f6f6c652d2533453d342e352d627269676874677265656e2e7376673f6d61784167653d32353932303030)](https://github.com/swoole/swoole-src) [![ License](https://camo.githubusercontent.com/72c46779a0ed1f36f58145a7d78e505dec1e7fea669217190b72f294adbb9784/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6c70686b78642f6879706572662d76616c69646174652e7376673f6d61784167653d32353932303030)](https://github.com/lphkxd/hyperf-validate/blob/master/LICENSE)

安装方法
----

[](#安装方法)

```
 安装方法  composer require mzh/hyperf-validate

```

具体使用方法可以参考项目
-----------------------------------------------------

[](#具体使用方法可以参考项目-httpsgithubcomlphkxdhyperf-admin)

注解
--

[](#注解)

```
 @Validation
 @RequestValidation

```

### @RequestValidation 参数说明

[](#requestvalidation-参数说明)

```
方式一：
validate = 验证类 例如   validate=AdminValidation::class
方式2：
mode="Admin" 验证的模块规则/app/Validate/AdminValidation.php 文件的验证规则
方式3：
不填写以上2个参数，默认取验证器为当前控制器类文件名的验证器文件
          （如果使用了mzh/hyperf-swagger 根据验证器规则生成文档 见下图1）

scene="场景" 场景，验证哪个场景。默认不写为默认的验证规则
filter=true 过滤掉规则外无用参数 过滤后会重新写入$this->request->getParsedBody()内，
              需要时直接取，数据是安全的，验证过的
security=true 严格验证模式，如果开启，则用户传入无用参数，直接抛出异常，提示传入的字段xx无效，
              （如果使用了mzh/hyperf-swagger 会在生成的接口上标记接口需token验证的小锁 见下图2，如果使用了hyperf-admin，则设为false此方法为开放资源，不验证此任何权限）
userOpen = true  (可选)，此参数是配合 mzh/hyperf-admin权限验证，设为true为只要登录该路由的url就可以访问，否则需要授权才可以访问

```

- 图1 [![Image 文档](./screenshot/1.png)](./screenshot/1.png)
- 图2 [![Image 文档](./screenshot/2.png)](./screenshot/2.png)

### @Validation 参数说明

[](#validation-参数说明)

```
方式一：
validate = 验证类 例如   validate=AdminValidation::class
方式2：
mode="Admin" 验证的模块规则/app/Validate/AdminValidation.php 文件的验证规则
方式3：
不填写以上2个参数，默认取验证器为当前类文件名的验证器文件

scene="场景" 场景，验证哪个场景。默认不写为默认的验证规则
filter=true 过滤掉规则外无用参数 过滤后会重新写入对应的字段内，需要时直接取，数据是安全的，验证过的
security=true 严格验证模式，如果开启，则用户传入无用参数，直接抛出异常，提示传入的字段xx无效，
field="data" 方法的参数名，例如 function($data,$array,$array3) 需要验证这个方法的$array参数，这里填array

```

验证控制器数据方法如下
-----------

[](#验证控制器数据方法如下)

### 例如 验证控制器Admin 的login方法传的数据，需要如下操作

[](#例如-验证控制器admin-的login方法传的数据需要如下操作)

```
use Mzh\Validate\Annotations\RequestValidation;

/**
 * @RequestValidation(filter=true,throw=true)
 */
public function login(){
      //这里取到的 $data 是安全的。
     $data = $this->request->getParsedBody();
}

```

验证类方法数据方法如下
-----------

[](#验证类方法数据方法如下)

例如 验证AdminService类的login方法传的数据，需要如下操作

```
use Mzh\Validate\Annotations\Validation;

/**
 * @Validation(mode="Admin",scene="login",field="data")
 * @Validation(mode="Admin",scene="array的规则",field="array")
 */
public function login($data,$array,$array2){
      //这里取到的 $data,$array,$array2 是安全的，经过验证器验证过的

}

```

多维数组验证
------

[](#多维数组验证)

```

 protected $rule = [
        'username' => 'max:12',
        'password' => 'max:18',
        'data' => [ #划重点  数组形式将需要验证的字段和规则写入  看图3、4、5
            "user_id" => 'require|in:0,1',
            'status' => 'require|in:0,1',
        ],
    ];

```

- 图3 使用注解方式 [![Image 文档](./screenshot/3.png)](./screenshot/3.png)
- 图4 写验证器规则和场景 [![Image 文档](./screenshot/4.png)](./screenshot/4.png)
- 图5 测试结果 [![Image 文档](./screenshot/5.png)](./screenshot/5.png)

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 70% 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 ~26 days

Recently: every ~14 days

Total

11

Last Release

2078d ago

Major Versions

v0.1.7.x-dev → v1.0.12020-08-01

### Community

Maintainers

![](https://www.gravatar.com/avatar/6064d6ea21c78c19ac11c7918d833822adf51797f1920b2c1b45968795a71074?d=identicon)[lphkxd](/maintainers/lphkxd)

---

Top Contributors

[![lphkxd](https://avatars.githubusercontent.com/u/16346895?v=4)](https://github.com/lphkxd "lphkxd (35 commits)")[![great-zh](https://avatars.githubusercontent.com/u/57761451?v=4)](https://github.com/great-zh "great-zh (15 commits)")

---

Tags

phpframeworkswoolehyperf

###  Code Quality

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/mzh-hyperf-validate/health.svg)

```
[![Health](https://phpackages.com/badges/mzh-hyperf-validate/health.svg)](https://phpackages.com/packages/mzh-hyperf-validate)
```

###  Alternatives

[hyperf/hyperf-skeleton

A coroutine framework that focuses on hyperspeed and flexible, specifically use for build microservices and middlewares.

301187.4k](/packages/hyperf-hyperf-skeleton)

PHPackages © 2026

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