FluentValidation:默认情况下自动将规则应用于所有属性

时间:2019-03-15 05:41:27

标签: c# reflection fluentvalidation

我有一个具有某些属性的Person类,因此我想默认情况下自动为所有属性应用FluentValidation Rule

  • 示例:
    • string: NotNull(), NotEmpty(), Length()...
    • enum: IsInEnum()
    • List: NotNull() or something else...
    • ...

人员和扩展方式

public enum Gender { Male, Female }

public class Person
{
    public Gender Gender { get; set; }
    public string Name { get; set; }
    public List<string> Notes { get; set; }
}

public static class Extension
{
    public static Expression<Func<T, TProperty>> GenerateExpression<T, TProperty>(PropertyInfo propInfo)
    {
        ParameterExpression paramExp = Expression.Parameter(typeof(T));
        MemberExpression memExp = Expression.Property(paramExp, propInfo);
        UnaryExpression unaryExp = Expression.Convert(memExp, propInfo.PropertyType);
        return Expression.Lambda<Func<T, TProperty>>(unaryExp, paramExp);
    }
}

基本验证器

我使用Extension.GenerateExpression基于列表属性构建Expression,然后将其传递到RuleFor(),但它仅适用于字符串类型。 我不知道如何处理其他数据类型。

public class BaseValidator<T> : AbstractValidator<T>
{
    public BaseValidator()
    {
        ParameterExpression paramExp = Expression.Parameter(typeof(T));

        foreach (PropertyInfo propInfo in typeof(T).GetProperties())
        {
            // String [WORKED]
            if (propInfo.PropertyType == typeof(string))
            {
                Expression<Func<T, string>> expression = Extension.GenerateExpression<T, string>(propInfo);
                RuleFor(expression).Length(1, 10); //.Matches("pattern");
            }

            // List [NOT WORK]
            else if (propInfo.PropertyType.IsGenericType)
            {
                Expression<Func<T, object>> expression = Extension.GenerateExpression<T, object>(propInfo);
                RuleFor(expression).NotNull(); //ItemsInRange(1, 2);
            }

            // Enum [EXCEPTION]
            else if (propInfo.PropertyType.IsEnum)
            {
                Expression<Func<T, Enum>> expression = Extension.GenerateExpression<T, Enum>(propInfo);
                // Expression of type 'Gender' cannot be used for return type 'System.Enum''
                RuleFor(expression).IsInEnum();
            }

            // Other type [How to handle?]
            else
            {
                //Expression<Func<T, ???>> expression = GenerateExpression<T, ???>(propInfo);
            }
        }
    }
}

人员验证器

public class PersonValidator : BaseValidator<Person> { }

程序

public class Program
{
    public static void Main(string[] args)
    {
        Person person = new Person
        {
            Name = "Name",
            Gender = Gender.Male,
            Notes = new List<string> { "Note 1", "Note 2" }
        };

        PersonValidator validation = new PersonValidator();
        ValidationResult result = validation.Validate(person);

        foreach (var error in result.Errors)
        {
            Console.WriteLine(error);
        }

        Console.ReadKey();
    }
}

1 个答案:

答案 0 :(得分:0)

您可以尝试使用此功能:

def convert_JSON(data_as_string):
    try:
        dict_representation = ast.literal_eval(data_as_string)
        return dict_representation
    except ValueError:
        return []

data["crew"] = data["crew"].map(lambda x: sorted([d['name'] if d['job'] == 'Casting' else '' for d in convert_JSON(x)])).map(lambda x: ','.join(map(str, x))

代替:

UnaryExpression unaryExp = Expression.Convert(memExp, typeof(TProperty));

它应该有助于泛型属性,但恐怕它对枚举没有帮助。 UnaryExpression unaryExp = Expression.Convert(memExp, propInfo.PropertyType); 方法必须知道枚举属性的真实类型,而不仅仅是IsInEnum