bugarinov/chain-validation
Composer 安装命令:
composer require bugarinov/chain-validation
包简介
Classes which implements the Chain of Responsibility pattern in validating data. Intended primary use is for handling complex validations.
README 文档
README
Notice:
The latest build is 0.2.x and is not backwards compatible with 0.1.x versions. See Migration for details.
Alternatively, you can read 0.1.x-README.md for versions 0.1.x.
Chain Validation is a simple PHP library for handling very complex validations that goes beyond checking of data types, format and values of a given data. It uses the Chain of Responsibility design pattern in order to execute each validation in a consecutive manner. The execution will stop once an error occured during the validation process within a link. You can alter the data as it goes through the chain, depending on your requirements.
Usage
Defining and executing validations
Usage is simple. First, group your validations by purpose/relevance and create a new class for each validation group which extends the AbstractLink class.
class ValidationOne extends AbstractLink { public function evaluate(?array $data): EvaluationResult { /* * Run your validation process here which will * set the value of $validationFailed whether * the validation failed (true) or not (false) */ if ($validationFailed) { return new ResultFailed('your error message here', 400); } /* * If the validation did not fail, return an * evaluation result which contains the data */ return new ResultSuccess($data); } }
You can run the validations using the ChainValidation class. The chain process each validation in exact order or in First in, First out manner.
$chain = new ChainValidation(); $chain->add(new ValidationOne()); $chain->add(new ValidationTwo()); $chain->add(new ValidationThree()); $chain->add(new ValidationFour()); $validatedData = $chain->execute($data);
Catching errors
To know whether an error occured or not, you can use the hasError() function of the ChainValidation class and get the error message using the getErrorMessage() function after the validation execution. You can get the corresponding error code using the getErrorCode() function.
$validatedData = $chain->execute($data); if ($chain->hasError()) { throw new Exception($chain->getErrorMessage()); // or your preferred way of handling errors e.g. returing a response } // Some processes here if the validation succeeds e.g. committing of data to the database commitToDatabase($validatedData);
If the chain validation succeeds, it will return the validated data, otherwise it will return null.
Errors with a body
There are instances where errors messages are not enough, especially on front-end applications that highlight specific errors on their UI. Hence, a new parameter was added to the constructor of the ResultFailed class in order to pass the this data to the chain.
$errorBody = [ 'items_with_error' => [ [ 'item_uid' => '100012', 'reason' => 'something went wrong' ] ] ]; return new ResultFailed('Some items have errors!', 400, $errorBody);
The error data can be obtained through the getErrorBody() function of the ChainValidation class and LinkInterface implementations.
// Example of returning an error response using PSR's ResponseInterface $validatedData = $chain->execute($data); if ($chain->hasError()) { $payload = [ 'statusCode' => $chain->getErrorCode(), 'error' => $chain->getErrorMessage(), 'body' => $chain->getErrorBody() ]; $response->getBody() ->write(json_encode($payload)); return $response->withStatus($error_code) ->withHeader('Content-type', 'application/json'); }
Migration from 0.1.x to 0.2.x
In order to migrate from 0.1.x to 0.2.x, you just need to replace the execute function on each link with the evaluate function.
From v0.1.x
class ValidationOne extends AbstractLink { public function execute(?array $data): ?array { if ($validationFailed) { return $this->throwError('your error message here', 400); } return $this->executeNext($data); } }
to v0.2.x
class ValidationOne extends AbstractLink { public function evaluate(?array $data): EvaluationResult { if ($validationFailed) { return new ResultFailed('your error message here', 400); } return new ResultSuccess($data); } }
Tests
Run the tests using composer test.
Tests with mocking of Link's evaluate function are added.
bugarinov/chain-validation 适用场景与选型建议
bugarinov/chain-validation 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 35 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 01 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 bugarinov/chain-validation 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bugarinov/chain-validation 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 35
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-01-22