rudra/validation
Composer 安装命令:
composer require rudra/validation
包简介
Rudra framework
README 文档
README
Rudra-Validation | API
Installation
composer require rudra/validation
Features
- Fluent Interface for chain validation
- Built-in sanitization (HTML tags, trimming)
- CSRF protection
- Custom validation rules
- Human-readable error messages with field aliases
- Strict type checking
Available Rules
| Rule | Description | Example |
|---|---|---|
required() |
Field must not be empty | ->required() |
min(int $length) |
Minimum string length | ->min(3) |
max(int $length) |
Maximum string length | ->max(255) |
email() |
Valid email address | ->email() |
url() |
Valid URL | ->url() |
integer() |
Integer value | ->integer() |
numeric() |
Numeric value (int or float) | ->numeric() |
between(int|float $min, int|float $max) |
Value in range | ->between(1, 100) |
equals(mixed $value) |
Strict equality | ->equals('password123') |
in(array $allowed) |
Value in allowed list | ->in(['admin', 'user']) |
regex(string $pattern) |
Match regex pattern | ->regex('/^[A-Z]{3}$/') |
date(string $format) |
Valid date format | ->date('Y-m-d') |
csrf(array $tokens) |
CSRF token validation | ->csrf($_SESSION['csrf']) |
custom(callable $callback) |
Custom validation | ->custom(fn($v) => strlen($v) > 5) |
sanitize(string $value) |
Strip HTML tags | ->sanitize($input, '<p><a>') |
Example of usage
use Rudra\Validation\ValidationFacade; $_SESSION['csrf'][] = '123456'; $processed = [ 'set_without_validation' => ValidationFacade::set('set_without_validation')->run(), 'set_with_data_clearing' => ValidationFacade::sanitize(' <p>String</p> ')->run(), 'required' => ValidationFacade::set('required')->required()->run(), 'integer' => ValidationFacade::set(12345)->required()->integer()->run(), 'minimum' => ValidationFacade::set('12345')->required()->min(5)->run(), 'maximum' => ValidationFacade::set('12345')->required()->max(5)->run(), 'equals' => ValidationFacade::set('12345')->equals('12345')->run(), 'email' => ValidationFacade::email('user@example.com')->run(), 'csrf' => ValidationFacade::set('123456')->csrf($_SESSION['csrf'])->run() ];
Data is validated in a chain
For example:
ValidationFacade::sanitize(' <p>12345</p> ')->required()->min(3)->max(10)->equals('12345')->run(); ValidationFacade::email('user@example.com')->max(25)->run();
Data validation check
if (ValidationFacade::approve($processed)) { $validated = ValidationFacade::getValidated($processed, ["csrf", "_method"]); }
getValidated
Gets an array of validated data excluding the keys ["csrf", "_method"]
Get all error messages
ValidationFacade::getErrors($processed, ['required']);
getAlerts
Gets an array with error messages excluding the keys ['required']
Using field aliases
use Rudra\Validation\ValidationFacade; ValidationFacade::setAliases([ 'usr_name' => 'Username', 'usr_email' => 'Email Address' ]); $processed = [ 'usr_name' => ValidationFacade::set($_POST['usr_name'])->required()->min(3)->run(), 'usr_email' => ValidationFacade::email($_POST['usr_email'])->run(), ]; $errors = ValidationFacade::getErrors($processed); // Returns: ['usr_name' => ['msg' => 'Поле должно быть заполнено', 'alias' => 'Username'], ...]
License
This project is licensed under the Mozilla Public License 2.0 (MPL-2.0) — a free, open-source license that:
- Requires preservation of copyright and license notices,
- Allows commercial and non-commercial use,
- Requires that any modifications to the original files remain open under MPL-2.0,
- Permits combining with proprietary code in larger works.
📄 Full license text: LICENSE
🌐 Official MPL-2.0 page: https://mozilla.org/MPL/2.0/
rudra/validation 适用场景与选型建议
rudra/validation 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 528 次下载、GitHub Stars 达 1, 最近一次更新时间为 2016 年 07 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「validator」 「validation」 「rudra」 「validation-library」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rudra/validation 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rudra/validation 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rudra/validation 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Runn Me! Validation and Sanitization Library
Extension for Opis JSON Schema
Modified Smarty for Rudrax
Modified Smarty for Rudrax
Adds request-parameter validation to the SLIM 3.x PHP framework
redbeanphp service for Rudrax
统计信息
- 总下载量: 528
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 11
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MPL-2.0
- 更新时间: 2016-07-25