Skip to content

Commit

Permalink
Merge pull request #6342 from elsa-workflows/vaidate-unique-input-output
Browse files Browse the repository at this point in the history
Validate unique input/output names before publishing workflow
  • Loading branch information
sfmskywalker authored Jan 28, 2025
2 parents a2efe5a + ea8c3da commit 0a43b99
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ public override async Task<Response> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ public override void Apply()
.AddNotificationHandler<DeleteWorkflowInstances>()
.AddNotificationHandler<RefreshActivityRegistry>()
.AddNotificationHandler<UpdateConsumingWorkflows>()
.AddNotificationHandler<ValidateWorkflow>()
;

Services.Configure<ManagementOptions>(options =>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<WorkflowDefinitionValidating>
{
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<ArgumentDefinition> variables, string variableType, ICollection<WorkflowValidationError> 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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 0a43b99

Please sign in to comment.