Skip to content

Commit

Permalink
修正required_if和required_unless的中文语言包问题
Browse files Browse the repository at this point in the history
  • Loading branch information
caoyong2619 committed Jul 21, 2022
1 parent 8629684 commit 7633937
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
35 changes: 35 additions & 0 deletions issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package validate_test

import (
"fmt"
"github.com/gookit/validate/locales/zhcn"
"testing"
"time"

Expand Down Expand Up @@ -964,3 +965,37 @@ func TestIssue_143(t *testing.T) {
assert.False(t, ok)
assert.Equal(t, "age min value is 30", v.Errors.One())
}

func TestIssue_152(t *testing.T) {
zhcn.RegisterGlobal()

// test required if
type requiredIf struct {
Type int64 `validate:"required" label:"类型"`
Data string `validate:"required_if:Type,1" label:"数据"`
}

v := validate.Struct(requiredIf{
Type: 1,
Data: "",
})

v.Validate()

assert.Equal(t, `当 类型 为 [1] 时 数据 不能为空。`, v.Errors.One())

// test required unless
type requiredUnless struct {
Type int64 `label:"类型"`
Data string `validate:"required_unless:Type,1" label:"数据"`
}

v = validate.Struct(requiredUnless{
Type: 0,
Data: "",
})

v.Validate()

assert.Equal(t, `当 类型 不为 [1] 时 数据 不能为空。`, v.Errors.One())
}
4 changes: 2 additions & 2 deletions locales/zhcn/zhcn.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ var Data = map[string]string{
"range": "{field} 值必须在此范围内 %d - %d",
// required
"required": "{field} 是必填项",
"requiredIf": "当 %v 为 {args} 时 {field} 不能为空。",
"requiredUnless": "当 %v 不为 {args} 时 {field} 不能为空。",
"requiredIf": "当 {args0} 为 {args1end} 时 {field} 不能为空。",
"requiredUnless": "当 {args0} 不为 {args1end} 时 {field} 不能为空。",
"requiredWith": "当 {values} 存在时 {field} 不能为空。",
"requiredWithAll": "当 {values} 存在时 {field} 不能为空。",
"requiredWithout": "当 {values} 不存在时 {field} 不能为空。",
Expand Down

0 comments on commit 7633937

Please sign in to comment.