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
5.1k
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
980
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
ExaDB-XSで利用されているExadata Exascaleについて
oracle4engineer
PRO
3
280
Introduction to OpenSearch Project - Search Engineering Tech Talk 2025 Winter
tkykenmt
2
150
Cracking the Coding Interview 6th Edition
gdplabs
14
28k
目標と時間軸 〜ベイビーステップでケイパビリティを高めよう〜
kakehashi
PRO
8
870
事業モメンタムを生み出すプロダクト開発
macchiitaka
0
100
リクルートのエンジニア組織を下支えする 新卒の育成の仕組み
recruitengineers
PRO
1
140
サバイバルモード下でのエンジニアリングマネジメント
konifar
12
2.5k
OPENLOGI Company Profile for engineer
hr01
1
20k
入門 PEAK Threat Hunting @SECCON
odorusatoshi
0
170
技術スタックだけじゃない、業務ドメイン知識のオンボーディングも同じくらいの量が必要な話
niftycorp
PRO
0
120
Amazon Athenaから利用時のGlueのIcebergテーブルのメンテナンスについて
nayuts
0
110
フォーイット_エンジニア向け会社紹介資料_Forit_Company_Profile.pdf
forit_tech
1
1.7k
Featured
See All Featured
KATA
mclloyd
29
14k
Optimising Largest Contentful Paint
csswizardry
34
3.1k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
21
2.5k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
Product Roadmaps are Hard
iamctodd
PRO
51
11k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
30
4.6k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
40
2k
Building Adaptive Systems
keathley
40
2.4k
Stop Working from a Prison Cell
hatefulcrawdad
268
20k
Being A Developer After 40
akosma
89
590k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3k
Bash Introduction
62gerente
611
210k
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?