| | | 1 | | using System.Reflection; |
| | | 2 | | using Elsa.Mediator.Contracts; |
| | | 3 | | using Elsa.Workflows.Activities; |
| | | 4 | | using Elsa.Workflows.Management.Models; |
| | | 5 | | using Elsa.Workflows.Management.Notifications; |
| | | 6 | | using Elsa.Workflows.Runtime.Contracts; |
| | | 7 | | using Elsa.Workflows.Runtime.Entities; |
| | | 8 | | using JetBrains.Annotations; |
| | | 9 | | using Microsoft.Extensions.DependencyInjection; |
| | | 10 | | |
| | | 11 | | namespace Elsa.Workflows.Runtime.Handlers; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// A <see cref="WorkflowDefinitionValidating"/> handler that validates a workflow path and return any errors. |
| | | 15 | | /// </summary> |
| | | 16 | | [UsedImplicitly] |
| | 467 | 17 | | public class ValidateWorkflowRequestHandler( |
| | 467 | 18 | | ITriggerIndexer triggerIndexer, |
| | 467 | 19 | | IServiceProvider serviceProvider) : INotificationHandler<WorkflowDefinitionValidating> |
| | | 20 | | { |
| | | 21 | | public async Task HandleAsync(WorkflowDefinitionValidating notification, CancellationToken cancellationToken) |
| | | 22 | | { |
| | 54 | 23 | | Workflow workflow = notification.Workflow; |
| | 54 | 24 | | ICollection<WorkflowValidationError> validationErrors = notification.ValidationErrors; |
| | 136 | 25 | | foreach (StoredTrigger trigger in await triggerIndexer.GetTriggersAsync(workflow, cancellationToken)) |
| | | 26 | | { |
| | 14 | 27 | | if (trigger.Payload is null) |
| | | 28 | | { |
| | 0 | 29 | | validationErrors.Add(new($"Trigger should have a payload", |
| | 0 | 30 | | trigger.ActivityId)); |
| | | 31 | | } |
| | | 32 | | else |
| | | 33 | | { |
| | 14 | 34 | | MethodInfo method = typeof(ValidateWorkflowRequestHandler) |
| | 14 | 35 | | .GetMethod(nameof(ValidateInternalAsync), BindingFlags.Instance | BindingFlags.NonPublic)! |
| | 14 | 36 | | .MakeGenericMethod(trigger.Payload.GetType()); |
| | | 37 | | |
| | 14 | 38 | | object? result = method.Invoke(this, [trigger.Payload, workflow, trigger, validationErrors, cancellation |
| | 14 | 39 | | if (result is Task task) |
| | | 40 | | { |
| | 14 | 41 | | await task; |
| | | 42 | | } |
| | | 43 | | } |
| | | 44 | | } |
| | 54 | 45 | | } |
| | | 46 | | |
| | | 47 | | private async Task ValidateInternalAsync<TPayload>(TPayload instance, |
| | | 48 | | Workflow workflow, |
| | | 49 | | StoredTrigger trigger, |
| | | 50 | | ICollection<WorkflowValidationError> validationErrors, |
| | | 51 | | CancellationToken cancellationToken) |
| | | 52 | | { |
| | 14 | 53 | | ITriggerPayloadValidator<TPayload>? payloadValidator = serviceProvider.GetService<ITriggerPayloadValidator<TPayl |
| | | 54 | | |
| | 14 | 55 | | if (payloadValidator != null) |
| | 12 | 56 | | await payloadValidator.ValidateAsync(instance, workflow, trigger, validationErrors, cancellationToken); |
| | 14 | 57 | | } |
| | | 58 | | } |