maatify/validation
Composer 安装命令:
composer require maatify/validation
包简介
Schema-based validation library for PHP applications
README 文档
README
A type-safe, framework-agnostic input validation module built on top of
Respect/Validation, designed for clean architecture, strict static analysis,
and future extraction as a standalone library.
This module is designed for standalone usage and is not coupled to:
- Authentication
- Authorization (Guards)
- Domain Logic
- HTTP Frameworks (Slim, PSR-7)
- UI / Templates
🎯 Goals
- Centralize input validation in a clean, reusable layer
- Eliminate duplicated validation logic in controllers
- Enforce type-safety using DTOs and Enums
- Pass PHPStan level max with zero suppressions
- Prepare the module for future extraction as a standalone package
🧱 Architectural Principles
1. Validation is a Cross-Cutting Concern
Validation:
- Touches Controllers and Requests
- Does not belong to Domain, Auth, or Guards
- Produces no side effects (no audit, no security events)
2. Validation ≠ Authorization
- Validation checks data correctness
- Authorization checks permissions
- They are strictly separated
3. No Strings, No Magic
- All error codes are Enums
- All responses are DTOs
- No hard-coded strings in schemas
📁 Directory Structure
src/
├── Contracts/
│ ├── SchemaInterface.php
│ ├── ValidatorInterface.php
│ ├── ErrorMapperInterface.php
│ └── SystemErrorMapperInterface.php
│
├── DTO/
│ ├── ValidationResultDTO.php
│ └── ApiErrorResponseDTO.php
│
├── Enum/
│ ├── ValidationErrorCodeEnum.php
│ ├── AuthErrorCodeEnum.php
│ └── HttpStatusCodeEnum.php
│
├── ErrorMapper/
│ ├── ApiErrorMapper.php
│ └── SystemApiErrorMapper.php
│
├── Rules/
│ ├── EmailRule.php
│ ├── PasswordRule.php
│ └── RequiredStringRule.php
│
├── Schemas/
│ ├── AbstractSchema.php
│ ├── AuthLoginSchema.php
│ └── AdminCreateSchema.php
│
└── Validator/
└── RespectValidator.php
📦 Dependency
This module relies on:
composer require respect/validation
No other external dependencies are required.
🧩 Core Concepts
1️⃣ Rules
Rules are pure validation units built on Respect/Validation.
- One rule = one responsibility
- No HTTP, no DTOs, no Domain logic
- Return
Validatablevia docblocks for PHPStan compatibility
Example:
EmailRule::rule()
2️⃣ Schemas
Schemas describe request-level validation.
- One schema per endpoint / use-case
- Declarative rules
- No try/catch duplication
- All schemas extend
AbstractSchema
Example:
final class AuthLoginSchema extends AbstractSchema { protected function rules(): array { return [ 'email' => [v::email(), ValidationErrorCodeEnum::INVALID_EMAIL], 'password' => [CredentialInputRule::rule(), ValidationErrorCodeEnum::INVALID_PASSWORD], ]; } }
3️⃣ ValidationResultDTO
Schemas always return a ValidationResultDTO:
isValid(): boolgetErrors(): array<string, list<ValidationErrorCodeEnum>>
No exceptions are thrown for invalid input.
4️⃣ Error Mapping
Errors are mapped once at the system boundary.
- Validation →
ValidationErrorCodeEnum - Auth / Guards →
AuthErrorCodeEnum - Transport →
HttpStatusCodeEnum
All errors are converted into a single response shape via:
SystemApiErrorMapper
5️⃣ ApiErrorResponseDTO
All API error responses are represented as a DTO:
ApiErrorResponseDTO
- Contains HTTP status
- Contains error code
- Contains structured field errors
- No arrays returned directly from mappers
🌐 Typical Flow (API)
-
Controller receives input
-
Schema validates input
-
ValidationResultDTOis returned -
If invalid:
- Errors mapped via
SystemApiErrorMapper - Controller sends HTTP response
- Errors mapped via
-
If valid:
- Controller proceeds to Service layer
❌ What This Module Does NOT Do
- ❌ No authentication logic
- ❌ No authorization checks
- ❌ No audit logging
- ❌ No database access
- ❌ No localization (i18n)
- ❌ No HTTP framework coupling
🧪 Static Analysis
- Designed to pass PHPStan level max
- No suppressions
- No dynamic magic exposed to type system
- Respect/Validation handled via docblocks where needed
🔮 Future Extensions (Planned)
- Localization mapping (Enum → i18n keys)
- Composite schemas
- Context-aware validation (create vs update)
- Standalone package extraction (
maatify/validation) - Shared SuccessResponseDTO for APIs
🧠 Architectural Decision (LOCKED)
All input validation must be expressed as Schemas using Rules + Enums, and mapped through a single system-level ErrorMapper. No strings, no duplication, no side effects.
✅ Status
- Architecture: LOCKED
- Implementation: STABLE
- PHPStan: PASS (level max)
- Ready for reuse and extraction
🪪 License
This library is licensed under the MIT License. See the LICENSE file for details.
👤 Author
Engineered by Mohamed Abdulalim (@megyptm) Backend Lead & Technical Architect https://www.maatify.dev
🤝 Contributors
Special thanks to the Maatify.dev engineering team and all open-source contributors. Contributions are welcome.
Built with ❤️ by Maatify.dev — Unified Ecosystem for Modern PHP Libraries
maatify/validation 适用场景与选型建议
maatify/validation 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 31 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 maatify/validation 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 maatify/validation 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 31
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-05