From 8dd7d9ba2600d24771388f9ce943aa31166c96f5 Mon Sep 17 00:00:00 2001 From: James Cole Date: Mon, 7 Feb 2022 09:42:23 +0100 Subject: [PATCH] Add validation for at least one active action. --- .../V1/Requests/Models/Rule/StoreRequest.php | 62 ++++++++++++++++++- resources/lang/en_US/validation.php | 2 + 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/app/Api/V1/Requests/Models/Rule/StoreRequest.php b/app/Api/V1/Requests/Models/Rule/StoreRequest.php index 9f6978d218..8664e50e74 100644 --- a/app/Api/V1/Requests/Models/Rule/StoreRequest.php +++ b/app/Api/V1/Requests/Models/Rule/StoreRequest.php @@ -153,12 +153,14 @@ class StoreRequest extends FormRequest function (Validator $validator) { $this->atLeastOneTrigger($validator); $this->atLeastOneAction($validator); + $this->atLeastOneActiveTrigger($validator); + $this->atLeastOneActiveAction($validator); } ); } /** - * Adds an error to the validator when there are no repetitions in the array of data. + * Adds an error to the validator when there are no triggers in the array of data. * * @param Validator $validator */ @@ -172,6 +174,64 @@ class StoreRequest extends FormRequest } } + /** + * Adds an error to the validator when there are no ACTIVE triggers in the array of data. + * + * @param Validator $validator + */ + protected function atLeastOneActiveTrigger(Validator $validator): void + { + $data = $validator->getData(); + $triggers = $data['triggers'] ?? []; + // need at least one trigger + if (!is_countable($triggers) || empty($triggers)) { + return; + } + $allInactive = true; + $inactiveIndex = 0; + foreach ($triggers as $index => $trigger) { + $active = array_key_exists('active', $trigger) ? $trigger['active'] : true; // assume true + if (true === $active) { + $allInactive = false; + } + if (false === $active) { + $inactiveIndex = $index; + } + } + if (true === $allInactive) { + $validator->errors()->add(sprintf('triggers.%d.active', $inactiveIndex), (string)trans('validation.at_least_one_active_trigger')); + } + } + + /** + * Adds an error to the validator when there are no ACTIVE actions in the array of data. + * + * @param Validator $validator + */ + protected function atLeastOneActiveAction(Validator $validator): void + { + $data = $validator->getData(); + $actions = $data['actions'] ?? []; + // need at least one trigger + if (!is_countable($actions) || empty($actions)) { + return; + } + $allInactive = true; + $inactiveIndex = 0; + foreach ($actions as $index => $action) { + $active = array_key_exists('active', $action) ? $action['active'] : true; // assume true + if (true === $active) { + $allInactive = false; + } + if (false === $active) { + $inactiveIndex = $index; + } + } + if (true === $allInactive) { + $validator->errors()->add(sprintf('actions.%d.active', $inactiveIndex), (string)trans('validation.at_least_one_active_action')); + } + } + /** * Adds an error to the validator when there are no repetitions in the array of data. * diff --git a/resources/lang/en_US/validation.php b/resources/lang/en_US/validation.php index c531d26ab3..5f5e96124c 100644 --- a/resources/lang/en_US/validation.php +++ b/resources/lang/en_US/validation.php @@ -61,7 +61,9 @@ return [ 'accepted' => 'The :attribute must be accepted.', 'bic' => 'This is not a valid BIC.', 'at_least_one_trigger' => 'Rule must have at least one trigger.', + 'at_least_one_active_trigger' => 'Rule must have at least one active trigger.', 'at_least_one_action' => 'Rule must have at least one action.', + 'at_least_one_active_action' => 'Rule must have at least one active action.', 'base64' => 'This is not valid base64 encoded data.', 'model_id_invalid' => 'The given ID seems invalid for this model.', 'less' => ':attribute must be less than 10,000,000',