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