catlabinteractive/validator
Composer 安装命令:
composer require catlabinteractive/validator
包简介
A php input model that can handle Swagger V1 specs.
关键字:
README 文档
README
A php input validator that can handle Swagger V1 specs.
Example usage:
$validator = \CatLab\Validator\Validator::fromSwagger('specs/'); $input = array ('id' => 1, 'name' => 'Example'); if ($validator->validate('ModelId', $input)) { echo 'Success!'; } else { echo 'Failure' . "\n"; foreach ($validator->getErrors() as $v) { echo $v . "\n"; } }
Without swagger:
Regular models:
$specs = array ( 'message' => 'required|string', 'user' => array ( 'id' => 'numeric|required', 'company' => array ( 'id' => 'numeric|required', 'name' => 'string|required' ) ) ); $data = array ( 'message' => 'This is a message', 'user' => array ( 'id' => 1, 'company' => array ( 'id' => 1, 'name' => 'This is a company name' ) ) ); $validator = new Validator(); $validator->addModel(Model::make('test', $specs)); $valid = $validator->validate('test', $data);
Optional attributes: Append a question mark to your property name to mark a property as optional.
$specs = array ( 'message' => 'required|string', 'user' => array ( 'id' => 'numeric|required', 'company?' => array ( 'id' => 'numeric|required', 'name' => 'string|required' ) ) ); $data = array ( 'message' => 'This is a message', 'user' => array ( 'id' => 1 ) ); $validator = new Validator(); $validator->addModel(Model::make('test', $specs)); $correct = $validator->validate('test', $data);
Advanced examples:
Arrays:
$validator = new Validator(); $model = Model::make( 'ArrayModel', array ( 'id' => 'required|int', 'collection' => array ( 'count' => 'int|required', 'items[]' => 'int' ) ) ); $data = array ( 'id' => 1, 'collection' => array ( 'count' => 2, 'items' => array (1, 2, 3) ) ); $correct = $validator->validate('ArrayModel', $data); $validator->addModel($model);
Arrays of models:
$validator = new Validator(); $model = Model::make( 'ArrayModel', array ( 'id' => 'required|int', 'collection' => array ( 'count' => 'int|required', 'items[]?' => array ( 'id' => 'int|required', 'name' => 'string' ) ) ) ); $validator->addModel($model); $data = array ( 'id' => 1, 'collection' => array ( 'count' => 2, 'items' => array ( array ( 'id' => 15, 'name' => 'Foo' ), array ( 'id' => 16, 'name' => 'Bar' ) ) ) ); $correct = $validator->validate('ArrayModel', $data);
Put a question mark at the end of your property name to mark the array property as optional.
$validator = new Validator(); $model = Model::make( 'ArrayModel', array ( 'id' => 'required|int', 'collection' => array ( 'count' => 'int|required', 'items[]?' => array ( 'id' => 'int|required', 'name' => 'string' ) ) ) ); $validator->addModel($model); $correctData = array ( 'id' => 1, 'collection' => array ( 'count' => 2, 'items' => array ( array ( 'id' => 15, 'name' => 'Foo' ), array ( 'id' => 16, 'name' => 'Bar' ) ) ) ); $correct = $validator->validate('ArrayModel', $correctData);
Usage with PHPUnit:
$model = Model::make( 'ModelWithValues', array ( 'id' => 'required|string', 'name' => 'required|string', 'value1' => 'required|string' ) ); $data = array ( 'id' => 'I am a string', 'name' => 'My name is Paul', 'value1' => 'I\'m very happy here' ); $this->assertThat($data, new IsValidModel($model));
Checking for exact values (with conversion to comparable types):
$validator = new Validator(); $model = Model::make( 'ModelWithValues', array ( 'id' => 'required|string', 'name' => 'required|string', 'value1' => 'required|string' ) ); $validator->addModel($model); // Set the expected values $model->setValues(array ( 'id' => 15, 'name' => 'Thijs', 'value1' => 'woop' )); // Input data $data = array ( 'id' => 15, 'name' => 'Thijs', 'value1' => 'woop' ); // Check if model match expectations & compare values $correct = $validator->validate ('ModelWithValues', $data);
catlabinteractive/validator 适用场景与选型建议
catlabinteractive/validator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.66k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 01 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「framework」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 catlabinteractive/validator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 catlabinteractive/validator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 catlabinteractive/validator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Framework HLEB2 is the foundation of the web application. Provides ease of development and application performance.
High-performance, opinionated PHP 8 web framework with O(1) routing, parallel async MySQL, type-safe asset bridge, and React-island frontend
The skeleton application for the Geoffrey agent framework.
Framework used for the Darflen social media project.
Speedrunner package for programming certification exam
Micro-framework PHP enxuto e modular: ORM, Router, Auth, Validation, Security, Container, View, Queue, Console e Schema.
统计信息
- 总下载量: 1.66k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 25
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-01-11