2017年12月25日 22:54
原创作品,转载时请务必以超链接形式标明文章原始出处,否则将追究法律责任。

最近在做Web API,用到了流式验证,就简单的说说这个流式验证。

首先我们定义一个Filter,如下

public class ValidateResponseFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
        {
            if (!actionContext.ModelState.IsValid)
            {
                //actionContext.ModelState.Keys
                actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, actionContext.ModelState);
            }
        }
    }

重写Action执行方法,如果请求model存在异常,则将500error返回给客户端。

接下来我们要怎么做,定义一个BaseController

[ValidateResponseFilter]
    public class BaseController : ApiController
    {
        protected HttpResponseMessage CreateSystemErrorResponse(string errorMsg)
        {
            return Request.CreateResponse<object>(
                   new
                   {
                       IsSuc = false,
                       ErrorMsg = errorMsg
                   });
        }
 
        protected HttpResponseMessage CreateErrorResponse(string responseCode, Type type = null, HttpStatusCode statusCode = HttpStatusCode.OK)
        {
            return Request.CreateResponse<object>(statusCode,
                   new
                   {
                       IsSuc = false,
                       ErrorMsg = MessageResHelper.GetMessage(type != null ? type.Name : this.GetType().Name, responseCode)
                   });
        }
 
        protected HttpResponseMessage CreateSucResponse(string responseCode = "")
        {
            if (string.IsNullOrEmpty(responseCode))
            {
                return Request.CreateResponse<object>(new
                   {
                       IsSuc = true
                   });
            }
 
            return Request.CreateResponse<object>(
                   new
                   {
                       IsSuc = true,
                       ErrorMsg = MessageResHelper.GetMessage(this.GetType().Name, responseCode)
                   });
        }
}

在BaseController上我们标记上面的Attribute,验证不通过进行请求拦截,然后所有的APIController都继承BaseController,这样所有继承自BaseController的Controller都会走验证。

接下来我们看一下Request的定义

public class CustomerValidateRequest : IValidatableObject
{
    private readonly IValidator _validator;
 
    public CustomerValidateRequest()
    {
        _validator = new CustomerValidateRequestValidator();
    }
 
    public string Email { get; set; }
 
    public string Password { get; set; }
 
    public string ValidateCode { get; set; }
 
    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        return _validator.Validate(this).ToValidationResult();
    }
}

Request定义好之后,我们在最下面写方法获取验证的结果。接下来再看看我们的Validator

public class CustomerValidateRequestValidator : AbstractValidator<CustomerValidateRequest>
{
    public CustomerValidateRequestValidator()
    {
        RuleFor(dto => dto.Email).NotNull().NotEmpty();
 
        RuleFor(dto => dto.Password).NotNull().NotEmpty();
 
        RuleFor(dto => dto.ValidateCode).NotNull().NotEmpty().Length(WebAppSettings.ValidateCodeLength);
 
        When(dto => !string.IsNullOrWhiteSpace(dto.Email), () =>
        {
            RuleFor(c => c.Email).Matches(@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
        });
    }
}

在这里就是我们所要验证的逻辑,可以验证最基本的非空,长度,还可以验证正则。这里RuleFor返回的是如下的接口类型

Public IRuleBuilderInitial<T, TProperty> RuleFor<TProperty>(Expression<Func<T, TProperty>> expression)

该接口继承IRuleBuilder接口

public interface IRuleBuilderInitial<T, out TProperty> : IFluentInterface, IRuleBuilder<T, TProperty>, IConfigurable<PropertyRule, IRuleBuilderInitial<T, TProperty>>

IRuleBuild有很多扩展方法在DefaultValidatorExtensions类中,如下

wKiom1dex4vgC2RPAADiaPwWg-Q515.png

简直是太多了,验证信用卡,邮箱,比较大小,区域,不等于等等,当然你自己也可以扩展一些出来。

我们用Google DHC看一下效果

wKiom1deyIXRH6aMAAA64QyvxHc413.png

如果什么都不传,就会根据上面的验证规则进行验证。

wKioL1deye_wOztiAAA8hgpHD3A410.png

如果传了Email,则会验证Email是否正确。

最后记得在Globle.asax.cs中增加如下代码,注意在Nuget中找到FluentValidation和FluentValidation.MVC

DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
ModelValidatorProviders.Providers.Add(new FluentValidationModelValidatorProvider(new AttributedValidatorFactory()));
ValidatorOptions.CascadeMode = CascadeMode.StopOnFirstFailure;
FluentValidationModelValidatorProvider.Configure();

想了解更多,看这里

GitHub: https://github.com/JeremySkinner/FluentValidation

Example: https://chsakell.com/2015/01/17/web-api-powerful-custom-model-validation-with-fluentvalidation/

发表评论
匿名  
用户评论
暂无评论