diff --git a/go.mod b/go.mod index 5705319..52d18a4 100644 --- a/go.mod +++ b/go.mod @@ -7,27 +7,28 @@ require ( git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241223084948-de2e49144fcd git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241223040803-7b56590b5791 + github.com/smartystreets/goconvey v1.8.1 github.com/stretchr/testify v1.9.0 github.com/tidwall/gjson v1.18.0 github.com/tidwall/sjson v1.2.5 ) require ( - git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20241101082529-28a6c68e38a4 // indirect git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0 // indirect github.com/BurntSushi/toml v1.4.0 // indirect github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/go-ini/ini v1.67.0 // indirect + github.com/gopherjs/gopherjs v1.17.2 // indirect + github.com/jtolds/gls v4.20.0+incompatible // indirect github.com/mitchellh/go-homedir v1.1.0 // indirect github.com/mozillazg/go-pinyin v0.20.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/smarty/assertions v1.15.0 // indirect github.com/spaolacci/murmur3 v1.1.0 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) -replace ( - git.zhangdeman.cn/zhangdeman/consts => ../consts -) +replace git.zhangdeman.cn/zhangdeman/consts => ../consts diff --git a/go.sum b/go.sum index 5a32964..70e3005 100644 --- a/go.sum +++ b/go.sum @@ -1,31 +1,9 @@ -git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241023090605-10cff9173059 h1:TPAYdTKKUjgxtCnK38d1Tb4teyQp1C7wYHPdR32yZtM= -git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241023090605-10cff9173059/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= -git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125061350-1f5050978fc3 h1:/40XIygeSxRhPQc3/7pKGpV5hg8jwrMwh1+YiyCHdNI= -git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125061350-1f5050978fc3/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= -git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125065114-f919222003d9 h1:TP/M3WnGsxh0Vr6YuS1i28hw1oV//YbdCoI46PUBIA0= -git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125065114-f919222003d9/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= -git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241225030757-c00354fe4630 h1:KTp27pHr25X5khTi4B/OdFv/34UWkq6sgDfz5aBtj74= -git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241225030757-c00354fe4630/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= -git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250122075709-5ecf3edb4a00 h1:obyJF0CXVR93TOnOtzN5xXxxSLpw1UFMBc4niWiyoQI= -git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250122075709-5ecf3edb4a00/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= -git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20241024134352-ce2d84d282ba h1:hgajrPZGoDY9P+x6iqcS06pnu5t+N7DOfpmRwb+TZ4s= -git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20241024134352-ce2d84d282ba/go.mod h1:V4Dfg1v/JVIZGEKCm6/aehs8hK+Xow1dkL1yiQymXlQ= -git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20241101082529-28a6c68e38a4 h1:s6d4b6yY+NaK1AzoBD1pxqsuygEHQz0Oie86c45geDw= -git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20241101082529-28a6c68e38a4/go.mod h1:V4Dfg1v/JVIZGEKCm6/aehs8hK+Xow1dkL1yiQymXlQ= git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0 h1:gUDlQMuJ4xNfP2Abl1Msmpa3fASLWYkNlqDFF/6GN0Y= git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0/go.mod h1:VHb9qmhaPDAQDcS6vUiDCamYjZ4R5lD1XtVsh55KsMI= -git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241023104258-2e0a298aa558 h1:ZVJyyDKfYeA3TsN8UOi4IprkouK4wIIfCKe+F9byLWA= -git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241023104258-2e0a298aa558/go.mod h1:/9eicss/Dt9tp2jwZ/4cXDqDKo/Dez+HuT5/NGdqW+s= -git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241108082010-42ae8fe5ebdc h1:jtdEMr/xNchJDEoCnvMr4JXT9+biYQu625Cj+dz025w= -git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241108082010-42ae8fe5ebdc/go.mod h1:XqgER4jDYwskFgj2riJ9XptIjzgYWubY+Zq8iB2WkY0= git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241223084948-de2e49144fcd h1:q7GG14qgXKB4MEXQFOe7/UYebsqMfPaSX80TcPdOosI= git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241223084948-de2e49144fcd/go.mod h1:+D6uPSljwHywjVY5WSBY4TRVMj26TN5f5cFGEYMldjs= git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e h1:Q973S6CcWr1ICZhFI1STFOJ+KUImCl2BaIXm6YppBqI= git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e/go.mod h1:VpPjBlwz8U+OxZuxzHQBv1aEEZ3pStH6bZvT21ADEbI= -git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241024135251-6dd5f7ac6254 h1:20NfKC/5EwTpSLodvXCPM2CmKWuDgggratYB2r9ihLM= -git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241024135251-6dd5f7ac6254/go.mod h1:7D7XZqEMCHSTnmDM4t3xWcrpJn1TC2EyDLd9ScP5gN4= -git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241125062526-91423fb146e0 h1:aGQADmQKTF7c8+s3acnn569sTJwjlLhhhZfQ6f4nAH0= -git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241125062526-91423fb146e0/go.mod h1:lvVOIrYDvCQHUzBdaOwKSrxT9ubcXQJBsafWMWu+I14= git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241223040803-7b56590b5791 h1:TRCgenNef5rssrQ+47R5lToVrA0RUvj8ebXuO7AP/yk= git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241223040803-7b56590b5791/go.mod h1:QSdmz0is8gBG8EmCi2KPj0YXifGiU6DP5JxhPIoFtgY= github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0= @@ -36,6 +14,10 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A= github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8= +github.com/gopherjs/gopherjs v1.17.2 h1:fQnZVsXk8uxXIStYb0N4bGk7jeyTalG/wsZjQ25dO0g= +github.com/gopherjs/gopherjs v1.17.2/go.mod h1:pRRIvn/QzFLrKfvEz3qUuEhtE/zLCWfreZ6J5gM2i+k= +github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= +github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= @@ -44,6 +26,10 @@ github.com/mozillazg/go-pinyin v0.20.0 h1:BtR3DsxpApHfKReaPO1fCqF4pThRwH9uwvXzm+ github.com/mozillazg/go-pinyin v0.20.0/go.mod h1:iR4EnMMRXkfpFVV5FMi4FNB6wGq9NV6uDWbUuPhP4Yc= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/smarty/assertions v1.15.0 h1:cR//PqUBUiQRakZWqBiFFQ9wb8emQGDb0HeGdqGByCY= +github.com/smarty/assertions v1.15.0/go.mod h1:yABtdzeQs6l1brC900WlRNwj6ZR55d7B+E8C6HtKdec= +github.com/smartystreets/goconvey v1.8.1 h1:qGjIddxOk4grTu9JPOU31tVfq3cNdBlNa5sSznIX1xY= +github.com/smartystreets/goconvey v1.8.1/go.mod h1:+/u4qLyY6x1jReYOp7GOM2FSt8aP9CzCZL03bI28W60= github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= diff --git a/v10/default.go b/v10/default.go index 63fe1b5..90f1ef9 100644 --- a/v10/default.go +++ b/v10/default.go @@ -32,7 +32,7 @@ func DefaultValidateRuleGenerateFunc(validateRule define.GenerateRuleExpressConf if ruleConfig.MinParamCnt > 0 && len(validateRule.ParamList) < ruleConfig.MinParamCnt { return "", errors.New(ruleConfig.ValidatorRule.String() + " : validate rule param count is less than min param cnt -> " + fmt.Sprintf("%v", ruleConfig.MinParamCnt)) } - if ruleConfig.MaxParamCnt > 0 && len(validateRule.ParamList) > ruleConfig.MinParamCnt { + if ruleConfig.MaxParamCnt > 0 && len(validateRule.ParamList) > ruleConfig.MaxParamCnt { return "", errors.New(ruleConfig.ValidatorRule.String() + " : validate rule param count is more than max param cnt -> " + fmt.Sprintf("%v", ruleConfig.MaxParamCnt)) } if ruleConfig.ParamCntMustEven && len(validateRule.ParamList)%2 != 0 { diff --git a/v10/default_test.go b/v10/default_test.go new file mode 100644 index 0000000..7e77933 --- /dev/null +++ b/v10/default_test.go @@ -0,0 +1,80 @@ +// Package v10 ... +// +// Description : v10 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2025-01-24 12:01 +package v10 + +import ( + "git.zhangdeman.cn/gateway/validator/v10/define" + "git.zhangdeman.cn/zhangdeman/consts" + . "github.com/smartystreets/goconvey/convey" + "testing" +) + +// TestDefaultValidateRuleGenerateFunc 表达式生成函数测试 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 12:02 2025/1/24 +func TestDefaultValidateRuleGenerateFunc(t *testing.T) { + Convey("传入的验证规则未注册", t, func() { + testRule := consts.ValidatorRule("invalid_rule") + res, err := DefaultValidateRuleGenerateFunc(define.GenerateRuleExpressConfig{ + Rule: &testRule, + ParamList: nil, + }) + So(err, ShouldBeError) + So(res, ShouldBeEmpty) + }) + Convey("传入规则无参数", t, func() { + res, err := DefaultValidateRuleGenerateFunc(define.GenerateRuleExpressConfig{ + Rule: &consts.ValidatorRuleCommonRequired, + ParamList: nil, + }) + So(err, ShouldBeNil) + So(res, ShouldEqual, consts.ValidatorRuleCommonRequired.String()) + }) + Convey("传入oneof枚举值缺少枚举值列表", t, func() { + res, err := DefaultValidateRuleGenerateFunc(define.GenerateRuleExpressConfig{ + Rule: &consts.ValidatorRuleCommonOneOf, + ParamList: nil, + }) + So(err, ShouldBeError) + So(res, ShouldEqual, "") + }) + Convey("传入oneof枚举值构建成功", t, func() { + res, err := DefaultValidateRuleGenerateFunc(define.GenerateRuleExpressConfig{ + Rule: &consts.ValidatorRuleCommonOneOf, + ParamList: []any{"name", "1", 2, 3}, + }) + So(err, ShouldBeNil) + So(res, ShouldNotBeEmpty) + }) + Convey("传入RequiredIf枚举值 -> 参数少于2", t, func() { + res, err := DefaultValidateRuleGenerateFunc(define.GenerateRuleExpressConfig{ + Rule: &consts.ValidatorRuleCommonRequiredIf, + ParamList: []any{}, + }) + So(err, ShouldNotBeNil) + So(res, ShouldBeEmpty) + }) + Convey("传入RequiredIf枚举值 -> 参数奇数个", t, func() { + res, err := DefaultValidateRuleGenerateFunc(define.GenerateRuleExpressConfig{ + Rule: &consts.ValidatorRuleCommonRequiredIf, + ParamList: []any{1, 2, 3}, + }) + So(err, ShouldNotBeNil) + So(res, ShouldBeEmpty) + }) + Convey("传入RequiredIf枚举值 -> 构建成功", t, func() { + res, err := DefaultValidateRuleGenerateFunc(define.GenerateRuleExpressConfig{ + Rule: &consts.ValidatorRuleCommonRequiredIf, + ParamList: []any{1, 2, 3, 4}, + }) + So(err, ShouldBeNil) + So(res, ShouldNotBeEmpty) + }) +}