小编Abh*_*dha的帖子

我应该在 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
查看次数

通过少于生成列表python运行列表

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]

知道如何得到我想要的东西吗?

python

1
推荐指数
2
解决办法
91
查看次数

标签 统计

.net ×1

asp.net ×1

c# ×1

fluentvalidation ×1

python ×1

validation ×1