[OptionsValidator] source generator: emit ValidateAsync() for IAsyncValidateOptions<T>
Background
The approved API in https://github.com/dotnet/runtime/issues/128100 specifies that the [OptionsValidator] source generator should emit a ValidateAsync() method when the annotated validator type implements IAsyncValidateOptions<T>.
The runtime pieces (interfaces, validators, hosting integration) are being implemented in https://github.com/dotnet/runtime/pull/128788, but the source generator work is deferred to this tracking issue.
What needs to happen
When [OptionsValidator] is applied to a type that implements IAsyncValidateOptions<T>, the source generator should:
- Emit
ValidateAsync()alongside the existing syncValidate()method. - Use
Validator.TryValidateValueAsync()per member (instead of the syncTryValidateValue). - Use
Task.WhenAllfor types with 2+ validated members to run member validation in parallel. - Handle
IAsyncValidatableObject: if the options type implements it, callIAsyncValidatableObject.ValidateAsync()for self-validation. - Fall back to sync
IValidatableObject.Validate()if the options type only implements the sync interface.
Expected behavior matrix
| Validator implements | Options type implements | Generator emits |
|---|---|---|
IValidateOptions<T> only |
- | Validate() only (unchanged) |
IAsyncValidateOptions<T> |
- | ValidateAsync() |
IAsyncValidateOptions<T> |
IAsyncValidatableObject |
ValidateAsync() with async self-validation |
IAsyncValidateOptions<T> |
IValidatableObject only |
ValidateAsync() with sync self-validation fallback |
References
- Approved API: https://github.com/dotnet/runtime/issues/128100
- Runtime implementation PR: https://github.com/dotnet/runtime/pull/128788
- Comment requesting this issue: https://github.com/dotnet/runtime/pull/128788#issuecomment-4595543516
[!NOTE] This issue was created with Copilot assistance.
Tagging subscribers to this area: @dotnet/area-extensions-options See info in area-owners.md if you want to be subscribed.