Configuration.Utils
1.0.0
dotnet add package Configuration.Utils --version 1.0.0
NuGet\Install-Package Configuration.Utils -Version 1.0.0
<PackageReference Include="Configuration.Utils" Version="1.0.0" />
<PackageVersion Include="Configuration.Utils" Version="1.0.0" />
<PackageReference Include="Configuration.Utils" />
paket add Configuration.Utils --version 1.0.0
#r "nuget: Configuration.Utils, 1.0.0"
#:package Configuration.Utils@1.0.0
#addin nuget:?package=Configuration.Utils&version=1.0.0
#tool nuget:?package=Configuration.Utils&version=1.0.0
Configuration.Utils
This is a library/package focused on functionalities related to the validation of the application's configuration.
Table of Content
Features
- Object Validations: Allows efficient and fast validation of objects that use DataAnnotations in their class.
- Custom Message: Once an object is validated and an error is found, the message provided offers a clean and good message.
Installation
You can install the library/package using NuGet. Open the terminal and execute the following command:
dotnet add package Configuration.Utils
Usage / Implementation
This is an example focused on validating values to be replaced in the project's configuration
Assuming you have the following values in your appsettings.json
:
{
"CoreSettings": {
"Host": "https://example.com"
}
}
And also, you have the following Settings class:
using System.ComponentModel.DataAnnotations;
public class CoreSettings
{
[Required(ErrorMessage = "Core Host must be configured")]
public string Host { get; init; } = string.Empty;
[Required(ErrorMessage = "Core Port must be configured")]
public int Port { get; init; }
}
You can then proceed to use the extension in your Program.cs
file:
using Configuration.Utils;
builder.Services.Configure<CoreSettings>(builder.Configuration.GetSection("CoreSettings"));
builder.Configuration.ValidateConfiguration<CoreSettings>("CoreSettings"); // Defined Extension Method
The ValidateConfiguration
method is implemented in ConfigurationValidationExtension, and in this case it would throw an exception of type InvalidConfigurationException
with a message like the following:
Invalid Configuration for '{CoreSettings}':
- Core Port must be configured
Contribution
Contributions are welcome! Please fork the repository and submit a pull request.
Test
This project uses xUnit for Unit Tests.
To run the tests:
cd src\Configuration.Utils.Tests
dotnet test
Product | Versions 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. |
-
net8.0
- Microsoft.Extensions.Configuration (>= 9.0.7)
- Microsoft.Extensions.Configuration.Binder (>= 9.0.7)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.