Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Go API Validation error handling
Search
Kazuki Higashiguchi
May 27, 2019
Technology
4
2k
Go API Validation error handling
GoのAPIでのValidation Error Handlingの実装例です。
Kazuki Higashiguchi
May 27, 2019
Tweet
Share
More Decks by Kazuki Higashiguchi
See All by Kazuki Higashiguchi
Design of a Stateful system for Robust Deployment and Observability
hgsgtk
0
1k
A guide to joining operational work in your new DevOps team
hgsgtk
1
1.2k
HTTP Tunneling in Go
hgsgtk
0
1k
ブラウザ自動操作技術の深層へ、直接触れて学ぶ WebDriver と Chrome DevTools Protocol
hgsgtk
3
6k
HTTP Server on random available port in Go
hgsgtk
0
770
Agile Testingを夢見たテスト自動化 〜ATDDへの挑戦から始まる 1年間の試行錯誤〜 / dreaming agile testing at basebank
hgsgtk
14
7.1k
Create Go WebDriver client from scratch
hgsgtk
1
1.9k
PHPでWeb Driver Clientを自作する〜己の手でブラウザ操作自動化を完全理解する方法〜 / phpcon2021
hgsgtk
2
2.2k
振り返りを積み上げて自分たちのプラクティスとして昇華•体得していくための仕組みと考え方 / ScrumFestMikawa2021
hgsgtk
3
2.2k
Other Decks in Technology
See All in Technology
エンジニアのキャリアをちょっと楽しくする3本の軸/Three Pillars to Make an Engineer's Career More Enjoyable
kwappa
0
2.6k
コードを書く隙間を見つけて生きていく技術/Findy 思考の現在地
fujiwara3
27
5.8k
本当のAWS基礎
toru_kubota
0
500
KubeConにproposalを送りたい人へのアドバイス
sat
PRO
3
230
チームでロジカルシンキングに改めて向き合っている話 〜学習環境と実践⽅法〜
sansantech
PRO
2
1.9k
ChatworkのSRE部って実は 半分くらいPlatform Engineering部かもしれない
saramune
0
160
非同期推論システムによるコスト削減と信頼性向上
koki_nishihara
0
190
反実仮想機械学習とは何か
usaito
PRO
11
3.8k
オーナーシップを持つ領域を明確にする
konifar
13
3.1k
20240418_Google ColabにLLMが搭載されたようなのでPython x データ分析の勉強方法を考えてみる
doradora09
0
120
Reducing Cross-Zone Egress at Spotify with Custom gRPC Load Balancing Recap
koh_naga
0
200
ここが嬉しいABAC ここが辛いよABAC #再解説+補足編
masahirokawahara
1
270
Featured
See All Featured
Documentation Writing (for coders)
carmenintech
60
3.9k
How GitHub (no longer) Works
holman
304
140k
Designing for Performance
lara
601
67k
How GitHub Uses GitHub to Build GitHub
holman
468
290k
Code Reviewing Like a Champion
maltzj
514
39k
What's in a price? How to price your products and services
michaelherold
237
11k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
14
1.5k
Facilitating Awesome Meetings
lara
42
5.6k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
274
13k
Writing Fast Ruby
sferik
621
60k
Designing Experiences People Love
moore
136
23k
Keith and Marios Guide to Fast Websites
keithpitt
408
22k
Transcript
© - BASE, Inc. Go製APIの Validation error handling . .
#gouncon kg Go Un Conference kg - @hgsgtk
© - BASE, Inc. このトークで持ち帰れること • Goでのバリデーションハンドリングの実例 • ネット上でなかなか⾒つからない(個⼈談) •
UseCase: HTTPリクエスト時‧DB保存時 • Question/Suggestionください • 即時に反映されるかも知れません
© - BASE, Inc. ⾃⼰紹介 東⼝和暉 (Kazuki Higashiguchi) Twitter /
GitHub : @hgsgtk バックエンドエンジニア BASE BANK, Inc. / Dev Division Go歴: - 趣味:2017.7〜 - 仕事:2018.6〜
© - BASE, Inc. 前提:全体パッケージ構成とUseCase バリデーション発⽣箇所 バリデーション発⽣箇所
© - BASE, Inc. こんな感じのバリデーションエラーを返したい
© - BASE, Inc. バリデーション実装全体像
© - BASE, Inc. バリデーション実装STEP >
© - BASE, Inc. Step Validatorを作成する > 使⽤ライブラリ • go-playground/validator
• https://github.com/go-playground/validator • ginフレームワークで公式採⽤されている • →このライブラリをwrapした validation パッケージ を作成
© - BASE, Inc. go-playground/validatorの利⽤例 こんな感じで validate タグ設定できる
© - BASE, Inc. Step : Validatorを作成する > validation パッケージ
• go-playground/validator を各種import
© - BASE, Inc. Step : Validatorを作成する • NewRequestValidator: Request
Validator • NewDBValidator: Database Validator
© - BASE, Inc. バリデータを作成する NewValidator() Step : Validatorを作成する
© - BASE, Inc. カスタムエラーメッセージの定義 デフォルトは構造体名が出る → タグ名に変更 Step :
Validatorを作成する
© - BASE, Inc. Step : Validatorを作成する > tagNameFuncの作成 •
tagNameFuncを作成 • See also: https://github.com/go-playground/ validator/issues/ •
© - BASE, Inc. Step : Validatorを作成する > 作成されたValidator
© - BASE, Inc. バリデーション実装STEP >
© - BASE, Inc. Step : バリデーション実⾏
© - BASE, Inc. Step : カスタムタグ登録
© - BASE, Inc. Step : カスタムタグを作って利⽤する • “inlist”というタグを作る例
© - BASE, Inc. Step : カスタムタグを作って利⽤する • “inlist”というタグを作る例 “Inlist”タグは、
“inStatusList()”というルールを実⾏
© - BASE, Inc. バリデーション実装STEP >
© - BASE, Inc. Step : バリデーション結果 • error interfaceを満たした独⾃エラー型を作成する
• 独⾃エラー型は、GetDetail()をシグネチャに持つ
© - BASE, Inc. Step : バリデーション結果 > バリデータが返すバリデーション結果
© - BASE, Inc. Step : バリデーション結果 > バリデータが返すバリデーション結果 Detail内に
バリデーション結果が含まれる
© - BASE, Inc. バリデーション実装STEP >
© - BASE, Inc. Step : バリデーション結果を利⽤ > 構造体への設定 •
バリデーションしたい構造体に validate タグを設定 • 構造体⾃⾝がvalidatorを実⾏する振る舞いを持つ
© - BASE, Inc. Step : バリデーション結果を利⽤ > エラー結果を利⽤ •
error 型で返却される • validation.Error interface型か判定
© - BASE, Inc. Step : バリデーション結果を利⽤ > エラーレスポンス
© - BASE, Inc. Step : バリデーション結果を利⽤ > こんな感じのバリデーションエラーを返る
© - BASE, Inc. Step : バリデーション結果を利⽤ > DB保存時のバリデーション
© - BASE, Inc. Step : バリデーション結果を利⽤ > DB保存時のバリデーション INSERTのSQL実⾏前に
バリデーション
© - BASE, Inc. まとめ • ネット上にGoでのバリデーションハンドリングの実 例を晒した • Question/Suggestionください
• 即時に反映されるかも知れません
Any Question or Suggestion?