我是 Fluent 验证的新手,需要一些帮助。我有一个接口,它有另一个接口一个属性,并且我已经为这两个接口编写了验证。挑战在于它没有显示接口属性的验证消息。
以下是我的情况的代码
public interface IAddress
{
string City { get; set; }
string Town { get; set; }
}
public interface IAccount
{
string FullName { get; set; }
int Age { get; set; }
IAddress Address { get; set; }
}
public AccountValidator ()
{
RuleFor(x => x.FullName).NotEmpty().WithMessage("Full Name can not be empty");
RuleFor(x => x.Age).GreaterThan(18).WithMessage("Age cannot be less than 18 years");
RuleFor(x => x.Address).SetValidator( new AddressValidator());
}
public AddressValidator()
{
RuleFor(x => x.City).NotEmpty().NotNull().WithMessage("City can not be empty");
RuleFor(x => x.Town).NotEmpty().NotNull().WithMessage("Town can not be empty");
}
我用来调用验证的客户端代码是:
var accountValidator = new AccountValidator();
accountValidator.ValidateAndThrow(_account );
提前致谢。