FluentValidation rule for null object

EDIT 2022-07-19
As some commenters have pointed out, please check out answer https://stackoverflow.com/a/52784357/1943 for a newer implementation. I haven’t personally vetted, but it’s worth a try to give that a go first.

If you’re using an older version, or you enjoy nostalgia, my original answer below is from 2013.


You should be able to override the Validate method in your CustomerValidator class.

public class CustomerValidator: AbstractValidator<Customer> 
{
    // constructor...
    
    public override ValidationResult Validate(Customer instance)
    {
        return instance == null 
            ? new ValidationResult(new [] { new ValidationFailure("Customer", "Customer cannot be null") }) 
            : base.Validate(instance);
    }
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)