| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- namespace App\Module\OpenAPI\Validations;
- use App\Module\OpenAPI\Validators\AppNameUniqueValidator;
- use App\Module\OpenAPI\Validators\ScopeListValidator;
- use App\Module\OpenAPI\Validators\RateLimitConfigValidator;
- use App\Module\OpenAPI\Validators\IpWhitelistValidator;
- use UCore\ValidationCore;
- /**
- * 应用创建验证类
- */
- class AppCreateValidation extends ValidationCore
- {
- /** @var array|null 处理后的权限范围列表 */
- public ?array $processedScopes = null;
- /** @var array|null 处理后的频率限制配置 */
- public ?array $processedRateLimits = null;
- /** @var array|null 处理后的IP白名单 */
- public ?array $processedIpWhitelist = null;
- /**
- * 验证规则
- */
- public function rules($rules = []): array
- {
- return [
- // 基础字段验证
- ['name', 'required'],
- ['name', 'string', 'min' => 2, 'max' => 100],
- ['description', 'string', 'max' => 500],
- ['website', 'url'],
- ['callback_url', 'url'],
- ['contact_email', 'email'],
- ['user_id', 'required'],
- ['user_id', 'integer', 'min' => 1],
- // 状态和认证类型验证
- ['status', 'in', 'range' => ['ACTIVE', 'INACTIVE', 'SUSPENDED']],
- ['auth_type', 'in', 'range' => ['API_KEY', 'JWT', 'OAUTH2', 'SIGNATURE', 'BASIC', 'BEARER']],
- // 业务验证(按顺序执行)
- [
- 'name', new AppNameUniqueValidator($this),
- 'msg' => '应用名称已存在'
- ],
- [
- 'scopes', new ScopeListValidator($this, ['processedScopes']),
- 'msg' => '权限范围配置无效'
- ],
- [
- 'rate_limits', new RateLimitConfigValidator($this, ['processedRateLimits']),
- 'msg' => '频率限制配置无效',
- 'when' => function($data) {
- return isset($data['rate_limits']);
- }
- ],
- [
- 'ip_whitelist', new IpWhitelistValidator($this, ['processedIpWhitelist']),
- 'msg' => 'IP白名单配置无效',
- 'when' => function($data) {
- return isset($data['ip_whitelist']);
- }
- ],
- ];
- }
- /**
- * 默认值
- */
- public function default(): array
- {
- return [
- 'status' => 'ACTIVE',
- 'auth_type' => 'API_KEY',
- 'scopes' => ['USER_READ', 'GAME_READ'],
- 'rate_limits' => [
- 'requests_per_minute' => 60,
- 'requests_per_hour' => 1000,
- 'requests_per_day' => 10000,
- ],
- 'ip_whitelist' => null,
- ];
- }
- }
|