validate/validate_test.go

86 lines
1.6 KiB
Go

// Package validate ...
//
// Description : validate ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2025-03-18 17:36
package validate
import (
"encoding/json"
"fmt"
"git.zhangdeman.cn/zhangdeman/consts"
"testing"
)
// TestRun_Simple_Data 无嵌套、无复杂数据类型的处理
func TestRun_Simple_Data(t *testing.T) {
userInfo := map[string]any{
"age": 1800,
"height": 179.5,
"name": "baicha",
}
companyInfo := map[string]any{
"address": "beijing",
"name": "xxxx",
}
testMap := map[string]any{
"user": userInfo,
"company": companyInfo,
}
sourceByteData, _ := json.Marshal(testMap)
fieldList := []StructField{
{
JsonTag: "user_age",
Type: consts.DataTypeInt,
Required: false,
RuleList: []Rule{
{
Tag: "min",
Args: []string{"1"},
},
{
Tag: "max",
Args: []string{"2000"},
},
},
DefaultValue: "",
SourcePath: "user.age",
TargetPath: "user_age",
Errmsg: "年龄必须在[1,2000]之间",
},
{
JsonTag: "company",
Type: consts.DataTypeString,
Required: false,
RuleList: []Rule{
{
Tag: "required",
},
},
DefaultValue: "",
SourcePath: "company",
TargetPath: "company",
Errmsg: "公司信息必传",
},
{
JsonTag: "company_name",
Type: consts.DataTypeString,
Required: false,
RuleList: []Rule{
{
Tag: "required",
},
},
DefaultValue: "",
SourcePath: "company.name",
TargetPath: "company.company_name",
Errmsg: "公司名称必须在[1,20]之间",
},
}
res, err := Run(sourceByteData, fieldList)
fmt.Println(err, string(res))
}