runtime icon indicating copy to clipboard operation
runtime copied to clipboard

[OptionsValidator] source generator: emit ValidateAsync() for IAsyncValidateOptions<T>

Open ViveliDuCh opened this issue 6 months ago • 1 comments

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:

  1. Emit ValidateAsync() alongside the existing sync Validate() method.
  2. Use Validator.TryValidateValueAsync() per member (instead of the sync TryValidateValue).
  3. Use Task.WhenAll for types with 2+ validated members to run member validation in parallel.
  4. Handle IAsyncValidatableObject: if the options type implements it, call IAsyncValidatableObject.ValidateAsync() for self-validation.
  5. 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.

ViveliDuCh avatar Jun 02 '26 06:06 ViveliDuCh

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

dotnet-policy-service avatar Jun 02 '26 06:06 dotnet-policy-service