优化字段表达式构建, 增加单元测试
This commit is contained in:
parent
44e6c5cdcf
commit
5fe0557d90
@ -51,6 +51,10 @@ func DefaultValidateRuleGenerateFunc(validateRule define.GenerateRuleExpressConf
|
||||
//
|
||||
// Date : 11:44 2025/1/24
|
||||
func DefaultFieldValidateRuleGenerateFunc(fieldValidateRule define.FieldValidateGenerateConfig) (string, error) {
|
||||
fieldValidateRule.Field = strings.TrimSpace(fieldValidateRule.Field)
|
||||
if len(fieldValidateRule.Field) == 0 {
|
||||
return "", define.ErrValidatorRuleFieldIsEmpty
|
||||
}
|
||||
if len(fieldValidateRule.RuleGroup) == 0 && len(fieldValidateRule.RuleSimple.Rule.String()) == 0 {
|
||||
return "", define.ErrValidatorRuleGroupOrSimpleRuleAllEmpty
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ import (
|
||||
"git.zhangdeman.cn/gateway/validator/v10/define"
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@ -23,7 +24,7 @@ func TestDefaultValidateRuleGenerateFunc(t *testing.T) {
|
||||
Convey("传入的验证规则未注册", t, func() {
|
||||
testRule := consts.ValidatorRule("invalid_rule")
|
||||
res, err := DefaultValidateRuleGenerateFunc(define.GenerateRuleExpressConfig{
|
||||
Rule: &testRule,
|
||||
Rule: testRule,
|
||||
ParamList: nil,
|
||||
})
|
||||
So(err, ShouldBeError)
|
||||
@ -32,7 +33,7 @@ func TestDefaultValidateRuleGenerateFunc(t *testing.T) {
|
||||
})
|
||||
Convey("传入规则无参数", t, func() {
|
||||
res, err := DefaultValidateRuleGenerateFunc(define.GenerateRuleExpressConfig{
|
||||
Rule: &consts.ValidatorRuleCommonRequired,
|
||||
Rule: consts.ValidatorRuleCommonRequired,
|
||||
ParamList: nil,
|
||||
})
|
||||
So(err, ShouldBeNil)
|
||||
@ -40,7 +41,7 @@ func TestDefaultValidateRuleGenerateFunc(t *testing.T) {
|
||||
})
|
||||
Convey("传入oneof枚举值缺少枚举值列表", t, func() {
|
||||
res, err := DefaultValidateRuleGenerateFunc(define.GenerateRuleExpressConfig{
|
||||
Rule: &consts.ValidatorRuleCommonOneOf,
|
||||
Rule: consts.ValidatorRuleCommonOneOf,
|
||||
ParamList: nil,
|
||||
})
|
||||
So(err, ShouldBeError)
|
||||
@ -49,15 +50,16 @@ func TestDefaultValidateRuleGenerateFunc(t *testing.T) {
|
||||
})
|
||||
Convey("传入oneof枚举值构建成功", t, func() {
|
||||
res, err := DefaultValidateRuleGenerateFunc(define.GenerateRuleExpressConfig{
|
||||
Rule: &consts.ValidatorRuleCommonOneOf,
|
||||
Rule: consts.ValidatorRuleCommonOneOf,
|
||||
ParamList: []any{"name", "1", 2, 3},
|
||||
})
|
||||
So(err, ShouldBeNil)
|
||||
So(res, ShouldNotBeEmpty)
|
||||
So(res, ShouldEqual, consts.ValidatorRuleCommonOneOf.String()+"=name 1 2 3")
|
||||
})
|
||||
Convey("传入RequiredIf枚举值 -> 参数少于2", t, func() {
|
||||
res, err := DefaultValidateRuleGenerateFunc(define.GenerateRuleExpressConfig{
|
||||
Rule: &consts.ValidatorRuleCommonRequiredIf,
|
||||
Rule: consts.ValidatorRuleCommonRequiredIf,
|
||||
ParamList: []any{},
|
||||
})
|
||||
So(err, ShouldNotBeNil)
|
||||
@ -66,7 +68,7 @@ func TestDefaultValidateRuleGenerateFunc(t *testing.T) {
|
||||
})
|
||||
Convey("传入RequiredIf枚举值 -> 参数奇数个", t, func() {
|
||||
res, err := DefaultValidateRuleGenerateFunc(define.GenerateRuleExpressConfig{
|
||||
Rule: &consts.ValidatorRuleCommonRequiredIf,
|
||||
Rule: consts.ValidatorRuleCommonRequiredIf,
|
||||
ParamList: []any{1, 2, 3},
|
||||
})
|
||||
So(err, ShouldNotBeNil)
|
||||
@ -75,10 +77,56 @@ func TestDefaultValidateRuleGenerateFunc(t *testing.T) {
|
||||
})
|
||||
Convey("传入RequiredIf枚举值 -> 构建成功", t, func() {
|
||||
res, err := DefaultValidateRuleGenerateFunc(define.GenerateRuleExpressConfig{
|
||||
Rule: &consts.ValidatorRuleCommonRequiredIf,
|
||||
Rule: consts.ValidatorRuleCommonRequiredIf,
|
||||
ParamList: []any{1, 2, 3, 4},
|
||||
})
|
||||
So(err, ShouldBeNil)
|
||||
So(res, ShouldNotBeEmpty)
|
||||
So(res, ShouldEqual, consts.ValidatorRuleCommonRequiredIf.String()+"=1 2 3 4")
|
||||
})
|
||||
}
|
||||
|
||||
// TestDefaultFieldValidateRuleGenerateFunc 测试字段验证规则生成逻辑
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:53 2025/1/24
|
||||
func TestDefaultFieldValidateRuleGenerateFunc(t *testing.T) {
|
||||
Convey("未传入待验证字段", t, func() {
|
||||
res, err := DefaultFieldValidateRuleGenerateFunc(define.FieldValidateGenerateConfig{
|
||||
Field: "",
|
||||
})
|
||||
So(err, ShouldNotBeNil)
|
||||
So(define.IsErr(err, define.ErrValidatorRuleFieldIsEmpty.Error()), ShouldBeTrue)
|
||||
So(res, ShouldBeEmpty)
|
||||
})
|
||||
Convey("传入待验证字段有空格组成", t, func() {
|
||||
res, err := DefaultFieldValidateRuleGenerateFunc(define.FieldValidateGenerateConfig{
|
||||
Field: "",
|
||||
})
|
||||
So(err, ShouldNotBeNil)
|
||||
So(define.IsErr(err, define.ErrValidatorRuleFieldIsEmpty.Error()), ShouldBeTrue)
|
||||
So(res, ShouldBeEmpty)
|
||||
})
|
||||
Convey("未传入任何字段验证规则", t, func() {
|
||||
res, err := DefaultFieldValidateRuleGenerateFunc(define.FieldValidateGenerateConfig{
|
||||
Field: "test",
|
||||
})
|
||||
So(err, ShouldBeError)
|
||||
So(define.IsErr(err, define.ErrValidatorRuleGroupOrSimpleRuleAllEmpty.Error()), ShouldBeTrue)
|
||||
So(res, ShouldBeEmpty)
|
||||
})
|
||||
Convey("传入SimpleRule", t, func() {
|
||||
res, err := DefaultFieldValidateRuleGenerateFunc(define.FieldValidateGenerateConfig{
|
||||
Field: "test",
|
||||
RuleGroup: nil,
|
||||
RuleSimple: define.GenerateRuleExpressConfig{
|
||||
Rule: consts.ValidatorRuleCommonRequired,
|
||||
ParamList: nil,
|
||||
},
|
||||
})
|
||||
So(err, ShouldBeNil)
|
||||
So(res, ShouldNotBeEmpty)
|
||||
So(strings.HasPrefix(res, consts.ValidatorRuleCommonRequired.String()), ShouldBeTrue)
|
||||
})
|
||||
}
|
||||
|
@ -19,6 +19,7 @@ var (
|
||||
ErrValidatorRuleParamCntIsNotEven = errors.New("validate rule param count is not even")
|
||||
|
||||
ErrValidatorRuleGroupOrSimpleRuleAllEmpty = errors.New("validator rule group or simple rule all empty")
|
||||
ErrValidatorRuleFieldIsEmpty = errors.New("field is empty")
|
||||
)
|
||||
|
||||
// IsErr 是否指定类型Err
|
||||
|
@ -26,6 +26,6 @@ type FieldValidateGenerateConfig struct {
|
||||
//
|
||||
// Date : 11:27 2025/1/24
|
||||
type GenerateRuleExpressConfig struct {
|
||||
Rule *consts.ValidatorRule `json:"rule,omitempty"` // 条件配置
|
||||
ParamList []any `json:"param_list,omitempty"` // 规则验证的参数列表
|
||||
Rule consts.ValidatorRule `json:"rule,omitempty"` // 条件配置
|
||||
ParamList []any `json:"param_list,omitempty"` // 规则验证的参数列表
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user