c# - 检查属性在 FluentValidation 列表中是否唯一

标签 c# .net fluentvalidation

我正在努力确保列表具有唯一的 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; }
    }

最佳答案

我使用的是 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);
            }
        }
}

关于c# - 检查属性在 FluentValidation 列表中是否唯一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34983790/

相关文章:

c# - 具有默认值的 Asp.net webapi 枚举参数

.net - 搜索中的重要数据结构

c# - 具有流畅验证集合的自定义消息

c# - 在 F# 中使用 C# 流利库

c# - 取消 ListBox SelectedIndexChange 事件

C# - 图像作为可点击的按钮

c# - 有哪些不同的方法可以在不使用水平或垂直对齐设置为左侧和顶部的情况下拖动和移动对象

c# - 带有 SOS 的 Windgb 不显示结构字段值

c# - 如何编码可变大小的结构数组? C# 和 C++ 互操作帮助

.net - 如果对象为空,如何忽略 FluentValidation 规则