ConfigurationOptionsValidator 1.0.0

Additional Details

Version 1.0.0: The initial release of the ConfigurationOptionsValidator. Documentation was not included in this version.

There is a newer version of this package available.
See the version list below for details.
dotnet add package ConfigurationOptionsValidator --version 1.0.0
                    
NuGet\Install-Package ConfigurationOptionsValidator -Version 1.0.0
                    
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="ConfigurationOptionsValidator" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ConfigurationOptionsValidator" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="ConfigurationOptionsValidator" />
                    
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 ConfigurationOptionsValidator --version 1.0.0
                    
#r "nuget: ConfigurationOptionsValidator, 1.0.0"
                    
#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 ConfigurationOptionsValidator@1.0.0
                    
#: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=ConfigurationOptionsValidator&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=ConfigurationOptionsValidator&version=1.0.0
                    
Install as a Cake Tool

ConfigurationOptionsValidator

ConfigurationOptionsValidator is a powerful library for .NET that simplifies the process of validating configuration settings injected into your ASP.NET Core application using the Options pattern. This library ensures that your appsettings.json configurations are correctly set and that all required settings are present and valid.

ConfigurationOptionsValidator targets .NET 8 and ensures compatibility with the latest features and improvements in the .NET ecosystem.

With ConfigurationOptionsValidator, developers can validate their configurations easily, ensuring the application runs smoothly with the correct configuration values.

Note: The solution uses Serilog for logging configuration validation errors. Logs are stored in the specified logPath. If the logPath is not provided, a log file will be created by default in C:\TaskLogs.

ConfigurationOptionsValidator works seamlessly with the Options Pattern, which is a recommended approach for managing configuration in .NET. The Options pattern removes magic strings from your code, making it more readable and flexible. While it�s a great practice to use, even if you�re not using the Options pattern, this library can still help by validating your appsettings values and checking for any missing or incorrect properties.


Features

  • Validates configuration settings injected using the Options pattern.
  • Ensures all required configurations are present and correctly set in appsettings.json.
  • Provides a simple and effective way to validate configuration types during application startup.
  • Supports validation for both Blazor and API projects.
  • Includes security features to prevent exposing error details to external users.

Installation

Install the package via NuGet:

dotnet add package ConfigurationOptionsValidator --version 1.0.0

Setup

1. Register Services

In your Program.cs file, add the following to register the ConfigurationValidationManager service:

builder.Services.AddSingleton<ConfigurationValidationManager>();

Then, register the configuration settings by binding them to classes in your appsettings.json:

builder.Services.Configure<YourConfigOptions>(builder.Configuration.GetSection(nameof(YourConfigOptions)));

Usage Examples

2. Validate Configurations in Blazor

To validate your configurations in a Blazor application, retrieve the ConfigurationValidationManager from dependency injection and call the ValidateConfigurationTypes method:

var configValidator = app.Services.GetRequiredService<ConfigurationValidationManager>();

configValidator.ValidateConfigurationTypes(
    typeof(YourFirstConfigOptions),
    typeof(YourSecondConfigOptions),
    typeof(YourThirdConfigOptions)
);
);

var serilogOptions = app.Services.GetRequiredService<IOptions<SerilogOptions>>().Value;

app.MapBlazorConfigError( configValidator.MissingProperties,serilogOptions.WriteTo.FirstOrDefault()?.Args.Path);

3. Validate Configurations in API

In your API project, validate configurations by calling the ValidateApiConfiguration method:

if(!builder.ValidateApiConfiguration(logPath: logPath,
    typeof(YourFirstConfigOptions),
    typeof(YourSecondConfigOptions),
    typeof(YourThirdConfigOptions)
{
    Environment.Exit(1);
}

Error Handling and Security

For enhanced security, configure error handling to restrict access to error pages in production environments. You can add the following settings to your appsettings.json to prevent exposing errors to end users:

"ConfigurationErrorPageSettings": {
  "ForbiddenMessage": "Access to this page is restricted to authorized users only.",
  "SupportContact": {
    "Email": "support@company.com",
    "Phone": "+44 7890 123456",
    "Website": "https://support.company.com"
  }
}

This ensures that error messages are not displayed to the public, but are only available to the server hosting the application.


Why Use ConfigurationOptionsValidator?

With ConfigurationOptionsValidator, you no longer need to:

  • Manually check each configuration setting during application startup.
  • Expose sensitive configuration errors to end users.

ConfigurationOptionsValidator saves developers time and ensures a smoother, more reliable setup process for your ASP.NET Core applications.


Contributing

We welcome contributions! Please feel free to:


License

This project is licensed under the MIT License. See the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  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.4 193 4/10/2025
1.0.3 192 2/12/2025
1.0.2 193 2/11/2025 1.0.2 is deprecated.
1.0.1 179 2/11/2025 1.0.1 is deprecated.
1.0.0 176 2/11/2025 1.0.0 is deprecated because it has critical bugs.