https://github.com/wilsonneto-dev/strongly_typed_configurations_validations_example_ioptions
Some examples on how to validate the strongly typed configuration (IOptions)
https://github.com/wilsonneto-dev/strongly_typed_configurations_validations_example_ioptions
dotnet ioptions validation
Last synced: 3 months ago
JSON representation
Some examples on how to validate the strongly typed configuration (IOptions)
- Host: GitHub
- URL: https://github.com/wilsonneto-dev/strongly_typed_configurations_validations_example_ioptions
- Owner: wilsonneto-dev
- Created: 2022-10-07T16:59:19.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2022-10-12T14:04:54.000Z (over 2 years ago)
- Last Synced: 2025-01-12T19:14:37.644Z (5 months ago)
- Topics: dotnet, ioptions, validation
- Language: C#
- Homepage:
- Size: 1.28 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Validating Strongly Typed Configurations (IOptions)
.Net 6 brings us some cool ways to validate the options, lazy or eager...
So, here are some examples on how to validate it```csharp
// we can validate with data annotations
// the below validation will happen when we try to access it, not at the startup
builder.Services.AddOptions()
.Bind(builder.Configuration.GetSection("Rabbit"))
.ValidateDataAnnotations();// we can use a method for the validation, this validation will happen when we try to access it
builder.Services.AddOptions()
.Bind(builder.Configuration.GetSection("Carrier"))
.Validate(
x => (!string.IsNullOrWhiteSpace(x.ApiUrl)) || (!string.IsNullOrWhiteSpace(x.Username)) || (!string.IsNullOrWhiteSpace(x.Pass)),
"There are problems with your Carrier configurations, pls check it...");// we also can validate using an OptionValidate class as below
// this validation will happen at the stratup time! (Avoiding surprises \o/)
builder.Services.AddOptions()
.Bind(builder.Configuration.GetSection("Database"))
.ValidateOnStart();builder.Services.AddSingleton, DatabaseOptionsValidation>();
```Program file:
https://github.com/wilsonneto-dev/Strongly_Typed_Configurations_Validations_Example_IOptions/blob/master/TypedConfigurationsValidation_IOptions_Example/Program.cs