MP.FluentValidation.Options
is a NuGet package that provides an extension method to integrate FluentValidation
with Microsoft.Extensions.Options
. It allows you to automatically validate configuration settings using
FluentValidation validators.
To install the package, run the following command in your NuGet Package Manager Console
Install-Package MP.FluentValidation.Options
The AddValidatedOptions
extension method:
- Finds the
AbstractValidator
for the specified configuration class. - Validates the configuration values from
appsettings.json
or environment variables. - Registers the configuration class with the validated values in the
IOptions<T>
service.
Create a configuration class that represents the settings you want to validate.
public class MyConfiguration
{
public string Name { get; set; }
public int Age { get; set; }
}
Create a validator class that inherits from AbstractValidator<T>
, where T
is the configuration class.
public class MyConfigurationValidator : AbstractValidator<MyConfiguration>
{
public MyConfigurationValidator()
{
RuleFor(x => x.Name).NotEmpty();
RuleFor(x => x.Age).GreaterThan(0);
}
}
Use the AddValidatedOptions
extension method to register your configuration class and its validator.
services.AddValidatedOptions<MyConfiguration>();
OBS: The AddValidatedOptions
will by default use the MyConfiguration
class as the section name for the configuration
settings. If you want to use a different key, you can pass it as a parameter to the method like this:
services.AddValidatedOptions<MyConfiguration>(sectionName: "MySettings");
Inject IOptions<MyConfiguration>
into your classes to use the validated configuration settings.
public class MyService
{
private readonly MyConfiguration _configuration;
public MyService(IOptions<MyConfiguration> options)
{
_configuration = options.Value;
}
public void DoSomething()
{
// Use the configuration settings
Console.WriteLine($"Name: {_configuration.Name}");
Console.WriteLine($"Age: {_configuration.Age}");
}
}
This project is licensed under the MIT License. See the LICENSE file for more details.