Collect step args validation errors

[MAILPOET-4659]
This commit is contained in:
Jan Jakes
2022-10-03 15:52:22 +02:00
committed by Jan Jakeš
parent a41445cdaa
commit 9da204489d
2 changed files with 28 additions and 0 deletions

View File

@ -27,6 +27,7 @@ class Exceptions {
private const WORKFLOW_STRUCTURE_MODIFICATION_NOT_SUPPORTED = 'mailpoet_automation_workflow_structure_modification_not_supported';
private const WORKFLOW_STRUCTURE_NOT_VALID = 'mailpoet_automation_workflow_structure_not_valid';
private const WORKFLOW_STEP_MODIFIED_WHEN_UNKNOWN = 'mailpoet_automation_workflow_step_modified_when_unknown';
private const WORKFLOW_NOT_VALID = 'mailpoet_automation_workflow_not_valid';
public function __construct() {
throw new InvalidStateException(
@ -180,4 +181,12 @@ class Exceptions {
)
);
}
public static function workflowNotValid(string $detail, array $errors): UnexpectedValueException {
return UnexpectedValueException::create()
->withErrorCode(self::WORKFLOW_NOT_VALID)
// translators: %s is a detailed information
->withMessage(sprintf(__("Workflow validation failed: %s", 'mailpoet'), $detail))
->withErrors($errors);
}
}

View File

@ -3,6 +3,7 @@
namespace MailPoet\Automation\Engine\Validation\WorkflowRules;
use MailPoet\Automation\Engine\Data\Workflow;
use MailPoet\Automation\Engine\Exceptions;
use MailPoet\Automation\Engine\Registry;
use MailPoet\Automation\Engine\Validation\WorkflowGraph\WorkflowNode;
use MailPoet\Automation\Engine\Validation\WorkflowGraph\WorkflowNodeVisitor;
@ -15,6 +16,9 @@ class ValidStepArgsRule implements WorkflowNodeVisitor {
/** @var Validator */
private $validator;
/** @var array<string, array{step_id: string, message: string}> */
private $errors = [];
public function __construct(
Registry $registry,
Validator $validator
@ -24,6 +28,7 @@ class ValidStepArgsRule implements WorkflowNodeVisitor {
}
public function initialize(Workflow $workflow): void {
$this->errors = [];
}
public function visitNode(Workflow $workflow, WorkflowNode $node): void {
@ -35,10 +40,24 @@ class ValidStepArgsRule implements WorkflowNodeVisitor {
// validate args schema only for active workflows
if ($workflow->getStatus() !== Workflow::STATUS_ACTIVE) {
return;
}
try {
$this->validator->validate($registryStep->getArgsSchema(), $step->getArgs());
} catch (Throwable $e) {
$this->errors[$step->getId()] = [
'step_id' => $step->getId(),
'message' => $e instanceof ValidationException
? $e->getWpError()->get_error_message()
: __('Unknown error.', 'mailpoet'),
];
}
}
public function complete(Workflow $workflow): void {
if ($this->errors) {
throw Exceptions::workflowNotValid(__('Some steps have invalid arguments', 'mailpoet'), $this->errors);
}
}
}