mitsuki / validators
There is no license information available for the latest version (v1.0.0) of this package.
A Symfony Validator–based request validation layer for PHP, providing an abstract RequestValidator and structured ValidationException for HTTP 422 responses.
v1.0.0
2026-02-16 21:46 UTC
Requires
- doctrine/annotations: ^2.0
- mitsuki/contracts: ^1.0
- mitsuki/http: ^1.0
- symfony/validator: ^8.0
Requires (Dev)
- php: ^8.4
- pestphp/pest: ^4.3
README
A lightweight request validation layer built on top of Symfony Validator, designed to work seamlessly with custom HTTP requests in PHP applications.
Features
- Abstract
RequestValidatorclass that merges query, request body, and files into a single dataset. - Easy rule definition via the
rules()method using SymfonyConstraintobjects. - Throws a structured
ValidationException(HTTP 422) when validation fails. - Clean, reusable validators for your API or web controllers.
Installation
Install the package via Composer:
composer require mitsuki/hermite-validators
- Create a validator class extending RequestValidator:
use Mitsuki\Hermite\Validators\RequestValidator; use Symfony\Component\Validator\Constraints as Assert; class CreateUserRequest extends RequestValidator { public function rules(): array { return [ 'email' => [new Assert\Email(), new Assert\NotBlank()], 'age' => [new Assert\Type('numeric'), new Assert\GreaterThan(17)], ]; } }
- Inject request data (e.g., in tests or controllers) and validate:
$request = new CreateUserRequest(); $request->initialize( [], // GET ['email' => 'user@example.com', 'age' => 25] ); $request->validateOrFail(); // Throws ValidationException on failure
- Catch and inspect validation errors:
use Mitsuki\Hermite\Validators\Exceptions\ValidationException; try { $request->validateOrFail(); } catch (ValidationException $e) { $errors = $e->getErrors(); // $errors['email'] = 'This value is not a valid email address.' }