Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress CA1062 with fluent validation

I have a fluent, extensible validation helper like:

Assert.That(aParameter).IsNotNull();

It is extensible because the That method is actually generic (That<T>) and uses implicit typing to return a generic IAssertCondition<T> object. IsNotNull is actually an extension method.

Anyway, the problem using this approach to validate the parameters passed into a method is that I get CA1062 warnings instructing me to validate the arguments before using them which, of course, I am already doing.

I read Eric Smith's post (here) about using a ValidatedNotNullAttribute to inform FxCop that the argument is being validated but I don't see how I can accomplish this using the fluent interface I've described.

What are my options so that Code Analysis will recognize that the above statement satisfies the requirements and the warning will not appear?

like image 428
SonOfPirate Avatar asked Oct 10 '22 09:10

SonOfPirate


1 Answers

The only place you could add the attribute in this case is on the parameter of the That<T> method. Unfortunately, while that would prevent CA1062 from firing, it could lead to false negatives since you need to call more than just That<T> to actually implement a "not null" verification. If you want to use Code Analysis to properly check for parameter validation in a manner that recognizes your validation helper, you're pretty much going to have to write your own rule to replace CA1062.

like image 89
Nicole Calinoiu Avatar answered Oct 13 '22 10:10

Nicole Calinoiu