AppCreateValidation.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace App\Module\OpenAPI\Validations;
  3. use App\Module\OpenAPI\Validators\AppNameUniqueValidator;
  4. use App\Module\OpenAPI\Validators\ScopeListValidator;
  5. use App\Module\OpenAPI\Validators\RateLimitConfigValidator;
  6. use App\Module\OpenAPI\Validators\IpWhitelistValidator;
  7. use UCore\ValidationCore;
  8. /**
  9. * 应用创建验证类
  10. */
  11. class AppCreateValidation extends ValidationCore
  12. {
  13. /** @var array|null 处理后的权限范围列表 */
  14. public ?array $processedScopes = null;
  15. /** @var array|null 处理后的频率限制配置 */
  16. public ?array $processedRateLimits = null;
  17. /** @var array|null 处理后的IP白名单 */
  18. public ?array $processedIpWhitelist = null;
  19. /**
  20. * 验证规则
  21. */
  22. public function rules($rules = []): array
  23. {
  24. return [
  25. // 基础字段验证
  26. ['name', 'required'],
  27. ['name', 'string', 'min' => 2, 'max' => 100],
  28. ['description', 'string', 'max' => 500],
  29. ['website', 'url'],
  30. ['callback_url', 'url'],
  31. ['contact_email', 'email'],
  32. ['user_id', 'required'],
  33. ['user_id', 'integer', 'min' => 1],
  34. // 状态和认证类型验证
  35. ['status', 'in', 'range' => ['ACTIVE', 'INACTIVE', 'SUSPENDED']],
  36. ['auth_type', 'in', 'range' => ['API_KEY', 'JWT', 'OAUTH2', 'SIGNATURE', 'BASIC', 'BEARER']],
  37. // 业务验证(按顺序执行)
  38. [
  39. 'name', new AppNameUniqueValidator($this),
  40. 'msg' => '应用名称已存在'
  41. ],
  42. [
  43. 'scopes', new ScopeListValidator($this, ['processedScopes']),
  44. 'msg' => '权限范围配置无效'
  45. ],
  46. [
  47. 'rate_limits', new RateLimitConfigValidator($this, ['processedRateLimits']),
  48. 'msg' => '频率限制配置无效',
  49. 'when' => function($data) {
  50. return isset($data['rate_limits']);
  51. }
  52. ],
  53. [
  54. 'ip_whitelist', new IpWhitelistValidator($this, ['processedIpWhitelist']),
  55. 'msg' => 'IP白名单配置无效',
  56. 'when' => function($data) {
  57. return isset($data['ip_whitelist']);
  58. }
  59. ],
  60. ];
  61. }
  62. /**
  63. * 默认值
  64. */
  65. public function default(): array
  66. {
  67. return [
  68. 'status' => 'ACTIVE',
  69. 'auth_type' => 'API_KEY',
  70. 'scopes' => ['USER_READ', 'GAME_READ'],
  71. 'rate_limits' => [
  72. 'requests_per_minute' => 60,
  73. 'requests_per_hour' => 1000,
  74. 'requests_per_day' => 10000,
  75. ],
  76. 'ip_whitelist' => null,
  77. ];
  78. }
  79. }