我应该在 FluentValidation 中为 Collection 创建一个新类型吗?
Should i create a new Type for Collection in FluentValidation?
我正在尝试查找 FluentValidation 中是否有可用的方法允许在根级别为验证器验证集合。
例如如下所示,验证器可用于 CustomerValidator
用于 class 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;
问题是,如果我有一个 List<Customer>
并且我需要验证至少一个客户应该 Surname
我如何验证列表。 fluentvalidation中是否有开箱即用的功能,此时,我可以考虑以下两种方式之一。 你能建议什么是最好的方法吗?
1.循环迭代,然后为每个Customer调用validate方法。
List<ValidationResult> listOfValidationErrors = List<ValidationResult>();
// listCustomer is of type List<Customer>
foreach (var customer in listCustomer)
{
CustomerValidator validator = new CustomerValidator();
listOfValidationErrors.Add(validator.Validate(customer);
}
2. 为 Customer 集合 CustomerCollection
创建一个新集合 class 然后创建一个验证器 class CustomerCollectionValidator
public class CustomerCollection
{
public List<Customer> ListOfCustomers { get; set; }
public CustomerCollection(List<Customer> listOfCustomers )
{
this.ListOfCustomers = listOfCustomers ;
}
}
然后是验证器class
public class CustomerCollectionValidator: CompositeValidator<CustomerCollection>
{
public CustomerCollectionValidator()
{
RuleFor(x => x.ListOfCustomers)
.Must(ShouldHaveOneSurName)
.WithMessage("Should have one Surname in list");
RuleForEach(x => x.ListOfCustomers).SetValidator<CustomerValidator>();
}
public bool ShouldHaveOneSurName(List<Customer> lstCustomers)
{
if (lstCustomers== null)
{
return false;
}
return lstCustomers.Any(x => !String.IsNullOrWhiteSpace(x.SurName);
}
}
除了上述两种方法,Jeremy Skinner 还推荐了另一种方法here, which uses inheritance from AbstractValidator<List<Customer>>
. This didn't work with original code, but Jeremy committed the change in Fluent Validation source code in version 6.3 here。
以下代码介绍了对根级别集合进行验证的第三种方法。
public class CustomerCollectionValidator : AbstractValidator<List<Customer>> {
public CustomerCollectionValidator() {
RuleFor(list => list).SetCollectionValidator(new CustomerValidator());
}
}
我正在尝试查找 FluentValidation 中是否有可用的方法允许在根级别为验证器验证集合。
例如如下所示,验证器可用于 CustomerValidator
用于 class 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;
问题是,如果我有一个 List<Customer>
并且我需要验证至少一个客户应该 Surname
我如何验证列表。 fluentvalidation中是否有开箱即用的功能,此时,我可以考虑以下两种方式之一。 你能建议什么是最好的方法吗?
1.循环迭代,然后为每个Customer调用validate方法。
List<ValidationResult> listOfValidationErrors = List<ValidationResult>();
// listCustomer is of type List<Customer>
foreach (var customer in listCustomer)
{
CustomerValidator validator = new CustomerValidator();
listOfValidationErrors.Add(validator.Validate(customer);
}
2. 为 Customer 集合 CustomerCollection
创建一个新集合 class 然后创建一个验证器 class CustomerCollectionValidator
public class CustomerCollection
{
public List<Customer> ListOfCustomers { get; set; }
public CustomerCollection(List<Customer> listOfCustomers )
{
this.ListOfCustomers = listOfCustomers ;
}
}
然后是验证器class
public class CustomerCollectionValidator: CompositeValidator<CustomerCollection>
{
public CustomerCollectionValidator()
{
RuleFor(x => x.ListOfCustomers)
.Must(ShouldHaveOneSurName)
.WithMessage("Should have one Surname in list");
RuleForEach(x => x.ListOfCustomers).SetValidator<CustomerValidator>();
}
public bool ShouldHaveOneSurName(List<Customer> lstCustomers)
{
if (lstCustomers== null)
{
return false;
}
return lstCustomers.Any(x => !String.IsNullOrWhiteSpace(x.SurName);
}
}
除了上述两种方法,Jeremy Skinner 还推荐了另一种方法here, which uses inheritance from AbstractValidator<List<Customer>>
. This didn't work with original code, but Jeremy committed the change in Fluent Validation source code in version 6.3 here。
以下代码介绍了对根级别集合进行验证的第三种方法。
public class CustomerCollectionValidator : AbstractValidator<List<Customer>> {
public CustomerCollectionValidator() {
RuleFor(list => list).SetCollectionValidator(new CustomerValidator());
}
}