支持动态生成结构体并验证
This commit is contained in:
parent
50440cc8f9
commit
4b94376e7c
6
go.mod
6
go.mod
@ -6,7 +6,8 @@ require (
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250122075709-5ecf3edb4a00
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241223084948-de2e49144fcd
|
||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e
|
||||
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241223040803-7b56590b5791
|
||||
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20250124091620-c757e551a8c9
|
||||
github.com/go-playground/validator/v10 v10.24.0
|
||||
github.com/smartystreets/goconvey v1.8.1
|
||||
github.com/stretchr/testify v1.9.0
|
||||
github.com/tidwall/gjson v1.18.0
|
||||
@ -22,7 +23,6 @@ require (
|
||||
github.com/go-ini/ini v1.67.0 // indirect
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||
github.com/go-playground/validator/v10 v10.24.0 // indirect
|
||||
github.com/gopherjs/gopherjs v1.17.2 // indirect
|
||||
github.com/jtolds/gls v4.20.0+incompatible // indirect
|
||||
github.com/leodido/go-urn v1.4.0 // indirect
|
||||
@ -41,3 +41,5 @@ require (
|
||||
)
|
||||
|
||||
replace git.zhangdeman.cn/zhangdeman/consts => ../consts
|
||||
|
||||
replace git.zhangdeman.cn/zhangdeman/wrapper => ../wrapper
|
||||
|
4
go.sum
4
go.sum
@ -4,8 +4,6 @@ git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241223084948-de2e49144fcd h1:q7G
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241223084948-de2e49144fcd/go.mod h1:+D6uPSljwHywjVY5WSBY4TRVMj26TN5f5cFGEYMldjs=
|
||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e h1:Q973S6CcWr1ICZhFI1STFOJ+KUImCl2BaIXm6YppBqI=
|
||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e/go.mod h1:VpPjBlwz8U+OxZuxzHQBv1aEEZ3pStH6bZvT21ADEbI=
|
||||
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241223040803-7b56590b5791 h1:TRCgenNef5rssrQ+47R5lToVrA0RUvj8ebXuO7AP/yk=
|
||||
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241223040803-7b56590b5791/go.mod h1:QSdmz0is8gBG8EmCi2KPj0YXifGiU6DP5JxhPIoFtgY=
|
||||
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
|
||||
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
|
||||
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ=
|
||||
@ -16,6 +14,8 @@ github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3G
|
||||
github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8=
|
||||
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
|
||||
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
|
||||
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
|
||||
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
|
||||
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
|
||||
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
|
||||
|
@ -9,6 +9,7 @@ package v10
|
||||
|
||||
import (
|
||||
"git.zhangdeman.cn/gateway/validator/v10/define"
|
||||
"git.zhangdeman.cn/zhangdeman/wrapper"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@ -63,3 +64,27 @@ func ValidateMap(inputMap map[string]any, ruleConfigMap map[string]define.FieldV
|
||||
wg.Wait()
|
||||
return errTable
|
||||
}
|
||||
|
||||
// ValidateDynamicStruct 验证动态结构体
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:28 2025/1/24
|
||||
func ValidateDynamicStruct(inputMap map[string]any, ruleConfigMap map[string]define.FieldValidateGenerateConfig) error {
|
||||
realMapData := make(map[string]any)
|
||||
tagMap := make(map[string]string)
|
||||
for mapKey, mapVal := range inputMap {
|
||||
if _, exist := ruleConfigMap[mapKey]; !exist {
|
||||
continue
|
||||
}
|
||||
realMapData[mapKey] = mapVal
|
||||
express, err := fieldValidatorRuleExpressGenerateFunc(ruleConfigMap[mapKey])
|
||||
if nil != err {
|
||||
return err
|
||||
}
|
||||
tagMap[mapKey] = "validate:\"" + express + "\""
|
||||
}
|
||||
dynamicStruct := wrapper.NewMap2DynamicStruct(realMapData, tagMap)
|
||||
structVal := dynamicStruct.ToStructDefaultValue()
|
||||
return validateInstance.Struct(structVal)
|
||||
}
|
||||
|
39
v10/validate_value_test.go
Normal file
39
v10/validate_value_test.go
Normal file
@ -0,0 +1,39 @@
|
||||
// Package v10 ...
|
||||
//
|
||||
// Description : v10 ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2025-01-24 17:29
|
||||
package v10
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.zhangdeman.cn/gateway/validator/v10/define"
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestValidateDynamicStruct(t *testing.T) {
|
||||
err := ValidateDynamicStruct(map[string]any{
|
||||
"name": "zhang",
|
||||
"age": 15,
|
||||
}, map[string]define.FieldValidateGenerateConfig{
|
||||
"name": {
|
||||
Field: "name",
|
||||
RuleGroup: nil,
|
||||
RuleSimple: define.GenerateRuleExpressConfig{
|
||||
Rule: consts.ValidatorRuleCommonRequired,
|
||||
},
|
||||
},
|
||||
"age": {
|
||||
Field: "age",
|
||||
RuleGroup: nil,
|
||||
RuleSimple: define.GenerateRuleExpressConfig{
|
||||
Rule: consts.ValidatorRuleCommonMax,
|
||||
ParamList: []any{17},
|
||||
},
|
||||
},
|
||||
})
|
||||
fmt.Println(err)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user