WebhookUpdateValidation.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace App\Module\OpenAPI\Validations;
  3. use App\Module\OpenAPI\Validators\WebhookUrlValidator;
  4. use App\Module\OpenAPI\Validators\WebhookEventsValidator;
  5. use UCore\ValidationCore;
  6. /**
  7. * Webhook更新验证类
  8. */
  9. class WebhookUpdateValidation extends ValidationCore
  10. {
  11. /** @var array|null 处理后的事件列表 */
  12. public ?array $processedEvents = null;
  13. /**
  14. * 验证规则
  15. */
  16. public function rules($rules = []): array
  17. {
  18. return [
  19. // 基础字段验证(可选)
  20. ['name', 'string', 'min' => 2, 'max' => 100, 'when' => function($data) {
  21. return isset($data['name']);
  22. }],
  23. ['status', 'in', 'range' => ['ACTIVE', 'INACTIVE'], 'when' => function($data) {
  24. return isset($data['status']);
  25. }],
  26. ['timeout', 'integer', 'min' => 1, 'max' => 300, 'when' => function($data) {
  27. return isset($data['timeout']);
  28. }],
  29. ['retry_count', 'integer', 'min' => 0, 'max' => 10, 'when' => function($data) {
  30. return isset($data['retry_count']);
  31. }],
  32. // 业务验证(按顺序执行)
  33. [
  34. 'url', new WebhookUrlValidator($this),
  35. 'msg' => 'Webhook URL无效',
  36. 'when' => function($data) {
  37. return isset($data['url']) && !empty($data['url']);
  38. }
  39. ],
  40. [
  41. 'events', new WebhookEventsValidator($this, ['processedEvents']),
  42. 'msg' => '事件类型配置无效',
  43. 'when' => function($data) {
  44. return isset($data['events']);
  45. }
  46. ],
  47. ];
  48. }
  49. /**
  50. * 默认值
  51. */
  52. public function default(): array
  53. {
  54. return [];
  55. }
  56. }