laurynasgadl/php-validator 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 laurynasgadl/php-validator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 452.19k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-10-21