github.com/gogf/gf/v2@v2.7.4/util/gvalid/gvalid_z_unit_feature_meta_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gvalid_test 8 9 import ( 10 "context" 11 "testing" 12 13 "github.com/gogf/gf/v2/errors/gerror" 14 "github.com/gogf/gf/v2/frame/g" 15 "github.com/gogf/gf/v2/test/gtest" 16 "github.com/gogf/gf/v2/util/gvalid" 17 ) 18 19 type UserCreateReq struct { 20 g.Meta `v:"UserCreateReq"` 21 Name string 22 Pass string 23 } 24 25 func RuleUserCreateReq(ctx context.Context, in gvalid.RuleFuncInput) error { 26 var req *UserCreateReq 27 if err := in.Data.Scan(&req); err != nil { 28 return gerror.Wrap(err, `Scan data to UserCreateReq failed`) 29 } 30 return gerror.Newf(`The name "%s" is already token by others`, req.Name) 31 } 32 33 func Test_Meta(t *testing.T) { 34 var user = &UserCreateReq{ 35 Name: "john", 36 Pass: "123456", 37 } 38 39 gtest.C(t, func(t *gtest.T) { 40 err := g.Validator().RuleFunc("UserCreateReq", RuleUserCreateReq). 41 Data(user). 42 Assoc(g.Map{ 43 "Name": "john smith", 44 }).Run(ctx) 45 t.Assert(err.String(), `The name "john smith" is already token by others`) 46 }) 47 }