limoncello-php/validation
Composer 安装命令:
composer require limoncello-php/validation
包简介
Validation framework.
关键字:
README 文档
README
This validation library fast, easy to use yet very powerful and flexible solution. Unlike many other libraries it does not try to give you 'validation rules' for all possible cases because those implementations might not fit your requirements and using such libraries is a pain. Instead it provides an extremely simple way of adding custom validation rules.
Also it supports caching of validation rules which makes it very fast. Custom error codes and messages are supported as well. Error messages could be customized/localized and support placeholders.
Usage sample
$validator = v::validator([ 'sku' => r::required(r::sku()), 'amount' => r::required(r::amount(5)), 'delivery_date' => r::nullable(r::deliveryDate()), 'email' => r::email(), 'address1' => r::required(r::address1()), 'address2' => r::address2(), 'accepted' => r::required(r::areTermsAccepted()), ]); $input = [ 'sku' => '...', 'amount' => '...', ... ]; if ($validator->validate($input)) { // use validated/converted/sanitized inputs $validated = $validator->getCaptures(); } else { // print validation errors $errors = $validator->getErrors(); }
Full sample code is here.
As you can see such custom rules as sku, amount, deliveryDate, address1, address2 and areTermsAccepted could be perfectly combined with built-in required and nullable. It makes the rules reusable in CREATE and UPDATE operations where typically inputs are required on creation and optional on update.
How easy to write those rules? Many could be made from built-in ones below (e.g. amount, address1, address2 and areTermsAccepted)
equals,notEquals,inValues,lessThan,lessOrEquals,moreThan,moreOrEquals,between,stringLengthBetween,stringLengthMin,stringLengthMax,regexp,nullable,stringToBool,stringToDateTime,stringToFloat,stringToInt,stringArrayToIntArray,andX,orX,ifX,success,fail,required,enum,filter,isArray,isString,isBool,isInt,isFloat,isNumeric,isDateTime
class Rules extends \Limoncello\Validation\Rules { public static function sku(): RuleInterface { return static::stringToInt(new IsSkuRule()); } public static function amount(int $max): RuleInterface { return static::stringToInt(static::between(1, $max)); } public static function deliveryDate(): RuleInterface { return static::stringToDateTime(DateTime::ISO8601, new IsDeliveryDateRule()); } public static function email(): RuleInterface { return static::isString( static::filter(FILTER_VALIDATE_EMAIL, null, Errors::IS_EMAIL, static::stringLengthMax(255)) ); } public static function address1(): RuleInterface { return static::isString(static::stringLengthBetween(1, 255)); } public static function address2(): RuleInterface { return static::nullable(static::isString(static::stringLengthMax(255))); } public static function areTermsAccepted(): RuleInterface { return static::stringToBool(static::equals(true)); } }
Custom rule such as IsSkuRule might require quering database and could be added with minimal overhead
class IsSkuRule extends ExecuteRule { public static function execute($value, ContextInterface $context): array { $pdo = $context->getContainer()->get(PDO::class); $isSku = ...; return $isSku === true ? self::createSuccessReply($value) : self::createErrorReply($context, $value, Errors::IS_VALID_SKU); } }
When validator is created a developer can pass PSR Container with custom services and have access to this container from validation rules. Thus validation could be easily integrated with application logic.
Installation
$ composer require limoncello-php/validation
Note: for message translation PHP-intl is needed.
Issues
Any related issues please send to limoncello.
Testing
$ composer test
limoncello-php/validation 适用场景与选型建议
limoncello-php/validation 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.23k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2016 年 06 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「validation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 limoncello-php/validation 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 limoncello-php/validation 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 limoncello-php/validation 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Adds request-parameter validation to the SLIM 3.x PHP framework
A jQuery augmented PHP library for creating and validating HTML forms
A Laravel validator for delimiter-separated list of emails.
A CakePHP behavior to validate foreign keys
PHP Validator for stuff.
Stand-alone PHP Class for Data Sanitization and Validation
统计信息
- 总下载量: 3.23k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 5
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2016-06-30