From be94a51b8c35fc58abc4d48d84e4b95b258482bf Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Mon, 27 Jan 2025 18:44:47 +0100 Subject: [PATCH 1/2] 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 a46f191edc..016bdb4889 100644 --- a/src/modules/Elsa.Workflows.Management/Features/WorkflowManagementFeature.cs +++ b/src/modules/Elsa.Workflows.Management/Features/WorkflowManagementFeature.cs @@ -241,6 +241,7 @@ public override void Apply() .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 0000000000..4e3ed5258b --- /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 From ea8c3da169b26448b41a54fed64c6aa889c75d3f Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Mon, 27 Jan 2025 18:55:28 +0100 Subject: [PATCH 2/2] Refactor object initialization and remove unused import. Updated the object initialization syntax for cleaner code and removed an unused import in WorkflowValidator.cs to improve code readability and maintainability. --- .../Endpoints/WorkflowDefinitions/BulkPublish/Endpoint.cs | 2 +- .../Elsa.Workflows.Management/Services/WorkflowValidator.cs | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/modules/Elsa.Workflows.Api/Endpoints/WorkflowDefinitions/BulkPublish/Endpoint.cs b/src/modules/Elsa.Workflows.Api/Endpoints/WorkflowDefinitions/BulkPublish/Endpoint.cs index 1c36f7fb11..309ff4c9d2 100644 --- a/src/modules/Elsa.Workflows.Api/Endpoints/WorkflowDefinitions/BulkPublish/Endpoint.cs +++ b/src/modules/Elsa.Workflows.Api/Endpoints/WorkflowDefinitions/BulkPublish/Endpoint.cs @@ -70,6 +70,6 @@ public override async Task ExecuteAsync(Request request, CancellationT updatedConsumers.AddRange(result.AffectedWorkflows.WorkflowDefinitions.Select(x => x.DefinitionId)); } - return new Response(published, alreadyPublished, notFound, skipped, updatedConsumers); + return new(published, alreadyPublished, notFound, skipped, updatedConsumers); } } \ No newline at end of file diff --git a/src/modules/Elsa.Workflows.Management/Services/WorkflowValidator.cs b/src/modules/Elsa.Workflows.Management/Services/WorkflowValidator.cs index ed43c2bad0..666a608561 100644 --- a/src/modules/Elsa.Workflows.Management/Services/WorkflowValidator.cs +++ b/src/modules/Elsa.Workflows.Management/Services/WorkflowValidator.cs @@ -2,7 +2,6 @@ using Elsa.Workflows.Activities; using Elsa.Workflows.Management.Models; using Elsa.Workflows.Management.Notifications; -using Elsa.Workflows.Management.Requests; namespace Elsa.Workflows.Management.Services;