Skip to content

Add a fluent Validate overload for adding validator types#127264

Merged
cincuranet merged 12 commits intodotnet:mainfrom
89netraM:feature/options-builder-validator-type
Apr 27, 2026
Merged

Add a fluent Validate overload for adding validator types#127264
cincuranet merged 12 commits intodotnet:mainfrom
89netraM:feature/options-builder-validator-type

Conversation

@89netraM
Copy link
Copy Markdown
Contributor

Adds a Validate overload to the OptionsBuilder that registers an IValidateOptions type that validates the option type.

Closes #115200

Adds a Validate overload to the OptionsBuilder that registers an
IValidateOptions type that validates the option type.
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Apr 22, 2026
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-extensions-options
See info in area-owners.md if you want to be subscribed.

@cincuranet cincuranet self-assigned this Apr 22, 2026
Comment thread src/libraries/Microsoft.Extensions.Options/src/OptionsBuilder.cs
Adds a test case that verifies that adding an `IValidateOption`
validator to a named `OptionsBuilder` does not scope it to that name
only.
A decorator is used to skip the registered implementation of
`IValidateOptions` if the options name is not matching the registration.
@cincuranet cincuranet added this to the 11.0.0 milestone Apr 23, 2026
@rosebyte rosebyte self-requested a review April 23, 2026 11:34
Co-authored-by: Tarek Mahmoud Sayed <10833894+tarekgh@users.noreply.github.com>
Comment thread src/libraries/Microsoft.Extensions.Options/src/NamedValidateOptionsFilter.cs Outdated
Comment thread src/libraries/Microsoft.Extensions.Options/src/OptionsBuilder.cs
By observing an error instead of relying on "nothing" happening when
validating a valid option.
Copy link
Copy Markdown
Member

@tarekgh tarekgh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @89netraM!

I added minor comments, LGTM otherwise.

89netraM and others added 2 commits April 23, 2026 21:57
Co-authored-by: Tarek Mahmoud Sayed <10833894+tarekgh@users.noreply.github.com>
Co-authored-by: Tarek Mahmoud Sayed <10833894+tarekgh@users.noreply.github.com>
@89netraM
Copy link
Copy Markdown
Contributor Author

Thank you @tarekgh! Thank you for the review and comments!

Comment thread src/libraries/Microsoft.Extensions.Options/src/NamedValidateOptionsFilter.cs Outdated
@89netraM
Copy link
Copy Markdown
Contributor Author

Would it be a good idea to also update the examples in PACKAGE.md to use the new method instead of the adding IValidateOptions manually?

Changing

// Configuration to validate
builder.Services.Configure<MyConfigOptions>(builder.Configuration.GetSection(
                                        MyConfigOptions.MyConfig));

// OPtions validation through the DI container
builder.Services.AddSingleton<IValidateOptions
                              <MyConfigOptions>, MyConfigValidation>();

to

// Configuration to validate
builder.Services.AddOptions<MyConfigOptions>()
    .Bind(builder.Configuration.GetSection(MyConfigOptions.MyConfig))
    // Validate with custom IValidateOptions
    .Validate<MyConfigValidation>();

@cincuranet
Copy link
Copy Markdown
Contributor

@89netraM Yes, please.

Instead of registering the `IValidateOptions` types manually, the new
`Validate` overload is used in the examples in PACKAGE.md.
@cincuranet
Copy link
Copy Markdown
Contributor

/ba-g Failures are unrelated.

@cincuranet cincuranet merged commit 0f91c41 into dotnet:main Apr 27, 2026
91 of 93 checks passed
@cincuranet
Copy link
Copy Markdown
Contributor

@89netraM Thanks for your contribution.

@89netraM
Copy link
Copy Markdown
Contributor Author

Thank you all for having me! It's been fun!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-Extensions-Options community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[API Proposal]: Add a fluent OptionsBuilder method that adds validator types

4 participants