Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise exceptions when async validators are invoked synchronously #1705

Closed
JeremySkinner opened this issue Apr 17, 2021 · 1 comment
Closed
Milestone

Comments

@JeremySkinner
Copy link
Member

JeremySkinner commented Apr 17, 2021

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.
@JeremySkinner
Copy link
Member Author

Step 1 will be part of 11.0. Nothing further will be done on steps 2 and 3 for now.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant