From 4b94376e7ccd0001e39e317d2c4d626e869b6f1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 24 Jan 2025 18:10:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=8A=A8=E6=80=81=E7=94=9F?= =?UTF-8?q?=E6=88=90=E7=BB=93=E6=9E=84=E4=BD=93=E5=B9=B6=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 6 ++++-- go.sum | 4 ++-- v10/validate_value.go | 25 ++++++++++++++++++++++++ v10/validate_value_test.go | 39 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 v10/validate_value_test.go diff --git a/go.mod b/go.mod index e42bacc..6d5fb2f 100644 --- a/go.mod +++ b/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 diff --git a/go.sum b/go.sum index ac2b7e0..76c97de 100644 --- a/go.sum +++ b/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= diff --git a/v10/validate_value.go b/v10/validate_value.go index f19450b..e9bca1f 100644 --- a/v10/validate_value.go +++ b/v10/validate_value.go @@ -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) +} diff --git a/v10/validate_value_test.go b/v10/validate_value_test.go new file mode 100644 index 0000000..7295819 --- /dev/null +++ b/v10/validate_value_test.go @@ -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) +}