FluentValidation rule for null object

后端 未结 9 1139
遥遥无期
遥遥无期 2020-12-25 10:44

I\'ve been trying to work out how to create a FluentValidation rule that checks if the instance of an object it\'s validating is not null, prior to validating it\'s properti

9条回答
  •  臣服心动
    2020-12-25 11:18

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

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

提交回复
热议问题