https://github.com/furkandeveloper/fluentoptionvalidation
This repo, provides option pattern validation with Fluent Validation
https://github.com/furkandeveloper/fluentoptionvalidation
fluentvalidation option option-pattern validation
Last synced: 9 months ago
JSON representation
This repo, provides option pattern validation with Fluent Validation
- Host: GitHub
- URL: https://github.com/furkandeveloper/fluentoptionvalidation
- Owner: furkandeveloper
- License: mit
- Created: 2025-02-01T15:50:13.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-01T19:12:43.000Z (about 1 year ago)
- Last Synced: 2025-02-01T20:18:31.984Z (about 1 year ago)
- Topics: fluentvalidation, option, option-pattern, validation
- Language: C#
- Homepage:
- Size: 24.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Security: SECURITY.md
Awesome Lists containing this project
README
## Readme
## Give a Star 🌟
If you liked the project or if **FluentOptionValidation** helped you, please give a star.
### Purpose
**FluentValidationOption(OptionValidationSharp)** provides option validation with Fluent Validation.
### How To Use(?)
### Install
```bash
dotnet add package OptionValidationSharp
```
Example appsettings.json file
```json
"AppSettings": {
"ApplicationName": "MyApp",
"MaxUsers": 500,
"Url": "https://github.com"
}
```
Example Option Class
```csharp
public class AppSettings
{
public string? ApplicationName { get; set; }
public int MaxUsers { get; set; }
public string? Url { get; set; }
}
```
Example Option Validation Class
```csharp
public class AppSettingsValidator : AbstractValidator
{
public AppSettingsValidator()
{
RuleFor(r => r.ApplicationName).NotEmpty();
RuleFor(r => r.MaxUsers).InclusiveBetween(1,100);
RuleFor(r => r.Url).NotEmpty();
RuleFor(r => r.Url)
.Must(url => Uri.TryCreate(url, UriKind.Absolute, out _))
.When(r => !string.IsNullOrWhiteSpace(r.Url));
}
}
```
Use in Program.cs
```csharp
builder.Services.AddValidatorsFromAssembly(typeof(Program).Assembly);
builder.Services.AddOptions()
.Bind(builder.Configuration.GetSection("AppSettings"))
.ValidateOptionSharp() // <- 🔨 Validation Option with Fluent Validation
.ValidateOnStart();
```
Result:
```bash
fail: Microsoft.Extensions.Hosting.Internal.Host[11]
Hosting failed to start
Microsoft.Extensions.Options.OptionsValidationException: Options validation failed for type 'AppSettings'. 'Max Users' must be between 1 and 100. You entered 500.
```