maatify / validation
Schema-based validation library for PHP applications
Requires
- php: ^8.2
- respect/validation: ^2.3
Requires (Dev)
- php-di/php-di: ^7.0
- phpstan/phpstan: ^1.11
- phpunit/phpunit: ^11.0
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