Line data Source code
1 : package handlerutil
2 :
3 : import (
4 : "fmt"
5 :
6 : "github.com/gin-gonic/gin"
7 : "github.com/go-playground/validator/v10"
8 :
9 : vtrans "resume/internal/adapter/validation/rules"
10 : "resume/internal/shared/requestid"
11 : "resume/internal/shared/util"
12 : )
13 :
14 : // BuildValidationEnvelope は vtrans.MapValidationErrors の結果を “既存エンベロープに詰める”
15 : func BuildValidationEnvelope(
16 : c *gin.Context, // gin.Context 互換(GetHeader, MustGetなど必要なものだけ)
17 : verrs validator.ValidationErrors,
18 : scope string, // 例: "domain.address"
19 0 : ) ErrorEnvelope {
20 0 :
21 0 : //locale := middleware.From(c) // あなたの i18n middleware から取得
22 0 : // ① i18n 版 details を作る
23 0 : i18nDetails := vtrans.MapValidationErrors(verrs, scope)
24 0 : // ② 旧形(param/tag/value)も必要なら作る
25 0 : legacy := map[string]map[string]string{}
26 0 : for _, fe := range verrs {
27 0 : // フィールドキーは既存に合わせる(例: StructField ではなく scope + snake/camel 統一)
28 0 : fieldKey := scope + "." + util.ToCamel(fe.Field())
29 0 : legacy[fieldKey] = map[string]string{
30 0 : "param": fe.Param(),
31 0 : "tag": fe.Tag(),
32 0 : "value": fmt.Sprint(fe.Value()), // 型に関係なく安全に文字列化
33 0 : }
34 0 : }
35 :
36 0 : return ErrorEnvelope{
37 0 : Code: "UNPROCESSABLE",
38 0 : Message: "The request contains semantically invalid data.",
39 0 : RequestID: requestid.Get(c), // 既存のリクエストID取り出し
40 0 : Details: i18nDetails, // ← 新フォーマット(フロントはここを見る)
41 0 : LegacyDetails: legacy, // ← 当面併載。要らなくなったら削除
42 0 : }
43 : }
|