mirror of https://gitee.com/answerdev/answer.git
Merge branch 'test' of git.backyard.segmentfault.com:opensource/answer into test
This commit is contained in:
commit
495f9de65a
|
@ -3,6 +3,7 @@ package validator
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/answerdev/answer/internal/base/reason"
|
"github.com/answerdev/answer/internal/base/reason"
|
||||||
"github.com/answerdev/answer/internal/base/translator"
|
"github.com/answerdev/answer/internal/base/translator"
|
||||||
|
@ -97,9 +98,19 @@ func (m *MyValidator) Check(value interface{}) (errField *ErrorField, err error)
|
||||||
|
|
||||||
for _, fieldError := range valErrors {
|
for _, fieldError := range valErrors {
|
||||||
errField = &ErrorField{
|
errField = &ErrorField{
|
||||||
Key: translator.GlobalTrans.Tr(m.Lang, fieldError.Field()),
|
Key: fieldError.Field(),
|
||||||
Value: fieldError.Translate(m.Tran),
|
Value: fieldError.Translate(m.Tran),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get original tag name from value for set err field key.
|
||||||
|
structNamespace := fieldError.StructNamespace()
|
||||||
|
_, fieldName, found := strings.Cut(structNamespace, ".")
|
||||||
|
if found {
|
||||||
|
originalTag := getObjectTagByFieldName(value, fieldName)
|
||||||
|
if len(originalTag) > 0 {
|
||||||
|
errField.Key = originalTag
|
||||||
|
}
|
||||||
|
}
|
||||||
return errField, myErrors.BadRequest(reason.RequestFormatError).WithMsg(fieldError.Translate(m.Tran))
|
return errField, myErrors.BadRequest(reason.RequestFormatError).WithMsg(fieldError.Translate(m.Tran))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -117,3 +128,24 @@ func (m *MyValidator) Check(value interface{}) (errField *ErrorField, err error)
|
||||||
type Checker interface {
|
type Checker interface {
|
||||||
Check() (errField *ErrorField, err error)
|
Check() (errField *ErrorField, err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getObjectTagByFieldName(obj interface{}, fieldName string) (tag string) {
|
||||||
|
defer func() {
|
||||||
|
if err := recover(); err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
objT := reflect.TypeOf(obj)
|
||||||
|
objT = objT.Elem()
|
||||||
|
|
||||||
|
structField, exists := objT.FieldByName(fieldName)
|
||||||
|
if !exists {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
tag = structField.Tag.Get("json")
|
||||||
|
if len(tag) == 0 {
|
||||||
|
return structField.Tag.Get("form")
|
||||||
|
}
|
||||||
|
return tag
|
||||||
|
}
|
||||||
|
|
|
@ -77,9 +77,9 @@ type InitBaseInfoReq struct {
|
||||||
SiteName string `validate:"required,gt=0,lte=30" json:"site_name"`
|
SiteName string `validate:"required,gt=0,lte=30" json:"site_name"`
|
||||||
SiteURL string `validate:"required,gt=0,lte=512,url" json:"site_url"`
|
SiteURL string `validate:"required,gt=0,lte=512,url" json:"site_url"`
|
||||||
ContactEmail string `validate:"required,email,gt=0,lte=500" json:"contact_email"`
|
ContactEmail string `validate:"required,email,gt=0,lte=500" json:"contact_email"`
|
||||||
AdminName string `validate:"required,gt=4,lte=30" json:"admin_name"`
|
AdminName string `validate:"required,gt=4,lte=30" json:"name"`
|
||||||
AdminPassword string `validate:"required,gte=8,lte=32" json:"admin_password"`
|
AdminPassword string `validate:"required,gte=8,lte=32" json:"password"`
|
||||||
AdminEmail string `validate:"required,email,gt=0,lte=500" json:"admin_email"`
|
AdminEmail string `validate:"required,email,gt=0,lte=500" json:"email"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *InitBaseInfoReq) FormatSiteUrl() {
|
func (r *InitBaseInfoReq) FormatSiteUrl() {
|
||||||
|
|
Loading…
Reference in New Issue