增加IsErr方法 + 相关单元测试
This commit is contained in:
parent
a2a3a2f653
commit
a69f3a1e01
@ -22,7 +22,7 @@ import (
|
|||||||
func DefaultValidateRuleGenerateFunc(validateRule define.GenerateRuleExpressConfig) (express string, err error) {
|
func DefaultValidateRuleGenerateFunc(validateRule define.GenerateRuleExpressConfig) (express string, err error) {
|
||||||
ruleConfig := validateRule.Rule.Config()
|
ruleConfig := validateRule.Rule.Config()
|
||||||
if len(ruleConfig.ValidatorRule.String()) == 0 {
|
if len(ruleConfig.ValidatorRule.String()) == 0 {
|
||||||
return "", errors.New("validate rule is empty")
|
return "", define.ErrValidatorRuleIsEmpty
|
||||||
}
|
}
|
||||||
if ruleConfig.WithoutParam {
|
if ruleConfig.WithoutParam {
|
||||||
// 验证规则不需要任何参数, 舍弃参数
|
// 验证规则不需要任何参数, 舍弃参数
|
||||||
@ -30,13 +30,13 @@ func DefaultValidateRuleGenerateFunc(validateRule define.GenerateRuleExpressConf
|
|||||||
}
|
}
|
||||||
// 验证参数数量
|
// 验证参数数量
|
||||||
if ruleConfig.MinParamCnt > 0 && len(validateRule.ParamList) < ruleConfig.MinParamCnt {
|
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))
|
return "", errors.New(ruleConfig.ValidatorRule.String() + " : " + define.ErrValidatorRuleParamCntIsTooLess.Error() + "-> " + fmt.Sprintf("%v", ruleConfig.MinParamCnt))
|
||||||
}
|
}
|
||||||
if ruleConfig.MaxParamCnt > 0 && len(validateRule.ParamList) > ruleConfig.MaxParamCnt {
|
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))
|
return "", errors.New(ruleConfig.ValidatorRule.String() + " :" + define.ErrValidatorRuleParamCntIsTooMore.Error() + " -> " + fmt.Sprintf("%v", ruleConfig.MaxParamCnt))
|
||||||
}
|
}
|
||||||
if ruleConfig.ParamCntMustEven && len(validateRule.ParamList)%2 != 0 {
|
if ruleConfig.ParamCntMustEven && len(validateRule.ParamList)%2 != 0 {
|
||||||
return "", errors.New(ruleConfig.ValidatorRule.String() + " : validate rule param count should be even")
|
return "", errors.New(ruleConfig.ValidatorRule.String() + " : " + define.ErrValidatorRuleParamCntIsNotEven.Error())
|
||||||
}
|
}
|
||||||
paramStrList := make([]string, 0)
|
paramStrList := make([]string, 0)
|
||||||
for _, param := range validateRule.ParamList {
|
for _, param := range validateRule.ParamList {
|
||||||
@ -52,7 +52,7 @@ func DefaultValidateRuleGenerateFunc(validateRule define.GenerateRuleExpressConf
|
|||||||
// Date : 11:44 2025/1/24
|
// Date : 11:44 2025/1/24
|
||||||
func DefaultFieldValidateRuleGenerateFunc(fieldValidateRule define.FieldValidateGenerateConfig) (string, error) {
|
func DefaultFieldValidateRuleGenerateFunc(fieldValidateRule define.FieldValidateGenerateConfig) (string, error) {
|
||||||
if len(fieldValidateRule.RuleGroup) == 0 && len(fieldValidateRule.RuleSimple.Rule.String()) == 0 {
|
if len(fieldValidateRule.RuleGroup) == 0 && len(fieldValidateRule.RuleSimple.Rule.String()) == 0 {
|
||||||
return "", errors.New("rule group and rule simple all empty")
|
return "", define.ErrValidatorRuleGroupOrSimpleRuleAllEmpty
|
||||||
}
|
}
|
||||||
if len(fieldValidateRule.RuleGroup) == 0 {
|
if len(fieldValidateRule.RuleGroup) == 0 {
|
||||||
// note: 当 fieldValidateRule.RuleGroup 不为空时, 会忽略 fieldValidateRule.RuleSimple 配置
|
// note: 当 fieldValidateRule.RuleGroup 不为空时, 会忽略 fieldValidateRule.RuleSimple 配置
|
||||||
|
34
v10/define/err.go
Normal file
34
v10/define/err.go
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
// Package define ...
|
||||||
|
//
|
||||||
|
// Description : define ...
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 2025-01-24 15:38
|
||||||
|
package define
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrValidatorRuleIsEmpty = errors.New("validator rule is empty")
|
||||||
|
ErrValidatorRuleParamCntIsTooLess = errors.New("validate rule param count is less than min param cnt")
|
||||||
|
ErrValidatorRuleParamCntIsTooMore = errors.New("validate rule param count is more than min param cnt")
|
||||||
|
ErrValidatorRuleParamCntIsNotEven = errors.New("validate rule param count is not even")
|
||||||
|
|
||||||
|
ErrValidatorRuleGroupOrSimpleRuleAllEmpty = errors.New("validator rule group or simple rule all empty")
|
||||||
|
)
|
||||||
|
|
||||||
|
// IsErr 是否指定类型Err
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 15:45 2025/1/24
|
||||||
|
func IsErr(err error, subMsg string) bool {
|
||||||
|
if nil == err {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return strings.Contains(err.Error(), subMsg)
|
||||||
|
}
|
29
v10/define/err_test.go
Normal file
29
v10/define/err_test.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
// Package define ...
|
||||||
|
//
|
||||||
|
// Description : define ...
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 2025-01-24 15:46
|
||||||
|
package define
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
. "github.com/smartystreets/goconvey/convey"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestIsErr(t *testing.T) {
|
||||||
|
Convey("传入error为nil", t, func() {
|
||||||
|
isErrRes := IsErr(nil, "")
|
||||||
|
So(isErrRes, ShouldBeFalse)
|
||||||
|
})
|
||||||
|
Convey("传入error为test类型", t, func() {
|
||||||
|
isErrRes := IsErr(errors.New("aaatestbbb"), "test")
|
||||||
|
So(isErrRes, ShouldBeTrue)
|
||||||
|
})
|
||||||
|
Convey("传入error非test类型", t, func() {
|
||||||
|
isErrRes := IsErr(errors.New("aaatecccstbbb"), "test")
|
||||||
|
So(isErrRes, ShouldBeFalse)
|
||||||
|
})
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user