vipwan/Biwen.QuickApi

提供内建验证器同时对DataAnnotations和FluentValidation同时支持!

Closed this issue · 0 comments

比如你可以这样定义Req

    public class HelloApiRequest : BaseRequest<HelloApiRequest>
    {
        /// <summary>
        /// DataAnnotations内建特性测试
        /// </summary>
        [Description("DataAnnotations内建特性 测试")]
        [StringLength(12,MinimumLength =6)]
        [EmailAddress]
        [Required("Department是必须的")]
        public string Department { get; set; }

        public HelloApiRequest()
        {
            RuleFor(x => x.UserName).EmailAddress();//要求邮箱
        }
    }

//Req:

{
  "Department": null,
  "UserName": "vipwan",
  "Password": "p234565"
}

//Rsp会得到一下报错

{
  "type": "https://tools.ietf.org/html/rfc9110#section-15.5.1",
  "title": "One or more validation errors occurred.",
  "status": 400,
  "errors": {
    "UserName": [
      "'User Name' 不是有效的电子邮件地址。"
    ],
    "Department": [
      "Department是必须的"
    ]
  }
}