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

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

changhorizon/validation-interface
=================================

Unified, testable, and extensible validation for any PHP project

v2.1.2(4w ago)0271MITPHPPHP &gt;=8.2CI passing

Since May 12Pushed 4w ago1 watchersCompare

[ Source](https://github.com/changhorizon/validation-interface)[ Packagist](https://packagist.org/packages/changhorizon/validation-interface)[ RSS](/packages/changhorizon-validation-interface/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (3)Dependencies (4)Versions (8)Used By (1)

Validation Interface
====================

[](#validation-interface)

> Unified, testable, and extensible validation for any PHP project

[![License](https://camo.githubusercontent.com/8cf16dac9f3b9bf52a7acc0cfef5b6f02e33fb89d7fc0a2c80898c6b0d57d882/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6368616e67686f72697a6f6e2f76616c69646174696f6e2d696e746572666163653f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/8cf16dac9f3b9bf52a7acc0cfef5b6f02e33fb89d7fc0a2c80898c6b0d57d882/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6368616e67686f72697a6f6e2f76616c69646174696f6e2d696e746572666163653f7374796c653d666c61742d737175617265)[![Latest Version](https://camo.githubusercontent.com/145f49c76ad6394bea951128152457e8f774958ec46e775e3babdb145fd313db/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6368616e67686f72697a6f6e2f76616c69646174696f6e2d696e746572666163653f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/145f49c76ad6394bea951128152457e8f774958ec46e775e3babdb145fd313db/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6368616e67686f72697a6f6e2f76616c69646174696f6e2d696e746572666163653f7374796c653d666c61742d737175617265)[![PHP Version](https://camo.githubusercontent.com/10b897c523f00fa3f8f7b54dfe73999190e622480655d5b5f011e31fc32a7111/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d382e322d2d382e342d626c75653f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/10b897c523f00fa3f8f7b54dfe73999190e622480655d5b5f011e31fc32a7111/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d382e322d2d382e342d626c75653f7374796c653d666c61742d737175617265)[![Static Analysis](https://camo.githubusercontent.com/1a477f5e7e742a33c1ff5b685167579083a26c342c4e997fe056ea4ef7bea73e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7374617469635f616e616c797369732d5048505374616e2d626c75653f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/1a477f5e7e742a33c1ff5b685167579083a26c342c4e997fe056ea4ef7bea73e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7374617469635f616e616c797369732d5048505374616e2d626c75653f7374796c653d666c61742d737175617265)[![Tests](https://camo.githubusercontent.com/72829871c802983bff15745f71c846973b36a6fea0bb5298dadaeeb690463604/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f74657374732d504850556e69742d627269676874677265656e3f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/72829871c802983bff15745f71c846973b36a6fea0bb5298dadaeeb690463604/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f74657374732d504850556e69742d627269676874677265656e3f7374796c653d666c61742d737175617265)[![codecov](https://camo.githubusercontent.com/62ec5909ba0529296bdebc7461453ac2c797a6c9b64c18025c6f338bbaa31cb5/68747470733a2f2f636f6465636f762e696f2f67682f6368616e67686f72697a6f6e2f76616c69646174696f6e2d696e746572666163652f6272616e63682f6d61696e2f67726170682f62616467652e737667)](https://codecov.io/gh/changhorizon/validation-interface)[![CI](https://github.com/changhorizon/validation-interface/actions/workflows/ci.yml/badge.svg?style=flat-square)](https://github.com/changhorizon/validation-interface/actions/workflows/ci.yml/badge.svg?style=flat-square)

A lightweight, framework-agnostic validation contract package that provides generic validator and validation result interfaces. It is designed to help developers build consistent, testable, and easily extensible validation logic, suitable for scenarios such as upload validation, form validation, and custom rule implementation.

✨ 特性
----

[](#-特性)

- 轻量级与无依赖：独立于任何框架或外部库，易于集成到现有项目中
- 基于抽象类的验证器：提供 `AbstractValidator`，子类只需实现 `validate()` 方法
- 构造注入对象：验证器接收目标对象，避免类型不确定问题
- 保留 `ValidationResult` 和 `ValidationResultInterface`，提供统一的验证结果封装
- 灵活集成与扩展：适合各种业务对象和自定义规则实现

📦 安装
----

[](#-安装)

```
composer require changhorizon/validation-interface
```

📂 目录结构
------

[](#-目录结构)

```
src
├── Result
│   └── ValidationResult.php
├── Validator
│   └── AbstractValidator.php
├── ValidationResultInterface.php
└── ValidatorInterface.php
```

🚀 用法示例
------

[](#-用法示例)

### 示例 1：自定义 Email 验证器

[](#示例-1自定义-email-验证器)

```
use ChangHorizon\ValidationInterface\Result\ValidationResult;
use ChangHorizon\ValidationInterface\Validator\AbstractValidator;

class EmailValidator extends AbstractValidator
{
    public function validate(): ValidationResult
    {
        if (!is_string($this->target) || !filter_var($this->target, FILTER_VALIDATE_EMAIL)) {
            return $this->fail('Invalid email address', 'INVALID_EMAIL');
        }

        return $this->ok();
    }
}
```

### 示例 2：执行验证并处理结果

[](#示例-2执行验证并处理结果)

```
$email = 'user@example.com';
$validator = new EmailValidator($email);  // 目标对象通过构造传入
$result = $validator->validate();

if ($result->isValid()) {
    echo "Email is valid.";
} else {
    echo "Validation failed: " . $result->getError();
}
```

📐 接口说明
------

[](#-接口说明)

### AbstractValidator

[](#abstractvalidator)

> 验证器基类，每个子类接收目标对象并实现 validate() 方法返回 ValidationResult

```
namespace ChangHorizon\ValidationInterface\Validator;

use ChangHorizon\ValidationInterface\Result\ValidationResult;
use ChangHorizon\ValidationInterface\ValidatorInterface;

abstract class AbstractValidator implements ValidatorInterface
{
    protected object $target;

    public function __construct(object $target)
    {
        $this->target = $target;
    }

    abstract public function validate(): ValidationResult;

    protected function ok(): ValidationResult
    {
        return ValidationResult::ok();
    }

    protected function fail(string $error, ?string $code = null): ValidationResult
    {
        return ValidationResult::fail($error, $code);
    }
}
```

### ValidationResultInterface &amp; ValidationResult

[](#validationresultinterface--validationresult)

> 用于表示验证结果，封装验证是否通过、错误信息与错误代码

```
namespace ChangHorizon\ValidationInterface\Result;

ValidationResult::ok();                   // 构造成功结果
ValidationResult::fail('error message');  // 构造失败结果
```

🔍 静态分析
------

[](#-静态分析)

使用 PHPStan 工具进行静态分析，确保代码的质量和一致性：

```
composer stan
```

🎯 代码风格
------

[](#-代码风格)

使用 PHP-CS-Fixer 工具检查代码风格：

```
composer cs:chk
```

使用 PHP-CS-Fixer 工具自动修复代码风格问题：

```
composer cs:fix
```

✅ 单元测试
------

[](#-单元测试)

执行 PHPUnit 单元测试：

```
composer test
```

执行 PHPUnit 单元测试并生成代码覆盖率报告：

```
composer test:coverage
```

🤝 贡献指南
------

[](#-贡献指南)

欢迎 Issue 与 PR，建议遵循以下流程：

1. Fork 仓库
2. 创建新分支进行开发
3. 提交 PR 前请确保测试通过、风格一致
4. 提交详细描述

📜 License
---------

[](#-license)

MIT License. See the [LICENSE](LICENSE) file for details.

###  Health Score

44

—

FairBetter than 90% of packages

Maintenance94

Actively maintained with recent releases

Popularity10

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity54

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

Total

7

Last Release

29d ago

Major Versions

v1.0.0 → v2.0.02025-09-03

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/191742660?v=4)[ChangHorizon](/maintainers/changhorizon)[@changhorizon](https://github.com/changhorizon)

---

Top Contributors

[![changhorizon](https://avatars.githubusercontent.com/u/191742660?v=4)](https://github.com/changhorizon "changhorizon (7 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[ziming/laravel-zxcvbn

Zxcvbn Password validation rule for Laravel

3064.3k](/packages/ziming-laravel-zxcvbn)

PHPackages © 2026

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