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.5k
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
Practical Monitoring for Knative Serving / KubeCon + CloudNativeCon Japan 2025
hgsgtk
0
28
Cell-Based Architecture Design in AWS
hgsgtk
1
120
インフラコストとセキュリティ課題解決のためのリアーキテクチャリング / srekaigi2025
hgsgtk
3
6.2k
Design of a Stateful system for Robust Deployment and Observability
hgsgtk
0
1.3k
A guide to joining operational work in your new DevOps team
hgsgtk
1
1.4k
HTTP Tunneling in Go
hgsgtk
0
1.5k
ブラウザ自動操作技術の深層へ、直接触れて学ぶ WebDriver と Chrome DevTools Protocol
hgsgtk
3
6.7k
HTTP Server on random available port in Go
hgsgtk
0
1k
Agile Testingを夢見たテスト自動化 〜ATDDへの挑戦から始まる 1年間の試行錯誤〜 / dreaming agile testing at basebank
hgsgtk
14
8k
Other Decks in Technology
See All in Technology
ひとり情シスなCTOがLLMと始めるオペレーション最適化 / CTO's LLM-Powered Ops
yamitzky
0
380
AWS CDK 実践的アプローチ N選 / aws-cdk-practical-approaches
gotok365
4
520
ユーザーのプロフィールデータを活用した推薦精度向上の取り組み
yudai00
0
490
Liquid Glass革新とSwiftUI/UIKit進化
fumiyasac0921
0
150
IIWレポートからみるID業界で話題のMCP
fujie
0
740
Amazon Bedrockで実現する 新たな学習体験
kzkmaeda
1
410
Oracle Audit Vault and Database Firewall 20 概要
oracle4engineer
PRO
3
1.6k
標準技術と独自システムで作る「つらくない」SaaS アカウント管理 / Effortless SaaS Account Management with Standard Technologies & Custom Systems
yuyatakeyama
2
1k
解析の定理証明実践@Lean 4
dec9ue
0
110
米国国防総省のDevSecOpsライフサイクルをAWSのセキュリティサービスとOSSで実現
syoshie
2
820
Абьюзим random_bytes(). Фёдор Кулаков, разработчик Lamoda Tech
lamodatech
0
300
VISITS_AIIoTビジネス共創ラボ登壇資料.pdf
iotcomjpadmin
0
150
Featured
See All Featured
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
7
700
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
Rails Girls Zürich Keynote
gr2m
94
14k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
16
940
The World Runs on Bad Software
bkeepers
PRO
69
11k
Build The Right Thing And Hit Your Dates
maggiecrowley
36
2.8k
Bootstrapping a Software Product
garrettdimon
PRO
307
110k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
181
53k
Agile that works and the tools we love
rasmusluckow
329
21k
Faster Mobile Websites
deanohume
307
31k
KATA
mclloyd
29
14k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
130
19k
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?