Automate the validation process on properties using FluentValidation Library

 ̄綄美尐妖づ 提交于 2019-12-13 07:37:10

问题


Wondering is there a possibility to avoid writing rules for every fields that needs to get validated, for an example all the properties must be validated except Remarks. And I was thinking to avoid writing RuleFor for every property.

public class CustomerDto
{
    public int CustomerId { get; set; }         //mandatory
    public string CustomerName { get; set; }    //mandatory
    public DateTime DateOfBirth { get; set; }   //mandatory
    public decimal Salary { get; set; }         //mandatory
    public string Remarks { get; set; }         //optional 
}

public class CustomerValidator : AbstractValidator<CustomerDto>
{
    public CustomerValidator()
    {
        RuleFor(x => x.CustomerId).NotEmpty();
        RuleFor(x => x.CustomerName).NotEmpty();
        RuleFor(x => x.DateOfBirth).NotEmpty();
        RuleFor(x => x.Salary).NotEmpty();
    }
}

回答1:


Here you go:

public class Validator<T> : AbstractValidator<T>
{
    public Validator(Func<PropertyInfo, bool> filter) {
        foreach (var propertyInfo in typeof(T)
            .GetProperties()
            .Where(filter)) {
            var expression = CreateExpression(propertyInfo);
            RuleFor(expression).NotEmpty();
        }
    }

    private Expression<Func<T, object>> CreateExpression(PropertyInfo propertyInfo) {
        var parameter = Expression.Parameter(typeof(T), "x");
        var property = Expression.Property(parameter, propertyInfo);
        var conversion = Expression.Convert(property, typeof(object));
        var lambda = Expression.Lambda<Func<T, object>>(conversion, parameter);

        return lambda;
    }
}

And it can be used as such:

    private static void ConfigAndTestFluent()
    {
        CustomerDto customer = new CustomerDto();
        Validator<CustomerDto> validator = new Validator<CustomerDto>(x=>x.Name!="Remarks"); //we specify here the matching filter for properties
        ValidationResult results = validator.Validate(customer);
    }


来源:https://stackoverflow.com/questions/36147409/automate-the-validation-process-on-properties-using-fluentvalidation-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!