SonarSource/sonar-dotnet

Fix S6966 FP: FluentValidation ValidateAsync should not be proposed

Closed this issue · 0 comments

Originally reported at https://community.sonarsource.com/t/c-s6966-with-fluentvalidation/116030.

Description

Fluent validation supports asynchronous validation rules as described here.
The validator fails with an exception if asynchronous rules are present and Validate() is called. In most cases, validation is synchronous, and sometimes, the context requires synchronous processing:

Warning
If your validator contains asynchronous validators or asynchronous conditions, it’s important that you always call ValidateAsync on your validator and never Validate. If you call Validate, then an exception will be thrown.
You should not use asynchronous rules when using automatic validation with ASP.NET as ASP.NET’s validation pipeline is not asynchronous. If you use asynchronous rules with ASP.NET’s automatic validation, they will always be run synchronously (10.x and older) or throw an exception (11.x and newer).

Source

FluentValidation has built-in guards about properly using "Validate vs. ValidateAsync". ValidateAsync is even the wrong choice in some circumstances, so S6966 has a high false positive ratio for this API.