laurynasgadl/php-validator
Composer 安装命令:
composer require laurynasgadl/php-validator
包简介
Array value validation library for PHP
关键字:
README 文档
README
inspired by Laravel's form validation
use Luur\Validator\Validator; use Luur\Validator\Rules\Concrete\MinRule; use Luur\Validator\Rules\Concrete\RequiredRule; use Luur\Validator\Exceptions\ValidationFailed; $validator = new Validator(); $rules = [ 'client_id' => 'required|integer', 'amount' => [new RequiredRule(), new MinRule(0)], 'count' => 'between:-12,50', 'details' => 'array|required', '*.name' => 'string|required', ]; $params = [ 'client_id' => 12345, 'amount' => 1234, 'details' => [ 'name' => 'John Doe', ], ]; try { $allParams = $validator->validate($rules, $params); $validatedParams = $validator->validated(); } catch (ValidationFailed $exception) { var_dump($exception->getMessage()); }
Installation
composer require laurynasgadl/php-validator
Documentation
Rules
Custom rules
use Luur\Validator\Rules\AbstractRule; use Luur\Validator\Validator; class CustomRule extends AbstractRule { /** * @param mixed $value * @return bool */ public function passes($value) { return true; } } $validator = new Validator(); $params = ['test' => 123]; $result = $validator->validate([ 'test' => new CustomRule(), ], $params);
You can also register the rule to be able to use it as a string:
use Luur\Validator\Rules\AbstractRule; use Luur\Validator\Validator; class CustomRule extends AbstractRule { /** * @param mixed $value * @return bool */ public function passes($value) { return true; } } $validator = new Validator(); $validator->registerRule(CustomRule::getSlug(), CustomRule::class); $params = ['test' => 123]; $result = $validator->validate([ 'test' => 'test', ], $params);
Existing rules
array : the value needs to be an array
between:{from_size},{to_size} : the value needs to be between the given range
float : the value needs to be a float
integer : the value needs to be an integer
max:{max_size} : the value needs to be less or equal to the given amount
min:{min_size} : the value needs to be greater or equal to the given amount
required : the parameter needs to exist in the data set
required_with:{param_1}... : the parameter needs to exist in the data set if all the other parameters exist
required_without:{param_1}... : the parameter needs to exist in the data set if one of the other parameters do not exist
size : the size of the value needs to be equal to the given amount. The size of a string is its length, the size of an array is the number of elements inside it, the size of a boolean is 0 or 1.
string : the value needs to be a string
boolean : the value needs to be a boolean
numeric : the value needs to be a numeric
default:{value} : the rule will always be applied first, meaning, params can successfully pass required rules even if their value is not set or is null
alpha_dash : the value can only be made up of uppercase and lowercase letters, numbers and - or _ symbols
alpha_numeric : the value can only be made up of uppercase and lowercase letters and numbers
regex:{pattern} : the value needs to match the given pattern
email : the value needs to be an email
url : the value needs to start with http:// or https:// and only contain URL-valid symbols
ip : the value needs to be an IP address
Custom messages
You can set custom validation messages either via the Validator constructor or the validate method:
use Luur\Validator\Validator; $validator = new Validator(); $rules = [ 'options.*.key' => 'string|required', ]; $params = [ 'options' => [ [ 'key' => 'passes', 'value' => true, ], [ 'key' => ['passes'], 'value' => false, ], ], ]; $messages = [ 'options.*.key.string' => 'Option key should be a string', ]; $validator->validate($rules, $params, $messages);
laurynasgadl/php-validator 适用场景与选型建议
laurynasgadl/php-validator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 452.19k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2019 年 10 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「validator」 「validation」 「array」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 laurynasgadl/php-validator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 laurynasgadl/php-validator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 laurynasgadl/php-validator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Runn Me! Validation and Sanitization Library
Extension for Opis JSON Schema
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
统计信息
- 总下载量: 452.19k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-10-21