An open API service indexing awesome lists of open source software.

https://github.com/nkz-soft/nkzsoft.fluentvalidation.options

Provides validation to strongly typed configuration objects using FluentValidation library
https://github.com/nkz-soft/nkzsoft.fluentvalidation.options

dotnet dotnet-core dotnet7 fluentvalidation nuget-package

Last synced: 8 months ago
JSON representation

Provides validation to strongly typed configuration objects using FluentValidation library

Awesome Lists containing this project

README

          

# NKZSoft.FluentValidation.Options

[![Nuget](https://img.shields.io/nuget/v/NKZSoft.FluentValidation.Options?style=plastic)](https://www.nuget.org/packages/NKZSoft.FluentValidation.Options/)

Provides validation to strongly typed configuration objects using FluentValidation library

I've been actually inspired by this article "[Adding validation to strongly typed configuration objects using FluentValidation](https://andrewlock.net/adding-validation-to-strongly-typed-configuration-objects-using-flentvalidation/)" to create this project.

## Using

First let's add a validator:
```csharp
public class TestOptionsValidator : AbstractValidator {
// ...
}
```
There are two ways to configure it via a configuration file:
```csharp
services.AddWithValidation(configuration.GetSection("TestOptions"));
```
or via Action:
```csharp
services.AddWithValidation(options =>
{
options.BoolValue = true;
});
```
And check that everything works:
```csharp
var options = serviceProvider.GetRequiredService>();

try
{
options.Invoking(x => x.Value).Invoke();
}
catch (OptionsValidationException ex)
{
}
```