ket-php / validator
Simple PHP validator
Installs: 4
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/ket-php/validator
Requires
- php: ^8.1
- ext-mbstring: *
Requires (Dev)
- phpunit/phpunit: ^10.5
Suggests
- ext-mbstring: Recommended for multibyte string safety
This package is auto-updated.
Last update: 2026-01-04 12:24:58 UTC
README
KetPHP Validator is a lightweight and highly typed validation library.
Opportunities
- Validation of values and objects
- PHP 8 Attributes support
- Extensible validation rules
- Clear error model (
Result/Violation) - Optional stop at the first error
- No dependencies
The library is suitable for validating DTOs, Form Objects, Request objects, and any input data.
Supported rules
| Rule | Description |
|---|---|
NotBlank |
Value must not be empty |
MinLength |
Minimum line length |
MaxLength |
Maximum line length |
Range |
Range of numbers |
Optional |
Makes the rule optional |
Installation
Install via Composer:
composer require ket-php/validator
Usage
Validation of the value
use KetPHP\Validator\Validator; use KetPHP\Validator\Annotation\Rule\MaxLength; use KetPHP\Validator\Annotation\Rule\NotBlank; $result = Validator::create('Hello world')->add(new NotBlank())->add(new MaxLength(5))->validate(); // returned Result if ($result->isValid() === false) { foreach ($result->all() as $violation) { echo $violation->getMessage(); print_r($violation->getParams()); } }
Validation of the object via attributes
Object:
use KetPHP\Validator\Annotation\Rule\Attribute\MaxLength; use KetPHP\Validator\Annotation\Rule\Attribute\NotBlank; use KetPHP\Validator\Annotation\Rule\Attribute\Range; final class UserDto { #[NotBlank] #[MaxLength(50)] public string $name; #[Range(min: 18, max: 99)] public int $age; }
Example:
use KetPHP\Validator\Validator; use KetPHP\Validator\Context\Result; $user = new UserDto(); $user->name = ''; $user->age = 15; $result = Validator::of($user); // returned UserDto (if valid) or Result (if invalid) if ($result instanceof Result) { foreach ($result->all() as $violation) { echo $violation->getMessage(); print_r($violation->getParams()); } }
Optional (nullable values)
Example:
use KetPHP\Validator\Annotation\Rule\Optional; use KetPHP\Validator\Annotation\Rule\MaxLength; $rule = new Optional(new MaxLength(10));
Attribute:
#[Optional(new MaxLength(10))] public ?string $comment;
Error model
new Violation( message: 'Max length is {max}', // string code: 'max_length', // string|int params: ['max' => 10, 'property' => 'name'] // array );
Result:
$result->isValid(); // bool $result->all(); // Violation[]
Creating your own rule
Rule
use KetPHP\Validator\Common\ValidationRuleInterface; use KetPHP\Validator\Context\Violation; final class StartsWithA implements ValidationRuleInterface { public function validate(mixed $value): ?Violation { if (str_starts_with($value, 'A') === false) { return new Violation('Value must start with letter A', 'starts_with_a'); } return null; } }
Attribute
use KetPHP\Validator\Annotation\Rule\Attribute\Rule; use Attribute; #[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] final class StartsWithA extends Rule { public function __construct() { parent::__construct(\Your\App\StartsWithA::class); } }
Using
Rule:
$rule = new StartsWithA(); $rule->validate('Architecture'); // null $rule->validate('Non Architecture'); // Violation
Attribute:
use KetPHP\Validator\Annotation\Rule\Attribute\Rule; use Your\App\StartsWithA; #[StartsWithA] // With your attribute #[Rule(StartsWithA::class)] // Without your attribute public string $code;