Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/aragas/aragas.extensions.options.fluentvalidation
Validates ASP NET Core IOptions at startup via FluentValidation
https://github.com/aragas/aragas.extensions.options.fluentvalidation
asp-net-core aspnet-core aspnetcore configuration fluentvalidation ioptions microsoft-extensions-options options
Last synced: 5 days ago
JSON representation
Validates ASP NET Core IOptions at startup via FluentValidation
- Host: GitHub
- URL: https://github.com/aragas/aragas.extensions.options.fluentvalidation
- Owner: Aragas
- License: mit
- Created: 2022-03-26T09:28:54.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2023-06-13T15:04:38.000Z (over 1 year ago)
- Last Synced: 2024-12-17T08:15:05.163Z (9 days ago)
- Topics: asp-net-core, aspnet-core, aspnetcore, configuration, fluentvalidation, ioptions, microsoft-extensions-options, options
- Language: C#
- Homepage:
- Size: 32.2 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Aragas.Extensions.Options.FluentValidation
[![Publish](https://github.com/Aragas/Aragas.Extensions.Options.FluentValidation/actions/workflows/publish.yml/badge.svg)](https://github.com/Aragas/Aragas.Extensions.Options.FluentValidation/actions/workflows/publish.yml)
[![Daily Code Format Check](https://github.com/Aragas/Aragas.Extensions.Options.FluentValidation/actions/workflows/dotnet-format-daily.yml/badge.svg)](https://github.com/Aragas/Aragas.Extensions.Options.FluentValidation/actions/workflows/dotnet-format-daily.yml)
[![NuGet](http://img.shields.io/nuget/v/Aragas.Extensions.Options.FluentValidation.svg?style=flat)](https://www.nuget.org/packages/Aragas.Extensions.Options.FluentValidation/)
Validates ASP NET Core IOptions at startup via FluentValidation. If the options are not valid,
will throw an exception detailing what's wrong with the options and stop the app's execution.## Installation
Aragas.Extensions.Options.FluentValidation can be installed using the Nuget Package Manager or the dotnet CLI.
```
# NuGet Package Manager
Install-Package Aragas.Extensions.Options.FluentValidation
```
```
# dotnet CLI
dotnet add package Aragas.Extensions.Options.FluentValidation
```## Usage
### Standard Usage
Simply change the code you most likely used to do like this:
```csharp
public sealed class SomeOptionsValidator : AbstractValidator
{
public SomeOptionsValidator()
{
RuleFor(x => x.Host).IsUri().IsUrlTcpEndpointAvailable().When(x => x.Enabled);
}
}public sealed record SomeOptions
{
public bool Enabled { get; init; } = default!;
public string Host { get; init; } = default!;
}// Old code
services.Configure(ctx.Configuration.GetSection("SomeOptions"));
// New code
services.AddValidatedOptions(ctx.Configuration.GetSection("SomeOptions"));
```### HttpClient Usage
You can inject and configure a HttpClient in the validator
```csharp
public sealed class SomeHttpOptionsValidator : AbstractValidator
{
public SomeOptionsValidator(HttpClient client)
{
RuleFor(x => x.Url).IsUri().IsUrlAvailable(client).When(x => x.Enabled);
}
}public sealed record SomeHttpOptions
{
public bool Enabled { get; init; } = default!;
public string Url { get; init; } = default!;
}// Old code
services.Configure(ctx.Configuration.GetSection("SomeHttpOptions"));
// New code
services.AddValidatedOptionsWithHttp(ctx.Configuration.GetSection("SomeHttpOptions"), httpBuilder =>
{
httpBuilder.ConfigureHttpClient(client =>
{
client.Timeout = TimeSpan.FromSeconds(30);
});
});
```### Dependency Injection Usage
You can inject a custom dependency and easily retrieve it in the validator
```csharp
public sealed class CustomService
{
public bool IsValid(string str) => str.Contains("Hello World!");
}public sealed class SomeDIOptionsValidator : AbstractValidator
{
public SomeDIOptionsValidator(CustomService cs)
{
RuleFor(x => x.Url).IsUri().When(x => cs.IsValid(x.ToVerify));
}
}public sealed record SomeDIOptions
{
public string ToVerify { get; init; } = default!;
public string Url { get; init; } = default!;
}
// Old code
services.Configure(ctx.Configuration.GetSection("SomeDIOptions"));
// New code
services.AddTransient();
services.AddValidatedOptions(ctx.Configuration.GetSection("SomeDIOptions"));
```