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

时间:2019-03-17 00:48:39

标签: validation go

即使我传递了必需的查询字符串参数,我的请求也无法通过其他服务发出。 这是验证代码:

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

0 个答案:

没有答案