检查 属性 在 FluentValidation 的列表中是否唯一
Check a property is unique in list in FluentValidation
我正在努力确保列表具有唯一的 SSN。我收到 "Property name could not be automatically determined for expression element => element. Please specify either a custom property name by calling 'WithName'" 错误。我们会知道我在这里做错了什么吗?
using FluentValidation;
using FluentValidation.Validators;
public class PersonsValidator : AbstractValidator<Persons>
{
public PersonsValidator()
{
this.RuleFor(element => element)
.SetValidator(new SSNNumbersInHouseHoldShouldBeUnique<Persons>())
.WithName("SSN");
.WithMessage("SSN's in household should be unique");
}
}
public class SSNNumbersInHouseHoldShouldBeUnique<T> : PropertyValidator
{
public SSNNumbersInHouseHoldShouldBeUnique()
: base("SSN's in household should be unique")
{
}
protected override bool IsValid(PropertyValidatorContext context)
{
var persons = context.Instance as Persons;
try
{
if (persons == null)
{
return false;
}
var persons = persons.Where(element => element.SSN.Trim().Length > 0);
var allSSNs = persons.Select(element => element.SSN.Trim());
if (allSSNs.Count() > allSSNs.Distinct().Count())
{
return false;
}
return true;
}
catch (Exception ex)
{
return false;
}
}
}
public class Persons : List<Person>
{}
public class Person
{
public string SSN{ get; set; }
}
我使用的是 FluentValidation 4.6 版。根据 Jeremy Skinner(FluentValidation 的作者)的说法,我需要至少达到 5.6 才能使用 model-level 规则(比如 RuleFor(element => element))。
作为解决方法,我在实际 class 本身上添加了此验证,而不是创建验证 class。希望这对某人有帮助。
using FluentValidation;
using FluentValidation.Results;
public class Persons : List<Person>
{
public void ValidateAndThrow()
{
var errors = new List<ValidationFailure>();
try
{
var persons = this.Where(element => element.SSN.Trim().Length > 0);
var allSSNs = persons.Select(element => element.SSN.Trim());
if (allSSNs.Count() > allSSNs.Distinct().Count())
{
var validationFailure = new ValidationFailure("UniqueSSNsInHouseHold", "SSN's in a household should be unique");
errors.Add(validationFailure);
}
}
catch (Exception ex)
{
}
if (errors.Any())
{
throw new ValidationException(errors);
}
}
}
我正在努力确保列表具有唯一的 SSN。我收到 "Property name could not be automatically determined for expression element => element. Please specify either a custom property name by calling 'WithName'" 错误。我们会知道我在这里做错了什么吗?
using FluentValidation;
using FluentValidation.Validators;
public class PersonsValidator : AbstractValidator<Persons>
{
public PersonsValidator()
{
this.RuleFor(element => element)
.SetValidator(new SSNNumbersInHouseHoldShouldBeUnique<Persons>())
.WithName("SSN");
.WithMessage("SSN's in household should be unique");
}
}
public class SSNNumbersInHouseHoldShouldBeUnique<T> : PropertyValidator
{
public SSNNumbersInHouseHoldShouldBeUnique()
: base("SSN's in household should be unique")
{
}
protected override bool IsValid(PropertyValidatorContext context)
{
var persons = context.Instance as Persons;
try
{
if (persons == null)
{
return false;
}
var persons = persons.Where(element => element.SSN.Trim().Length > 0);
var allSSNs = persons.Select(element => element.SSN.Trim());
if (allSSNs.Count() > allSSNs.Distinct().Count())
{
return false;
}
return true;
}
catch (Exception ex)
{
return false;
}
}
}
public class Persons : List<Person>
{}
public class Person
{
public string SSN{ get; set; }
}
我使用的是 FluentValidation 4.6 版。根据 Jeremy Skinner(FluentValidation 的作者)的说法,我需要至少达到 5.6 才能使用 model-level 规则(比如 RuleFor(element => element))。 作为解决方法,我在实际 class 本身上添加了此验证,而不是创建验证 class。希望这对某人有帮助。
using FluentValidation;
using FluentValidation.Results;
public class Persons : List<Person>
{
public void ValidateAndThrow()
{
var errors = new List<ValidationFailure>();
try
{
var persons = this.Where(element => element.SSN.Trim().Length > 0);
var allSSNs = persons.Select(element => element.SSN.Trim());
if (allSSNs.Count() > allSSNs.Distinct().Count())
{
var validationFailure = new ValidationFailure("UniqueSSNsInHouseHold", "SSN's in a household should be unique");
errors.Add(validationFailure);
}
}
catch (Exception ex)
{
}
if (errors.Any())
{
throw new ValidationException(errors);
}
}
}