2024-04-29 17:29:46 +08:00
|
|
|
// Package validator ...
|
|
|
|
//
|
|
|
|
// Description : validator ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2024-04-29 14:14
|
|
|
|
package validator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-11-01 17:22:41 +08:00
|
|
|
"git.zhangdeman.cn/gateway/validator/define"
|
2024-11-01 18:45:03 +08:00
|
|
|
"git.zhangdeman.cn/zhangdeman/consts"
|
2024-09-05 17:49:50 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2024-04-29 17:29:46 +08:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2024-11-01 18:35:00 +08:00
|
|
|
// TestRunString 测试字符串类型
|
2024-11-01 17:22:41 +08:00
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 16:48 2024/11/1
|
2024-11-01 18:35:00 +08:00
|
|
|
func TestRunString(t *testing.T) {
|
2024-11-01 17:22:41 +08:00
|
|
|
sourceData := map[string]interface{}{
|
|
|
|
"name": "白茶清欢",
|
|
|
|
}
|
|
|
|
ruleListForNotFound := []*define.FieldRule{
|
|
|
|
&define.FieldRule{
|
|
|
|
Path: "name1",
|
|
|
|
Type: "string",
|
|
|
|
DisableRewrite: false,
|
|
|
|
DefaultValue: nil,
|
|
|
|
IsRequired: true,
|
|
|
|
AllowEmpty: false,
|
|
|
|
AllowZero: false,
|
|
|
|
AllowNil: false,
|
|
|
|
DisableAutoTrimSpace: false,
|
|
|
|
DisableAutoConvert: false,
|
|
|
|
RequiredConditionGroup: nil,
|
|
|
|
ValueLimit: nil,
|
|
|
|
SliceConfig: nil,
|
|
|
|
MapConfig: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := Run(sourceData, ruleListForNotFound, nil)
|
|
|
|
if nil != err {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
sourceData = map[string]interface{}{
|
|
|
|
"name": "",
|
|
|
|
}
|
|
|
|
ruleListForEmpty := []*define.FieldRule{
|
2024-11-01 17:57:53 +08:00
|
|
|
&define.FieldRule{
|
|
|
|
Path: "name",
|
|
|
|
Type: "string",
|
|
|
|
DisableRewrite: false,
|
|
|
|
DefaultValue: nil,
|
|
|
|
IsRequired: true,
|
|
|
|
AllowEmpty: false,
|
|
|
|
AllowZero: false,
|
|
|
|
AllowNil: false,
|
|
|
|
DisableAutoTrimSpace: false,
|
|
|
|
DisableAutoConvert: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = Run(sourceData, ruleListForEmpty, nil)
|
|
|
|
if nil != err {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
sourceData = map[string]interface{}{
|
|
|
|
"name": 18,
|
|
|
|
}
|
|
|
|
ruleListForDisableAutoConvert := []*define.FieldRule{
|
2024-11-01 17:22:41 +08:00
|
|
|
&define.FieldRule{
|
2024-11-01 18:32:13 +08:00
|
|
|
Path: "name",
|
|
|
|
Type: "string",
|
|
|
|
DisableRewrite: false,
|
|
|
|
DefaultValue: nil,
|
|
|
|
IsRequired: true,
|
|
|
|
AllowEmpty: false,
|
|
|
|
AllowZero: false,
|
|
|
|
AllowNil: false,
|
|
|
|
DisableAutoTrimSpace: false,
|
|
|
|
DisableAutoConvert: true,
|
2024-11-01 17:22:41 +08:00
|
|
|
},
|
|
|
|
}
|
2024-11-01 17:57:53 +08:00
|
|
|
err = Run(sourceData, ruleListForDisableAutoConvert, nil)
|
2024-11-01 17:22:41 +08:00
|
|
|
if nil != err {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
|
|
|
assert.Error(t, err)
|
2024-11-01 18:32:13 +08:00
|
|
|
|
|
|
|
sourceData = map[string]interface{}{
|
|
|
|
"name": " ",
|
|
|
|
}
|
|
|
|
ruleListForAllowTrim := []*define.FieldRule{
|
|
|
|
&define.FieldRule{
|
|
|
|
Path: "name",
|
|
|
|
Type: "string",
|
|
|
|
DisableRewrite: false,
|
|
|
|
DefaultValue: nil,
|
|
|
|
IsRequired: true,
|
|
|
|
AllowEmpty: false,
|
|
|
|
AllowZero: false,
|
|
|
|
AllowNil: false,
|
|
|
|
DisableAutoTrimSpace: false,
|
|
|
|
DisableAutoConvert: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = Run(sourceData, ruleListForAllowTrim, nil)
|
|
|
|
if nil != err {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
sourceData = map[string]interface{}{
|
|
|
|
"name": " ",
|
|
|
|
}
|
|
|
|
ruleListForDisableTrim := []*define.FieldRule{
|
|
|
|
&define.FieldRule{
|
|
|
|
Path: "name",
|
|
|
|
Type: "string",
|
|
|
|
DisableRewrite: false,
|
|
|
|
DefaultValue: nil,
|
|
|
|
IsRequired: true,
|
|
|
|
AllowEmpty: false,
|
|
|
|
AllowZero: false,
|
|
|
|
AllowNil: false,
|
|
|
|
DisableAutoTrimSpace: true,
|
|
|
|
DisableAutoConvert: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = Run(sourceData, ruleListForDisableTrim, nil)
|
|
|
|
if nil != err {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
2024-11-01 17:22:41 +08:00
|
|
|
}
|
2024-11-01 18:45:03 +08:00
|
|
|
|
|
|
|
// TestRunFieldDepend 测试字段依赖关系
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 18:40 2024/11/1
|
|
|
|
func TestRunFieldDepend(t *testing.T) {
|
|
|
|
sourceData := map[string]interface{}{
|
|
|
|
"name": "",
|
|
|
|
}
|
|
|
|
ruleListForDepend := []*define.FieldRule{
|
|
|
|
&define.FieldRule{
|
|
|
|
Path: "name",
|
|
|
|
Type: "string",
|
|
|
|
DisableRewrite: false,
|
|
|
|
DefaultValue: nil,
|
|
|
|
IsRequired: false,
|
|
|
|
AllowEmpty: false,
|
|
|
|
AllowZero: false,
|
|
|
|
AllowNil: false,
|
|
|
|
DisableAutoTrimSpace: false,
|
|
|
|
DisableAutoConvert: false,
|
|
|
|
RequiredConditionGroup: [][]define.RequiredCondition{
|
|
|
|
[]define.RequiredCondition{
|
|
|
|
define.RequiredCondition{
|
|
|
|
DependOnField: "age",
|
|
|
|
DependOnFieldType: "int",
|
|
|
|
DependOnFieldStatus: []string{consts.DataStatusIsEmpty, consts.DataStatusIsNil},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ValueLimit: nil,
|
|
|
|
SliceConfig: nil,
|
|
|
|
MapConfig: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := Run(sourceData, ruleListForDepend, nil)
|
|
|
|
if nil != err {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|