Skip to content

chnzhang/NetDataAnnotations

Repository files navigation

NUGET地址 NetDataAnnotations
自定义注解,可分组,错误信息xml化

public class UserInput : BaseValidate
{
    public int UserId { get; set; }

    /// <summary>
    /// 使用message文件配置消息 key 支持分组规则校验,及一级子类传递
    /// </summary>
    [NetRequired(MessageKey = "uservalidate:usernamempty", Groups = new[] { typeof(BaseModelType.Insert), typeof(BaseModelType.SelectOne) })]
    public string UserName { get; set; }

    /// <summary>
    /// 使用消息 uservalidate:userphoneempty
    /// </summary>
    [NetRequired(Message = "电话号码不能为空", Groups = new[] { typeof(BaseModelType.Insert), typeof(BaseModelType.Update) })]
    [NetRegularExpression(@"^[1][0-9][0-9]{9}$", MessageKey = "uservalidate:userphone.regex", Groups = new[] { typeof(BaseModelType.Insert), typeof(BaseModelType.Update) })]
    public string UserPhone { get; set; }

    /// <summary>
    /// 链表实体验证
    /// </summary>
    [BaseModelType.Validate]
    public Order order { get; set; }

    /// <summary>
    /// List链表实体验证
    /// </summary>
    [BaseModelType.Validate]
    public List<Order> orderList { get; set; }
}

可设置xml的错误信息Message 可分组验证不同属性规则Groups

使用方式: Startup设置

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc(opt =>
    {
        opt.Filters.Add<GlobalActionFilterAttribute>();
    });
}

[HttpPost]
[Insert]//设置验证规则group
public bool PostUser(UserInput input)
{           
            return false;
}

返回结果:

{
    "code": 400,
    "data": "",
    "msg": "电话号码不能为空"
}

About

自定义注解,可分组,错误信息xml化

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages