Skip to content

Commit

Permalink
up: add func for get coped global messages, fix some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Aug 24, 2022
1 parent 67c93b3 commit 46c7146
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 52 deletions.
8 changes: 6 additions & 2 deletions issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,11 @@ func TestIssues_148(t *testing.T) {

// https://github.com/gookit/validate/issues/152
func TestIssue_152(t *testing.T) {
old := validate.CopyGlobalMessages()
defer func() {
validate.SetBuiltinMessages(old)
}()

zhcn.RegisterGlobal()

// test required if
Expand Down Expand Up @@ -1031,7 +1036,6 @@ func TestIssue_152(t *testing.T) {
})

v.Validate()

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

Expand Down Expand Up @@ -1077,7 +1081,7 @@ func TestIssues_159(t *testing.T) {

v := validate.Struct(ts)
ok := v.Validate()
dump.Println(v.Errors)
// dump.Println(v.Errors)
assert.False(t, ok)
assert.Equal(t, "end value should be greater or equal to field start", v.Errors.One())
}
Expand Down
8 changes: 7 additions & 1 deletion locales/ruru/ruru_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package ruru

import (
"testing"

"github.com/gookit/validate"
"github.com/stretchr/testify/assert"
"testing"
)

func TestRegister(t *testing.T) {
Expand All @@ -21,6 +22,11 @@ func TestRegister(t *testing.T) {
}

func TestRegisterGlobal(t *testing.T) {
old := validate.CopyGlobalMessages()
defer func() {
validate.SetBuiltinMessages(old)
}()

RegisterGlobal()

is := assert.New(t)
Expand Down
2 changes: 1 addition & 1 deletion locales/zhcn/zhcn.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func Register(v *validate.Validation) {
v.AddMessages(Data)
}

// RegisterGlobal register to the validate global messages
// RegisterGlobal register to validate global messages
func RegisterGlobal() {
validate.AddGlobalMessages(Data)
}
Expand Down
5 changes: 5 additions & 0 deletions locales/zhcn/zhcn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ func TestRegister(t *testing.T) {
}

func TestRegisterGlobal(t *testing.T) {
old := validate.CopyGlobalMessages()
defer func() {
validate.SetBuiltinMessages(old)
}()

RegisterGlobal()

is := assert.New(t)
Expand Down
5 changes: 5 additions & 0 deletions locales/zhtw/zhtw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ func TestRegister(t *testing.T) {
}

func TestRegisterGlobal(t *testing.T) {
old := validate.CopyGlobalMessages()
defer func() {
validate.SetBuiltinMessages(old)
}()

RegisterGlobal()

is := assert.New(t)
Expand Down
20 changes: 13 additions & 7 deletions messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,17 +257,23 @@ func AddGlobalMessages(mp map[string]string) {
}

// AddBuiltinMessages alias of the AddGlobalMessages()
func AddBuiltinMessages(mp map[string]string) {
for name, msg := range mp {
builtinMessages[name] = msg
}
}
func AddBuiltinMessages(mp map[string]string) { AddGlobalMessages(mp) }

// BuiltinMessages get builtin messages
func BuiltinMessages() map[string]string {
return builtinMessages
func BuiltinMessages() map[string]string { return builtinMessages }

// CopyGlobalMessages copy get builtin messages
func CopyGlobalMessages() map[string]string {
cp := make(map[string]string, len(builtinMessages))
for name, msg := range builtinMessages {
cp[name] = msg
}
return cp
}

// SetBuiltinMessages override set builtin messages
func SetBuiltinMessages(mp map[string]string) { builtinMessages = mp }

/*************************************************************
* Error messages translator
*************************************************************/
Expand Down
11 changes: 5 additions & 6 deletions rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestRule(t *testing.T) {
func TestRule_basic(t *testing.T) {
is := assert.New(t)
data := url.Values{
"name": []string{"inhere"},
Expand All @@ -33,7 +33,7 @@ func TestRule(t *testing.T) {
v.AddRule("key0", "inRule").SetCheckFunc(func(s string) bool {
return s == "val0"
})
v.AddRule("name", "gtField", "key0")
v.AddRule("name", "ltField", "key0")

// validate. will skip validate field "name"
v.Validate()
Expand Down Expand Up @@ -63,8 +63,7 @@ func TestRule_SetBeforeFunc(t *testing.T) {

// use SetBeforeFunc
v = Map(mp)
v.
AddRule("avatar", "isFile").
v.AddRule("avatar", "isFile").
SetBeforeFunc(func(v *Validation) bool {
// return false for skip validate
return false
Expand All @@ -81,8 +80,7 @@ func TestRule_SetFilterFunc(t *testing.T) {
"age": "abc",
})

v.
AddRule("age", "int", 1, 100).
v.AddRule("age", "int", 1, 100).
SetFilterFunc(func(val interface{}) (i interface{}, e error) {
return filter.Int(val)
})
Expand All @@ -102,6 +100,7 @@ func TestRule_SetSkipEmpty(t *testing.T) {
v.AddRule("age", "int", 1)
v.AddRule("name", "string", 1, 10)
is.True(v.Validate())

sd := v.SafeData()
is.Contains(sd, "name")
is.NotContains(sd, "age")
Expand Down
42 changes: 11 additions & 31 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/gookit/filter"
"github.com/gookit/goutil/mathutil"
"github.com/gookit/goutil/reflects"
"github.com/gookit/goutil/strutil"
)

Expand All @@ -21,7 +22,7 @@ var nilObj = NilObject{}
// init a reflect nil value
var nilRVal = reflect.ValueOf(nilObj)

// TODO a reflect nil value
// NilValue TODO a reflect nil value, use for instead of nilRVal
var NilValue = reflect.Zero(reflect.TypeOf((*interface{})(nil)).Elem())

// IsNilObj check value is internal NilObject
Expand Down Expand Up @@ -126,32 +127,14 @@ func ValueIsEmpty(v reflect.Value) bool {
return reflect.DeepEqual(v.Interface(), reflect.Zero(v.Type()).Interface())
}

// ValueLen get value length. TODO use reflects.Len()
// ValueLen get value length.
// Deprecated: please use reflects.Len()
func ValueLen(v reflect.Value) int {
v = reflect.Indirect(v)
k := v.Kind()

// (u)int use width.
switch k {
case reflect.String:
return len([]rune(v.String()))
case reflect.Map, reflect.Array, reflect.Chan, reflect.Slice:
return v.Len()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return len(strconv.FormatInt(int64(v.Uint()), 10))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return len(strconv.FormatInt(v.Int(), 10))
case reflect.Float32, reflect.Float64:
return len(fmt.Sprint(v.Interface()))
}

// cannot get length
return -1
return reflects.Len(v)
}

var (
ErrConvertFail = errors.New("convert value is failure")
)
// ErrConvertFail error
var ErrConvertFail = errors.New("convert value is failure")

func valueToInt64(v interface{}, strict bool) (i64 int64, err error) {
switch tVal := v.(type) {
Expand Down Expand Up @@ -202,11 +185,8 @@ func CalcLength(val interface{}) int {
return -1
}

// fix: issues#39 dont use `return len(str)` for string.
if str, ok := val.(string); ok {
return len([]rune(str))
}
return ValueLen(reflect.ValueOf(val))
// return ValueLen(reflect.ValueOf(val))
return reflects.Len(reflect.ValueOf(val))
}

// value compare.
Expand All @@ -215,8 +195,8 @@ func CalcLength(val interface{}) int {
func valueCompare(srcVal, dstVal interface{}, op string) (ok bool) {
// string compare
if str1, ok := srcVal.(string); ok {
str2, ok := dstVal.(string)
if !ok {
str2, err := strutil.ToString(dstVal)
if err != nil {
return false
}

Expand Down
8 changes: 4 additions & 4 deletions validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func TestLtGt(t *testing.T) {
is.True(Gt(0.3, 0.2))
is.True(Gt(2.1, 2))
is.False(Gt(2, 3))
is.False(Gt("invalid", 3))
is.False(Gt([]int{23}, 3))
}

func TestMin(t *testing.T) {
Expand All @@ -261,7 +261,7 @@ func TestMin(t *testing.T) {
{val: float32(3.2), min: 3.1},
{val: 3.2, min: 3.2},
{val: 3.2, min: "3.2"},
{val: "3", min: 3.2},
{val: 3, min: 3.2},
{val: 0.02, min: 0.01},
{val: 0.02, min: 0.02},
}
Expand All @@ -271,9 +271,9 @@ func TestMin(t *testing.T) {

// fail
tests = []struct{ val, min interface{} }{
{val: "3.2", min: 3.2},
{val: 3.1, min: 3.2},
{nil, 3},
{"str", 3},
{"abc", "def"},
{3, nil},
{3, 4},
{3, "abc"},
Expand Down

0 comments on commit 46c7146

Please sign in to comment.