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
5
2.4k
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
インフラコストとセキュリティ課題解決のためのリアーキテクチャリング / srekaigi2025
hgsgtk
3
4.8k
Design of a Stateful system for Robust Deployment and Observability
hgsgtk
0
1.2k
A guide to joining operational work in your new DevOps team
hgsgtk
1
1.4k
HTTP Tunneling in Go
hgsgtk
0
1.4k
ブラウザ自動操作技術の深層へ、直接触れて学ぶ WebDriver と Chrome DevTools Protocol
hgsgtk
3
6.5k
HTTP Server on random available port in Go
hgsgtk
0
970
Agile Testingを夢見たテスト自動化 〜ATDDへの挑戦から始まる 1年間の試行錯誤〜 / dreaming agile testing at basebank
hgsgtk
14
7.8k
Create Go WebDriver client from scratch
hgsgtk
1
2.2k
PHPでWeb Driver Clientを自作する〜己の手でブラウザ操作自動化を完全理解する方法〜 / phpcon2021
hgsgtk
2
2.5k
Other Decks in Technology
See All in Technology
Datadogとともにオブザーバビリティを布教しよう
mego2221
0
130
データ資産をシームレスに伝達するためのイベント駆動型アーキテクチャ
kakehashi
PRO
2
380
SA Night #2 FinatextのSA思想/SA Night #2 Finatext session
satoshiimai
1
130
Helm , Kustomize に代わる !? 次世代 k8s パッケージマネージャー Glasskube 入門 / glasskube-entry
parupappa2929
0
110
【Developers Summit 2025】プロダクトエンジニアから学ぶ、 ユーザーにより高い価値を届ける技術
niwatakeru
2
980
現場で役立つAPIデザイン
nagix
30
11k
Platform Engineeringは自由のめまい
nwiizo
4
2k
「海外登壇」という 選択肢を与えるために 〜Gophers EX
logica0419
0
570
転生CISOサバイバル・ガイド / CISO Career Transition Survival Guide
kanny
3
820
スクラムのイテレーションを導入してチームの雰囲気がより良くなった話
eccyun
0
110
『AWS Distinguished Engineerに学ぶ リトライの技術』 #ARC403/Marc Brooker on Try again: The tools and techniques behind resilient systems
quiver
0
140
ユーザーストーリーマッピングから始めるアジャイルチームと並走するQA / Starting QA with User Story Mapping
katawara
0
130
Featured
See All Featured
BBQ
matthewcrist
86
9.5k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Faster Mobile Websites
deanohume
306
31k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.8k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
33
2.8k
KATA
mclloyd
29
14k
Music & Morning Musume
bryan
46
6.3k
Optimising Largest Contentful Paint
csswizardry
34
3.1k
Docker and Python
trallard
44
3.3k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
GitHub's CSS Performance
jonrohan
1030
460k
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?