相关疑难解决方法(0)

我应该在 FluentValidation 中为集合创建一个新类型吗?

我试图找出在 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)

.net c# asp.net validation fluentvalidation

4
推荐指数
1
解决办法
585
查看次数

标签 统计

.net ×1

asp.net ×1

c# ×1

fluentvalidation ×1

validation ×1