diff --git a/.example/net/ghttp/server/request/request_validation.go b/.example/net/ghttp/server/request/request_validation.go index fa23190b2..2e180ad77 100644 --- a/.example/net/ghttp/server/request/request_validation.go +++ b/.example/net/ghttp/server/request/request_validation.go @@ -18,13 +18,13 @@ func main() { s.Group("/", func(rgroup *ghttp.RouterGroup) { rgroup.ALL("/user", func(r *ghttp.Request) { user := new(User) - if err := r.GetToStruct(user); err != nil { + if err := r.GetStruct(user); err != nil { r.Response.WriteJsonExit(g.Map{ "message": err, "errcode": 1, }) } - if err := gvalid.CheckStruct(user, nil); err != nil { + if err := gvalid.CheckStruct(r.Context(), user, nil); err != nil { r.Response.WriteJsonExit(g.Map{ "message": err.Maps(), "errcode": 1, diff --git a/.example/util/gvalid/gvalid.go b/.example/util/gvalid/gvalid.go index 9975bd443..a21b63f77 100644 --- a/.example/util/gvalid/gvalid.go +++ b/.example/util/gvalid/gvalid.go @@ -1,31 +1,32 @@ package main import ( + "context" "github.com/gogf/gf/frame/g" "github.com/gogf/gf/util/gvalid" ) func main() { //rule := "length:6,16" - //if m := gvalid.Check("123456", rule, nil); m != nil { + //if m := gvalid.Check(context.TODO(), "123456", rule, nil); m != nil { // fmt.Println(m) //} - //if m := gvalid.Check("12345", rule, nil); m != nil { + //if m := gvalid.Check(context.TODO(), "12345", rule, nil); m != nil { // fmt.Println(m) // // map[length:字段长度为6到16个字符] //} //rule := "integer|between:6,16" //msgs := "请输入一个整数|参数大小不对啊老铁" - //fmt.Println(gvalid.Check("5.66", rule, msgs)) + //fmt.Println(gvalid.Check(context.TODO(), "5.66", rule, msgs)) //// map[integer:请输入一个整数 between:参数大小不对啊老铁] //// 参数长度至少为6个数字或者6个字母,但是总长度不能超过16个字符 //rule := `regex:\d{6,}|\D{6,}|max-length:16` - //if m := gvalid.Check("123456", rule, nil); m != nil { + //if m := gvalid.Check(context.TODO(), "123456", rule, nil); m != nil { // fmt.Println(m) //} - //if m := gvalid.Check("abcde6", rule, nil); m != nil { + //if m := gvalid.Check(context.TODO(), "abcde6", rule, nil); m != nil { // fmt.Println(m) // // map[regex:字段值不合法] //} @@ -40,7 +41,7 @@ func main() { // "password" : "required|length:6,16|same:password2", // "password2" : "required|length:6,16", //} - //fmt.Println(gvalid.CheckMap(params, rules)) + //fmt.Println(gvalid.CheckMap(context.TODO(), params, rules)) //// map[passport:map[length:字段长度为6到16个字符] password:map[same:字段值不合法]] params := map[string]interface{}{ @@ -60,7 +61,7 @@ func main() { "same": "两次密码输入不相等", }, } - if e := gvalid.CheckMap(params, rules, msgs); e != nil { + if e := gvalid.CheckMap(context.TODO(), params, rules, msgs); e != nil { g.Dump(e.Maps()) } // map[passport:map[length:账号长度应当在6到16之间] password:map[same:两次密码输入不相等]] diff --git a/.example/util/gvalid/gvalid_custom_message.go b/.example/util/gvalid/gvalid_custom_message.go index 816749f10..3c8798693 100644 --- a/.example/util/gvalid/gvalid_custom_message.go +++ b/.example/util/gvalid/gvalid_custom_message.go @@ -1,6 +1,7 @@ package main import ( + "context" "fmt" "github.com/gogf/gf/frame/g" "github.com/gogf/gf/util/gvalid" @@ -8,6 +9,6 @@ import ( func main() { g.I18n().SetLanguage("cn") - err := gvalid.Check("", "required", nil) + err := gvalid.Check(context.TODO(), "", "required", nil) fmt.Println(err.String()) } diff --git a/.example/util/gvalid/gvalid_error.go b/.example/util/gvalid/gvalid_error.go index fe5c0a2f2..1f5d350d4 100644 --- a/.example/util/gvalid/gvalid_error.go +++ b/.example/util/gvalid/gvalid_error.go @@ -1,6 +1,7 @@ package main import ( + "context" "github.com/gogf/gf/frame/g" "github.com/gogf/gf/util/gvalid" ) @@ -17,7 +18,7 @@ func main() { ConfiemPassword: "", } - e := gvalid.CheckStruct(user, nil) + e := gvalid.CheckStruct(context.TODO(), user, nil) g.Dump(e.Map()) g.Dump(e.Maps()) g.Dump(e.String()) diff --git a/.example/util/gvalid/gvalid_result.go b/.example/util/gvalid/gvalid_result.go index 0190f0df4..ff9e01e6d 100644 --- a/.example/util/gvalid/gvalid_result.go +++ b/.example/util/gvalid/gvalid_result.go @@ -1,6 +1,7 @@ package main import ( + "context" "github.com/gogf/gf/frame/g" "github.com/gogf/gf/util/gvalid" ) @@ -18,7 +19,7 @@ func main() { Pass2: "123", } - e := gvalid.CheckStruct(user, nil) + e := gvalid.CheckStruct(context.TODO(), user, nil) g.Dump(e.String()) g.Dump(e.FirstString()) } diff --git a/.example/util/gvalid/gvalid_sequence.go b/.example/util/gvalid/gvalid_sequence.go index 8274cd1c9..0296a4ab5 100644 --- a/.example/util/gvalid/gvalid_sequence.go +++ b/.example/util/gvalid/gvalid_sequence.go @@ -1,6 +1,7 @@ package main import ( + "context" "fmt" "github.com/gogf/gf/util/gvalid" @@ -17,7 +18,7 @@ func main() { "password@required|length:6,16|same:password2#密码不能为空}|两次密码输入不相等", "password2@required|length:6,16#", } - if e := gvalid.CheckMap(params, rules); e != nil { + if e := gvalid.CheckMap(context.TODO(), params, rules); e != nil { fmt.Println(e.Map()) fmt.Println(e.FirstItem()) fmt.Println(e.FirstString()) diff --git a/.example/util/gvalid/gvalid_struct1.go b/.example/util/gvalid/gvalid_struct1.go index d992860ee..6995ec2be 100644 --- a/.example/util/gvalid/gvalid_struct1.go +++ b/.example/util/gvalid/gvalid_struct1.go @@ -1,6 +1,7 @@ package main import ( + "context" "github.com/gogf/gf/frame/g" "github.com/gogf/gf/util/gvalid" ) @@ -20,7 +21,7 @@ func main() { } // 使用结构体定义的校验规则和错误提示进行校验 - g.Dump(gvalid.CheckStruct(user, nil).Map()) + g.Dump(gvalid.CheckStruct(context.TODO(), user, nil).Map()) // 自定义校验规则和错误提示,对定义的特定校验规则和错误提示进行覆盖 rules := map[string]string{ @@ -31,5 +32,5 @@ func main() { "password3": "名称不能为空", }, } - g.Dump(gvalid.CheckStruct(user, rules, msgs).Map()) + g.Dump(gvalid.CheckStruct(context.TODO(), user, rules, msgs).Map()) } diff --git a/.example/util/gvalid/gvalid_struct2.go b/.example/util/gvalid/gvalid_struct2.go index a4c9442fb..8e6c11601 100644 --- a/.example/util/gvalid/gvalid_struct2.go +++ b/.example/util/gvalid/gvalid_struct2.go @@ -1,6 +1,7 @@ package main import ( + "context" "github.com/gogf/gf/frame/g" "github.com/gogf/gf/util/gvalid" ) @@ -13,5 +14,5 @@ func main() { user := &User{} - g.Dump(gvalid.CheckStruct(user, nil)) + g.Dump(gvalid.CheckStruct(context.TODO(), user, nil)) } diff --git a/.example/util/gvalid/gvalid_struct3.go b/.example/util/gvalid/gvalid_struct3.go index de9980ca3..fdd0ea965 100644 --- a/.example/util/gvalid/gvalid_struct3.go +++ b/.example/util/gvalid/gvalid_struct3.go @@ -1,6 +1,7 @@ package main import ( + "context" "github.com/gogf/gf/frame/g" "github.com/gogf/gf/util/gvalid" ) @@ -15,5 +16,5 @@ func main() { Pass: "1", } - g.Dump(gvalid.CheckStruct(user, nil).Maps()) + g.Dump(gvalid.CheckStruct(context.TODO(), user, nil).Maps()) } diff --git a/i18n/gi18n/gi18n_ctx.go b/i18n/gi18n/gi18n_ctx.go index 4a9bc06ce..a2c7293a7 100644 --- a/i18n/gi18n/gi18n_ctx.go +++ b/i18n/gi18n/gi18n_ctx.go @@ -15,12 +15,18 @@ const ( // WithLanguage append language setting to the context and returns a new context. func WithLanguage(ctx context.Context, language string) context.Context { + if ctx == nil { + ctx = context.TODO() + } return context.WithValue(ctx, ctxLanguage, language) } // LanguageFromCtx retrieves and returns language name from context. // It returns an empty string if it is not set previously. func LanguageFromCtx(ctx context.Context) string { + if ctx == nil { + return "" + } v := ctx.Value(ctxLanguage) if v != nil { return v.(string) diff --git a/net/ghttp/ghttp_request_param.go b/net/ghttp/ghttp_request_param.go index 991967df1..c9bfdcbec 100644 --- a/net/ghttp/ghttp_request_param.go +++ b/net/ghttp/ghttp_request_param.go @@ -102,7 +102,7 @@ func (r *Request) doParse(pointer interface{}, requestType int) error { } } // Validation. - if err := gvalid.CheckStructWithParamMap(pointer, data, nil); err != nil { + if err := gvalid.CheckStructWithParamMap(r.Context(), pointer, data, nil); err != nil { return err } @@ -120,6 +120,7 @@ func (r *Request) doParse(pointer interface{}, requestType int) error { } for i := 0; i < reflectVal2.Len(); i++ { if err := gvalid.CheckStructWithParamMap( + r.Context(), reflectVal2.Index(i), j.GetMap(gconv.String(i)), nil, diff --git a/net/ghttp/ghttp_unit_request_struct_test.go b/net/ghttp/ghttp_unit_request_struct_test.go index 687c936c4..5f4601744 100644 --- a/net/ghttp/ghttp_unit_request_struct_test.go +++ b/net/ghttp/ghttp_unit_request_struct_test.go @@ -425,7 +425,7 @@ func Test_Params_Struct(t *testing.T) { if err := r.GetStruct(user); err != nil { r.Response.WriteExit(err) } - if err := gvalid.CheckStruct(user, nil); err != nil { + if err := gvalid.CheckStruct(r.Context(), user, nil); err != nil { r.Response.WriteExit(err) } } diff --git a/util/gvalid/gvalid.go b/util/gvalid/gvalid.go index 0b9e18d7e..4a660a954 100644 --- a/util/gvalid/gvalid.go +++ b/util/gvalid/gvalid.go @@ -8,6 +8,7 @@ package gvalid import ( + "context" "regexp" "strings" @@ -170,8 +171,8 @@ var ( // string/map/struct/*struct. // The optional parameter `params` specifies the extra validation parameters for some rules // like: required-*、same、different, etc. -func Check(value interface{}, rules string, messages interface{}, params ...interface{}) *Error { - return defaultValidator.Check(value, rules, messages, params...) +func Check(ctx context.Context, value interface{}, rules string, messages interface{}, params ...interface{}) *Error { + return defaultValidator.Ctx(ctx).Check(value, rules, messages, params...) } // CheckMap validates map and returns the error result. It returns nil if with successful validation. @@ -179,8 +180,8 @@ func Check(value interface{}, rules string, messages interface{}, params ...inte // The parameter `rules` can be type of []string/map[string]string. It supports sequence in error result // if `rules` is type of []string. // The optional parameter `messages` specifies the custom error messages for specified keys and rules. -func CheckMap(params interface{}, rules interface{}, messages ...CustomMsg) *Error { - return defaultValidator.CheckMap(params, rules, messages...) +func CheckMap(ctx context.Context, params interface{}, rules interface{}, messages ...CustomMsg) *Error { + return defaultValidator.Ctx(ctx).CheckMap(params, rules, messages...) } // CheckStruct validates strcut and returns the error result. @@ -189,8 +190,8 @@ func CheckMap(params interface{}, rules interface{}, messages ...CustomMsg) *Err // The parameter `rules` can be type of []string/map[string]string. It supports sequence in error result // if `rules` is type of []string. // The optional parameter `messages` specifies the custom error messages for specified keys and rules. -func CheckStruct(object interface{}, rules interface{}, messages ...CustomMsg) *Error { - return defaultValidator.CheckStruct(object, rules, messages...) +func CheckStruct(ctx context.Context, object interface{}, rules interface{}, messages ...CustomMsg) *Error { + return defaultValidator.Ctx(ctx).CheckStruct(object, rules, messages...) } // CheckStructWithParamMap validates struct with given parameter map and returns the error result. @@ -199,8 +200,8 @@ func CheckStruct(object interface{}, rules interface{}, messages ...CustomMsg) * // The parameter `rules` can be type of []string/map[string]string. It supports sequence in error result // if `rules` is type of []string. // The optional parameter `messages` specifies the custom error messages for specified keys and rules. -func CheckStructWithParamMap(object interface{}, paramMap interface{}, rules interface{}, messages ...CustomMsg) *Error { - return defaultValidator.CheckStructWithParamMap(object, paramMap, rules, messages...) +func CheckStructWithParamMap(ctx context.Context, object interface{}, paramMap interface{}, rules interface{}, messages ...CustomMsg) *Error { + return defaultValidator.Ctx(ctx).CheckStructWithParamMap(object, paramMap, rules, messages...) } // parseSequenceTag parses one sequence tag to field, rule and error message. diff --git a/util/gvalid/gvalid_validator.go b/util/gvalid/gvalid_validator.go index 5f516a2c6..de6ace5f1 100644 --- a/util/gvalid/gvalid_validator.go +++ b/util/gvalid/gvalid_validator.go @@ -21,8 +21,8 @@ type Validator struct { // New creates and returns a new Validator. func New() *Validator { return &Validator{ - ctx: context.TODO(), - i18nManager: gi18n.Instance(), + ctx: context.TODO(), // Initialize an empty context. + i18nManager: gi18n.Instance(), // Use default i18n manager. } } diff --git a/util/gvalid/gvalid_z_example_test.go b/util/gvalid/gvalid_z_example_test.go index 2fa6cedfa..fac360194 100644 --- a/util/gvalid/gvalid_z_example_test.go +++ b/util/gvalid/gvalid_z_example_test.go @@ -7,6 +7,7 @@ package gvalid_test import ( + "context" "errors" "fmt" "github.com/gogf/gf/container/gvar" @@ -28,7 +29,7 @@ func ExampleCheckMap() { "password@required|length:6,16|same:password2#密码不能为空|密码长度应当在:min到:max之间|两次密码输入不相等", "password2@required|length:6,16#", } - if e := gvalid.CheckMap(params, rules); e != nil { + if e := gvalid.CheckMap(context.TODO(), params, rules); e != nil { fmt.Println(e.Map()) fmt.Println(e.FirstItem()) fmt.Println(e.FirstString()) @@ -50,7 +51,7 @@ func ExampleCheckMap2() { "password@required|length:6,16|same:password2#密码不能为空|密码长度应当在:min到:max之间|两次密码输入不相等", "password2@required|length:6,16#", } - if e := gvalid.CheckMap(params, rules); e != nil { + if e := gvalid.CheckMap(context.TODO(), params, rules); e != nil { fmt.Println(e.Map()) fmt.Println(e.FirstItem()) fmt.Println(e.FirstString()) @@ -72,7 +73,7 @@ func ExampleCheckStruct() { Page: 1, Size: 10, } - err := gvalid.CheckStruct(obj, nil) + err := gvalid.CheckStruct(context.TODO(), obj, nil) fmt.Println(err == nil) // Output: // true @@ -89,7 +90,7 @@ func ExampleCheckStruct2() { Page: 1, Size: 10, } - err := gvalid.CheckStruct(obj, nil) + err := gvalid.CheckStruct(context.TODO(), obj, nil) fmt.Println(err == nil) // Output: // true @@ -106,7 +107,7 @@ func ExampleCheckStruct3() { Page: 1, Size: 10, } - err := gvalid.CheckStruct(obj, nil) + err := gvalid.CheckStruct(context.TODO(), obj, nil) fmt.Println(err) // Output: // project id must between 1, 10000 @@ -138,7 +139,7 @@ func ExampleRegisterRule() { Name: "john", Pass: "123456", } - err := gvalid.CheckStruct(user, nil) + err := gvalid.CheckStruct(context.TODO(), user, nil) fmt.Println(err.Error()) // May Output: // 用户名称已被占用 @@ -172,14 +173,14 @@ func ExampleRegisterRule_OverwriteRequired() { } return nil }) - fmt.Println(gvalid.Check("", "required", "It's required")) - fmt.Println(gvalid.Check(0, "required", "It's required")) - fmt.Println(gvalid.Check(false, "required", "It's required")) + fmt.Println(gvalid.Check(context.TODO(), "", "required", "It's required")) + fmt.Println(gvalid.Check(context.TODO(), 0, "required", "It's required")) + fmt.Println(gvalid.Check(context.TODO(), false, "required", "It's required")) gvalid.DeleteRule(rule) fmt.Println("rule deleted") - fmt.Println(gvalid.Check("", "required", "It's required")) - fmt.Println(gvalid.Check(0, "required", "It's required")) - fmt.Println(gvalid.Check(false, "required", "It's required")) + fmt.Println(gvalid.Check(context.TODO(), "", "required", "It's required")) + fmt.Println(gvalid.Check(context.TODO(), 0, "required", "It's required")) + fmt.Println(gvalid.Check(context.TODO(), false, "required", "It's required")) // Output: // It's required // It's required diff --git a/util/gvalid/gvalid_z_unit_basic_all_test.go b/util/gvalid/gvalid_z_unit_basic_all_test.go index bfde55995..55e7687ab 100755 --- a/util/gvalid/gvalid_z_unit_basic_all_test.go +++ b/util/gvalid/gvalid_z_unit_basic_all_test.go @@ -7,6 +7,7 @@ package gvalid_test import ( + "context" "github.com/gogf/gf/errors/gerror" "github.com/gogf/gf/os/gtime" "testing" @@ -23,9 +24,9 @@ func Test_Check(t *testing.T) { val1 := 0 val2 := 7 val3 := 20 - err1 := gvalid.Check(val1, rule, nil) - err2 := gvalid.Check(val2, rule, nil) - err3 := gvalid.Check(val3, rule, nil) + err1 := gvalid.Check(context.TODO(), val1, rule, nil) + err2 := gvalid.Check(context.TODO(), val2, rule, nil) + err3 := gvalid.Check(context.TODO(), val3, rule, nil) t.Assert(err1, "invalid_rules: abc:6,16") t.Assert(err2, "invalid_rules: abc:6,16") t.Assert(err3, "invalid_rules: abc:6,16") @@ -33,16 +34,16 @@ func Test_Check(t *testing.T) { } func Test_Required(t *testing.T) { - if m := gvalid.Check("1", "required", nil); m != nil { + if m := gvalid.Check(context.TODO(), "1", "required", nil); m != nil { t.Error(m) } - if m := gvalid.Check("", "required", nil); m == nil { + if m := gvalid.Check(context.TODO(), "", "required", nil); m == nil { t.Error(m) } - if m := gvalid.Check("", "required-if: id,1,age,18", nil, map[string]interface{}{"id": 1, "age": 19}); m == nil { + if m := gvalid.Check(context.TODO(), "", "required-if: id,1,age,18", nil, map[string]interface{}{"id": 1, "age": 19}); m == nil { t.Error("Required校验失败") } - if m := gvalid.Check("", "required-if: id,1,age,18", nil, map[string]interface{}{"id": 2, "age": 19}); m != nil { + if m := gvalid.Check(context.TODO(), "", "required-if: id,1,age,18", nil, map[string]interface{}{"id": 2, "age": 19}); m != nil { t.Error("Required校验失败") } } @@ -50,20 +51,20 @@ func Test_Required(t *testing.T) { func Test_RequiredIf(t *testing.T) { gtest.C(t, func(t *gtest.T) { rule := "required-if:id,1,age,18" - t.AssertNE(gvalid.Check("", rule, nil, g.Map{"id": 1}), nil) - t.Assert(gvalid.Check("", rule, nil, g.Map{"id": 0}), nil) - t.AssertNE(gvalid.Check("", rule, nil, g.Map{"age": 18}), nil) - t.Assert(gvalid.Check("", rule, nil, g.Map{"age": 20}), nil) + t.AssertNE(gvalid.Check(context.TODO(), "", rule, nil, g.Map{"id": 1}), nil) + t.Assert(gvalid.Check(context.TODO(), "", rule, nil, g.Map{"id": 0}), nil) + t.AssertNE(gvalid.Check(context.TODO(), "", rule, nil, g.Map{"age": 18}), nil) + t.Assert(gvalid.Check(context.TODO(), "", rule, nil, g.Map{"age": 20}), nil) }) } func Test_RequiredUnless(t *testing.T) { gtest.C(t, func(t *gtest.T) { rule := "required-unless:id,1,age,18" - t.Assert(gvalid.Check("", rule, nil, g.Map{"id": 1}), nil) - t.AssertNE(gvalid.Check("", rule, nil, g.Map{"id": 0}), nil) - t.Assert(gvalid.Check("", rule, nil, g.Map{"age": 18}), nil) - t.AssertNE(gvalid.Check("", rule, nil, g.Map{"age": 20}), nil) + t.Assert(gvalid.Check(context.TODO(), "", rule, nil, g.Map{"id": 1}), nil) + t.AssertNE(gvalid.Check(context.TODO(), "", rule, nil, g.Map{"id": 0}), nil) + t.Assert(gvalid.Check(context.TODO(), "", rule, nil, g.Map{"age": 18}), nil) + t.AssertNE(gvalid.Check(context.TODO(), "", rule, nil, g.Map{"age": 20}), nil) }) } @@ -81,9 +82,9 @@ func Test_RequiredWith(t *testing.T) { "id": 100, "name": "john", } - err1 := gvalid.Check(val1, rule, nil, params1) - err2 := gvalid.Check(val1, rule, nil, params2) - err3 := gvalid.Check(val1, rule, nil, params3) + err1 := gvalid.Check(context.TODO(), val1, rule, nil, params1) + err2 := gvalid.Check(context.TODO(), val1, rule, nil, params2) + err3 := gvalid.Check(context.TODO(), val1, rule, nil, params3) t.Assert(err1, nil) t.AssertNE(err2, nil) t.AssertNE(err3, nil) @@ -101,9 +102,9 @@ func Test_RequiredWith(t *testing.T) { params3 := g.Map{ "time": time.Time{}, } - err1 := gvalid.Check(val1, rule, nil, params1) - err2 := gvalid.Check(val1, rule, nil, params2) - err3 := gvalid.Check(val1, rule, nil, params3) + err1 := gvalid.Check(context.TODO(), val1, rule, nil, params1) + err2 := gvalid.Check(context.TODO(), val1, rule, nil, params2) + err3 := gvalid.Check(context.TODO(), val1, rule, nil, params3) t.Assert(err1, nil) t.AssertNE(err2, nil) t.Assert(err3, nil) @@ -120,9 +121,9 @@ func Test_RequiredWith(t *testing.T) { params3 := g.Map{ "time": time.Now(), } - err1 := gvalid.Check(val1, rule, nil, params1) - err2 := gvalid.Check(val1, rule, nil, params2) - err3 := gvalid.Check(val1, rule, nil, params3) + err1 := gvalid.Check(context.TODO(), val1, rule, nil, params1) + err2 := gvalid.Check(context.TODO(), val1, rule, nil, params2) + err3 := gvalid.Check(context.TODO(), val1, rule, nil, params3) t.Assert(err1, nil) t.AssertNE(err2, nil) t.AssertNE(err3, nil) @@ -139,7 +140,7 @@ func Test_RequiredWith(t *testing.T) { StartTime: nil, EndTime: nil, } - t.Assert(gvalid.CheckStruct(data, nil), nil) + t.Assert(gvalid.CheckStruct(context.TODO(), data, nil), nil) }) gtest.C(t, func(t *gtest.T) { type UserApiSearch struct { @@ -152,7 +153,7 @@ func Test_RequiredWith(t *testing.T) { StartTime: nil, EndTime: gtime.Now(), } - t.AssertNE(gvalid.CheckStruct(data, nil), nil) + t.AssertNE(gvalid.CheckStruct(context.TODO(), data, nil), nil) }) } @@ -170,9 +171,9 @@ func Test_RequiredWithAll(t *testing.T) { "id": 100, "name": "john", } - err1 := gvalid.Check(val1, rule, nil, params1) - err2 := gvalid.Check(val1, rule, nil, params2) - err3 := gvalid.Check(val1, rule, nil, params3) + err1 := gvalid.Check(context.TODO(), val1, rule, nil, params1) + err2 := gvalid.Check(context.TODO(), val1, rule, nil, params2) + err3 := gvalid.Check(context.TODO(), val1, rule, nil, params3) t.Assert(err1, nil) t.Assert(err2, nil) t.AssertNE(err3, nil) @@ -193,9 +194,9 @@ func Test_RequiredWithOut(t *testing.T) { "id": 100, "name": "john", } - err1 := gvalid.Check(val1, rule, nil, params1) - err2 := gvalid.Check(val1, rule, nil, params2) - err3 := gvalid.Check(val1, rule, nil, params3) + err1 := gvalid.Check(context.TODO(), val1, rule, nil, params1) + err2 := gvalid.Check(context.TODO(), val1, rule, nil, params2) + err3 := gvalid.Check(context.TODO(), val1, rule, nil, params3) t.AssertNE(err1, nil) t.AssertNE(err2, nil) t.Assert(err3, nil) @@ -216,9 +217,9 @@ func Test_RequiredWithOutAll(t *testing.T) { "id": 100, "name": "john", } - err1 := gvalid.Check(val1, rule, nil, params1) - err2 := gvalid.Check(val1, rule, nil, params2) - err3 := gvalid.Check(val1, rule, nil, params3) + err1 := gvalid.Check(context.TODO(), val1, rule, nil, params1) + err2 := gvalid.Check(context.TODO(), val1, rule, nil, params2) + err3 := gvalid.Check(context.TODO(), val1, rule, nil, params3) t.AssertNE(err1, nil) t.Assert(err2, nil) t.Assert(err3, nil) @@ -235,13 +236,13 @@ func Test_Date(t *testing.T) { val5 := "2010.11.01" val6 := "2010/11/01" val7 := "2010=11=01" - err1 := gvalid.Check(val1, rule, nil) - err2 := gvalid.Check(val2, rule, nil) - err3 := gvalid.Check(val3, rule, nil) - err4 := gvalid.Check(val4, rule, nil) - err5 := gvalid.Check(val5, rule, nil) - err6 := gvalid.Check(val6, rule, nil) - err7 := gvalid.Check(val7, rule, nil) + err1 := gvalid.Check(context.TODO(), val1, rule, nil) + err2 := gvalid.Check(context.TODO(), val2, rule, nil) + err3 := gvalid.Check(context.TODO(), val3, rule, nil) + err4 := gvalid.Check(context.TODO(), val4, rule, nil) + err5 := gvalid.Check(context.TODO(), val5, rule, nil) + err6 := gvalid.Check(context.TODO(), val6, rule, nil) + err7 := gvalid.Check(context.TODO(), val7, rule, nil) t.Assert(err1, nil) t.Assert(err2, nil) t.Assert(err3, nil) @@ -260,12 +261,12 @@ func Test_DateFormat(t *testing.T) { val4 := "201011-01" val5 := "2010~11~01" val6 := "2010-11~01" - err1 := gvalid.Check(val1, "date-format:Y", nil) - err2 := gvalid.Check(val2, "date-format:Ym", nil) - err3 := gvalid.Check(val3, "date-format:Y.m", nil) - err4 := gvalid.Check(val4, "date-format:Ym-d", nil) - err5 := gvalid.Check(val5, "date-format:Y~m~d", nil) - err6 := gvalid.Check(val6, "date-format:Y~m~d", nil) + err1 := gvalid.Check(context.TODO(), val1, "date-format:Y", nil) + err2 := gvalid.Check(context.TODO(), val2, "date-format:Ym", nil) + err3 := gvalid.Check(context.TODO(), val3, "date-format:Y.m", nil) + err4 := gvalid.Check(context.TODO(), val4, "date-format:Ym-d", nil) + err5 := gvalid.Check(context.TODO(), val5, "date-format:Y~m~d", nil) + err6 := gvalid.Check(context.TODO(), val6, "date-format:Y~m~d", nil) t.Assert(err1, nil) t.Assert(err2, nil) t.Assert(err3, nil) @@ -276,8 +277,8 @@ func Test_DateFormat(t *testing.T) { gtest.C(t, func(t *gtest.T) { t1 := gtime.Now() t2 := time.Time{} - err1 := gvalid.Check(t1, "date-format:Y", nil) - err2 := gvalid.Check(t2, "date-format:Y", nil) + err1 := gvalid.Check(context.TODO(), t1, "date-format:Y", nil) + err2 := gvalid.Check(context.TODO(), t2, "date-format:Y", nil) t.Assert(err1, nil) t.AssertNE(err2, nil) }) @@ -290,10 +291,10 @@ func Test_Email(t *testing.T) { value2 := "m@www@johngcn" value3 := "m-m_m@mail.johng.cn" value4 := "m.m-m@johng.cn" - err1 := gvalid.Check(value1, rule, nil) - err2 := gvalid.Check(value2, rule, nil) - err3 := gvalid.Check(value3, rule, nil) - err4 := gvalid.Check(value4, rule, nil) + err1 := gvalid.Check(context.TODO(), value1, rule, nil) + err2 := gvalid.Check(context.TODO(), value2, rule, nil) + err3 := gvalid.Check(context.TODO(), value3, rule, nil) + err4 := gvalid.Check(context.TODO(), value4, rule, nil) t.AssertNE(err1, nil) t.AssertNE(err2, nil) t.Assert(err3, nil) @@ -303,10 +304,10 @@ func Test_Email(t *testing.T) { func Test_Phone(t *testing.T) { gtest.C(t, func(t *gtest.T) { - err1 := gvalid.Check("1361990897", "phone", nil) - err2 := gvalid.Check("13619908979", "phone", nil) - err3 := gvalid.Check("16719908979", "phone", nil) - err4 := gvalid.Check("19719908989", "phone", nil) + err1 := gvalid.Check(context.TODO(), "1361990897", "phone", nil) + err2 := gvalid.Check(context.TODO(), "13619908979", "phone", nil) + err3 := gvalid.Check(context.TODO(), "16719908979", "phone", nil) + err4 := gvalid.Check(context.TODO(), "19719908989", "phone", nil) t.AssertNE(err1.String(), nil) t.Assert(err2, nil) t.Assert(err3, nil) @@ -316,12 +317,12 @@ func Test_Phone(t *testing.T) { func Test_PhoneLoose(t *testing.T) { gtest.C(t, func(t *gtest.T) { - err1 := gvalid.Check("13333333333", "phone-loose", nil) - err2 := gvalid.Check("15555555555", "phone-loose", nil) - err3 := gvalid.Check("16666666666", "phone-loose", nil) - err4 := gvalid.Check("23333333333", "phone-loose", nil) - err5 := gvalid.Check("1333333333", "phone-loose", nil) - err6 := gvalid.Check("10333333333", "phone-loose", nil) + err1 := gvalid.Check(context.TODO(), "13333333333", "phone-loose", nil) + err2 := gvalid.Check(context.TODO(), "15555555555", "phone-loose", nil) + err3 := gvalid.Check(context.TODO(), "16666666666", "phone-loose", nil) + err4 := gvalid.Check(context.TODO(), "23333333333", "phone-loose", nil) + err5 := gvalid.Check(context.TODO(), "1333333333", "phone-loose", nil) + err6 := gvalid.Check(context.TODO(), "10333333333", "phone-loose", nil) t.Assert(err1, nil) t.Assert(err2, nil) t.Assert(err3, nil) @@ -338,11 +339,11 @@ func Test_Telephone(t *testing.T) { val3 := "86292651" val4 := "028-8692651" val5 := "0830-8692651" - err1 := gvalid.Check(val1, rule, nil) - err2 := gvalid.Check(val2, rule, nil) - err3 := gvalid.Check(val3, rule, nil) - err4 := gvalid.Check(val4, rule, nil) - err5 := gvalid.Check(val5, rule, nil) + err1 := gvalid.Check(context.TODO(), val1, rule, nil) + err2 := gvalid.Check(context.TODO(), val2, rule, nil) + err3 := gvalid.Check(context.TODO(), val3, rule, nil) + err4 := gvalid.Check(context.TODO(), val4, rule, nil) + err5 := gvalid.Check(context.TODO(), val5, rule, nil) t.AssertNE(err1, nil) t.AssertNE(err2, nil) t.Assert(err3, nil) @@ -359,11 +360,11 @@ func Test_Passport(t *testing.T) { val3 := "aaaaa" val4 := "aaaaaa" val5 := "a123_456" - err1 := gvalid.Check(val1, rule, nil) - err2 := gvalid.Check(val2, rule, nil) - err3 := gvalid.Check(val3, rule, nil) - err4 := gvalid.Check(val4, rule, nil) - err5 := gvalid.Check(val5, rule, nil) + err1 := gvalid.Check(context.TODO(), val1, rule, nil) + err2 := gvalid.Check(context.TODO(), val2, rule, nil) + err3 := gvalid.Check(context.TODO(), val3, rule, nil) + err4 := gvalid.Check(context.TODO(), val4, rule, nil) + err5 := gvalid.Check(context.TODO(), val5, rule, nil) t.AssertNE(err1, nil) t.AssertNE(err2, nil) t.AssertNE(err3, nil) @@ -380,11 +381,11 @@ func Test_Password(t *testing.T) { val3 := "a12345-6" val4 := ">,/;'[09-" val5 := "a123_456" - err1 := gvalid.Check(val1, rule, nil) - err2 := gvalid.Check(val2, rule, nil) - err3 := gvalid.Check(val3, rule, nil) - err4 := gvalid.Check(val4, rule, nil) - err5 := gvalid.Check(val5, rule, nil) + err1 := gvalid.Check(context.TODO(), val1, rule, nil) + err2 := gvalid.Check(context.TODO(), val2, rule, nil) + err3 := gvalid.Check(context.TODO(), val3, rule, nil) + err4 := gvalid.Check(context.TODO(), val4, rule, nil) + err5 := gvalid.Check(context.TODO(), val5, rule, nil) t.AssertNE(err1, nil) t.AssertNE(err2, nil) t.Assert(err3, nil) @@ -403,13 +404,13 @@ func Test_Password2(t *testing.T) { val5 := "a123_456" val6 := "Nant1986" val7 := "Nant1986!" - err1 := gvalid.Check(val1, rule, nil) - err2 := gvalid.Check(val2, rule, nil) - err3 := gvalid.Check(val3, rule, nil) - err4 := gvalid.Check(val4, rule, nil) - err5 := gvalid.Check(val5, rule, nil) - err6 := gvalid.Check(val6, rule, nil) - err7 := gvalid.Check(val7, rule, nil) + err1 := gvalid.Check(context.TODO(), val1, rule, nil) + err2 := gvalid.Check(context.TODO(), val2, rule, nil) + err3 := gvalid.Check(context.TODO(), val3, rule, nil) + err4 := gvalid.Check(context.TODO(), val4, rule, nil) + err5 := gvalid.Check(context.TODO(), val5, rule, nil) + err6 := gvalid.Check(context.TODO(), val6, rule, nil) + err7 := gvalid.Check(context.TODO(), val7, rule, nil) t.AssertNE(err1, nil) t.AssertNE(err2, nil) t.AssertNE(err3, nil) @@ -430,13 +431,13 @@ func Test_Password3(t *testing.T) { val5 := "a123_456" val6 := "Nant1986" val7 := "Nant1986!" - err1 := gvalid.Check(val1, rule, nil) - err2 := gvalid.Check(val2, rule, nil) - err3 := gvalid.Check(val3, rule, nil) - err4 := gvalid.Check(val4, rule, nil) - err5 := gvalid.Check(val5, rule, nil) - err6 := gvalid.Check(val6, rule, nil) - err7 := gvalid.Check(val7, rule, nil) + err1 := gvalid.Check(context.TODO(), val1, rule, nil) + err2 := gvalid.Check(context.TODO(), val2, rule, nil) + err3 := gvalid.Check(context.TODO(), val3, rule, nil) + err4 := gvalid.Check(context.TODO(), val4, rule, nil) + err5 := gvalid.Check(context.TODO(), val5, rule, nil) + err6 := gvalid.Check(context.TODO(), val6, rule, nil) + err7 := gvalid.Check(context.TODO(), val7, rule, nil) t.AssertNE(err1, nil) t.AssertNE(err2, nil) t.AssertNE(err3, nil) @@ -452,8 +453,8 @@ func Test_Postcode(t *testing.T) { rule := "postcode" val1 := "12345" val2 := "610036" - err1 := gvalid.Check(val1, rule, nil) - err2 := gvalid.Check(val2, rule, nil) + err1 := gvalid.Check(context.TODO(), val1, rule, nil) + err2 := gvalid.Check(context.TODO(), val2, rule, nil) t.AssertNE(err1, nil) t.Assert(err2, nil) }) @@ -467,11 +468,11 @@ func Test_ResidentId(t *testing.T) { val3 := "311128500121201" val4 := "510521198607185367" val5 := "51052119860718536x" - err1 := gvalid.Check(val1, rule, nil) - err2 := gvalid.Check(val2, rule, nil) - err3 := gvalid.Check(val3, rule, nil) - err4 := gvalid.Check(val4, rule, nil) - err5 := gvalid.Check(val5, rule, nil) + err1 := gvalid.Check(context.TODO(), val1, rule, nil) + err2 := gvalid.Check(context.TODO(), val2, rule, nil) + err3 := gvalid.Check(context.TODO(), val3, rule, nil) + err4 := gvalid.Check(context.TODO(), val4, rule, nil) + err5 := gvalid.Check(context.TODO(), val5, rule, nil) t.AssertNE(err1, nil) t.AssertNE(err2, nil) t.AssertNE(err3, nil) @@ -485,8 +486,8 @@ func Test_BankCard(t *testing.T) { rule := "bank-card" val1 := "6230514630000424470" val2 := "6230514630000424473" - err1 := gvalid.Check(val1, rule, nil) - err2 := gvalid.Check(val2, rule, nil) + err1 := gvalid.Check(context.TODO(), val1, rule, nil) + err2 := gvalid.Check(context.TODO(), val2, rule, nil) t.AssertNE(err1, nil) t.Assert(err2, nil) }) @@ -500,11 +501,11 @@ func Test_QQ(t *testing.T) { val3 := "10000" val4 := "38996181" val5 := "389961817" - err1 := gvalid.Check(val1, rule, nil) - err2 := gvalid.Check(val2, rule, nil) - err3 := gvalid.Check(val3, rule, nil) - err4 := gvalid.Check(val4, rule, nil) - err5 := gvalid.Check(val5, rule, nil) + err1 := gvalid.Check(context.TODO(), val1, rule, nil) + err2 := gvalid.Check(context.TODO(), val2, rule, nil) + err3 := gvalid.Check(context.TODO(), val3, rule, nil) + err4 := gvalid.Check(context.TODO(), val4, rule, nil) + err5 := gvalid.Check(context.TODO(), val5, rule, nil) t.AssertNE(err1, nil) t.AssertNE(err2, nil) t.Assert(err3, nil) @@ -514,31 +515,31 @@ func Test_QQ(t *testing.T) { } func Test_Ip(t *testing.T) { - if m := gvalid.Check("10.0.0.1", "ip", nil); m != nil { + if m := gvalid.Check(context.TODO(), "10.0.0.1", "ip", nil); m != nil { t.Error(m) } - if m := gvalid.Check("10.0.0.1", "ipv4", nil); m != nil { + if m := gvalid.Check(context.TODO(), "10.0.0.1", "ipv4", nil); m != nil { t.Error(m) } - if m := gvalid.Check("0.0.0.0", "ipv4", nil); m != nil { + if m := gvalid.Check(context.TODO(), "0.0.0.0", "ipv4", nil); m != nil { t.Error(m) } - if m := gvalid.Check("1920.0.0.0", "ipv4", nil); m == nil { + if m := gvalid.Check(context.TODO(), "1920.0.0.0", "ipv4", nil); m == nil { t.Error("ipv4校验失败") } - if m := gvalid.Check("1920.0.0.0", "ip", nil); m == nil { + if m := gvalid.Check(context.TODO(), "1920.0.0.0", "ip", nil); m == nil { t.Error("ipv4校验失败") } - if m := gvalid.Check("fe80::5484:7aff:fefe:9799", "ipv6", nil); m != nil { + if m := gvalid.Check(context.TODO(), "fe80::5484:7aff:fefe:9799", "ipv6", nil); m != nil { t.Error(m) } - if m := gvalid.Check("fe80::5484:7aff:fefe:9799123", "ipv6", nil); m == nil { + if m := gvalid.Check(context.TODO(), "fe80::5484:7aff:fefe:9799123", "ipv6", nil); m == nil { t.Error(m) } - if m := gvalid.Check("fe80::5484:7aff:fefe:9799", "ip", nil); m != nil { + if m := gvalid.Check(context.TODO(), "fe80::5484:7aff:fefe:9799", "ip", nil); m != nil { t.Error(m) } - if m := gvalid.Check("fe80::5484:7aff:fefe:9799123", "ip", nil); m == nil { + if m := gvalid.Check(context.TODO(), "fe80::5484:7aff:fefe:9799123", "ip", nil); m == nil { t.Error(m) } } @@ -551,11 +552,11 @@ func Test_IPv4(t *testing.T) { val3 := "1.1.1.1" val4 := "255.255.255.0" val5 := "127.0.0.1" - err1 := gvalid.Check(val1, rule, nil) - err2 := gvalid.Check(val2, rule, nil) - err3 := gvalid.Check(val3, rule, nil) - err4 := gvalid.Check(val4, rule, nil) - err5 := gvalid.Check(val5, rule, nil) + err1 := gvalid.Check(context.TODO(), val1, rule, nil) + err2 := gvalid.Check(context.TODO(), val2, rule, nil) + err3 := gvalid.Check(context.TODO(), val3, rule, nil) + err4 := gvalid.Check(context.TODO(), val4, rule, nil) + err5 := gvalid.Check(context.TODO(), val5, rule, nil) t.AssertNE(err1, nil) t.Assert(err2, nil) t.Assert(err3, nil) @@ -572,11 +573,11 @@ func Test_IPv6(t *testing.T) { val3 := "1030::C9B4:FF12:48AA:1A2B" val4 := "2000:0:0:0:0:0:0:1" val5 := "0000:0000:0000:0000:0000:ffff:c0a8:5909" - err1 := gvalid.Check(val1, rule, nil) - err2 := gvalid.Check(val2, rule, nil) - err3 := gvalid.Check(val3, rule, nil) - err4 := gvalid.Check(val4, rule, nil) - err5 := gvalid.Check(val5, rule, nil) + err1 := gvalid.Check(context.TODO(), val1, rule, nil) + err2 := gvalid.Check(context.TODO(), val2, rule, nil) + err3 := gvalid.Check(context.TODO(), val3, rule, nil) + err4 := gvalid.Check(context.TODO(), val4, rule, nil) + err5 := gvalid.Check(context.TODO(), val5, rule, nil) t.AssertNE(err1, nil) t.Assert(err2, nil) t.Assert(err3, nil) @@ -591,9 +592,9 @@ func Test_MAC(t *testing.T) { val1 := "192.168.1.1" val2 := "44-45-53-54-00-00" val3 := "01:00:5e:00:00:00" - err1 := gvalid.Check(val1, rule, nil) - err2 := gvalid.Check(val2, rule, nil) - err3 := gvalid.Check(val3, rule, nil) + err1 := gvalid.Check(context.TODO(), val1, rule, nil) + err2 := gvalid.Check(context.TODO(), val2, rule, nil) + err3 := gvalid.Check(context.TODO(), val3, rule, nil) t.AssertNE(err1, nil) t.Assert(err2, nil) t.Assert(err3, nil) @@ -607,10 +608,10 @@ func Test_URL(t *testing.T) { val2 := "https://www.baidu.com" val3 := "http://127.0.0.1" val4 := "file:///tmp/test.txt" - err1 := gvalid.Check(val1, rule, nil) - err2 := gvalid.Check(val2, rule, nil) - err3 := gvalid.Check(val3, rule, nil) - err4 := gvalid.Check(val4, rule, nil) + err1 := gvalid.Check(context.TODO(), val1, rule, nil) + err2 := gvalid.Check(context.TODO(), val2, rule, nil) + err3 := gvalid.Check(context.TODO(), val3, rule, nil) + err4 := gvalid.Check(context.TODO(), val4, rule, nil) t.AssertNE(err1, nil) t.Assert(err2, nil) t.Assert(err3, nil) @@ -637,7 +638,7 @@ func Test_Domain(t *testing.T) { } var err error for k, v := range m { - err = gvalid.Check(k, "domain", nil) + err = gvalid.Check(context.TODO(), k, "domain", nil) if v { //fmt.Println(k) t.Assert(err, nil) @@ -651,10 +652,10 @@ func Test_Domain(t *testing.T) { func Test_Length(t *testing.T) { rule := "length:6,16" - if m := gvalid.Check("123456", rule, nil); m != nil { + if m := gvalid.Check(context.TODO(), "123456", rule, nil); m != nil { t.Error(m) } - if m := gvalid.Check("12345", rule, nil); m == nil { + if m := gvalid.Check(context.TODO(), "12345", rule, nil); m == nil { t.Error("长度校验失败") } } @@ -664,18 +665,18 @@ func Test_MinLength(t *testing.T) { msgs := map[string]string{ "min-length": "地址长度至少为:min位", } - if m := gvalid.Check("123456", rule, nil); m != nil { + if m := gvalid.Check(context.TODO(), "123456", rule, nil); m != nil { t.Error(m) } - if m := gvalid.Check("12345", rule, nil); m == nil { + if m := gvalid.Check(context.TODO(), "12345", rule, nil); m == nil { t.Error("长度校验失败") } - if m := gvalid.Check("12345", rule, msgs); m == nil { + if m := gvalid.Check(context.TODO(), "12345", rule, msgs); m == nil { t.Error("长度校验失败") } rule2 := "min-length:abc" - if m := gvalid.Check("123456", rule2, nil); m == nil { + if m := gvalid.Check(context.TODO(), "123456", rule2, nil); m == nil { t.Error("长度校验失败") } } @@ -685,31 +686,31 @@ func Test_MaxLength(t *testing.T) { msgs := map[string]string{ "max-length": "地址长度至大为:max位", } - if m := gvalid.Check("12345", rule, nil); m != nil { + if m := gvalid.Check(context.TODO(), "12345", rule, nil); m != nil { t.Error(m) } - if m := gvalid.Check("1234567", rule, nil); m == nil { + if m := gvalid.Check(context.TODO(), "1234567", rule, nil); m == nil { t.Error("长度校验失败") } - if m := gvalid.Check("1234567", rule, msgs); m == nil { + if m := gvalid.Check(context.TODO(), "1234567", rule, msgs); m == nil { t.Error("长度校验失败") } rule2 := "max-length:abc" - if m := gvalid.Check("123456", rule2, nil); m == nil { + if m := gvalid.Check(context.TODO(), "123456", rule2, nil); m == nil { t.Error("长度校验失败") } } func Test_Between(t *testing.T) { rule := "between:6.01, 10.01" - if m := gvalid.Check(10, rule, nil); m != nil { + if m := gvalid.Check(context.TODO(), 10, rule, nil); m != nil { t.Error(m) } - if m := gvalid.Check(10.02, rule, nil); m == nil { + if m := gvalid.Check(context.TODO(), 10.02, rule, nil); m == nil { t.Error("大小范围校验失败") } - if m := gvalid.Check("a", rule, nil); m == nil { + if m := gvalid.Check(context.TODO(), "a", rule, nil); m == nil { t.Error("大小范围校验失败") } } @@ -722,11 +723,11 @@ func Test_Min(t *testing.T) { val3 := "100" val4 := "1000" val5 := "a" - err1 := gvalid.Check(val1, rule, nil) - err2 := gvalid.Check(val2, rule, nil) - err3 := gvalid.Check(val3, rule, nil) - err4 := gvalid.Check(val4, rule, nil) - err5 := gvalid.Check(val5, rule, nil) + err1 := gvalid.Check(context.TODO(), val1, rule, nil) + err2 := gvalid.Check(context.TODO(), val2, rule, nil) + err3 := gvalid.Check(context.TODO(), val3, rule, nil) + err4 := gvalid.Check(context.TODO(), val4, rule, nil) + err5 := gvalid.Check(context.TODO(), val5, rule, nil) t.AssertNE(err1, nil) t.AssertNE(err2, nil) t.Assert(err3, nil) @@ -734,7 +735,7 @@ func Test_Min(t *testing.T) { t.AssertNE(err5, nil) rule2 := "min:a" - err6 := gvalid.Check(val1, rule2, nil) + err6 := gvalid.Check(context.TODO(), val1, rule2, nil) t.AssertNE(err6, nil) }) } @@ -747,11 +748,11 @@ func Test_Max(t *testing.T) { val3 := "100" val4 := "1000" val5 := "a" - err1 := gvalid.Check(val1, rule, nil) - err2 := gvalid.Check(val2, rule, nil) - err3 := gvalid.Check(val3, rule, nil) - err4 := gvalid.Check(val4, rule, nil) - err5 := gvalid.Check(val5, rule, nil) + err1 := gvalid.Check(context.TODO(), val1, rule, nil) + err2 := gvalid.Check(context.TODO(), val2, rule, nil) + err3 := gvalid.Check(context.TODO(), val3, rule, nil) + err4 := gvalid.Check(context.TODO(), val4, rule, nil) + err5 := gvalid.Check(context.TODO(), val5, rule, nil) t.Assert(err1, nil) t.Assert(err2, nil) t.Assert(err3, nil) @@ -759,7 +760,7 @@ func Test_Max(t *testing.T) { t.AssertNE(err5, nil) rule2 := "max:a" - err6 := gvalid.Check(val1, rule2, nil) + err6 := gvalid.Check(context.TODO(), val1, rule2, nil) t.AssertNE(err6, nil) }) } @@ -773,12 +774,12 @@ func Test_Json(t *testing.T) { val4 := "[]" val5 := "[1,2,3,4]" val6 := `{"list":[1,2,3,4]}` - err1 := gvalid.Check(val1, rule, nil) - err2 := gvalid.Check(val2, rule, nil) - err3 := gvalid.Check(val3, rule, nil) - err4 := gvalid.Check(val4, rule, nil) - err5 := gvalid.Check(val5, rule, nil) - err6 := gvalid.Check(val6, rule, nil) + err1 := gvalid.Check(context.TODO(), val1, rule, nil) + err2 := gvalid.Check(context.TODO(), val2, rule, nil) + err3 := gvalid.Check(context.TODO(), val3, rule, nil) + err4 := gvalid.Check(context.TODO(), val4, rule, nil) + err5 := gvalid.Check(context.TODO(), val5, rule, nil) + err6 := gvalid.Check(context.TODO(), val6, rule, nil) t.AssertNE(err1, nil) t.AssertNE(err2, nil) t.Assert(err3, nil) @@ -797,12 +798,12 @@ func Test_Integer(t *testing.T) { val4 := "1" val5 := "100" val6 := `999999999` - err1 := gvalid.Check(val1, rule, nil) - err2 := gvalid.Check(val2, rule, nil) - err3 := gvalid.Check(val3, rule, nil) - err4 := gvalid.Check(val4, rule, nil) - err5 := gvalid.Check(val5, rule, nil) - err6 := gvalid.Check(val6, rule, nil) + err1 := gvalid.Check(context.TODO(), val1, rule, nil) + err2 := gvalid.Check(context.TODO(), val2, rule, nil) + err3 := gvalid.Check(context.TODO(), val3, rule, nil) + err4 := gvalid.Check(context.TODO(), val4, rule, nil) + err5 := gvalid.Check(context.TODO(), val5, rule, nil) + err6 := gvalid.Check(context.TODO(), val6, rule, nil) t.AssertNE(err1, nil) t.AssertNE(err2, nil) t.Assert(err3, nil) @@ -821,12 +822,12 @@ func Test_Float(t *testing.T) { val4 := "1.0" val5 := "1.1" val6 := `0.1` - err1 := gvalid.Check(val1, rule, nil) - err2 := gvalid.Check(val2, rule, nil) - err3 := gvalid.Check(val3, rule, nil) - err4 := gvalid.Check(val4, rule, nil) - err5 := gvalid.Check(val5, rule, nil) - err6 := gvalid.Check(val6, rule, nil) + err1 := gvalid.Check(context.TODO(), val1, rule, nil) + err2 := gvalid.Check(context.TODO(), val2, rule, nil) + err3 := gvalid.Check(context.TODO(), val3, rule, nil) + err4 := gvalid.Check(context.TODO(), val4, rule, nil) + err5 := gvalid.Check(context.TODO(), val5, rule, nil) + err6 := gvalid.Check(context.TODO(), val6, rule, nil) t.AssertNE(err1, nil) t.AssertNE(err2, nil) t.Assert(err3, nil) @@ -845,12 +846,12 @@ func Test_Boolean(t *testing.T) { val4 := "1" val5 := "true" val6 := `off` - err1 := gvalid.Check(val1, rule, nil) - err2 := gvalid.Check(val2, rule, nil) - err3 := gvalid.Check(val3, rule, nil) - err4 := gvalid.Check(val4, rule, nil) - err5 := gvalid.Check(val5, rule, nil) - err6 := gvalid.Check(val6, rule, nil) + err1 := gvalid.Check(context.TODO(), val1, rule, nil) + err2 := gvalid.Check(context.TODO(), val2, rule, nil) + err3 := gvalid.Check(context.TODO(), val3, rule, nil) + err4 := gvalid.Check(context.TODO(), val4, rule, nil) + err5 := gvalid.Check(context.TODO(), val5, rule, nil) + err6 := gvalid.Check(context.TODO(), val6, rule, nil) t.AssertNE(err1, nil) t.AssertNE(err2, nil) t.Assert(err3, nil) @@ -874,9 +875,9 @@ func Test_Same(t *testing.T) { "id": 100, "name": "john", } - err1 := gvalid.Check(val1, rule, nil, params1) - err2 := gvalid.Check(val1, rule, nil, params2) - err3 := gvalid.Check(val1, rule, nil, params3) + err1 := gvalid.Check(context.TODO(), val1, rule, nil, params1) + err2 := gvalid.Check(context.TODO(), val1, rule, nil, params2) + err3 := gvalid.Check(context.TODO(), val1, rule, nil, params3) t.AssertNE(err1, nil) t.Assert(err2, nil) t.Assert(err3, nil) @@ -897,9 +898,9 @@ func Test_Different(t *testing.T) { "id": 100, "name": "john", } - err1 := gvalid.Check(val1, rule, nil, params1) - err2 := gvalid.Check(val1, rule, nil, params2) - err3 := gvalid.Check(val1, rule, nil, params3) + err1 := gvalid.Check(context.TODO(), val1, rule, nil, params1) + err2 := gvalid.Check(context.TODO(), val1, rule, nil, params2) + err3 := gvalid.Check(context.TODO(), val1, rule, nil, params3) t.Assert(err1, nil) t.AssertNE(err2, nil) t.AssertNE(err3, nil) @@ -913,10 +914,10 @@ func Test_In(t *testing.T) { val2 := "1" val3 := "100" val4 := "200" - err1 := gvalid.Check(val1, rule, nil) - err2 := gvalid.Check(val2, rule, nil) - err3 := gvalid.Check(val3, rule, nil) - err4 := gvalid.Check(val4, rule, nil) + err1 := gvalid.Check(context.TODO(), val1, rule, nil) + err2 := gvalid.Check(context.TODO(), val2, rule, nil) + err3 := gvalid.Check(context.TODO(), val3, rule, nil) + err4 := gvalid.Check(context.TODO(), val4, rule, nil) t.AssertNE(err1, nil) t.AssertNE(err2, nil) t.Assert(err3, nil) @@ -931,10 +932,10 @@ func Test_NotIn(t *testing.T) { val2 := "1" val3 := "100" val4 := "200" - err1 := gvalid.Check(val1, rule, nil) - err2 := gvalid.Check(val2, rule, nil) - err3 := gvalid.Check(val3, rule, nil) - err4 := gvalid.Check(val4, rule, nil) + err1 := gvalid.Check(context.TODO(), val1, rule, nil) + err2 := gvalid.Check(context.TODO(), val2, rule, nil) + err3 := gvalid.Check(context.TODO(), val3, rule, nil) + err4 := gvalid.Check(context.TODO(), val4, rule, nil) t.Assert(err1, nil) t.Assert(err2, nil) t.AssertNE(err3, nil) @@ -946,10 +947,10 @@ func Test_NotIn(t *testing.T) { val2 := "1" val3 := "100" val4 := "200" - err1 := gvalid.Check(val1, rule, nil) - err2 := gvalid.Check(val2, rule, nil) - err3 := gvalid.Check(val3, rule, nil) - err4 := gvalid.Check(val4, rule, nil) + err1 := gvalid.Check(context.TODO(), val1, rule, nil) + err2 := gvalid.Check(context.TODO(), val2, rule, nil) + err3 := gvalid.Check(context.TODO(), val3, rule, nil) + err4 := gvalid.Check(context.TODO(), val4, rule, nil) t.Assert(err1, nil) t.Assert(err2, nil) t.AssertNE(err3, nil) @@ -959,10 +960,10 @@ func Test_NotIn(t *testing.T) { func Test_Regex1(t *testing.T) { rule := `regex:\d{6}|\D{6}|length:6,16` - if m := gvalid.Check("123456", rule, nil); m != nil { + if m := gvalid.Check(context.TODO(), "123456", rule, nil); m != nil { t.Error(m) } - if m := gvalid.Check("abcde6", rule, nil); m == nil { + if m := gvalid.Check(context.TODO(), "abcde6", rule, nil); m == nil { t.Error("校验失败") } } @@ -973,9 +974,9 @@ func Test_Regex2(t *testing.T) { str1 := "" str2 := "data" str3 := "data:image/jpeg;base64,/9jrbattq22r" - err1 := gvalid.Check(str1, rule, nil) - err2 := gvalid.Check(str2, rule, nil) - err3 := gvalid.Check(str3, rule, nil) + err1 := gvalid.Check(context.TODO(), str1, rule, nil) + err2 := gvalid.Check(context.TODO(), str2, rule, nil) + err3 := gvalid.Check(context.TODO(), str3, rule, nil) t.AssertNE(err1, nil) t.AssertNE(err2, nil) t.Assert(err3, nil) @@ -992,7 +993,7 @@ func Test_InternalError_String(t *testing.T) { Name string `v:"hh"` } aa := a{Name: "2"} - err := gvalid.CheckStruct(&aa, nil) + err := gvalid.CheckStruct(context.TODO(), &aa, nil) t.Assert(err.String(), "invalid_rules: hh") t.Assert(err.Strings(), g.Slice{"invalid_rules: hh"}) diff --git a/util/gvalid/gvalid_z_unit_checkmap_test.go b/util/gvalid/gvalid_z_unit_checkmap_test.go index 4660ebf21..c74ab2925 100755 --- a/util/gvalid/gvalid_z_unit_checkmap_test.go +++ b/util/gvalid/gvalid_z_unit_checkmap_test.go @@ -7,6 +7,7 @@ package gvalid_test import ( + "context" "github.com/gogf/gf/errors/gerror" "testing" @@ -24,7 +25,7 @@ func Test_CheckMap1(t *testing.T) { "id": "required|between:1,100", "name": "required|length:6,16", } - if m := gvalid.CheckMap(data, rules); m == nil { + if m := gvalid.CheckMap(context.TODO(), data, rules); m == nil { t.Error("CheckMap校验失败") } else { t.Assert(len(m.Maps()), 2) @@ -37,7 +38,7 @@ func Test_CheckMap1(t *testing.T) { func Test_CheckMap2(t *testing.T) { var params interface{} gtest.C(t, func(t *gtest.T) { - if err := gvalid.CheckMap(params, nil, nil); err == nil { + if err := gvalid.CheckMap(context.TODO(), params, nil, nil); err == nil { t.Assert(err, nil) } }) @@ -57,7 +58,7 @@ func Test_CheckMap2(t *testing.T) { "length": "名称长度为:min到:max个字符", }, } - if m := gvalid.CheckMap(kvmap, rules, msgs); m == nil { + if m := gvalid.CheckMap(context.TODO(), kvmap, rules, msgs); m == nil { t.Error("CheckMap校验失败") } @@ -76,7 +77,7 @@ func Test_CheckMap2(t *testing.T) { "length": "名称长度为:min到:max个字符", }, } - if m := gvalid.CheckMap(kvmap, rules, msgs); m != nil { + if m := gvalid.CheckMap(context.TODO(), kvmap, rules, msgs); m != nil { t.Error(m) } @@ -95,7 +96,7 @@ func Test_CheckMap2(t *testing.T) { "length": "名称长度为:min到:max个字符", }, } - if m := gvalid.CheckMap(kvmap, rules, msgs); m != nil { + if m := gvalid.CheckMap(context.TODO(), kvmap, rules, msgs); m != nil { t.Error(m) } @@ -114,7 +115,7 @@ func Test_CheckMap2(t *testing.T) { "length": "名称长度为:min到:max个字符", }, } - if m := gvalid.CheckMap(kvmap, rules2, msgs); m != nil { + if m := gvalid.CheckMap(context.TODO(), kvmap, rules2, msgs); m != nil { t.Error(m) } @@ -133,7 +134,7 @@ func Test_CheckMap2(t *testing.T) { "length": "名称长度为:min到:max个字符", }, } - if m := gvalid.CheckMap(kvmap, rules2, msgs); m != nil { + if m := gvalid.CheckMap(context.TODO(), kvmap, rules2, msgs); m != nil { t.Error(m) } @@ -152,7 +153,7 @@ func Test_CheckMap2(t *testing.T) { "length": "名称长度为:min到:max个字符", }, } - if m := gvalid.CheckMap(kvmap, rules2, msgs); m != nil { + if m := gvalid.CheckMap(context.TODO(), kvmap, rules2, msgs); m != nil { t.Error(m) } } @@ -166,7 +167,7 @@ func Test_CheckMapWithNilAndNotRequiredField(t *testing.T) { "id": "required", "name": "length:4,16", } - if m := gvalid.CheckMap(data, rules); m != nil { + if m := gvalid.CheckMap(context.TODO(), data, rules); m != nil { t.Error(m) } } @@ -183,7 +184,7 @@ func Test_Sequence(t *testing.T) { "password@required|length:6,16|same:password2#密码不能为空|密码长度应当在:min到:max之间|两次密码输入不相等", "password2@required|length:6,16#", } - err := gvalid.CheckMap(params, rules) + err := gvalid.CheckMap(context.TODO(), params, rules) t.AssertNE(err, nil) t.Assert(len(err.Map()), 2) t.Assert(err.Map()["required"], "账号不能为空") diff --git a/util/gvalid/gvalid_z_unit_checkstruct_test.go b/util/gvalid/gvalid_z_unit_checkstruct_test.go index 786145ffb..da76790fb 100755 --- a/util/gvalid/gvalid_z_unit_checkstruct_test.go +++ b/util/gvalid/gvalid_z_unit_checkstruct_test.go @@ -7,6 +7,7 @@ package gvalid_test import ( + "context" "github.com/gogf/gf/container/gvar" "github.com/gogf/gf/os/gtime" "testing" @@ -35,7 +36,7 @@ func Test_CheckStruct(t *testing.T) { "Age": "年龄为18到30周岁", } obj := &Object{"john", 16} - err := gvalid.CheckStruct(obj, rules, msgs) + err := gvalid.CheckStruct(context.TODO(), obj, rules, msgs) t.Assert(err, nil) }) @@ -56,7 +57,7 @@ func Test_CheckStruct(t *testing.T) { "Age": "年龄为18到30周岁", } obj := &Object{"john", 16} - err := gvalid.CheckStruct(obj, rules, msgs) + err := gvalid.CheckStruct(context.TODO(), obj, rules, msgs) t.AssertNE(err, nil) t.Assert(len(err.Maps()), 2) t.Assert(err.Maps()["Name"]["required"], "") @@ -81,7 +82,7 @@ func Test_CheckStruct(t *testing.T) { "Age": "年龄为18到30周岁", } obj := &Object{"john", 16} - err := gvalid.CheckStruct(obj, rules, msgs) + err := gvalid.CheckStruct(context.TODO(), obj, rules, msgs) t.AssertNE(err, nil) t.Assert(len(err.Maps()), 2) t.Assert(err.Maps()["Name"]["required"], "") @@ -106,7 +107,7 @@ func Test_CheckStruct(t *testing.T) { "Age": "年龄为18到30周岁", } obj := &Object{"john", 16} - err := gvalid.CheckStruct(obj, rules, msgs) + err := gvalid.CheckStruct(context.TODO(), obj, rules, msgs) t.AssertNE(err, nil) t.Assert(len(err.Maps()), 2) t.Assert(err.Maps()["Name"]["required"], "") @@ -120,7 +121,7 @@ func Test_CheckStruct(t *testing.T) { Password string `json:"password" gvalid:"password@required#登录密码不能为空"` } var login LoginRequest - err := gvalid.CheckStruct(login, nil) + err := gvalid.CheckStruct(context.TODO(), login, nil) t.AssertNE(err, nil) t.Assert(len(err.Maps()), 2) t.Assert(err.Maps()["username"]["required"], "用户名不能为空") @@ -133,7 +134,7 @@ func Test_CheckStruct(t *testing.T) { Password string `json:"password" gvalid:"@required#登录密码不能为空"` } var login LoginRequest - err := gvalid.CheckStruct(login, nil) + err := gvalid.CheckStruct(context.TODO(), login, nil) t.Assert(err, nil) }) @@ -143,7 +144,7 @@ func Test_CheckStruct(t *testing.T) { Password string `json:"password" gvalid:"password@required#登录密码不能为空"` } var login LoginRequest - err := gvalid.CheckStruct(login, nil) + err := gvalid.CheckStruct(context.TODO(), login, nil) t.AssertNE(err, nil) t.Assert(err.Maps()["password"]["required"], "登录密码不能为空") }) @@ -161,7 +162,7 @@ func Test_CheckStruct(t *testing.T) { Username: "john", Password: "123456", } - err := gvalid.CheckStruct(user, nil) + err := gvalid.CheckStruct(context.TODO(), user, nil) t.AssertNE(err, nil) t.Assert(len(err.Maps()), 1) t.Assert(err.Maps()["uid"]["min"], "ID不能为空") @@ -184,7 +185,7 @@ func Test_CheckStruct(t *testing.T) { "username@required#用户名不能为空", } - err := gvalid.CheckStruct(user, rules) + err := gvalid.CheckStruct(context.TODO(), user, rules) t.AssertNE(err, nil) t.Assert(len(err.Maps()), 1) t.Assert(err.Maps()["uid"]["min"], "ID不能为空") @@ -202,7 +203,7 @@ func Test_CheckStruct(t *testing.T) { Username: "john", Password: "123456", } - err := gvalid.CheckStruct(user, nil) + err := gvalid.CheckStruct(context.TODO(), user, nil) t.AssertNE(err, nil) t.Assert(len(err.Maps()), 1) }) @@ -220,7 +221,7 @@ func Test_CheckStruct(t *testing.T) { Username: "john", Password: "123456", } - err := gvalid.CheckStruct(user, nil) + err := gvalid.CheckStruct(context.TODO(), user, nil) t.AssertNE(err, nil) t.Assert(len(err.Maps()), 1) t.Assert(err.Maps()["uid"]["min"], "ID不能为空") @@ -245,7 +246,7 @@ func Test_CheckStruct_With_EmbeddedObject(t *testing.T) { Pass2: "2", }, } - err := gvalid.CheckStruct(user, nil) + err := gvalid.CheckStruct(context.TODO(), user, nil) t.AssertNE(err, nil) t.Assert(err.Maps()["name"], g.Map{"required": "请输入您的姓名"}) t.Assert(err.Maps()["password1"], g.Map{"same": "您两次输入的密码不一致"}) @@ -271,7 +272,7 @@ func Test_CheckStruct_With_StructAttribute(t *testing.T) { Pass2: "2", }, } - err := gvalid.CheckStruct(user, nil) + err := gvalid.CheckStruct(context.TODO(), user, nil) t.AssertNE(err, nil) t.Assert(err.Maps()["name"], g.Map{"required": "请输入您的姓名"}) t.Assert(err.Maps()["password1"], g.Map{"same": "您两次输入的密码不一致"}) @@ -290,7 +291,7 @@ func Test_CheckStruct_Optional(t *testing.T) { Page: 1, Size: 10, } - err := gvalid.CheckStruct(obj, nil) + err := gvalid.CheckStruct(context.TODO(), obj, nil) t.Assert(err, nil) }) gtest.C(t, func(t *gtest.T) { @@ -303,7 +304,7 @@ func Test_CheckStruct_Optional(t *testing.T) { Page: 1, Size: 10, } - err := gvalid.CheckStruct(obj, nil) + err := gvalid.CheckStruct(context.TODO(), obj, nil) t.Assert(err, nil) }) gtest.C(t, func(t *gtest.T) { @@ -316,7 +317,7 @@ func Test_CheckStruct_Optional(t *testing.T) { Page: 1, Size: 10, } - err := gvalid.CheckStruct(obj, nil) + err := gvalid.CheckStruct(context.TODO(), obj, nil) t.Assert(err.String(), "project id must between 1, 10000") }) } @@ -332,7 +333,7 @@ func Test_CheckStruct_NoTag(t *testing.T) { Page: 1, Size: 10, } - err := gvalid.CheckStruct(obj, nil) + err := gvalid.CheckStruct(context.TODO(), obj, nil) t.Assert(err, nil) }) } @@ -349,7 +350,7 @@ func Test_CheckStruct_InvalidRule(t *testing.T) { Age: 18, Phone: "123", } - err := gvalid.CheckStruct(obj, nil) + err := gvalid.CheckStruct(context.TODO(), obj, nil) t.AssertNE(err, nil) }) } @@ -364,7 +365,7 @@ func TestValidator_CheckStructWithParamMap(t *testing.T) { Uid: 1, Nickname: "john", } - t.Assert(gvalid.CheckStructWithParamMap(data, g.Map{"uid": 1, "nickname": "john"}, nil), nil) + t.Assert(gvalid.CheckStructWithParamMap(context.TODO(), data, g.Map{"uid": 1, "nickname": "john"}, nil), nil) }) gtest.C(t, func(t *gtest.T) { type UserApiSearch struct { @@ -372,7 +373,7 @@ func TestValidator_CheckStructWithParamMap(t *testing.T) { Nickname string `v:"required-with:uid"` } data := UserApiSearch{} - t.AssertNE(gvalid.CheckStructWithParamMap(data, g.Map{}, nil), nil) + t.AssertNE(gvalid.CheckStructWithParamMap(context.TODO(), data, g.Map{}, nil), nil) }) gtest.C(t, func(t *gtest.T) { type UserApiSearch struct { @@ -382,7 +383,7 @@ func TestValidator_CheckStructWithParamMap(t *testing.T) { data := UserApiSearch{ Uid: 1, } - t.AssertNE(gvalid.CheckStructWithParamMap(data, g.Map{}, nil), nil) + t.AssertNE(gvalid.CheckStructWithParamMap(context.TODO(), data, g.Map{}, nil), nil) }) gtest.C(t, func(t *gtest.T) { @@ -396,7 +397,7 @@ func TestValidator_CheckStructWithParamMap(t *testing.T) { StartTime: nil, EndTime: nil, } - t.Assert(gvalid.CheckStructWithParamMap(data, g.Map{}, nil), nil) + t.Assert(gvalid.CheckStructWithParamMap(context.TODO(), data, g.Map{}, nil), nil) }) gtest.C(t, func(t *gtest.T) { type UserApiSearch struct { @@ -409,6 +410,6 @@ func TestValidator_CheckStructWithParamMap(t *testing.T) { StartTime: gtime.Now(), EndTime: nil, } - t.AssertNE(gvalid.CheckStructWithParamMap(data, g.Map{"start_time": gtime.Now()}, nil), nil) + t.AssertNE(gvalid.CheckStructWithParamMap(context.TODO(), data, g.Map{"start_time": gtime.Now()}, nil), nil) }) } diff --git a/util/gvalid/gvalid_z_unit_custom_rule_test.go b/util/gvalid/gvalid_z_unit_custom_rule_test.go index 2fdc36a92..326c42ebd 100644 --- a/util/gvalid/gvalid_z_unit_custom_rule_test.go +++ b/util/gvalid/gvalid_z_unit_custom_rule_test.go @@ -7,6 +7,7 @@ package gvalid_test import ( + "context" "errors" "github.com/gogf/gf/frame/g" "github.com/gogf/gf/util/gconv" @@ -30,9 +31,9 @@ func Test_CustomRule1(t *testing.T) { }) gtest.Assert(err, nil) gtest.C(t, func(t *gtest.T) { - err := gvalid.Check("123456", rule, "custom message") + err := gvalid.Check(context.TODO(), "123456", rule, "custom message") t.Assert(err.String(), "custom message") - err = gvalid.Check("123456", rule, "custom message", g.Map{"data": "123456"}) + err = gvalid.Check(context.TODO(), "123456", rule, "custom message", g.Map{"data": "123456"}) t.Assert(err, nil) }) // Error with struct validation. @@ -45,7 +46,7 @@ func Test_CustomRule1(t *testing.T) { Value: "123", Data: "123456", } - err := gvalid.CheckStruct(st, nil) + err := gvalid.CheckStruct(context.TODO(), st, nil) t.Assert(err.String(), "自定义错误") }) // No error with struct validation. @@ -58,7 +59,7 @@ func Test_CustomRule1(t *testing.T) { Value: "123456", Data: "123456", } - err := gvalid.CheckStruct(st, nil) + err := gvalid.CheckStruct(context.TODO(), st, nil) t.Assert(err, nil) }) } @@ -76,8 +77,8 @@ func Test_CustomRule2(t *testing.T) { // Check. gtest.C(t, func(t *gtest.T) { errStr := "data map should not be empty" - t.Assert(gvalid.Check(g.Map{}, rule, errStr).String(), errStr) - t.Assert(gvalid.Check(g.Map{"k": "v"}, rule, errStr).String(), nil) + t.Assert(gvalid.Check(context.TODO(), g.Map{}, rule, errStr).String(), errStr) + t.Assert(gvalid.Check(context.TODO(), g.Map{"k": "v"}, rule, errStr).String(), nil) }) // Error with struct validation. gtest.C(t, func(t *gtest.T) { @@ -89,7 +90,7 @@ func Test_CustomRule2(t *testing.T) { Value: map[string]string{}, Data: "123456", } - err := gvalid.CheckStruct(st, nil) + err := gvalid.CheckStruct(context.TODO(), st, nil) t.Assert(err.String(), "自定义错误") }) // No error with struct validation. @@ -102,7 +103,7 @@ func Test_CustomRule2(t *testing.T) { Value: map[string]string{"k": "v"}, Data: "123456", } - err := gvalid.CheckStruct(st, nil) + err := gvalid.CheckStruct(context.TODO(), st, nil) t.Assert(err, nil) }) } @@ -120,9 +121,9 @@ func Test_CustomRule_AllowEmpty(t *testing.T) { // Check. gtest.C(t, func(t *gtest.T) { errStr := "error" - t.Assert(gvalid.Check("", rule, errStr).String(), "") - t.Assert(gvalid.Check("gf", rule, errStr).String(), "") - t.Assert(gvalid.Check("gf2", rule, errStr).String(), errStr) + t.Assert(gvalid.Check(context.TODO(), "", rule, errStr).String(), "") + t.Assert(gvalid.Check(context.TODO(), "gf", rule, errStr).String(), "") + t.Assert(gvalid.Check(context.TODO(), "gf2", rule, errStr).String(), errStr) }) // Error with struct validation. gtest.C(t, func(t *gtest.T) { @@ -134,7 +135,7 @@ func Test_CustomRule_AllowEmpty(t *testing.T) { Value: "", Data: "123456", } - err := gvalid.CheckStruct(st, nil) + err := gvalid.CheckStruct(context.TODO(), st, nil) t.Assert(err.String(), "") }) // No error with struct validation. @@ -147,7 +148,7 @@ func Test_CustomRule_AllowEmpty(t *testing.T) { Value: "john", Data: "123456", } - err := gvalid.CheckStruct(st, nil) + err := gvalid.CheckStruct(context.TODO(), st, nil) t.Assert(err.String(), "自定义错误") }) } diff --git a/util/gvalid/gvalid_z_unit_customerror_test.go b/util/gvalid/gvalid_z_unit_customerror_test.go index 9f73afa4b..ecccf7e4d 100755 --- a/util/gvalid/gvalid_z_unit_customerror_test.go +++ b/util/gvalid/gvalid_z_unit_customerror_test.go @@ -7,6 +7,7 @@ package gvalid_test import ( + "context" "strings" "testing" @@ -19,7 +20,7 @@ func Test_Map(t *testing.T) { var ( rule = "ipv4" val = "0.0.0" - err = gvalid.Check(val, rule, nil) + err = gvalid.Check(context.TODO(), val, rule, nil) msg = map[string]string{ "ipv4": "The value must be a valid IPv4 address", } @@ -33,7 +34,7 @@ func Test_FirstString(t *testing.T) { var ( rule = "ipv4" val = "0.0.0" - err = gvalid.Check(val, rule, nil) + err = gvalid.Check(context.TODO(), val, rule, nil) n = err.FirstString() ) t.Assert(n, "The value must be a valid IPv4 address") @@ -46,7 +47,7 @@ func Test_CustomError1(t *testing.T) { "integer": "请输入一个整数", "length": "参数长度不对啊老铁", } - e := gvalid.Check("6.66", rule, msgs) + e := gvalid.Check(context.TODO(), "6.66", rule, msgs) if e == nil || len(e.Map()) != 2 { t.Error("规则校验失败") } else { @@ -66,7 +67,7 @@ func Test_CustomError1(t *testing.T) { func Test_CustomError2(t *testing.T) { rule := "integer|length:6,16" msgs := "请输入一个整数|参数长度不对啊老铁" - e := gvalid.Check("6.66", rule, msgs) + e := gvalid.Check(context.TODO(), "6.66", rule, msgs) if e == nil || len(e.Map()) != 2 { t.Error("规则校验失败") } else {