diff --git a/src/Lumina.Application/Common/Behaviors/ValidationBehavior.cs b/src/Lumina.Application/Common/Behaviors/ValidationBehavior.cs index 2246a76d..b4b4490d 100644 --- a/src/Lumina.Application/Common/Behaviors/ValidationBehavior.cs +++ b/src/Lumina.Application/Common/Behaviors/ValidationBehavior.cs @@ -35,7 +35,7 @@ public class ValidationBehavior : IPipelineBehavior /// Incoming request. /// Awaitable delegate for the next action in the pipeline. Eventually this delegate represents the handler. - /// Cancellation token. + /// Cancellation token that can be used to stop the execution. public async ValueTask Handle(TRequest request, CancellationToken cancellationToken, MessageHandlerDelegate next) { // if there is no validator, just invoke the handler diff --git a/src/Lumina.Presentation.Web/Common/Models/Configuration/ServerConfigurationModel.cs b/src/Lumina.Presentation.Web/Common/Models/Configuration/ServerConfigurationModel.cs new file mode 100644 index 00000000..96e938b5 --- /dev/null +++ b/src/Lumina.Presentation.Web/Common/Models/Configuration/ServerConfigurationModel.cs @@ -0,0 +1,23 @@ +namespace Lumina.Presentation.Web.Common.Models.Configuration; + +/// +/// Model for deserializing server configuration settings. +/// +public class ServerConfigurationModel +{ + #region ================================================================== FIELD MEMBERS ================================================================================ + public const string SectionName = "ServerConfiguration"; + #endregion + + #region ==================================================================== PROPERTIES ================================================================================= + /// + /// Gets or sets the remote API server path. + /// + public required string BaseAddress { get; init; } + + /// + /// Gets or sets the remote API server port. + /// + public required ushort Port { get; init; } + #endregion +} \ No newline at end of file diff --git a/src/Lumina.Presentation.Web/Common/Utilities/OptionsBuilderFluentValidationUtilities.cs b/src/Lumina.Presentation.Web/Common/Utilities/OptionsBuilderFluentValidationUtilities.cs new file mode 100644 index 00000000..8178a69d --- /dev/null +++ b/src/Lumina.Presentation.Web/Common/Utilities/OptionsBuilderFluentValidationUtilities.cs @@ -0,0 +1,32 @@ +#region ========================================================================= USING ===================================================================================== +using FluentValidation; +using Lumina.Presentation.Web.Common.Validation; +using Microsoft.Extensions.Options; +#endregion + +namespace Lumina.Presentation.Web.Common.Utilities; + +/// +/// Extension methods for adding configuration related options services to the DI container via . +/// +public static class OptionsBuilderFluentValidationUtilities +{ + #region ================================================================== METHODS =================================================================================== + /// + /// Extension method to add fluent validation to the options validation pipeline. + /// + /// The type of options being validated. + /// The options builder to add the validation to. + /// + /// This method allows validating options of type using a FluentValidation validator registered in the DI container. + /// + /// The same instance, to allow for chaining. + public static OptionsBuilder ValidateFluently(this OptionsBuilder optionsBuilder) where TOptions : class + { + // register a singleton instance of IValidateOptions using FluentValidation + optionsBuilder.Services.AddSingleton>( + serviceProvider => new FluentValidationOptions(optionsBuilder.Name, serviceProvider.GetRequiredService>())); + return optionsBuilder; + } + #endregion +} \ No newline at end of file diff --git a/src/Lumina.Presentation.Web/Common/Validation/FluentValidationOptions.cs b/src/Lumina.Presentation.Web/Common/Validation/FluentValidationOptions.cs new file mode 100644 index 00000000..5aa1abb2 --- /dev/null +++ b/src/Lumina.Presentation.Web/Common/Validation/FluentValidationOptions.cs @@ -0,0 +1,68 @@ +#region ========================================================================= USING ===================================================================================== +using FluentValidation; +using Microsoft.Extensions.Options; +#endregion + +namespace Lumina.Presentation.Web.Common.Validation; + +/// +/// Provides options for fluent validation of a specific options type. +/// +/// The type of options being validated +public class FluentValidationOptions : IValidateOptions where TOptions : class +{ + #region ================================================================== FIELD MEMBERS ================================================================================ + private readonly IValidator _validator; + #endregion + + #region ==================================================================== PROPERTIES ================================================================================= + /// + /// Gets the name of the options being validated. + /// + public string? Name { get; } + #endregion + + #region ====================================================================== CTOR ===================================================================================== + /// + /// Initializes a new instance of the class. + /// + /// The name of the options being validated. This can be . + /// The validator instance used to validate the options. + public FluentValidationOptions(string? name, IValidator validator) + { + Name = name; + _validator = validator; + } + #endregion + + #region ===================================================================== METHODS =================================================================================== + /// + /// Validates the specified . + /// + /// The name of the options instance being validated. + /// The options instance to validate. + /// A that indicates whether the validation was successful or not. + public ValidateOptionsResult Validate(string? name, TOptions options) + { + // check if the name of the options is specified and matches the provided one + if (Name is not null && Name != name) + return ValidateOptionsResult.Skip; + + ArgumentNullException.ThrowIfNull(options, nameof(options)); // don't go further if the options are null + + var validationResult = _validator.Validate(options); // validate the options using the validator + + // return a response based on the validation result + if (validationResult.IsValid) + return ValidateOptionsResult.Success; + + // if the validation fails, collect the error messages + // TODO: add translation support for the error messages + var errors = validationResult.Errors.Select(x => $"Options validation failed for '{x.PropertyName}' with error: '{x.ErrorMessage}'") + .ToArray(); + + // and return them with a failure result + return ValidateOptionsResult.Fail(errors); + } + #endregion +} \ No newline at end of file diff --git a/src/Lumina.Presentation.Web/Common/Validators/ServerConfigurationModelValidator.cs b/src/Lumina.Presentation.Web/Common/Validators/ServerConfigurationModelValidator.cs new file mode 100644 index 00000000..ef462354 --- /dev/null +++ b/src/Lumina.Presentation.Web/Common/Validators/ServerConfigurationModelValidator.cs @@ -0,0 +1,23 @@ +#region ========================================================================= USING ===================================================================================== +using FluentValidation; +using Lumina.Presentation.Web.Common.Models.Configuration; +#endregion + +namespace Lumina.Presentation.Web.Common.Validators; + +/// +/// Validates the needed validation rules for the ServerConfiguration application configuration settings section. +/// +public class ServerConfigurationModelValidator : AbstractValidator +{ + #region ====================================================================== CTOR ===================================================================================== + /// + /// Initializes a new instance of the class. + /// + public ServerConfigurationModelValidator() + { + RuleFor(x => x.BaseAddress).NotEmpty().WithMessage("Base address cannot be empty!"); + RuleFor(x => x.Port).InclusiveBetween((ushort)0, (ushort)65535).WithMessage("Port number must be between 0 and 65535!"); + } + #endregion +} \ No newline at end of file diff --git a/src/Lumina.Presentation.Web/wwwroot/appsettings.json b/src/Lumina.Presentation.Web/wwwroot/appsettings.json new file mode 100644 index 00000000..fee5dc4f --- /dev/null +++ b/src/Lumina.Presentation.Web/wwwroot/appsettings.json @@ -0,0 +1,6 @@ +{ + "ServerConfiguration": { + "BaseAddress": "https://localhost", + "Port": 7057 + } +} \ No newline at end of file