Line data Source code
1 : package handlerutil
2 :
3 : import (
4 : "net/http"
5 :
6 : "github.com/gin-gonic/gin"
7 :
8 : rules "resume/internal/adapter/validation/rules"
9 : )
10 :
11 : // ErrorItem は 1 件の検証エラー項目を表す型です。
12 : // 検証タグ名・パラメータ・入力値などを保持します。
13 : type ErrorItem = rules.ErrorItem
14 :
15 : // ErrorEnvelope は 422 Unprocessable Entity 用のエラーレスポンス本体です。
16 : // code / message / requestId と、フィールドごとの details を含みます。
17 : type ErrorEnvelope struct {
18 : Code string `json:"code"`
19 : Message string `json:"message"`
20 : RequestID string `json:"requestId,omitempty"`
21 : Details map[string][]ErrorItem `json:"details,omitempty"`
22 : // 移行期だけ有効化したい場合はポインタでnil可
23 : LegacyDetails map[string]map[string]string `json:"legacyDetails,omitempty"`
24 : }
25 :
26 : // WriteValidationError は与えられた ErrorEnvelope を "error" キーでラップし、
27 : // 422 Unprocessable Entity として JSON レスポンスを返します。
28 1 : func WriteValidationError(c *gin.Context, env ErrorEnvelope) {
29 1 : c.JSON(http.StatusUnprocessableEntity, gin.H{"error": env})
30 1 : }
|