{"id":1354,"date":"2022-03-24T10:00:00","date_gmt":"2022-03-24T01:00:00","guid":{"rendered":"https:\/\/www.miracleave.co.jp\/contents\/?p=1354"},"modified":"2023-05-23T10:06:49","modified_gmt":"2023-05-23T01:06:49","slug":"gin-make-validator","status":"publish","type":"post","link":"https:\/\/www.miracleave.co.jp\/contents\/1354\/gin-make-validator\/","title":{"rendered":"Gin\u3067\u30ab\u30b9\u30bf\u30e0\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u3092\u5b9f\u88c5\u3057\u3066\u307f\u305f\u3002"},"content":{"rendered":"\n<p>\u3053\u3093\u306b\u3061\u308f\uff01\u76f8\u3082\u5909\u308f\u3089\u305aGolang\u304c\u5927\u597d\u304d\u306a\u79c1\u3001\u4f50\u3005\u6728\u3067\u3059\uff01<\/p>\n\n\n\n<p>\u4eca\u65e5\u306fGolang\u306e\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3001 Gin\u3092\u4f7f\u3063\u3066\u3044\u308b\u4e2d\u3067\u72ec\u81ea\u306e\u30d0\u30ea\u30c7\u30fc\u30bf\u3092\u4f5c\u308b\u306e\u304c\u3068\u3066\u3082\u7c21\u5358\u3060\u3063\u305f\u306e\u3067\u3054\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Gin<\/h2>\n\n\n\n<p>Gin\u306fGolang\u306eWeb\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3067\u3001\u540c\u3058\u304fGolang\u306eWeb\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u300cMartini\u300d\u306b\u30a4\u30f3\u30b9\u30d1\u30a4\u30a2\u3055\u308c\u305f\u975e\u5e38\u306b\u9ad8\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u304b\u3064\u3001\u4eba\u6c17\u306e\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3067\u3059\u3002<br>\u307e\u305f\u3001GithubStar\u3082\u6570\u3042\u308bGolang\u306eWeb\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u306e\u4e2d\u3067\u6700\u591a\u3068\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u5c11\u3057\u524d\u306e\u30ea\u30b9\u30c8\u3067\u3059\u304c\u3001\u9806\u4f4d\u306e\u5909\u52d5\u306f\u306a\u3055\u305d\u3046\u3067\u3059\u3002<\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/mingrammer\/go-web-framework-stars\">https:\/\/github.com\/mingrammer\/go-web-framework-stars<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u30c7\u30d5\u30a9\u30eb\u30c8\u30d0\u30ea\u30c7\u30fc\u30bf\u30fc<\/h2>\n\n\n\n<p>Gin\u3067\u306f\u3001\u30c7\u30d5\u30a9\u30eb\u30c8\u306e\u30d0\u30ea\u30c7\u30fc\u30bf\u30fc\u3068\u3057\u3066<a href=\"https:\/\/github.com\/go-playground\/validator\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/github.com\/go-playground\/validator<\/a>\u3092\u4f7f\u7528\u3057\u3066\u3044\u307e\u3059\u3002\u4e00\u4f8b\u3068\u3057\u3066\u3067\u3059\u304c\u3001\u30d0\u30ea\u30c7\u30fc\u30bf\u30fc\u3092\u9069\u7528\u3055\u305b\u308b\u969b\u306b\u3001\u5165\u529b\u3055\u308c\u308b\u6700\u5c0f\u5024\u3092\uff11\u3068\u3059\u308b\u306a\u3089\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u66f8\u304d\u307e\u3059\u306d\u3002\uff08binding:&#8221;required,min=1&#8243;\uff09<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-csharp\" data-lang=\"C#\"><code>type transferRequest struct {\n\tFromAccountID int64  `json:&quot;from_account_id&quot; binding:&quot;required,min=1&quot;`\n\t......\n}<\/code><\/pre><\/div>\n\n\n\n<p>\u72ec\u81ea\u9805\u76ee\u306a\u3069\u3067\u30d0\u30ea\u30c7\u30fc\u30bf\u30fc\u3092\u66f8\u304f\u306e\u3067\u3042\u308c\u3070\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u66f8\u304d\u65b9\u3082\u53ef\u80fd\u3067\u3059\u3002<br>\u3053\u3061\u3089\u306f\u8ca8\u5e63\u306e\u7565\u79f0\u3092\u30d0\u30ea\u30c7\u30fc\u30bf\u30fc\u306b\u304b\u3051\u305f\u3082\u306e\u3067\u3059\u3002(binding:&#8221;required,oneof=USD EUR JPY&#8221;)<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-csharp\" data-lang=\"C#\"><code>type createAccountRequest struct {\n        ......\n\tCurrency string `json:&quot;currency&quot; binding:&quot;required,oneof=USD EUR JPY&quot;`\n}<\/code><\/pre><\/div>\n\n\n\n<p>\u3053\u308c\u3067\u3082\u3084\u308a\u305f\u3044\u3053\u3068\u306f\u51fa\u6765\u307e\u3059\u304c\u3001\u5bfe\u8c61\u9805\u76ee\u304c10\u300120\u3068\u591a\u304f\u306a\u308c\u3070\u3001\u3053\u306e\u3088\u3046\u306a\u66f8\u304d\u65b9\u306f\u3042\u3093\u307e\u308a\u3057\u305f\u304f\u306a\u3044\u3067\u3059\u306d\u3002\u3002\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u30ab\u30b9\u30bf\u30e0\u30d0\u30ea\u30c7\u30fc\u30bf\u30fc<\/h2>\n\n\n\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u30ab\u30b9\u30bf\u30e0\u30d0\u30ea\u30c7\u30fc\u30bf\u30fc\u3092\u8abf\u3079\u305f\u3089\u3053\u3061\u3089\u306b\u4f5c\u6210\u65b9\u6cd5\u304c\u3042\u308a\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/gin-gonic\/examples\/blob\/master\/custom-validation\/server.go\">https:\/\/github.com\/gin-gonic\/examples\/blob\/master\/custom-validation\/server.go<\/a><\/p>\n\n\n\n<p>\u3053\u306e\u5185\u5bb9\u3092\u53c2\u8003\u306b\u4f5c\u6210\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n\n\n\n<p>API\u90e8\u5206\u3092\u3061\u3087\u3063\u3068\u96d1\u3067\u3057\u305f\u304c\u66f8\u3044\u3066\u307f\u307e\u3057\u305f\u3002\u3053\u3061\u3089\u306e\uff17\u884c\u76ee\u306b\u8a18\u8f09\u3057\u3066\u3044\u308b\u3001<br>\u300cv.RegisterValidation(&#8220;currency&#8221;, validCurrency)\u300d\u306e\u7b2c\u4e00\u5f15\u6570\u306b\u306f\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u3068\u3057\u3066\u4f7f\u7528\u3059\u308b\u30bf\u30b0\u540d\u3001\u7b2c\u4e8c\u5f15\u6570\u306f\u95a2\u6570\u3092\u8a18\u8f09\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p>\uff08&#8221;github.com\/go-playground\/validator\/v10&#8243;\u306eimport\u6587\u3067\u306f\u672b\u5c3e\u306e<strong>&#8220;v10&#8221;<\/strong>\u3092\u5fd8\u308c\u305a\u306b\u8a18\u8f09\u3057\u307e\u3059\u3002\uff09<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-csharp\" data-lang=\"C#\"><code>import (\n\t......\n\t&quot;github.com\/gin-gonic\/gin&quot;\n\t&quot;github.com\/gin-gonic\/gin\/binding&quot;\n\t&quot;github.com\/go-playground\/validator\/v10&quot;\n)\n\ntype Server struct {\nfunc NewServer(store db.Store) *Server {\n\tserver := &amp;Server{store: store}\n\trouter := gin.Default()\n\n\tif v, ok := binding.Validator.Engine().(*validator.Validate); ok {\n\t\tv.RegisterValidation(&quot;currency&quot;, validCurrency)\n\t}\n\n\trouter.GET(&quot;\/transfers\/:id&quot;, server.getTransfer)\n\trouter.POST(&quot;\/transfers&quot;, server.createTransfer)\n\n\tserver.router = router\n\treturn server\n}<\/code><\/pre><\/div>\n\n\n\n<p>\u7b2c\u4e8c\u5f15\u6570\u306e\u95a2\u6570\u90e8\u5206\u3078true or false\u3067\u8fd4\u305b\u308b\u3088\u3046\u306b\u5b9f\u88c5\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-csharp\" data-lang=\"C#\"><code>var listCurrencies = map[string]bool{\n\t&quot;USD&quot;: true,\n\t&quot;EUR&quot;: true,\n\t&quot;JPY&quot;: true,\n}\n\nvar validCurrency validator.Func = func(fieldLevel validator.FieldLevel) bool {\n\tif currency, ok := fieldLevel.Field().Interface().(string); ok {\n\t\treturn listCurrencies[currency]\n\t}\n\treturn false\n}<\/code><\/pre><\/div>\n\n\n\n<p>\u30ed\u30b8\u30c3\u30af\u306b\u3064\u3044\u3066\u306f\u5b8c\u4e86\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u6700\u5f8c\u306b\u30d0\u30ea\u30c7\u30fc\u30bf\u30fc\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8a18\u8f09\u3057\u307e\u3059\u3002\uff08binding:&#8221;required,currency&#8221;\uff09<br>\u524d\u8ff0\u306eAPI\u90e8\u5206\u3067\u89e6\u308c\u305f\u3001\u30d0\u30ea\u30c7\u30fc\u30bf\u30fc\u3068\u3057\u3066\u4f7f\u7528\u3059\u308b\u30bf\u30b0\u540d\u306f\u3053\u3053\u3067\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-csharp\" data-lang=\"C#\"><code>type createAccountRequest struct {\n        ......\n\tCurrency string `json:&quot;currency&quot; binding:&quot;required,currency&quot;`\n}<\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">\u6700\u5f8c\u306b<\/h2>\n\n\n\n<p>\u4eca\u56de\u306fGin\u306b\u3064\u3044\u3066\u89e6\u308c\u307e\u3057\u305f\u304c\u3001Golang\u3068\u3044\u3048\u3070\u3001\u300cnet\/http\u300d\u3067\u3059\u3088\u306d\u3002<\/p>\n\n\n\n<p>\u4eca\u5f8c\u306fnet\/http\u306b\u3064\u3044\u3066\u89e6\u308c\u3066\u3044\u304d\u305f\u3044\u306a\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u3067\u306f\u3001\u307e\u305f\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u308f\uff01\u76f8\u3082\u5909\u308f\u3089\u305aGolang\u304c\u5927\u597d\u304d\u306a\u79c1\u3001\u4f50\u3005\u6728\u3067\u3059\uff01 \u4eca\u65e5\u306fGolang\u306e\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3001 Gin\u3092\u4f7f\u3063\u3066\u3044\u308b\u4e2d\u3067\u72ec\u81ea\u306e\u30d0\u30ea\u30c7\u30fc\u30bf\u3092\u4f5c\u308b\u306e\u304c\u3068\u3066\u3082\u7c21\u5358\u3060\u3063\u305f\u306e\u3067\u3054\u7d39\u4ecb\u3057\u307e\u3059\u3002 Gin Gin\u306fGolang\u306eWe [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1868,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"swell_btn_cv_data":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[108,17],"class_list":["post-1354","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tech","tag-gin","tag-golang"],"jetpack_featured_media_url":"https:\/\/www.miracleave.co.jp\/contents\/wp-content\/uploads\/2022\/03\/GO_thmb.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.miracleave.co.jp\/contents\/wp-json\/wp\/v2\/posts\/1354","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.miracleave.co.jp\/contents\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.miracleave.co.jp\/contents\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.miracleave.co.jp\/contents\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.miracleave.co.jp\/contents\/wp-json\/wp\/v2\/comments?post=1354"}],"version-history":[{"count":36,"href":"https:\/\/www.miracleave.co.jp\/contents\/wp-json\/wp\/v2\/posts\/1354\/revisions"}],"predecessor-version":[{"id":2376,"href":"https:\/\/www.miracleave.co.jp\/contents\/wp-json\/wp\/v2\/posts\/1354\/revisions\/2376"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.miracleave.co.jp\/contents\/wp-json\/wp\/v2\/media\/1868"}],"wp:attachment":[{"href":"https:\/\/www.miracleave.co.jp\/contents\/wp-json\/wp\/v2\/media?parent=1354"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.miracleave.co.jp\/contents\/wp-json\/wp\/v2\/categories?post=1354"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.miracleave.co.jp\/contents\/wp-json\/wp\/v2\/tags?post=1354"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}