ddrv/dresscode
Composer 安装命令:
composer require ddrv/dresscode
包简介
OpenAPI schema validator
关键字:
README 文档
README
Install
composer require ddrv/dresscode
Usage
<?php use Ddrv\DressCode\Action; use Ddrv\DressCode\DressCode; require 'vendor/autoload.php'; $validator = new DressCode(); $validator->validate(Action::output(), ['type' => 'string'], 'it is ok');
Check errors
<?php use Ddrv\DressCode\Action; use Ddrv\DressCode\DressCode; use Ddrv\DressCode\Exception\InvalidValueException; require 'vendor/autoload.php'; $validator = new DressCode(); $rule = [ 'type' => 'object', 'properties' => [ 'email' => [ 'type' => 'string', 'format' => 'email', ], 'login' => [ 'type' => 'string', 'minLength' => 5, 'maxLength' => 32, 'pattern' => '^[a-z\-]+$', ], 'birthday' => [ 'type' => 'string', 'format' => 'date', ], ], 'required' => ['email', 'login'], 'additionalProperties' => true, 'nullable' => true, ]; $data = [ 'email' => 'ivan', 'login' => 'ddrv', 'birthday' => '2020-02-30', ]; try { $validator->validate(Action::input(), $rule, $data); } catch (InvalidValueException $e) { foreach ($e->getErrors() as $error) { echo $error->getPath() . ': ' . $error->getMessage() . PHP_EOL; } } /* email: ivan is not a email login: string size must be between 5 to 32 symbols birthday: 2020-02-30 is not a date */
Defining rules
<?php use Ddrv\DressCode\Action; use Ddrv\DressCode\DressCode; require 'vendor/autoload.php'; $validator = new DressCode(); $validator->setEntity('#/entities/email', [ 'type' => 'string', 'format' => 'email', ]); $validator->setEntity('#/entities/login', [ 'type' => 'string', 'minLength' => 5, 'maxLength' => 32, 'pattern' => '^[a-z\-]+$', ]); $validator->setEntity('#/entities/date', [ 'type' => 'string', 'format' => 'date', ]); $rule = [ 'type' => 'object', 'properties' => [ 'email' => [ '$ref' => '#/entities/email', ], 'login' => [ '$ref' => '#/entities/login', ], 'birthday' => [ '$ref' => '#/entities/date', ], 'password' => [ 'type' => 'string', 'minLength' => 8, 'maxLength' => 32, 'writeOnly' => true, ], ], 'required' => ['email', 'login'], 'additionalProperties' => true, 'nullable' => true, ]; $data = [ 'email' => 'ivan@ddrv.ru', 'login' => 'i-dudarev', 'password' => 'short', ]; $valid = $validator->validate(Action::input(), $rule, $data); // Error password $valid = $validator->validate(Action::output(), $rule, $data); // No error because password writeOnly $valid = $validator->validate(Action::input(), ['$ref' => '#/entities/date'], '2020-12-30');
Register your string formats
<?php use Ddrv\DressCode\Action; use Ddrv\DressCode\Exception\WrongFormatException; use Ddrv\DressCode\Format\Format; use Ddrv\DressCode\DressCode; require 'vendor/autoload.php'; $validator = new DressCode(); $myEmailFormat = new class extends Format { public function check(string $value) : void{ if (!filter_var($value, FILTER_VALIDATE_EMAIL) !== false) { throw new WrongFormatException('email'); } } }; $validator->registerFormat('email', $myEmailFormat); $rule = [ 'type' => 'string', 'format' => 'email', ]; $validator->validate(Action::input(), $rule, 'ivan@ddrv.ru');
Strict types
use Action::input(true) and Action::output(true) for strict type checking.
Warning
do not use it if you are checking data
application/x-www-form-urlencoded
Supported types
- boolean
- number
- number (format
float) - number (format
double) - integer
- integer (format
int32) - integer (format
int64) - string
- string (format
binary) - string (format
byte) - string (format
date) - string (format
date-time) - string (format
email) - string (format
hostname) - string (format
ip) - string (format
ipv4) - string (format
ipv6) - string (format
uri) - string (format
uuid) - array
- object
Supported keywords
All types
- nullable
- readOnly
- writeOnly
- default (only for object properties)
Integer and number types
- minimum
- maximum
- exclusiveMinimum
- exclusiveMaximum
- multipleOf
String type
- pattern
- minLength
- maxLength
- enum
Array type
- items
- minItems
- maxItems
- uniqueItems
Object type
- properties
- required
- additionalProperties
- minProperties
- maxProperties
Support references
- $ref (only after call
setEntity()method)
Supported polymorphism
- oneOf
- anyOf
- allOf
- not
ddrv/dresscode 适用场景与选型建议
ddrv/dresscode 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 12 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「validator」 「swagger」 「openapi」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ddrv/dresscode 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ddrv/dresscode 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ddrv/dresscode 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Runn Me! Validation and Sanitization Library
Extension for Opis JSON Schema
Swagger integration to Laravel 5
Easy Swagger endpoint using the Zend Expressive routes config
一个接口文档生成器
Laravel credit card validator 2 from rap2hpoutre
统计信息
- 总下载量: 26
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-12-30