From da9aa14f0e4c92b7dad7ee058ec484a6caa0ae0a 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:54:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=95=B0=E6=8D=AE=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define/rule.go | 12 +++++++----- go.mod | 2 +- go.sum | 2 ++ rule_instance.go | 4 ++-- run.go | 8 +++++--- 5 files changed, 17 insertions(+), 11 deletions(-) diff --git a/define/rule.go b/define/rule.go index fc5862d..977ae6c 100644 --- a/define/rule.go +++ b/define/rule.go @@ -7,7 +7,9 @@ // Date : 2024-04-29 10:51 package define -import "git.zhangdeman.cn/zhangdeman/consts/enums" +import ( + "git.zhangdeman.cn/zhangdeman/consts" +) // FieldRule 字段验证规则 // @@ -16,7 +18,7 @@ import "git.zhangdeman.cn/zhangdeman/consts/enums" // Date : 10:52 2024/4/29 type FieldRule struct { Path string `json:"path"` // 字段路径 - Type enums.DataType `json:"type"` // 数据类型, 具体枚举值参见 git.zhangdeman.cn/zhangdeman/consts + Type consts.DataType `json:"type"` // 数据类型, 具体枚举值参见 git.zhangdeman.cn/zhangdeman/consts DisableRewrite bool `json:"disable_rewrite"` // 验证完相关数据类型之后, 不要重新给字段赋值 DefaultValue any `json:"default_value"` // 默认值, 统一以字符串传入, 会转为最终设置的类型 IsRequired bool `json:"is_required"` // 是否必传 @@ -37,9 +39,9 @@ type FieldRule struct { // // Date : 10:58 2024/4/29 type RequiredCondition struct { - 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 + DependOnField string `json:"depend_on_field"` // 依赖数据源中的那一个字段 + DependOnFieldType consts.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 7cc62b1..5688d28 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module git.zhangdeman.cn/gateway/validator go 1.22.2 require ( - git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125061350-1f5050978fc3 + git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125065114-f919222003d9 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-20241125062526-91423fb146e0 diff --git a/go.sum b/go.sum index 532059f..3d1bfe1 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241023090605-10cff9173059 h1:TPAYdT 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/consts v0.0.0-20241125065114-f919222003d9 h1:TP/M3WnGsxh0Vr6YuS1i28hw1oV//YbdCoI46PUBIA0= +git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125065114-f919222003d9/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= diff --git a/rule_instance.go b/rule_instance.go index db27447..8338478 100644 --- a/rule_instance.go +++ b/rule_instance.go @@ -9,7 +9,7 @@ package validator import ( "git.zhangdeman.cn/gateway/validator/define" - "git.zhangdeman.cn/zhangdeman/consts/enums" + "git.zhangdeman.cn/zhangdeman/consts" ) // NewDefaultFieldRule ... @@ -17,7 +17,7 @@ import ( // Author : go_developer@163.com<白茶清欢> // // Date : 14:01 2024/4/29 -func NewDefaultFieldRule(path string, dataType enums.DataType, isRequired bool, defaultValue string) *define.FieldRule { +func NewDefaultFieldRule(path string, dataType consts.DataType, isRequired bool, defaultValue string) *define.FieldRule { r := &define.FieldRule{ Path: path, Type: dataType, diff --git a/run.go b/run.go index 6d93125..884b611 100644 --- a/run.go +++ b/run.go @@ -13,7 +13,6 @@ 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" @@ -127,7 +126,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 enums.DataType) string { +func getDataStatus(val gjson.Result, dataType consts.DataType) string { if !val.Exists() { return consts.DataStatusNotFound } @@ -247,7 +246,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 = enums.DataType(strings.ToLower(rule.Type.String())) + rule.Type = consts.DataType(strings.ToLower(rule.Type.String())) + if !rule.Type.IsValid() { + return nil, fmt.Errorf("%v : data type %v is invalid", rule.Path, rule.Type.String()) + } // 处理真实的map和序列化之后的map if strings.HasPrefix(rule.Type.String(), "map") { if strings.HasSuffix(rule.Type.String(), "_marshal") {