我试图找出在 FluentValidation 中是否有可用的方法,它允许在根级别为验证器验证集合。
例如,如下所示,一个验证器可CustomerValidator用于一个类Customer。使用 FluentValidation;
public class CustomerValidator: AbstractValidator<Customer> {
public CustomerValidator() {
RuleFor(customer => customer.Surname).NotEmpty();
RuleFor(customer => customer.Forename).NotEmpty().WithMessage("Please specify a first name");
RuleFor(customer => customer.Discount).NotEqual(0).When(customer => customer.HasDiscount);
RuleFor(customer => customer.Address).Length(20, 250);
RuleFor(customer => customer.Postcode).Must(BeAValidPostcode).WithMessage("Please specify a valid postcode");
}
private bool BeAValidPostcode(string postcode) {
// custom postcode validating logic goes here
}
}
Customer customer = new Customer();
CustomerValidator validator = new CustomerValidator();
ValidationResult results = validator.Validate(customer);
bool validationSucceeded = results.IsValid;
IList<ValidationFailure> failures = results.Errors; …Run Code Online (Sandbox Code Playgroud) a = [0,1,2,3,4,5,6]
for x in a:
list(x < 4):
Run Code Online (Sandbox Code Playgroud)
预期产出: [0,1,2,3]
实际产出: [True, True, True, True, False, False, False]
知道如何得到我想要的东西吗?