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?
Asked
Active
Viewed 677 times
1 Answers
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
-
link to SO discussion? – Raul Nohea Goodness Apr 01 '15 at 00:55