Skip to content

Please reconsider allowing async model validation #31905

@JeremySkinner

Description

@JeremySkinner

Suggestion

Hi, it would be great if the validation APIs could allow async model validation. This was previously suggested and closed in #7573, but it'd be greatly appreciated if you could reconsider.

Background information & context

I'm the author of the FluentValidaiton library, and we provide integration with ASP.NET's model validation API by implementing a custom ObjectModelValidator and ModelValidatorProvider. This works well for the most part, and is used extensively by the .NET community, however one sticking point is handling async validation.

FluentValidation's validators allow either sync or async execution, but because ASP.NET's validation APIs are only synchronous, we are unable to offer the library's full feature-set to ASP.NET users, and warn them that if their validator contains async code then it'll end up be run synchronously. If they need this to be async, they have to stop using auto validation and instead manually invoke the validator inside an async controller action. Neither of these are ideal.

Additionally, from an API design perspective we have to maintain 2 code paths inside FluentValidation for both the sync/async APIs which leads to a lot of duplication. This is mainly done for the sake of ASP.NET - if ASP.NET's validation pipeline was async, we could deprecate (and eventually drop) our synchronous implementation, but for as long as ASP.NET mandates synchronous validators we have to continue maintaining both implementations.

I believe the following types/methods would need to become async in order to support this:

  • ObjectModelValidator.Validate overloads
  • ValidationVisitor.Validate and the various methods called by this that recurse through the object graph
  • IModelValidator.Validate and its implementors

(#7573 specifically asked for the data annotation attributes themselves to add async validation methods too, but personally I think this is less important. If the infrastructure were in place with the above types, then the community can provide async implementations of IModelValidator as necessary, whether that's using FluentValidation, or custom attributes or something else)

If you could reconsider adding support for this, it would be a very welcome addition for the users of our library. I'd be happy to help implement this, if that'd be valuable.

Please let me know if you need any more info.

Thanks for your consideration.

//cc @pranavkm

Metadata

Metadata

Assignees

No one assigned

    Labels

    Pillar: Dev Experiencearea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesarea-ui-renderingIncludes: MVC Views/Pages, Razor Views/PagesenhancementThis issue represents an ask for new feature or an enhancement to an existing onefeature-model-binding

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions