AddValidatedOptions 1.0.7
dotnet add package AddValidatedOptions --version 1.0.7
NuGet\Install-Package AddValidatedOptions -Version 1.0.7
<PackageReference Include="AddValidatedOptions" Version="1.0.7" />
<PackageVersion Include="AddValidatedOptions" Version="1.0.7" />
<PackageReference Include="AddValidatedOptions" />
paket add AddValidatedOptions --version 1.0.7
#r "nuget: AddValidatedOptions, 1.0.7"
#:package AddValidatedOptions@1.0.7
#addin nuget:?package=AddValidatedOptions&version=1.0.7
#tool nuget:?package=AddValidatedOptions&version=1.0.7
AddValidatedOptions
AddValidatedOptions is a lightweight NuGet package that automatically validates strongly‑typed configuration options at startup using DataAnnotations, ensuring fail‑fast behavior for ASP.NET Core applications
Installation
Install the package from NuGet:
dotnet add package AddValidatedOptions
Usage
If configuration is invalid, clear error messages are logged and the app fails fast.
What kind of errors can occur?
Validation uses DataAnnotations ([Required], [Range], [EmailAddress], etc.) applied to your configuration class.
If the values in appsettings.json do not match these rules, the application will stop at startup and print clear error messages.
Example
public class MyAppSettings
{
[Required]
public string ApiKey { get; set; }
[Range(1, 10)]
public int RetryCount { get; set; }
[EmailAddress]
public string SupportEmail { get; set; }
}
---------------------------------------------
Program.cs:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddValidatedOptions<MyAppSettings>(builder.Configuration,"MyAppSettings");
//...
var app = builder.Build();
app.MapGet("/", ([FromServices] MyAppSettings settings) =>
{
return $"ApiKey: {settings.ApiKey}, RetryCount: {settings.RetryCount}";
});
app.Run();
---------------------------------------------
Valid appsettings.json:
{
"MyAppSettings": {
"ApiKey": "12345-ABCDE",
"RetryCount": 5,
"SupportEmail": "help@company.com"
}
}
---------------------------------------------
Invalid appsettings.json:
{
"MyAppSettings": {
"RetryCount": 0,
"SupportEmail": "not-an-email"
}
}
### Resulting errors
[Configuration Error] Section 'MyAppSettings': The ApiKey field is required.
[Configuration Error] Section 'MyAppSettings': The field RetryCount must be between 1 and 10.
[Configuration Error] Section 'MyAppSettings': The SupportEmail field is not a valid e-mail address.
Benefits
- Fail fast: detect missing or invalid configuration before the app runs.
- Clear logs: errors are printed in console with section name and message.
- Strong typing: leverage IOptions<T> with validation rules.
⚖️ License
This project is freely available under the MIT license.
You may use it without restrictions, as long as you retain the reference to the original license.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Microsoft.Extensions.Configuration (>= 10.0.8)
- Microsoft.Extensions.Configuration.Json (>= 10.0.8)
- Microsoft.Extensions.DependencyInjection (>= 10.0.8)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.8)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 10.0.8)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.