增加单元测试 + 修复BUG
This commit is contained in:
		@ -21,6 +21,7 @@ type FieldRule struct {
 | 
			
		||||
	AllowEmpty             bool                  `json:"allow_empty"`              // 必传时空字符串是否合法
 | 
			
		||||
	AllowZero              bool                  `json:"allow_zero"`               // 必传数字, 0 是否合法
 | 
			
		||||
	AllowNil               bool                  `json:"allow_nil"`                // 必传时, nil值是否合法
 | 
			
		||||
	AutoTrimSpace          bool                  `json:"auto_trim_space"`          // 自动去除前后空格
 | 
			
		||||
	DisableAutoConvert     bool                  `json:"disable_auto_convert"`     // 禁用自动格式转换, 不禁用情况下, ["1","2","3"] 可以转换成 [1,2,3] , "12.34" 可以转换成 12.34
 | 
			
		||||
	RequiredConditionGroup [][]RequiredCondition `json:"required_condition_group"` // 满足何种条件,字段必传,不配置则为无差别必传, 组之间是或条件, 满足一组即命中, 组之内为与条件
 | 
			
		||||
	ValueLimit             *ValueLimit           `json:"value_limit"`              // 数据值的限制
 | 
			
		||||
@ -56,7 +57,6 @@ type ValueLimit struct {
 | 
			
		||||
//
 | 
			
		||||
// Date : 11:16 2024/4/29
 | 
			
		||||
type StringValueLimit struct {
 | 
			
		||||
	AutoTrimSpace     bool     `json:"auto_trim_space"`      // 自动去除前后空格
 | 
			
		||||
	IncludeSubStrList []string `json:"include_sub_str_list"` // 必须包含指定的子串
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -99,9 +99,6 @@ func handleBool(inputVal any, rule *define.FieldRule) (bool, error) {
 | 
			
		||||
	if err = util.ConvertAssign(&formatData, inputVal); nil != err {
 | 
			
		||||
		return false, err
 | 
			
		||||
	}
 | 
			
		||||
	if _, err = handleFloat(formatData, rule); nil != err {
 | 
			
		||||
		return false, err
 | 
			
		||||
	}
 | 
			
		||||
	return formatData, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -80,3 +80,181 @@ func Test_handleFloat(t *testing.T) {
 | 
			
		||||
	as.Equal(float64(1), f1)
 | 
			
		||||
	as.Equal(nil, err)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Test_handleInt ...
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 16:34 2024/5/2
 | 
			
		||||
func Test_handleInt(t *testing.T) {
 | 
			
		||||
	minVal0 := float64(0)
 | 
			
		||||
	minVal1 := float64(1)
 | 
			
		||||
	maxVal := float64(100)
 | 
			
		||||
	as := assert.New(t)
 | 
			
		||||
	checkRule := &define.FieldRule{
 | 
			
		||||
		Path:                   "test_int",
 | 
			
		||||
		Type:                   consts.DataTypeInt,
 | 
			
		||||
		DisableRewrite:         false,
 | 
			
		||||
		DefaultValue:           "",
 | 
			
		||||
		IsRequired:             false,
 | 
			
		||||
		AllowEmpty:             false,
 | 
			
		||||
		AllowZero:              false,
 | 
			
		||||
		AllowNil:               false,
 | 
			
		||||
		DisableAutoConvert:     false,
 | 
			
		||||
		RequiredConditionGroup: nil,
 | 
			
		||||
		ValueLimit: &define.ValueLimit{
 | 
			
		||||
			EnumList: nil,
 | 
			
		||||
			Min:      nil,
 | 
			
		||||
			Max:      nil,
 | 
			
		||||
			String:   nil,
 | 
			
		||||
			Map:      nil,
 | 
			
		||||
		},
 | 
			
		||||
		SliceConfig: nil,
 | 
			
		||||
	}
 | 
			
		||||
	int1 := int64(1)
 | 
			
		||||
	f1, err := handleInt(int1, checkRule)
 | 
			
		||||
	as.Equal(nil, err)
 | 
			
		||||
	as.Equal(int64(1), f1)
 | 
			
		||||
	// 不允许零值
 | 
			
		||||
	int0 := int64(0)
 | 
			
		||||
	f0, err := handleInt(int0, checkRule)
 | 
			
		||||
	as.Equal(int64(0), f0)
 | 
			
		||||
	as.NotEqual(nil, err)
 | 
			
		||||
	// 允许零值
 | 
			
		||||
	checkRule.AllowZero = true
 | 
			
		||||
	f0, err = handleInt(int0, checkRule)
 | 
			
		||||
	as.Equal(int64(0), f0)
 | 
			
		||||
	as.Equal(nil, err)
 | 
			
		||||
	// 取值范围符合
 | 
			
		||||
	checkRule.ValueLimit.Min = &minVal0
 | 
			
		||||
	checkRule.ValueLimit.Max = &maxVal
 | 
			
		||||
	f0, err = handleInt(int0, checkRule)
 | 
			
		||||
	as.Equal(int64(0), f0)
 | 
			
		||||
	as.Equal(nil, err)
 | 
			
		||||
	// 取值范围不符合
 | 
			
		||||
	checkRule.ValueLimit.Min = &minVal1
 | 
			
		||||
	f0, err = handleInt(int0, checkRule)
 | 
			
		||||
	as.Equal(int64(0), f0)
 | 
			
		||||
	as.NotEqual(nil, err)
 | 
			
		||||
	// 验证枚举值(不包含)
 | 
			
		||||
	checkRule.ValueLimit.EnumList = []string{"0", "2"}
 | 
			
		||||
	f1, err = handleInt(int1, checkRule)
 | 
			
		||||
	as.Equal(int64(0), f1)
 | 
			
		||||
	as.NotEqual(nil, err)
 | 
			
		||||
	// 验证枚举值(包含)
 | 
			
		||||
	checkRule.ValueLimit.EnumList = []string{"0", "1", "2"}
 | 
			
		||||
	f1, err = handleInt(int1, checkRule)
 | 
			
		||||
	as.Equal(int64(1), f1)
 | 
			
		||||
	as.Equal(nil, err)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Test_handleUint...
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 16:41 2024/5/2
 | 
			
		||||
func Test_handleUint(t *testing.T) {
 | 
			
		||||
	minVal0 := float64(0)
 | 
			
		||||
	minVal1 := float64(1)
 | 
			
		||||
	maxVal := float64(100)
 | 
			
		||||
	as := assert.New(t)
 | 
			
		||||
	checkRule := &define.FieldRule{
 | 
			
		||||
		Path:                   "test_int",
 | 
			
		||||
		Type:                   consts.DataTypeInt,
 | 
			
		||||
		DisableRewrite:         false,
 | 
			
		||||
		DefaultValue:           "",
 | 
			
		||||
		IsRequired:             false,
 | 
			
		||||
		AllowEmpty:             false,
 | 
			
		||||
		AllowZero:              false,
 | 
			
		||||
		AllowNil:               false,
 | 
			
		||||
		DisableAutoConvert:     false,
 | 
			
		||||
		RequiredConditionGroup: nil,
 | 
			
		||||
		ValueLimit: &define.ValueLimit{
 | 
			
		||||
			EnumList: nil,
 | 
			
		||||
			Min:      nil,
 | 
			
		||||
			Max:      nil,
 | 
			
		||||
			String:   nil,
 | 
			
		||||
			Map:      nil,
 | 
			
		||||
		},
 | 
			
		||||
		SliceConfig: nil,
 | 
			
		||||
	}
 | 
			
		||||
	int1 := uint64(1)
 | 
			
		||||
	f1, err := handleUint(int1, checkRule)
 | 
			
		||||
	as.Equal(nil, err)
 | 
			
		||||
	as.Equal(uint64(1), f1)
 | 
			
		||||
	// 不允许零值
 | 
			
		||||
	int0 := uint64(0)
 | 
			
		||||
	f0, err := handleUint(int0, checkRule)
 | 
			
		||||
	as.Equal(uint64(0), f0)
 | 
			
		||||
	as.NotEqual(nil, err)
 | 
			
		||||
	// 允许零值
 | 
			
		||||
	checkRule.AllowZero = true
 | 
			
		||||
	f0, err = handleUint(int0, checkRule)
 | 
			
		||||
	as.Equal(uint64(0), f0)
 | 
			
		||||
	as.Equal(nil, err)
 | 
			
		||||
	// 取值范围符合
 | 
			
		||||
	checkRule.ValueLimit.Min = &minVal0
 | 
			
		||||
	checkRule.ValueLimit.Max = &maxVal
 | 
			
		||||
	f0, err = handleUint(int0, checkRule)
 | 
			
		||||
	as.Equal(uint64(0), f0)
 | 
			
		||||
	as.Equal(nil, err)
 | 
			
		||||
	// 取值范围不符合
 | 
			
		||||
	checkRule.ValueLimit.Min = &minVal1
 | 
			
		||||
	f0, err = handleUint(int0, checkRule)
 | 
			
		||||
	as.Equal(uint64(0), f0)
 | 
			
		||||
	as.NotEqual(nil, err)
 | 
			
		||||
	// 验证枚举值(不包含)
 | 
			
		||||
	checkRule.ValueLimit.EnumList = []string{"0", "2"}
 | 
			
		||||
	f1, err = handleUint(int1, checkRule)
 | 
			
		||||
	as.Equal(uint64(0), f1)
 | 
			
		||||
	as.NotEqual(nil, err)
 | 
			
		||||
	// 验证枚举值(包含)
 | 
			
		||||
	checkRule.ValueLimit.EnumList = []string{"0", "1", "2"}
 | 
			
		||||
	f1, err = handleUint(int1, checkRule)
 | 
			
		||||
	as.Equal(uint64(1), f1)
 | 
			
		||||
	as.Equal(nil, err)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Test_handleBool ...
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 17:31 2024/5/2
 | 
			
		||||
func Test_handleBool(t *testing.T) {
 | 
			
		||||
	as := assert.New(t)
 | 
			
		||||
	checkRule := &define.FieldRule{
 | 
			
		||||
		Path:                   "test_bool",
 | 
			
		||||
		Type:                   consts.DataTypeBool,
 | 
			
		||||
		DisableRewrite:         false,
 | 
			
		||||
		DefaultValue:           "",
 | 
			
		||||
		IsRequired:             false,
 | 
			
		||||
		AllowEmpty:             false,
 | 
			
		||||
		AllowZero:              false,
 | 
			
		||||
		AllowNil:               false,
 | 
			
		||||
		DisableAutoConvert:     false,
 | 
			
		||||
		RequiredConditionGroup: nil,
 | 
			
		||||
		ValueLimit:             nil,
 | 
			
		||||
		SliceConfig:            nil,
 | 
			
		||||
	}
 | 
			
		||||
	str1 := "1"
 | 
			
		||||
	b, err := handleBool(str1, checkRule)
 | 
			
		||||
	as.Equal(true, b)
 | 
			
		||||
	as.Equal(nil, err)
 | 
			
		||||
 | 
			
		||||
	str2 := "false"
 | 
			
		||||
	b, err = handleBool(str2, checkRule)
 | 
			
		||||
	as.Equal(false, b)
 | 
			
		||||
	as.Equal(nil, err)
 | 
			
		||||
 | 
			
		||||
	str3 := "true"
 | 
			
		||||
	b, err = handleBool(str3, checkRule)
 | 
			
		||||
	as.Equal(true, b)
 | 
			
		||||
	as.Equal(nil, err)
 | 
			
		||||
 | 
			
		||||
	b, err = handleBool(false, checkRule)
 | 
			
		||||
	as.Equal(false, b)
 | 
			
		||||
	as.Equal(nil, err)
 | 
			
		||||
	b, err = handleBool(true, checkRule)
 | 
			
		||||
	as.Equal(true, b)
 | 
			
		||||
	as.Equal(nil, err)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										18
									
								
								run.go
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								run.go
									
									
									
									
									
								
							@ -128,11 +128,21 @@ func validate(sourceData []byte, val gjson.Result, rule *define.FieldRule) (any,
 | 
			
		||||
		// TODO : 验证有条件必传
 | 
			
		||||
		inputVal = rule.DefaultValue
 | 
			
		||||
	} else {
 | 
			
		||||
		if inputVal == nil {
 | 
			
		||||
			if !rule.AllowNil {
 | 
			
		||||
				return nil, fmt.Errorf("%v : field value is nil, but not allowed", rule.Path)
 | 
			
		||||
		if nil == inputVal {
 | 
			
		||||
			if rule.IsRequired {
 | 
			
		||||
				return nil, fmt.Errorf("%v : data is required, but get nil", rule.Path)
 | 
			
		||||
			}
 | 
			
		||||
			if rule.DisableAutoConvert {
 | 
			
		||||
				inputVal = rule.DefaultValue
 | 
			
		||||
			} else {
 | 
			
		||||
				inputVal = strings.TrimSpace(rule.DefaultValue)
 | 
			
		||||
			}
 | 
			
		||||
		} else {
 | 
			
		||||
			if !rule.DisableAutoConvert {
 | 
			
		||||
				if inputValStr, ok := inputVal.(string); ok {
 | 
			
		||||
					inputVal = inputValStr
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			inputVal = rule.DefaultValue
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user