{"id":1774,"date":"2020-10-11T00:28:35","date_gmt":"2020-10-10T16:28:35","guid":{"rendered":"https:\/\/www.develop-note.com\/blog\/?p=1774"},"modified":"2022-02-16T21:51:23","modified_gmt":"2022-02-16T13:51:23","slug":"iris-model-validation-2021ironman","status":"publish","type":"post","link":"https:\/\/www.develop-note.com\/blog\/2020\/10\/11\/iris-model-validation-2021ironman\/","title":{"rendered":"Iris \u9019\u500b\u5728 go \u8a9e\u8a00\u4e0a\u5730\u8868\u6700\u5feb\u7684\u7db2\u9801\u6846\u67b6-iris\u7684model\u9a57\u8b49"},"content":{"rendered":"<h1>model\u9a57\u8b49<\/h1>\n<p>\u4ecb\u7d39\u5b8cquery string\u4ee5\u53caform POST\u4e4b\u5f8c\u76f8\u4fe1\u5927\u5bb6\u5c0d\u65bc\u4f7f\u7528\u8005\u63d0\u4ea4\u8acb\u6c42\u7684\u8655\u7406\u6709\u4e00\u4e9b\u6982\u5ff5\uff0c\u6240\u4ee5\u4eca\u5929\u518d\u8ddf\u5927\u5bb6\u4ecb\u7d39\u4e00\u4e0b\u5982\u4f55\u5229\u7528iris\u4f86\u9a57\u8b49model\u3002<\/p>\n<p><!--more--><\/p>\n<h2>model\u9a57\u8b49<\/h2>\n<p>\u4ec0\u9ebc\u662fmodel\u7684\u9a57\u8b49\u5462\uff0c\u5c31\u662f\u6aa2\u67e5\u63d0\u4ea4\u7684\u8cc7\u6599\u662f\u4e0d\u662f\u7b26\u5408\u7db2\u7ad9\u898f\u7bc4\uff0c\u6240\u4ee5\u63a5\u4e0b\u4f86\u5148\u8ddf\u5927\u5bb6\u70ba\u4ec0\u9ebc\u8981\u9a57\u8b49\uff0c\u518d\u4f86\u5c31\u662f\u8ddf\u5927\u5bb6\u5206\u4eab\u5982\u4f55\u505a\uff0c\u9996\u5148\u5148\u8ddf\u5927\u5bb6\u8aaa\u660e\u70ba\u4ec0\u9ebc\u8981\u9a57\u8b49\u3002<\/p>\n<h3>model\u70ba\u4ec0\u9ebc\u8981\u9a57\u8b49<\/h3>\n<p>\u8a66\u60f3\u4e00\u4e0b\u7576\u63d0\u4ea4\u4e00\u500b\u8a3b\u518a\u500b\u4eba\u8cc7\u6599\u7684\u8acb\u6c42\u6642\u5982\u679c\u6f0f\u6389\u4f7f\u7528\u8005\u540d\u7a31\uff0c\u6216\u8005\u4f7f\u7528\u8005\u540d\u7a31\u7684\u683c\u5f0f\u4e0d\u7b26\u5408\u7db2\u7ad9\u898f\u7bc4\uff0c\u90a3\u5728\u8655\u7406\u7684\u904e\u7a0b\u662f\u4e0d\u662f\u5f88\u56f0\u64fe\u5462\uff0c\u6240\u4ee5\u5982\u679c\u6709\u500b\u65b9\u5f0f\u53ef\u4ee5\u5148\u7be9\u9078\u4e0d\u5408\u683c\u7684\u8acb\u6c42\uff0c\u4e26\u4e14\u53ef\u4ee5\u63d0\u793a\u4f7f\u7528\u8005\u54ea\u88e1\u9700\u8981\u8abf\u6574\uff0c\u9019\u6a23\u53ef\u4ee5\u63d0\u6607\u670d\u52d9\u7684\u6548\u80fd\u4e5f\u53ef\u4ee5\u517c\u9867\u4f7f\u7528\u8005\u9ad4\u9a57\uff0c\u6240\u4ee5\u63a5\u4e0b\u4f86\u6211\u5011\u4f7f\u7528<a href=\"https:\/\/docs.iris-go.com\/iris\/contents\/model-validation\" title=\"iris-go model-validation\" rel=\"nofollow noopener\" target=\"_blank\">iris-go model-validation<\/a>\u9019\u7bc4\u4f8b\u4f86\u8aaa\u660e\u5982\u4f55\u5728iris\u9a57\u8b49model\u3002<\/p>\n<h3>iris\u600e\u9ebc\u9a57\u8b49model<\/h3>\n<p>\u63a5\u4e0b\u4f86\u8ddf\u5927\u5bb6\u4ecb\u7d39\u8a72\u5982\u4f55\u9a57\u8b49model\uff0c\u9996\u5148\u8981\u8acb\u5927\u5bb6\u5b89\u88dd\u4e0b\u5217\u5957\u4ef6\uff0c\u56e0\u70ba\u6211\u5011\u8981\u4f7f\u7528\u9019\u5957\u4ef6\u4f86\u9a57\u8b49\uff0c\u6240\u4ee5\u8acb\u5927\u5bb6\u5148\u5b89\u88dd\u4e0b\u5217\u5957\u4ef6<\/p>\n<pre><code class=\"language-bash\">$ go get github.com\/go-playground\/validator\/v10@latest<\/code><\/pre>\n<p>\u5b89\u88dd\u597d\u76f8\u4f9d\u7684\u5957\u4ef6\u5f8c\u63a5\u4e0b\u4f86\u7e7c\u7e8c\u8aaa\u660e\uff0c\u9996\u5148\u5148\u5b9a\u7fa9\u4e0b\u5217model<\/p>\n<pre><code class=\"language-go\">\/\/ User contains user information.\ntype User struct {\n    FirstName      string     `json:&quot;fname&quot;`\n    LastName       string     `json:&quot;lname&quot;`\n    Age            uint8      `json:&quot;age&quot; validate:&quot;gte=0,lte=130&quot;`\n    Email          string     `json:&quot;email&quot; validate:&quot;required,email&quot;`\n    FavouriteColor string     `json:&quot;favColor&quot; validate:&quot;hexcolor|rgb|rgba&quot;`\n    Addresses      []*Address `json:&quot;addresses&quot; validate:&quot;required,dive,required&quot;`\n}\n\n\/\/ Address houses a users address information.\ntype Address struct {\n    Street string `json:&quot;street&quot; validate:&quot;required&quot;`\n    City   string `json:&quot;city&quot; validate:&quot;required&quot;`\n    Planet string `json:&quot;planet&quot; validate:&quot;required&quot;`\n    Phone  string `json:&quot;phone&quot; validate:&quot;required&quot;`\n}<\/code><\/pre>\n<p>\u4e0a\u9762\u7684model\u9664\u4e86\u5ba3\u544a\u5c6c\u6027\u4ee5\u5916\u9084\u5b9a\u7fa9\u5c0d\u61c9\u5230json\u5c6c\u6027\u7684\u540d\u7a31\uff0c\u4ee5\u53ca\u6700\u5f8c\u9762\u7684\u9a57\u8b49\u5167\u5bb9\u5305\u62ec\u662f\u5426\u5fc5\u586b<code>required<\/code>\u4ee5\u53ca\u5176\u4ed6\u7684\u683c\u5f0f\u6aa2\u67e5\u3002<br \/>\n\u63a5\u4e0b\u4f86\u8981\u600e\u9ebc\u4f7f\u7528\u5462\uff0c\u8acb\u5927\u5bb6\u770b\u770b\u4e0b\u5217\u4f8b\u5b50<\/p>\n<pre><code class=\"language-go\">\n    \/\/ Use a single instance of Validate, it caches struct info.\n    v := validator.New()\n\n    \/\/ Register a custom struct validation for &#039;User&#039;\n    \/\/ NOTE: only have to register a non-pointer type for &#039;User&#039;, validator\n    \/\/ internally dereferences during it&#039;s type checks.\n    v.RegisterStructValidation(UserStructLevelValidation, User{})\n\n    app := iris.New()\n    \/\/ Register the validator to the Iris Application.\n    app.Validator = v\n\nfunc UserStructLevelValidation(sl validator.StructLevel) {\n    user := sl.Current().Interface().(User)\n\n    if len(user.FirstName) == 0 &amp;&amp; len(user.LastName) == 0 {\n        sl.ReportError(user.FirstName, &quot;FirstName&quot;, &quot;fname&quot;, &quot;fnameorlname&quot;, &quot;&quot;)\n        sl.ReportError(user.LastName, &quot;LastName&quot;, &quot;lname&quot;, &quot;fnameorlname&quot;, &quot;&quot;)\n    }\n}<\/code><\/pre>\n<p>\u4e0a\u9762\u4f8b\u5b50\u4ecb\u7d39\u7d66\u5927\u5bb6\u5982\u4f55\u8a3b\u518a\u9a57\u8b49\u5668\uff0c\u4e26\u4e14\u7d81\u5b9a\u8a72\u9a57\u8b49\u5668\u5230iris\u7684app\u5167\uff0c\u63a5\u4e0b\u4f86\u5927\u5bb6\u770b\u770b\u5982\u4f55\u5c07\u4f7f\u7528\u8005\u7684\u8acb\u6c42\u653e\u5230\u5b9a\u7fa9\u597d\u7684model\u5167<\/p>\n<pre><code class=\"language-go\">\n    app.Post(&quot;\/user&quot;, func(ctx iris.Context) {\n        var user User\n\n        \/\/ Returns InvalidValidationError for bad validation input,\n        \/\/ nil or ValidationErrors ( []FieldError )\n        err := ctx.ReadJSON(&amp;user)\n        if err != nil {\n            \/\/ This check is only needed when your code could produce\n            \/\/ an invalid value for validation such as interface with nil\n            \/\/ value most including myself do not usually have code like this.\n            if _, ok := err.(*validator.InvalidValidationError); ok {\n                ctx.StatusCode(iris.StatusInternalServerError)\n                ctx.WriteString(err.Error())\n                return\n            }\n\n            ctx.StatusCode(iris.StatusBadRequest)\n            for _, err := range err.(validator.ValidationErrors) {\n                fmt.Println()\n                fmt.Println(err.Namespace())\n                fmt.Println(err.Field())\n                fmt.Println(err.StructNamespace())\n                fmt.Println(err.StructField())\n                fmt.Println(err.Tag())\n                fmt.Println(err.ActualTag())\n                fmt.Println(err.Kind())\n                fmt.Println(err.Type())\n                fmt.Println(err.Value())\n                fmt.Println(err.Param())\n                fmt.Println()\n            }\n\n            return\n        }\n<\/code><\/pre>\n<p>\u4e0a\u9762\u8aaa\u660e\u5728ReadJson\u9019\u65b9\u6cd5\u88ab\u547c\u53eb\u6642\u6703\u5c07\u8cc7\u6599\u50b3\u5230model\u4e26\u4e14\u4e14\u9a57\u8b49\u8a72\u5fc5\u8981\u8cc7\u6599\u751a\u81f3\u65bc\u8cc7\u6599\u5167\u5bb9\u662f\u5426\u7b26\u5408\u683c\u5f0f\uff0c\u4e26\u4e14\u5c07\u932f\u8aa4\u7684\u5730\u65b9\u653e\u5230<code>err<\/code>\u4e4b\u4e2d\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u4eca\u5929\u8ddf\u5927\u5bb6\u5206\u4eab\u4e00\u4e0b\u8a72\u5982\u4f55\u7528\u4e00\u500b\u6709\u6548\u7387\u7684\u4f5c\u6cd5\u4f86\u7be9\u9078\u6389\u6709\u554f\u984c\u7684\u8acb\u6c42\uff0c\u4e26\u4e14\u56de\u994b\u4f7f\u7528\u8005\u8acb\u6c42\u7684\u8cc7\u6599\u54ea\u88e1\u6709\u554f\u984c\uff0c\u5e0c\u671b\u5c0d\u5927\u5bb6\u7684iris\u7684\u7db2\u9801\u958b\u767c\u6709\u5e6b\u52a9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>model\u9a57\u8b49 \u4ecb\u7d39\u5b8cquery string\u4ee5\u53caform POST\u4e4b\u5f8c\u76f8\u4fe1\u5927\u5bb6\u5c0d\u65bc\u4f7f\u7528\u8005\u63d0\u4ea4\u8acb\u6c42\u7684\u8655\u7406\u6709\u4e00\u4e9b &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.develop-note.com\/blog\/2020\/10\/11\/iris-model-validation-2021ironman\/\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008Iris \u9019\u500b\u5728 go \u8a9e\u8a00\u4e0a\u5730\u8868\u6700\u5feb\u7684\u7db2\u9801\u6846\u67b6-iris\u7684model\u9a57\u8b49\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[158,81,84],"class_list":["post-1774","post","type-post","status-publish","format-standard","hentry","category-develop","tag-2020ironman","tag-go","tag-iris"],"_links":{"self":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1774","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/comments?post=1774"}],"version-history":[{"count":10,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1774\/revisions"}],"predecessor-version":[{"id":2953,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1774\/revisions\/2953"}],"wp:attachment":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/media?parent=1774"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/categories?post=1774"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/tags?post=1774"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}