ipl/validator
Composer 安装命令:
composer require ipl/validator
包简介
Icinga PHP Library - Common validators and validator chaining
README 文档
README
ipl/validator provides a collection of reusable input validators and a
ValidatorChain to compose and execute multiple validators in a
user-defined order.
Installation
The recommended way to install this library is via Composer:
composer require ipl/validator
ipl/validator requires PHP 8.0 or later with the mbstring and openssl extensions.
Usage
Applying a Single Validator
Every validator exposes the same basic workflow:
- Configure the validator
- Call
isValid($value) - Read validation errors via
getMessages()if validation fails
use ipl\Validator\StringLengthValidator; $validator = new StringLengthValidator([ 'min' => 3, 'max' => 10, ]); // $value is the input to validate. if (! $validator->isValid($value)) { echo implode(PHP_EOL, $validator->getMessages()); }
Composing Multiple Validators with ValidatorChain
Use ValidatorChain when a value must satisfy multiple conditions. By default,
all validators run in sequence even when one fails, so every error is collected.
Set breakChainOnFailure: true when adding a validator to stop on the first failure.
use ipl\Validator\HostnameValidator; use ipl\Validator\RegexMatchValidator; use ipl\Validator\ValidatorChain; $chain = new ValidatorChain(); $chain->add(new HostnameValidator(), breakChainOnFailure: true); $chain->add(new RegexMatchValidator('/^api\./')); // $value is the input to validate. if (! $chain->isValid($value)) { echo implode(PHP_EOL, $chain->getMessages()); }
In this example, the chain stops early when HostnameValidator fails
because it was added with breakChainOnFailure: true.
Built-In Validators
ipl/validator ships with validators for common categories of input:
-
Value and range — string length, numeric comparisons, and membership tests
-
Text and network formats — email addresses, hostnames, CIDR notation, dates, and regular expressions
-
File and security — uploaded files, private keys, and X.509 certificates
-
Custom logic — callback-based validation for specific rules:
use ipl\Validator\CallbackValidator; // Illustrates cross-field validation, omits type checks for brevity. $validator = new CallbackValidator(function ($end, CallbackValidator $validator) use ($start): bool { if ($end <= $start) { $validator->addMessage('End must be after start.'); return false; } return true; });
Use this for rules that built-in validators cannot express — such as cross-field comparisons or checks against runtime state.
Changelog
See CHANGELOG.md for a list of notable changes.
License
ipl/validator is licensed under the terms of the MIT License.
ipl/validator 适用场景与选型建议
ipl/validator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 79.18k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 03 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 ipl/validator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ipl/validator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 79.18k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 3
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-03-11