繁体   English   中英

进入lang错误:“必需”标签上的“”字段验证失败

[英]go lang Error:Field validation for '' failed on the 'required' tag

我的请求从其他服务失败,即使我正在传递必需的查询字符串参数。 这是验证代码:

type ActionByRuleRequest struct {
    Code  string `json:"code" form:"code" query:"code" validate:"required,oneof=gde cse scc"`
    Name string `json:"name" form:"name" query:"name" validate:"required"` // Monitoring rule name
}

这是通过验证来验证查询字符串

func GetActionByRule(c echo.Context) error {
    // Get parameters from the query string
    req := new(model.ActionByRuleRequest)
    err := c.Bind(req);
    if  err != nil {
        log.Println(err)
        error := model.Error{
            Message: err.Error(),
        }
        responseError := &model.ResponseError{
            Status: http.StatusBadRequest,
            Code:   "INVALID_INPUT",
        }
        responseError.Errors = append(responseError.Errors, error)
        return echo.NewHTTPError(http.StatusBadRequest, responseError)
    }
    // Validate input
    log.Printf("[/action_by_rule] 00000000000 get req = %#v\n", req)

    v := validator.New()

    err = v.Struct(req); 

    if err == nil {
        log.Println("Input valid")
    } else {
        log.Println("Input validation failed:", err)
     }
}

请求网址:“ http://127.0.0.1:3015/action?code=csf&name=tbiubi-csf-rule

错误:

 Key: 'ActionByRuleRequest.Code' Error:Field validation for 'Code' failed on the 'required' tag
Key: 'ActionByRuleRequest.Name' Error:Field validation for 'Name' failed on the 'required' tag

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM