Skip to content

Commit

Permalink
fix: #111 argN maybe is a field name
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jan 24, 2022
1 parent a85e324 commit 4f070f2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
5 changes: 3 additions & 2 deletions issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ func TestIssue_111(t *testing.T) {
v.StringRule("password", "required|eq_field:password2|minLen:6|max_len:32")

assert.False(t, v.Validate())

dump.Println(v.Errors)
// dump.Println(v.Errors)
assert.Contains(t, v.Errors.String(), "密码 ")
assert.Contains(t, v.Errors.String(), " 重复密码")
}
34 changes: 24 additions & 10 deletions messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,14 @@ func (t *Translator) HasField(field string) bool {
return ok
}

// FieldName get in the t.fieldMap
func (t *Translator) FieldName(field string) string {
if trName, ok := t.fieldMap[field]; ok {
field = trName
}
return field
}

// HasMessage key in the t.messages
func (t *Translator) HasMessage(key string) bool {
_, ok := t.messages[key]
Expand All @@ -327,26 +335,32 @@ func (t *Translator) Message(validator, field string, args ...interface{}) (msg

// not found, fallback - use default error message
if errMsg == "" {
if trName, ok := t.fieldMap[field]; ok {
field = trName
}
return field + defaultErrMsg
return t.FieldName(field) + defaultErrMsg
}
}

return t.format(errMsg, validator, field, args)
return t.format(errMsg, field, args)
}

// format message for the validator
func (t *Translator) format(errMsg, validator, field string, args []interface{}) string {
func (t *Translator) format(errMsg, field string, args []interface{}) string {
argLen := len(args)

// fix: #111 argN maybe is a field name
for i, arg := range args {
if name, ok := arg.(string); ok {
if trName, ok := t.fieldMap[name]; ok {
args[i] = trName
}
}
}

// not contains vars. eg: {field}
if !strings.ContainsRune(errMsg, '{') {
// whether you need call fmt.Sprintf
if strings.ContainsRune(errMsg, '%') {
// TODO argN maybe is an field name, should use t.fieldMap[argN] translate.
if argLen > 0 && strings.ContainsRune(errMsg, '%') {
errMsg = fmt.Sprintf(errMsg, args...)
}

return errMsg
}

Expand All @@ -357,7 +371,7 @@ func (t *Translator) format(errMsg, validator, field string, args []interface{})
}
}

if argLen := len(args); argLen > 0 {
if argLen > 0 {
// whether you need call fmt.Sprintf
if strings.ContainsRune(errMsg, '%') {
errMsg = fmt.Sprintf(errMsg, args...)
Expand Down

0 comments on commit 4f070f2

Please sign in to comment.