Compare commits

..

No commits in common. "d87fb61db55ba037aac92f313d4f0f01754800ce" and "98d9eec6bbc3a520f38c31217b0861c7823eac28" have entirely different histories.

3 changed files with 17 additions and 20 deletions

4
go.mod
View File

@ -3,10 +3,10 @@ module git.zhangdeman.cn/gateway/validator
go 1.22.2
require (
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240924065029-c865046cd9e7
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240823041145-d4df71cf37e5
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240618035451-8d48a6bd39dd
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240924063449-ef80c6cb79d1
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240823103024-c38d16dc28d3
github.com/stretchr/testify v1.9.0
github.com/tidwall/gjson v1.17.3
github.com/tidwall/sjson v1.2.5

4
go.sum
View File

@ -10,8 +10,6 @@ git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240709134122-e1e2a2e421de h1:ksjcMH
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240709134122-e1e2a2e421de/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240823041145-d4df71cf37e5 h1:pmIHln0gWW+5xAB762h3WDsRkZuYLUDndvJDsGMKoOY=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240823041145-d4df71cf37e5/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240924065029-c865046cd9e7 h1:tyCPCMK+68PZ0axZylQHitMVp1d5mzNr9/YqMHXqo+A=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240924065029-c865046cd9e7/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211 h1:I/wOsRpCSRkU9vo1u703slQsmK0wnNeZzsWQOGtIAG0=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211/go.mod h1:SrtvrQRdzt+8KfYzvosH++gWxo2ShPTzR1m3VQ6uX7U=
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0 h1:gUDlQMuJ4xNfP2Abl1Msmpa3fASLWYkNlqDFF/6GN0Y=
@ -36,8 +34,6 @@ git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240627031706-9ff1c213bb50 h1:olo34
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240627031706-9ff1c213bb50/go.mod h1:US/pcq2vstE3iyxIHf53w8IeXKkZys7bj/ozLWkRYeE=
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240823103024-c38d16dc28d3 h1:RcWNxrHmhZksZWrP/HLEwAM8uIIHYlPLQ20HnLzC+j0=
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240823103024-c38d16dc28d3/go.mod h1:KcojKP22mv9/IZrQWlIBfa1EuBxtEOqfWMgN3SYK2N8=
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240924063449-ef80c6cb79d1 h1:LYw8NJeWcOiyrGjH9weyxnaMit94MlIngL+uskbLjtw=
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240924063449-ef80c6cb79d1/go.mod h1:+2qNxuRsfyfOvXk9HNwn+CmyPmmhhrQm/eIi1FDU1jw=
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=

29
run.go
View File

@ -23,9 +23,9 @@ import (
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:18 2024/4/29
func RunForStruct(sourceData any, ruleList []*define.FieldRule, receiver any) error {
func RunForStruct(sourceData any, ruleList []*define.FieldRule) (map[string]any, error) {
if nil == sourceData {
return nil
return map[string]any{}, nil
}
byteData, _ := json.Marshal(sourceData)
var (
@ -36,9 +36,12 @@ func RunForStruct(sourceData any, ruleList []*define.FieldRule, receiver any) er
d := json.NewDecoder(bytes.NewReader(byteData))
d.UseNumber()
if err = d.Decode(&sourceMapData); nil != err {
return err
return nil, err
}
return Run(sourceMapData, ruleList, receiver)
if err = Run(sourceMapData, ruleList); nil != err {
return nil, err
}
return sourceMapData, nil
}
// Run 运行参数验证
@ -46,7 +49,7 @@ func RunForStruct(sourceData any, ruleList []*define.FieldRule, receiver any) er
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:13 2024/4/29
func Run(sourceData map[string]any, ruleList []*define.FieldRule, receiver any) error {
func Run(sourceData map[string]any, ruleList []*define.FieldRule) error {
for _, itemRule := range ruleList {
itemRule.DefaultValue = nil
}
@ -54,12 +57,11 @@ func Run(sourceData map[string]any, ruleList []*define.FieldRule, receiver any)
if nil != err {
return err
}
if nil != receiver {
d := json.NewDecoder(bytes.NewReader(byteData))
d.UseNumber()
if err := d.Decode(&receiver); nil != err {
return err
}
sourceData = make(map[string]any)
d := json.NewDecoder(bytes.NewReader(byteData))
d.UseNumber()
if err := d.Decode(&sourceData); nil != err {
return err
}
return nil
}
@ -74,7 +76,6 @@ func RunWithResult(sourceData map[string]any, ruleList []*define.FieldRule) ([]b
sourceData = make(map[string]any)
}
byteData, _ := json.Marshal(sourceData)
resultByteData, _ := json.Marshal(sourceData)
for _, itemRule := range ruleList {
if len(itemRule.Path) == 0 {
// 未指定验证数据位置
@ -88,11 +89,11 @@ func RunWithResult(sourceData map[string]any, ruleList []*define.FieldRule) ([]b
} else {
if !itemRule.DisableRewrite {
// 更新数据
resultByteData, _ = sjson.SetBytes(resultByteData, itemRule.Path, formatValue)
byteData, _ = sjson.SetBytes(byteData, itemRule.Path, formatValue)
}
}
}
return resultByteData, nil
return byteData, nil
}
// checkRuleConditionRequiredRule 校验有条件必传