peak/array-validation 问题修复 & 功能扩展

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

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

peak/array-validation

Composer 安装命令:

composer require peak/array-validation

包简介

Validation utilities for array structure

README 文档

README

version Total Downloads License

Installation

 composer require peak/array-validation

What is this?

This component help you to validate array structure by:

  • validating the type of any key values
  • ensuring a data structure with expected keys requirements
  • preventing structure pollution by allowing only a set of keys

This is especially useful when dealing with json data request, before using the data, you must validate his content so you can afterward check the value of those keys with your business logic without worrying about the type or presence of any key value.

How to use

8 Usages

1- General validation "à la carte" (stateless)

$validator = new Validator();

if ($validator->expectExactlyKeys($data, $keys) === true) {
    // ...
}

2- Validation with fluent interface (stateful)

$data = [ // data
    'tags' => [],
    'name' => 'foobar'
];
$validation = new Validation($data);

$validation
    ->expectExactlyKeys(['tags', 'name'])
    ->expectKeyToBeArray('tags');
    ->expectKeyToBeString('name');

if ($validation->hasErrors()) {
    // $lastError = $validation->getLastError();
    // $errors = $validation->getErrors();
}

3- Strict validation with fluent interface (stateful)

$validation = new StrictValidation($data);

// will throw an exception if any of tests below fail
$validation
    ->expectOnlyKeys(['id', 'title', 'description', 'isPrivate', 'tags'])
    ->expectAtLeastKeys(['id', 'title', 'description'])
    ->expectKeyToBeInteger('id')
    ->expectKeysToBeString(['title', 'description'])
    ->expectKeyToBeBoolean('isPrivate')
    ->expectKeyToBeArray('tags');

// if we reach this point, it means the array structure is
// valid according to the validation rules above.

4- Create a ValidationDefinition for later usage

$vDef = new ValidationDefinition();
$vDef
    ->expectOnlyKeys(['title', 'content', 'description'])
    ->expectAtLeastKeys(['title', 'content'])
    ->expectKeysToBeString(['title', 'content', 'description']);

$validation = new ValidationFromDefinition($vDef, $data);

if ($validation->hasErrors()) {
    // $validation->getErrors();
}

5- Create a validation Schema for later usage

Schema is just another way to write validation definitions. This format is ideal when you want to store schemas in file (ex: json, php array file, yml, etc.)

$mySchema = [
    'title' => [
        'type' => 'string',
        'required' => true
    ],
    'content' => [
        'type' => 'string',
        'nullable' => true,
    ],
];

$schema = new Schema(new SchemaCompiler(), $mySchema, 'mySchemaName');

$validation = new ValidationFromSchema($schema, $data);

if ($validation->hasErrors()) {
    // $validation->getErrors();
}

6- Strict validation using ValidationDefinition

// all validation definitions are executed at object creation and an exception is thrown if any of tests failed
new StrictValidationFromDefinition($validationDefinition, $arrayToValidate);

7- Strict validation using Schema

// all validation definitions are executed at object creation and an exception is thrown if any of tests failed
new StrictValidationFromSchema($schema, $arrayToValidate);

8- Validation and Strict Validation with ValidationBuilder

$validation = new ValidationBuilder();
$validation
    ->expectOnlyKeys(['title', 'content', 'description'])
    ->expectAtLeastKeys(['title', 'content'])
    ->expectKeysToBeString(['title', 'content', 'description']);

if ($validation->validate($data) === false)  {
    // $validation->getErrors();
    // $validation->getLastError();
}

and with strict validation:

//  will throw an exception if any of tests fail
$validation->strictValidate($data);

Validation methods

interface ValidationInterface
{
    public function expectExactlyKeys(array $keys);
    public function expectOnlyOneFromKeys( array $keys);
    public function expectAtLeastKeys(array $keys);
    public function expectOnlyKeys(array $keys);
    public function expectNKeys(int $n);
    public function expectKeyToBeArray(string $key, bool $acceptNull = false);
    public function expectKeysToBeArray(array $keys, bool $acceptNull = false);
    public function expectKeyToBeInteger(string $key, bool $acceptNull = false);
    public function expectKeysToBeInteger(array $keys, bool $acceptNull = false);
    public function expectKeyToBeFloat(string $key, bool $acceptNull = false);
    public function expectKeysToBeFloat(array $keys, bool $acceptNull = false);
    public function expectKeyToBeString(string $key, bool $acceptNull = false);
    public function expectKeysToBeString(array $keys, bool $acceptNull = false);
    public function expectKeyToBeBoolean(string $key, bool $acceptNull = false);
    public function expectKeysToBeBoolean(array $keys, bool $acceptNull = false);
    public function expectKeyToBeObject(string $key, bool $acceptNull = false);
    public function expectKeysToBeObject(array $keys, bool $acceptNull = false);
}

peak/array-validation 适用场景与选型建议

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

它主要适用于以下技术方向: 「validation」 「array」 「peak」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 42
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 15
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-02-07