From be94a51b8c35fc58abc4d48d84e4b95b258482bf Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Mon, 27 Jan 2025 18:44:47 +0100 Subject: [PATCH] Add validation for unique input/output names in workflows Introduces a `ValidateWorkflow` notification handler to enforce validation of unique input and output names in workflows. Adds validation errors when duplicate names are detected, improving the integrity of workflow definitions. Integrates the handler into the workflow management feature. --- .../Features/WorkflowManagementFeature.cs | 1 + .../Handlers/Notification/ValidateWorkflow.cs | 31 +++++++++++++++++++ ...est.cs => WorkflowDefinitionValidating.cs} | 0 3 files changed, 32 insertions(+) create mode 100644 src/modules/Elsa.Workflows.Management/Handlers/Notification/ValidateWorkflow.cs rename src/modules/Elsa.Workflows.Management/Notifications/{ValidateWorkflowRequest.cs => WorkflowDefinitionValidating.cs} (100%) diff --git a/src/modules/Elsa.Workflows.Management/Features/WorkflowManagementFeature.cs b/src/modules/Elsa.Workflows.Management/Features/WorkflowManagementFeature.cs index a46f191ed..016bdb488 100644 --- a/src/modules/Elsa.Workflows.Management/Features/WorkflowManagementFeature.cs +++ b/src/modules/Elsa.Workflows.Management/Features/WorkflowManagementFeature.cs @@ -241,6 +241,7 @@ public class WorkflowManagementFeature : FeatureBase .AddNotificationHandler() .AddNotificationHandler() .AddNotificationHandler() + .AddNotificationHandler() ; Services.Configure(options => diff --git a/src/modules/Elsa.Workflows.Management/Handlers/Notification/ValidateWorkflow.cs b/src/modules/Elsa.Workflows.Management/Handlers/Notification/ValidateWorkflow.cs new file mode 100644 index 000000000..4e3ed5258 --- /dev/null +++ b/src/modules/Elsa.Workflows.Management/Handlers/Notification/ValidateWorkflow.cs @@ -0,0 +1,31 @@ +using Elsa.Mediator.Contracts; +using Elsa.Workflows.Management.Models; +using Elsa.Workflows.Management.Notifications; +using Elsa.Workflows.Models; + +namespace Elsa.Workflows.Management.Handlers.Notification; + +public class ValidateWorkflow : INotificationHandler +{ + public Task HandleAsync(WorkflowDefinitionValidating notification, CancellationToken cancellationToken) + { + var workflow = notification.Workflow; + var inputs = workflow.Inputs; + var outputs = workflow.Outputs; + + ValidateUniqueNames(inputs, "inputs", notification.ValidationErrors); + ValidateUniqueNames(outputs, "outputs", notification.ValidationErrors); + + return Task.CompletedTask; + } + + private void ValidateUniqueNames(IEnumerable variables, string variableType, ICollection validationErrors) + { + var duplicateNames = variables.GroupBy(x => x.Name).Where(x => x.Count() > 1).Select(x => x.Key).ToList(); + if (duplicateNames.Any()) + { + var message = $"The following {variableType} are defined more than once: {string.Join(", ", duplicateNames)}"; + validationErrors.Add(new(message)); + } + } +} \ No newline at end of file diff --git a/src/modules/Elsa.Workflows.Management/Notifications/ValidateWorkflowRequest.cs b/src/modules/Elsa.Workflows.Management/Notifications/WorkflowDefinitionValidating.cs similarity index 100% rename from src/modules/Elsa.Workflows.Management/Notifications/ValidateWorkflowRequest.cs rename to src/modules/Elsa.Workflows.Management/Notifications/WorkflowDefinitionValidating.cs