|
| 1 | +// Copyright © 2024-Present The Serverless Workflow Specification Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"), |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +using FluentValidation; |
| 15 | +using ServerlessWorkflow.Sdk.Models; |
| 16 | +using ServerlessWorkflow.Sdk.Properties; |
| 17 | + |
| 18 | +namespace ServerlessWorkflow.Sdk.Validation; |
| 19 | + |
| 20 | +/// <summary> |
| 21 | +/// Represents the <see cref="IValidator"/> used to validate <see cref="ErrorCatcherDefinition"/>s |
| 22 | +/// </summary> |
| 23 | +public class ErrorCatcherDefinitionValidator |
| 24 | + : AbstractValidator<ErrorCatcherDefinition> |
| 25 | +{ |
| 26 | + |
| 27 | + /// <inheritdoc/> |
| 28 | + public ErrorCatcherDefinitionValidator(IServiceProvider serviceProvider, ComponentDefinitionCollection? components) |
| 29 | + { |
| 30 | + this.ServiceProvider = serviceProvider; |
| 31 | + this.Components = components; |
| 32 | + this.RuleFor(e => e) |
| 33 | + .Must(HaveValidHandlers) |
| 34 | + .WithMessage("The catch node must define either a 'do' section or a retry policy"); |
| 35 | + this.RuleForEach(e => e.Do) |
| 36 | + .SetValidator(e => new TaskMapEntryValidator(this.ServiceProvider, this.Components, e.Do?.ToDictionary(kvp => kvp.Key, kvp => kvp.Value))) |
| 37 | + .When(e => e.Do != null); |
| 38 | + this.RuleFor(e => e.RetryReference!) |
| 39 | + .Must(ReferenceAnExistingRetryPolicy) |
| 40 | + .When(e => !string.IsNullOrWhiteSpace(e.RetryReference)); |
| 41 | + } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Gets the current <see cref="IServiceProvider"/> |
| 45 | + /// </summary> |
| 46 | + protected IServiceProvider ServiceProvider { get; } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Gets the configured reusable components |
| 50 | + /// </summary> |
| 51 | + protected ComponentDefinitionCollection? Components { get; } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Determines whether the error catcher has valid handlers (either 'do' tasks or a retry policy) |
| 55 | + /// </summary> |
| 56 | + /// <param name="catcher">The error catcher to validate</param> |
| 57 | + /// <returns>A boolean indicating whether the error catcher has valid handlers</returns> |
| 58 | + protected virtual bool HaveValidHandlers(ErrorCatcherDefinition catcher) |
| 59 | + { |
| 60 | + return (catcher.Do != null && catcher.Do.Count > 0) || catcher.Retry != null || !string.IsNullOrWhiteSpace(catcher.RetryReference); |
| 61 | + } |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Determines whether or not the specified retry policy is defined |
| 65 | + /// </summary> |
| 66 | + /// <param name="name">The name of the retry policy to check</param> |
| 67 | + /// <returns>A boolean indicating whether or not the specified retry policy is defined</returns> |
| 68 | + protected virtual bool ReferenceAnExistingRetryPolicy(string name) => this.Components?.Retries?.ContainsKey(name) == true; |
| 69 | +} |
0 commit comments