type BookInput struct {
Title string `json:"title" binding:"required"`
Price json.Number `json:"price" binding:"required,number"`
}
func PostBookHandler(ctx *gin.Context) {
var bookInput book.BookInput
err := ctx.ShouldBindJSON(&bookInput)
if err != nil {
errorMessages := []string{}
for _, e := range err.(validator.ValidationErrors) {
errorMessage := fmt.Sprintf("Error on filed %s, condition: %s", e.Field(), e.ActualTag())
errorMessages = append(errorMessages, errorMessage)
}
ctx.JSON(http.StatusBadRequest, gin.H {
"errors": errorMessages,
})
return
}
ctx.JSON(http.StatusOK, gin.H {
"title": bookInput.Title,
"price": bookInput.Price,
})
}
我试图验证价格输入,但得到的结果出乎意料。我写的代码和上面的一样,有人可以帮我吗?
慕婉清6462132
守候你守候我
繁星点点滴滴
相关分类