Skip to content

Raise exceptions when async validators are invoked synchronously #1705

@JeremySkinner

Description

@JeremySkinner

Is your feature request related to a problem? Please describe.

If a user invokes an async validator synchronously (either by calling Validate, or by using auto-validation in ASP.NET), then the validator will silently end up being run synchronously. This is not easily detectable and leads to bugs and potential deadlocks.

Describe the solution you'd like

Ideally, this should be caught at compile-time by having 2 base classes (a sync and an async version), but multiple attempts to build this have failed because of the complexity as well as the number of breaking changes that this would introduce. Instead, I'm proposing the following:

Step 1

Validators would be modified so that they throw an exception if an async rule is executed synchronously. While not as good as a compile-time error, this will at least surface the problem. This is a major change of behaviour, so would only be done in a major version release (11.0).

Step 2

Investigate whether it's possible to enable async validation in ASP.NET some other way.

  • I have opened an issue on the aspnet repo to ask MS to reconsider enabling async validation.
  • Also investigate switching to a filter-based implementation to perform validation, rather than using the ModelValidatorProvider api.

Step 3

  • If step 2 is possible and we can reliably implement async validation in aspnet projects, then synchronous validation should be deprecated, and eventually removed, with the intention that all validators only offer ValidateAsync as the entrypoint. For users who are unable to call FV from inside an async method, they can manually call GetAwaiter().GetResult() themselves, rather than the library doing this for them.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions