AddValidatedOptions 1.0.2
See the version list below for details.
dotnet add package AddValidatedOptions --version 1.0.2
NuGet\Install-Package AddValidatedOptions -Version 1.0.2
<PackageReference Include="AddValidatedOptions" Version="1.0.2" />
<PackageVersion Include="AddValidatedOptions" Version="1.0.2" />
<PackageReference Include="AddValidatedOptions" />
paket add AddValidatedOptions --version 1.0.2
#r "nuget: AddValidatedOptions, 1.0.2"
#:package AddValidatedOptions@1.0.2
#addin nuget:?package=AddValidatedOptions&version=1.0.2
#tool nuget:?package=AddValidatedOptions&version=1.0.2
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
builder.Services.AddValidatedOptions<MyAppSettings>(builder.Configuration,"MyAppSettings");
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
Configuration class
public class MyAppSettings
{
[Required]
public string ApiKey { get; set; }
[Range(1, 10)]
public int RetryCount { get; set; }
[EmailAddress]
public string SupportEmail { get; set; }
}
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.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.