From 0ece3237d7363317bbda4e33c46117914a1d1b06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 25 Nov 2024 14:33:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=B1=BB=E5=9E=8B=E6=9E=9A?= =?UTF-8?q?=E4=B8=BE=E5=80=BC=E9=80=82=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define/rule.go | 10 ++++++---- go.mod | 8 ++++---- go.sum | 8 ++++++++ run.go | 17 +++++++++-------- 4 files changed, 27 insertions(+), 16 deletions(-) diff --git a/define/rule.go b/define/rule.go index 931e84f..fc5862d 100644 --- a/define/rule.go +++ b/define/rule.go @@ -7,6 +7,8 @@ // Date : 2024-04-29 10:51 package define +import "git.zhangdeman.cn/zhangdeman/consts/enums" + // FieldRule 字段验证规则 // // Author : go_developer@163.com<白茶清欢> @@ -14,7 +16,7 @@ package define // Date : 10:52 2024/4/29 type FieldRule struct { Path string `json:"path"` // 字段路径 - Type string `json:"type"` // 数据类型, 具体枚举值参见 git.zhangdeman.cn/zhangdeman/consts + Type enums.DataType `json:"type"` // 数据类型, 具体枚举值参见 git.zhangdeman.cn/zhangdeman/consts DisableRewrite bool `json:"disable_rewrite"` // 验证完相关数据类型之后, 不要重新给字段赋值 DefaultValue any `json:"default_value"` // 默认值, 统一以字符串传入, 会转为最终设置的类型 IsRequired bool `json:"is_required"` // 是否必传 @@ -35,9 +37,9 @@ type FieldRule struct { // // Date : 10:58 2024/4/29 type RequiredCondition struct { - DependOnField string `json:"depend_on_field"` // 依赖数据源中的那一个字段 - DependOnFieldType string `json:"depend_on_field_type"` // 依赖数据源数据类型 - DependOnFieldStatus []string `json:"depend_on_field_status"` // 依赖数据状态 : NOT_FOUND / IS_NIL / IS_ZERO / IS_EMPTY / IS_FALSE + DependOnField string `json:"depend_on_field"` // 依赖数据源中的那一个字段 + DependOnFieldType enums.DataType `json:"depend_on_field_type"` // 依赖数据源数据类型 + DependOnFieldStatus []string `json:"depend_on_field_status"` // 依赖数据状态 : NOT_FOUND / IS_NIL / IS_ZERO / IS_EMPTY / IS_FALSE } // ValueLimit 取值的限制 diff --git a/go.mod b/go.mod index aaf07e3..7cc62b1 100644 --- a/go.mod +++ b/go.mod @@ -3,17 +3,17 @@ module git.zhangdeman.cn/gateway/validator go 1.22.2 require ( - git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241023090605-10cff9173059 - git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241023104258-2e0a298aa558 + git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125061350-1f5050978fc3 + git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241108082010-42ae8fe5ebdc git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e - git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241024135251-6dd5f7ac6254 + git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241125062526-91423fb146e0 github.com/stretchr/testify v1.9.0 github.com/tidwall/gjson v1.18.0 github.com/tidwall/sjson v1.2.5 ) require ( - git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20241024134352-ce2d84d282ba // indirect + git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20241101082529-28a6c68e38a4 // indirect git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0 // indirect github.com/BurntSushi/toml v1.4.0 // indirect github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 // indirect diff --git a/go.sum b/go.sum index 6818885..532059f 100644 --- a/go.sum +++ b/go.sum @@ -1,15 +1,23 @@ git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241023090605-10cff9173059 h1:TPAYdTKKUjgxtCnK38d1Tb4teyQp1C7wYHPdR32yZtM= git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241023090605-10cff9173059/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= +git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125061350-1f5050978fc3 h1:/40XIygeSxRhPQc3/7pKGpV5hg8jwrMwh1+YiyCHdNI= +git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125061350-1f5050978fc3/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20241024134352-ce2d84d282ba h1:hgajrPZGoDY9P+x6iqcS06pnu5t+N7DOfpmRwb+TZ4s= git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20241024134352-ce2d84d282ba/go.mod h1:V4Dfg1v/JVIZGEKCm6/aehs8hK+Xow1dkL1yiQymXlQ= +git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20241101082529-28a6c68e38a4 h1:s6d4b6yY+NaK1AzoBD1pxqsuygEHQz0Oie86c45geDw= +git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20241101082529-28a6c68e38a4/go.mod h1:V4Dfg1v/JVIZGEKCm6/aehs8hK+Xow1dkL1yiQymXlQ= git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0 h1:gUDlQMuJ4xNfP2Abl1Msmpa3fASLWYkNlqDFF/6GN0Y= git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0/go.mod h1:VHb9qmhaPDAQDcS6vUiDCamYjZ4R5lD1XtVsh55KsMI= git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241023104258-2e0a298aa558 h1:ZVJyyDKfYeA3TsN8UOi4IprkouK4wIIfCKe+F9byLWA= git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241023104258-2e0a298aa558/go.mod h1:/9eicss/Dt9tp2jwZ/4cXDqDKo/Dez+HuT5/NGdqW+s= +git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241108082010-42ae8fe5ebdc h1:jtdEMr/xNchJDEoCnvMr4JXT9+biYQu625Cj+dz025w= +git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241108082010-42ae8fe5ebdc/go.mod h1:XqgER4jDYwskFgj2riJ9XptIjzgYWubY+Zq8iB2WkY0= 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-20241024135251-6dd5f7ac6254 h1:20NfKC/5EwTpSLodvXCPM2CmKWuDgggratYB2r9ihLM= git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241024135251-6dd5f7ac6254/go.mod h1:7D7XZqEMCHSTnmDM4t3xWcrpJn1TC2EyDLd9ScP5gN4= +git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241125062526-91423fb146e0 h1:aGQADmQKTF7c8+s3acnn569sTJwjlLhhhZfQ6f4nAH0= +git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241125062526-91423fb146e0/go.mod h1:lvVOIrYDvCQHUzBdaOwKSrxT9ubcXQJBsafWMWu+I14= 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= diff --git a/run.go b/run.go index 44d3bcd..6d93125 100644 --- a/run.go +++ b/run.go @@ -13,6 +13,7 @@ import ( "fmt" "git.zhangdeman.cn/gateway/validator/define" "git.zhangdeman.cn/zhangdeman/consts" + "git.zhangdeman.cn/zhangdeman/consts/enums" "github.com/tidwall/gjson" "github.com/tidwall/sjson" "strings" @@ -126,7 +127,7 @@ func checkRuleConditionRequiredRule(sourceData []byte, rule *define.FieldRule) { // Author : go_developer@163.com<白茶清欢> // // Date : 15:59 2024/4/29 -func getDataStatus(val gjson.Result, dataType string) string { +func getDataStatus(val gjson.Result, dataType enums.DataType) string { if !val.Exists() { return consts.DataStatusNotFound } @@ -142,17 +143,17 @@ func getDataStatus(val gjson.Result, dataType string) string { return consts.DataStatusIsZero } default: - if strings.HasPrefix(dataType, "[]") { + if strings.HasPrefix(dataType.String(), "[]") { // 数组 if len(val.Array()) == 0 { return consts.DataStatusIsEmpty } - } else if strings.HasPrefix(dataType, "map") { + } else if strings.HasPrefix(dataType.String(), "map") { // 对象 if len(val.Map()) == 0 { return consts.DataStatusIsEmpty } - } else if strings.HasPrefix(dataType, "*") { + } else if strings.HasPrefix(dataType.String(), "*") { // 指针类型 if nil == val.Value() { return consts.DataStatusIsNil @@ -227,7 +228,7 @@ func validate(sourceData []byte, val gjson.Result, rule *define.FieldRule) (any, if rule.IsRequired { return nil, fmt.Errorf("%v : field is required, but not found", rule.Path) } - if strings.HasSuffix(rule.Type, "_ptr") { + if strings.HasSuffix(rule.Type.String(), "_ptr") { // 指针类型数据, 无需验证 return nil, nil } @@ -246,10 +247,10 @@ func validate(sourceData []byte, val gjson.Result, rule *define.FieldRule) (any, // // Date : 14:43 2024/4/29 func handleData(inputVal any, rule *define.FieldRule) (any, error) { - rule.Type = strings.ToLower(rule.Type) + rule.Type = enums.DataType(strings.ToLower(rule.Type.String())) // 处理真实的map和序列化之后的map - if strings.HasPrefix(rule.Type, "map") { - if strings.HasSuffix(rule.Type, "_marshal") { + if strings.HasPrefix(rule.Type.String(), "map") { + if strings.HasSuffix(rule.Type.String(), "_marshal") { rule.MapConfig = &define.MapConfig{Mode: consts.DataMapModelMarshal} } else { rule.MapConfig = &define.MapConfig{Mode: consts.DataMapModelReal}