spacetab-io/harmony-validation 问题修复 & 功能扩展

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

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

spacetab-io/harmony-validation

Composer 安装命令:

composer require spacetab-io/harmony-validation

包简介

Asynchronous validation library

README 文档

README

Latest Stable Version Build Status Build status Coverage Status License

Async validation library

Requirements

  • PHP 7.3
    • ext-ctype
    • ext-dom
    • ext-fileinfo
    • ext-filter
    • ext-gd
    • ext-hash
    • ext-json
    • ext-libxml
    • ext-mbstring
  • Redis

In addition for non-blocking context one of the following event libraries should be installed:

Installation

composer require harmonyio/validation

Usage

This library is based on the amphp concurrency framework.

Basic usage

Validating a value against a single rule:

<?php declare(strict_types=1);

namespace HarmonyIO\Validation\Examples;

use Amp\Loop;
use HarmonyIO\Validation\Result\Result;
use HarmonyIO\Validation\Rule\Numeric\NumericType;

require_once '/path/to/vendor/autoload.php';

Loop::run(static function () {
    /** @var Result $result */
    $result = yield (new NumericType())->validate(true);

    var_dump($result);
});

This will result in the following Result object:

object(HarmonyIO\Validation\Result\Result)#11 (2) {
  ["valid":"HarmonyIO\Validation\Result\Result":private]=>
  bool(false)
  ["errors":"HarmonyIO\Validation\Result\Result":private]=>
  array(1) {
    [0]=>
    object(HarmonyIO\Validation\Result\Error)#10 (2) {
      ["message":"HarmonyIO\Validation\Result\Error":private]=>
      string(19) "Numeric.NumericType"
      ["parameters":"HarmonyIO\Validation\Result\Error":private]=>
      array(0) {
      }
    }
  }
}

Stacking validation rules

Validation rules can be stacked on top of each other when you want to apply multiple rules to the same value:

<?php declare(strict_types=1);

namespace HarmonyIO\Validation\Examples;

use Amp\Loop;
use HarmonyIO\Validation\Result\Result;
use HarmonyIO\Validation\Rule\Combinator\All;
use HarmonyIO\Validation\Rule\Numeric\Maximum;
use HarmonyIO\Validation\Rule\Numeric\Minimum;
use HarmonyIO\Validation\Rule\Numeric\NumericType;

require_once '/path/to/vendor/autoload.php';

Loop::run(static function () {
    /** @var Result $result */
    $result = yield (new All(
        new NumericType(),
        new Minimum(1),
        new Maximum(100)
    ))->validate(102);

    var_dump($result);
});

This will result in the following Result object:

object(HarmonyIO\Validation\Result\Result)#11 (2) {
  ["valid":"HarmonyIO\Validation\Result\Result":private]=>
  bool(false)
  ["errors":"HarmonyIO\Validation\Result\Result":private]=>
  array(1) {
    [0]=>
    object(HarmonyIO\Validation\Result\Error)#26 (2) {
      ["message":"HarmonyIO\Validation\Result\Error":private]=>
      string(15) "Numeric.Maximum"
      ["parameters":"HarmonyIO\Validation\Result\Error":private]=>
      array(1) {
        [0]=>
        object(HarmonyIO\Validation\Result\Parameter)#24 (2) {
          ["key":"HarmonyIO\Validation\Result\Parameter":private]=>
          string(7) "maximum"
          ["value":"HarmonyIO\Validation\Result\Parameter":private]=>
          float(100)
        }
      }
    }
  }
}

Validation rules

There is already an elaborate list of validation rules implemented. For an overview of all supported rules please see the documentation in the wiki.

All validation rules adhere to the same interface:

<?php declare(strict_types=1);

namespace HarmonyIO\Validation\Rule;

use Amp\Promise;

interface Rule
{
    /**
     * @param mixed $value
     * @return Promise<HarmonyIO\Validation\Result\Result>
     */
    public function validate($value): Promise;
}

This will ensure any value can always be passed in to be validated and it will always result in a promise which resolved to a Result object.

If you are missing a validation rule feel free to open an issue.

Combinators

Combinators are a way to combine multiple validation rules into a single result.

This can be used to build more complex validation rules based on existing rules.

Results and errors

All validation always result in a HarmonyIO\Validation\Result\Result object.
To check whether a value is valid after validating we can simply call the HarmonyIO\Validation\Result\Result::isValid() method which returns a simple boolean.

If the validation fails the validation errors can be accessed using one of two methods on the Result object:

  • HarmonyIO\Validation\Result\Result::getFirstError() - Returns either null when there were no errors or a HarmonyIO\Validation\Result\Error object otherwise
  • HarmonyIO\Validation\Result\Result::getErrors() - Returns an array with HarmonyIO\Validation\Result\Error objects

Errors

When validation results in an invalid result at least one Error object will be added to the Result object.

An error object always has an error message (which can be used for translation for rendering in your project) and zero or more parameters.
Parameters are used when failed validation rules are based on dynamic values. I.e. when the validation rule for minimum length of text fails it will result in the following Error object:

object(HarmonyIO\Validation\Result\Error)#17 (2) {
      ["message":"HarmonyIO\Validation\Result\Error":private]=>
      string(18) "Text.MinimumLength"
      ["parameters":"HarmonyIO\Validation\Result\Error":private]=>
      array(1) {
        [0]=>
        object(HarmonyIO\Validation\Result\Parameter)#15 (2) {
          ["key":"HarmonyIO\Validation\Result\Parameter":private]=>
          string(6) "length"
          ["value":"HarmonyIO\Validation\Result\Parameter":private]=>
          int(4)
        }
      }
    }

Traditional / blocking usage

Although this project is based on async programming it is also possible to use it in a more traditional blocking way using the wait() helper function:

<?php declare(strict_types=1);

namespace HarmonyIO\Validation\Examples;

use HarmonyIO\Validation\Result\Result;
use HarmonyIO\Validation\Rule\Numeric\NumericType;
use function Amp\Promise\wait;

require_once '/path/to/vendor/autoload.php';

/** @var Result $result */
$result = wait((new NumericType())->validate(true));

var_dump($result);

Rules

An overview of all implemented rules (including usage and version information) is documented in the wiki.

spacetab-io/harmony-validation 适用场景与选型建议

spacetab-io/harmony-validation 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 387 次下载、GitHub Stars 达 1, 最近一次更新时间为 2020 年 04 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 spacetab-io/harmony-validation 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 spacetab-io/harmony-validation 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 387
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 5
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-04-16