{"id":208145,"date":"2025-07-08T13:32:10","date_gmt":"2025-07-08T05:32:10","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208145\/"},"modified":"2025-07-08T13:32:10","modified_gmt":"2025-07-08T05:32:10","slug":"%e6%8e%a5%e5%8f%a3%e8%bd%ac%e6%8d%a2%ef%bc%9a%e9%94%99%e8%af%af%e6%98%aferrors-errorstring%ef%bc%8c%e8%80%8c%e4%b8%8d%e6%98%afvalidator-validationerrors","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208145\/","title":{"rendered":"\u63a5\u53e3\u8f6c\u6362\uff1a\u9519\u8bef\u662f*errors.errorString\uff0c\u800c\u4e0d\u662fvalidator.ValidationErrors"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u63a5\u53e3\u8f6c\u6362\uff1a\u9519\u8bef\u662f*errors.errorString\uff0c\u800c\u4e0d\u662fvalidator.ValidationErrors<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-29 15:45:31<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4eca\u5929\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u63a5\u53e3\u8f6c\u6362\uff1a\u9519\u8bef\u662f*errors.errorString\uff0c\u800c\u4e0d\u662fvalidator.ValidationErrors\u300b<\/span>\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u662f\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u6216\u8005\u5df2\u7ecf\u662f\u5927\u4f6c\u7ea7\u522b\u4e86\uff0c\u90fd\u975e\u5e38\u6b22\u8fce\u4e5f\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u7ed9\u6211\u5efa\u8bae\u8bc4\u8bba\u54c8~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<pre>type BookInput struct {\n  Title string `json:\"title\" binding:\"required\"`\n  Price json.Number `json:\"price\" binding:\"required,number\"`\n}\n\nfunc PostBookHandler(ctx *gin.Context) {\n  var bookInput book.BookInput\n\n  err := ctx.ShouldBindJSON(&amp;bookInput)\n\n  if err != nil {\n    errorMessages := []string{}\n\n    for _, e := range err.(validator.ValidationErrors) {\n        errorMessage := fmt.Sprintf(\"Error on filed %s, condition: %s\", e.Field(), e.ActualTag())\n        errorMessages = append(errorMessages, errorMessage)\n    }\n\n    ctx.JSON(http.StatusBadRequest, gin.H {\n        \"errors\": errorMessages, \n    })\n    return\n  }\n\n  ctx.JSON(http.StatusOK, gin.H {\n    \"title\": bookInput.Title,\n    \"price\": bookInput.Price,\n  })\n}<\/pre>\n<p>\u6211\u5c1d\u8bd5\u9a8c\u8bc1\u4ef7\u683c\u8f93\u5165\uff0c\u4f46\u5f97\u5230\u7684\u7ed3\u679c\u51fa\u4e4e\u610f\u6599\u3002\u6211\u5199\u7684\u4ee3\u7801\u5c31\u50cf\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u6709\u4eba\u53ef\u4ee5\u5e2e\u52a9\u6211\u5417\uff1f<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u8fd4\u56de\u7684\u9519\u8bef\u53ef\u80fd<em>\u4e0d\u662f<\/em>\u662f <code>validator.validationerrors<\/code>\uff0c\u5b83\u53ef\u80fd\u662f\u5176\u4ed6\u4e1c\u897f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6b63\u6587\u662f\u65e0\u6548\u7684 json\uff0c\u5219\u6839\u672c\u65e0\u6cd5\u8fbe\u5230\u9a8c\u8bc1\u6b65\u9aa4\u3002<\/p>\n<p>\u5728\u60a8\u7684\u4ee3\u7801\u4e2d\uff0c\u60a8\u6b63\u5728\u6267\u884c\u672a\u7ecf\u68c0\u67e5\u7684\u65ad\u8a00 <code>range err\u3002(validator.validationerrors)<\/code> \u8fd9\u53ef\u80fd\u4f1a\u51fa\u73b0\u6050\u614c\u3002<\/p>\n<p>\u8fd9\u662f\u6709\u6761\u4ef6\u5730\u5904\u7406\u9519\u8bef\u7684\u65b9\u6cd5\uff1a<\/p>\n<pre>err := ctx.ShouldBindJSON(&amp;bookInput)\nif err != nil {\n    var ve validator.ValidationErrors\n    if errors.As(err, &amp;ve) {\n       \/\/ handle validator error\n    }\n    \/\/ handle non-validator error\n    return\n}<\/pre>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u63a5\u53e3\u8f6c\u6362\uff1a\u9519\u8bef\u662f*errors.errorString\uff0c\u800c\u4e0d\u662fvalidator.ValidationErrors\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03Golang\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-208145","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208145","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=208145"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208145\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}