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

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

hizpark/validation-interface
============================

Unified, testable, and extensible validation for any PHP project

v2.0.2(8mo ago)0271MITPHPPHP &gt;=8.2CI passing

Since May 12Pushed 8mo ago1 watchersCompare

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

READMEChangelog (4)Dependencies (4)Versions (5)Used By (1)

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

[](#validation-interface)

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

[![License](https://camo.githubusercontent.com/ad9f82fd14ae0e4df188f674fb603ff438768865ae93a666a65d6432fbbf4e39/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f68697a7061726b2f76616c69646174696f6e2d696e746572666163653f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/ad9f82fd14ae0e4df188f674fb603ff438768865ae93a666a65d6432fbbf4e39/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f68697a7061726b2f76616c69646174696f6e2d696e746572666163653f7374796c653d666c61742d737175617265)[![Latest Version](https://camo.githubusercontent.com/ca30cf82167f6b276558601a3568d0be0147ba20f00c36d18db437f2e95aba1c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f68697a7061726b2f76616c69646174696f6e2d696e746572666163653f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/ca30cf82167f6b276558601a3568d0be0147ba20f00c36d18db437f2e95aba1c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f68697a7061726b2f76616c69646174696f6e2d696e746572666163653f7374796c653d666c61742d737175617265)[![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/655fa7e639f9b2a937108d4aee5f60a83bddaa1c880c2ee81ba025ce8489c64f/68747470733a2f2f636f6465636f762e696f2f67682f68697a7061726b2f76616c69646174696f6e2d696e746572666163652f6272616e63682f6d61696e2f67726170682f62616467652e737667)](https://codecov.io/gh/hizpark/validation-interface)[![CI](https://github.com/hizpark/validation-interface/actions/workflows/ci.yml/badge.svg?style=flat-square)](https://github.com/hizpark/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 hizpark/validation-interface
```

📂 目录结构
------

[](#-目录结构)

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

🚀 用法示例
------

[](#-用法示例)

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

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

```
use Hizpark\ValidationInterface\Result\ValidationResult;
use Hizpark\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 Hizpark\ValidationInterface\Validator;

use Hizpark\ValidationInterface\Result\ValidationResult;
use Hizpark\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 Hizpark\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

34

—

LowBetter than 77% of packages

Maintenance60

Regular maintenance activity

Popularity7

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

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

Total

4

Last Release

256d ago

Major Versions

v1.0.0 → v2.0.02025-09-03

### Community

Maintainers

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

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[webmozart/assert

Assertions to validate method input/output with nice error messages.

7.6k894.0M1.2k](/packages/webmozart-assert)[bensampo/laravel-enum

Simple, extensible and powerful enumeration implementation for Laravel.

2.0k15.9M104](/packages/bensampo-laravel-enum)[swaggest/json-schema

High definition PHP structures with JSON-schema based validation

48612.5M73](/packages/swaggest-json-schema)[stevebauman/purify

An HTML Purifier / Sanitizer for Laravel

5325.6M19](/packages/stevebauman-purify)[ashallendesign/laravel-config-validator

A package for validating your Laravel app's config.

217905.3k5](/packages/ashallendesign-laravel-config-validator)[crazybooot/base64-validation

Laravel validators for base64 encoded files

1341.9M8](/packages/crazybooot-base64-validation)

PHPackages © 2026

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