2025-01-24 15:36:39 +08:00
|
|
|
// 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"
|
2025-01-24 16:40:29 +08:00
|
|
|
"strings"
|
2025-01-24 15:36:39 +08:00
|
|
|
"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{
|
2025-01-24 16:40:29 +08:00
|
|
|
Rule: testRule,
|
2025-01-24 15:36:39 +08:00
|
|
|
ParamList: nil,
|
|
|
|
})
|
|
|
|
So(err, ShouldBeError)
|
2025-01-24 15:52:29 +08:00
|
|
|
So(define.IsErr(err, define.ErrValidatorRuleIsEmpty.Error()), ShouldBeTrue)
|
2025-01-24 15:36:39 +08:00
|
|
|
So(res, ShouldBeEmpty)
|
|
|
|
})
|
|
|
|
Convey("传入规则无参数", t, func() {
|
|
|
|
res, err := DefaultValidateRuleGenerateFunc(define.GenerateRuleExpressConfig{
|
2025-01-24 16:40:29 +08:00
|
|
|
Rule: consts.ValidatorRuleCommonRequired,
|
2025-01-24 15:36:39 +08:00
|
|
|
ParamList: nil,
|
|
|
|
})
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(res, ShouldEqual, consts.ValidatorRuleCommonRequired.String())
|
|
|
|
})
|
|
|
|
Convey("传入oneof枚举值缺少枚举值列表", t, func() {
|
|
|
|
res, err := DefaultValidateRuleGenerateFunc(define.GenerateRuleExpressConfig{
|
2025-01-24 16:40:29 +08:00
|
|
|
Rule: consts.ValidatorRuleCommonOneOf,
|
2025-01-24 15:36:39 +08:00
|
|
|
ParamList: nil,
|
|
|
|
})
|
|
|
|
So(err, ShouldBeError)
|
2025-01-24 15:52:29 +08:00
|
|
|
So(define.IsErr(err, define.ErrValidatorRuleParamCntIsTooLess.Error()), ShouldBeTrue)
|
2025-01-24 15:36:39 +08:00
|
|
|
So(res, ShouldEqual, "")
|
|
|
|
})
|
|
|
|
Convey("传入oneof枚举值构建成功", t, func() {
|
|
|
|
res, err := DefaultValidateRuleGenerateFunc(define.GenerateRuleExpressConfig{
|
2025-01-24 16:40:29 +08:00
|
|
|
Rule: consts.ValidatorRuleCommonOneOf,
|
2025-01-24 15:36:39 +08:00
|
|
|
ParamList: []any{"name", "1", 2, 3},
|
|
|
|
})
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(res, ShouldNotBeEmpty)
|
2025-01-24 16:40:29 +08:00
|
|
|
So(res, ShouldEqual, consts.ValidatorRuleCommonOneOf.String()+"=name 1 2 3")
|
2025-01-24 15:36:39 +08:00
|
|
|
})
|
|
|
|
Convey("传入RequiredIf枚举值 -> 参数少于2", t, func() {
|
|
|
|
res, err := DefaultValidateRuleGenerateFunc(define.GenerateRuleExpressConfig{
|
2025-01-24 16:40:29 +08:00
|
|
|
Rule: consts.ValidatorRuleCommonRequiredIf,
|
2025-01-24 15:36:39 +08:00
|
|
|
ParamList: []any{},
|
|
|
|
})
|
|
|
|
So(err, ShouldNotBeNil)
|
2025-01-24 15:52:29 +08:00
|
|
|
So(define.IsErr(err, define.ErrValidatorRuleParamCntIsTooLess.Error()), ShouldBeTrue)
|
2025-01-24 15:36:39 +08:00
|
|
|
So(res, ShouldBeEmpty)
|
|
|
|
})
|
|
|
|
Convey("传入RequiredIf枚举值 -> 参数奇数个", t, func() {
|
|
|
|
res, err := DefaultValidateRuleGenerateFunc(define.GenerateRuleExpressConfig{
|
2025-01-24 16:40:29 +08:00
|
|
|
Rule: consts.ValidatorRuleCommonRequiredIf,
|
2025-01-24 15:36:39 +08:00
|
|
|
ParamList: []any{1, 2, 3},
|
|
|
|
})
|
|
|
|
So(err, ShouldNotBeNil)
|
2025-01-24 15:52:29 +08:00
|
|
|
So(define.IsErr(err, define.ErrValidatorRuleParamCntIsNotEven.Error()), ShouldBeTrue)
|
2025-01-24 15:36:39 +08:00
|
|
|
So(res, ShouldBeEmpty)
|
|
|
|
})
|
|
|
|
Convey("传入RequiredIf枚举值 -> 构建成功", t, func() {
|
|
|
|
res, err := DefaultValidateRuleGenerateFunc(define.GenerateRuleExpressConfig{
|
2025-01-24 16:40:29 +08:00
|
|
|
Rule: consts.ValidatorRuleCommonRequiredIf,
|
2025-01-24 15:36:39 +08:00
|
|
|
ParamList: []any{1, 2, 3, 4},
|
|
|
|
})
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(res, ShouldNotBeEmpty)
|
2025-01-24 16:40:29 +08:00
|
|
|
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)
|
2025-01-24 15:36:39 +08:00
|
|
|
})
|
|
|
|
}
|