AddValidatedOptions 1.0.7

dotnet add package AddValidatedOptions --version 1.0.7
                    
NuGet\Install-Package AddValidatedOptions -Version 1.0.7
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AddValidatedOptions" Version="1.0.7" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AddValidatedOptions" Version="1.0.7" />
                    
Directory.Packages.props
<PackageReference Include="AddValidatedOptions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add AddValidatedOptions --version 1.0.7
                    
#r "nuget: AddValidatedOptions, 1.0.7"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package AddValidatedOptions@1.0.7
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=AddValidatedOptions&version=1.0.7
                    
Install as a Cake Addin
#tool nuget:?package=AddValidatedOptions&version=1.0.7
                    
Install as a Cake Tool

AddValidatedOptions

NuGet Version NuGet Downloads

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.7 95 5/21/2026
1.0.6 89 5/21/2026
1.0.5 89 5/20/2026
1.0.4 92 5/20/2026
1.0.2 82 5/20/2026
1.0.1 86 5/19/2026
1.0.0 88 5/19/2026