validation - go lang 错误:“必需”标签上的“”字段验证失败
问题描述
即使我传递了所需的查询字符串参数,我的请求也无法从其他服务中获得。这是验证码:
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
解决方案
推荐阅读
- javascript - 纸牌游戏,不能将牌分给两个玩家
- javascript - 如何让 Javascript Const 与 Grunt Uglify 一起工作?
- python - 我不明白为什么我需要在生成 n 次问题时输入“q”才能退出程序
- excel - 根据条件从名称列表中自动填充 excel 中的单元格范围
- android - onActivityResult 后恢复 ViewPager
- php - Ubuntu 上的 Wordpress - home.php 的“找不到文件” - 尝试重定向到根文件夹
- reactjs - 如何从一个组件更新钩子并让它更新另一个组件?
- kotlin - 如何从 Micronaut 应用程序的库中注入 HTTP 客户端?
- python - 尝试定义输入以匹配随机生成的数字
- python - 根据其他列中的值将列添加到数据框