StoreRuleRequest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace App\Http\Requests;
  3. use Illuminate\Foundation\Http\FormRequest;
  4. use Illuminate\Validation\Rule;
  5. class StoreRuleRequest extends FormRequest
  6. {
  7. /**
  8. * Determine if the user is authorized to make this request.
  9. *
  10. * @return bool
  11. */
  12. public function authorize()
  13. {
  14. return true;
  15. }
  16. /**
  17. * Get the validation rules that apply to the request.
  18. *
  19. * @return array
  20. */
  21. public function rules()
  22. {
  23. return [
  24. 'name' => [
  25. 'required',
  26. 'string',
  27. 'max:50',
  28. ],
  29. 'conditions' => [
  30. 'required',
  31. 'array',
  32. 'max:5',
  33. ],
  34. 'conditions.*.type' => [
  35. 'required',
  36. Rule::in([
  37. 'subject',
  38. 'sender',
  39. 'alias',
  40. 'alias_description',
  41. ]),
  42. ],
  43. 'conditions.*.match' => [
  44. 'sometimes',
  45. 'required',
  46. Rule::in([
  47. 'is exactly',
  48. 'is not',
  49. 'contains',
  50. 'does not contain',
  51. 'starts with',
  52. 'does not start with',
  53. 'ends with',
  54. 'does not end with',
  55. ]),
  56. ],
  57. 'conditions.*.values' => [
  58. 'required',
  59. 'array',
  60. 'min:1',
  61. 'max:10',
  62. ],
  63. 'conditions.*.values.*' => [
  64. 'distinct',
  65. ],
  66. 'actions' => [
  67. 'required',
  68. 'array',
  69. 'max:5',
  70. ],
  71. 'actions.*.type' => [
  72. 'required',
  73. Rule::in([
  74. 'subject',
  75. 'displayFrom',
  76. 'encryption',
  77. 'banner',
  78. 'block',
  79. 'removeAttachments',
  80. 'forwardTo',
  81. //'webhook',
  82. ]),
  83. ],
  84. 'actions.*.value' => Rule::forEach(function ($value, $attribute, $data, $action) {
  85. if ($action['type'] === 'forwardTo') {
  86. return [Rule::in(user()->verifiedRecipients()->pluck('id')->toArray())]; // Must be a valid verified recipient
  87. }
  88. return [
  89. 'required',
  90. 'max:50',
  91. ];
  92. }),
  93. 'operator' => [
  94. 'required',
  95. 'in:AND,OR',
  96. ],
  97. 'forwards' => 'boolean',
  98. 'replies' => 'boolean',
  99. 'sends' => 'boolean',
  100. ];
  101. }
  102. }