0

I want to check for a null object before validating with FluentValidation. If the object is null I want to force a failed validation result. How do I do that?

pashute
  • 3,965
  • 3
  • 38
  • 65

1 Answers1

0

OK, I found the answer (in a SO discussion about null objects sent to FV):

ValidationResult failedResult = new ValidationResult(
       new[] { 
          new ValidationFailure("", "Data is null."),  
       });

So in a class with members of validation and data, instantiated previously (in constructor or initiation functions) I could use:

public ValidationResult Validate()
{ 
    if ( this.data == null )
       return new ValidationResult(
          new[] { new ValidationFailure("", "Data is null."),  });
   else return this.validation.Validate(data);
}
pashute
  • 3,965
  • 3
  • 38
  • 65