Microsoft\'s ASP.NET Core documentation briefly mentions that you can implement IValidateOptions
to validate configuration settings from appsettings
Just build a lib for integrate FluentValidation with Microsoft.Extensions.Options.
https://github.com/iron9light/FluentValidation.Extensions
The nuget is here: https://www.nuget.org/packages/IL.FluentValidation.Extensions.Options/
Sample:
public class MyOptionsValidator : AbstractValidator {
// ...
}
using IL.FluentValidation.Extensions.Options;
// Registration
services.AddOptions("optionalOptionsName")
.Configure(o => { })
.Validate(); // ❗ Register validator type
// Consumption
var monitor = services.BuildServiceProvider()
.GetService>();
try
{
var options = monitor.Get("optionalOptionsName");
}
catch (OptionsValidationException ex)
{
}