Merge pull request '优化数据验证' (#1) from feature/upgrade into master

Reviewed-on: #1
This commit is contained in:
白茶清欢 2024-09-25 17:17:43 +08:00
commit d87fb61db5
3 changed files with 20 additions and 17 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-20240823041145-d4df71cf37e5
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240924065029-c865046cd9e7
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-20240823103024-c38d16dc28d3
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240924063449-ef80c6cb79d1
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,6 +10,8 @@ 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=
@ -34,6 +36,8 @@ 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=

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