ket-php/validator
最新稳定版本:1.1.1-beta
Composer 安装命令:
composer require ket-php/validator
包简介
Simple PHP validator
README 文档
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;
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-01