【问题标题】:Check a property is unique in list in FluentValidation检查属性在 FluentValidation 列表中是否唯一
【发布时间】:2016-01-25 01:29:10
【问题描述】:

我正在尝试确保列表具有唯一的 SSN。我收到“无法为表达式元素 => 元素自动确定属性名称。请通过调用 '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; }
    }

【问题讨论】:

  • 不应该是element=&gt;element.SSN吗?或者保留element =&gt; element 并按照它的建议使用.WithName("SSN")
  • 谢谢罗伯。没有这样的 element=>element.SSN。使用 .WithName("SSN") 也会引发相同的错误。看来我在做一些根本错误的事情。我已经编辑了上面的问题。

标签: c# .net fluentvalidation


【解决方案1】:

我使用的是 FluentValidation 4.6 版。根据 Jeremy Skinner(FluentValidation 的作者)的说法,我需要至少 5.6 才能使用模型级规则(如 RuleFor(element => element))。 作为一种解决方法,我在实际类本身上添加了此验证,而不是创建验证类。希望这可以帮助某人。

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);
            }
        }
}

【讨论】:

    猜你喜欢
    • 2018-03-20
    • 2019-01-12
    • 2013-09-07
    • 1970-01-01
    • 2015-07-31
    • 2021-02-11
    • 2014-03-06
    • 2011-07-13
    • 1970-01-01
    相关资源
    最近更新 更多