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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Kazuki Higashiguchi
May 27, 2019
Technology
2.6k
5
Share
Go API Validation error handling
GoのAPIでのValidation Error Handlingの実装例です。
Kazuki Higashiguchi
May 27, 2019
More Decks by Kazuki Higashiguchi
See All by Kazuki Higashiguchi
Practical Monitoring for Knative Serving / KubeCon + CloudNativeCon Japan 2025
hgsgtk
0
110
Cell-Based Architecture Design in AWS
hgsgtk
1
220
インフラコストとセキュリティ課題解決のためのリアーキテクチャリング / srekaigi2025
hgsgtk
3
8.3k
Design of a Stateful system for Robust Deployment and Observability
hgsgtk
0
1.5k
A guide to joining operational work in your new DevOps team
hgsgtk
1
1.6k
HTTP Tunneling in Go
hgsgtk
0
1.6k
ブラウザ自動操作技術の深層へ、直接触れて学ぶ WebDriver と Chrome DevTools Protocol
hgsgtk
3
7k
HTTP Server on random available port in Go
hgsgtk
0
1.2k
Agile Testingを夢見たテスト自動化 〜ATDDへの挑戦から始まる 1年間の試行錯誤〜 / dreaming agile testing at basebank
hgsgtk
13
8.4k
Other Decks in Technology
See All in Technology
AI活用時代の事業判断高度化を導くエンジニアリング基盤 / 20260424 Atsushi Funahashi
shift_evolve
PRO
2
100
【技術書典20】OpenFOAM(自宅で深める流体解析)流れと熱移動(2)
kamakiri1225
0
320
独断と偏見で試してみる、 シングル or マルチエージェント どっちがいいの?
shichijoyuhi
1
190
Microsoft 365 / Microsoft 365 Copilot : 自分の状態を確認する「ラベル」について
taichinakamura
0
390
生成AIが変える SaaS の競争原理と弁護士ドットコムのプロダクト戦略
bengo4com
1
2.6k
目的ファーストのハーネス設計 ~ハーネスの変更容易性を高めるための優先順位~
gotalab555
9
3.1k
AgentCore Managed Harness を使ってみよう
yakumo
2
250
はじめての MagicPod生成AI機能 機能紹介から活用方法まで
magicpod
0
120
コードや知識を組み込む / Incorporate Code and Knowledge
ks91
PRO
0
180
Oracle AI Database@AWS:サービス概要のご紹介
oracle4engineer
PRO
4
2.4k
Chasing Real-Time Observability for CRuby
whitegreen
0
290
プラットフォームエンジニアリングの実践 - AWS コンテナサービスで構築する社内プラットフォーム / AWS Containers Platform Meetup #1
literalice
1
220
Featured
See All Featured
Technical Leadership for Architectural Decision Making
baasie
3
340
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
330
エンジニアに許された特別な時間の終わり
watany
106
240k
Side Projects
sachag
455
43k
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.2k
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
380
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
65
55k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
160
Done Done
chrislema
186
16k
A Soul's Torment
seathinner
6
2.7k
How to train your dragon (web standard)
notwaldorf
97
6.6k
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
750
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?