[API Proposal]: Add a fluent OptionsBuilder
method that adds validator types
#115200
Labels
api-suggestion
Early API idea and discussion, it is NOT ready for implementation
area-Extensions-Options
Milestone
Background and motivation
Since the introduction of compile-time generated options validators that are NativeAOT compatible, more developers will want to add this kind of validator instead of the reflection-based validators. Adding the reflection-based validator is easy with a fluent
OptionsBuilder
method, but adding a "user defined" (either implemented by a user or compile-time generated) requires manually adding the validator to the service collection.I propose a new overload to the
Validate
method onOptionsBuilder
that provides a fluent way of adding validator types for validating an option. Introducing this method would not only make users code more concise but make the way of adding validators more discoverable, since theOptionsBuilder
is already the place where most users add validation (often via theValidateDataAnnotations
method).API Proposal
I've made the method
virtual
because most other methods inOptionsBuilder
are. But I don't see the need for it.API Usage
It would be used in a similar way to how the
Validate(Func<TOptions, bool> validation)
orValidateDataAnnotations()
for example are used.Alternative Designs
Instead of being an overload to
Validate
this method could get its own name, likeValidateWithValidator
or something similar, which could help with discoverability. (Or theValidate
overload might be more discoverable. When looking for this method before realizing it doesn't exist, I looked for aValidate
overload, but that's just a sample size of one.)The same thing can already be achieved with this (slightly more verbose) code snippet, and the new method might not be necessary.
Risks
The only risk I can see is the increased API surface. I cannot imagine any security risks introducing this API since it is syntactical sugar over the already existing way of doing the same thing.
The text was updated successfully, but these errors were encountered: