From 3814638e1cb52ed8ec44e30b75f3615968aa103b 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, 31 Mar 2025 16:38:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=85=88format,=20=E5=9C=A8?= =?UTF-8?q?=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- validate.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/validate.go b/validate.go index 2a63152..ac4e8e0 100644 --- a/validate.go +++ b/validate.go @@ -20,6 +20,7 @@ import ( "github.com/tidwall/gjson" "github.com/tidwall/sjson" "strings" + jsonFilter "git.zhangdeman.cn/zhangdeman/json_filter" ) var validatorInstance *validator.Validate @@ -35,8 +36,23 @@ func init() { // // Date : 15:12 2025/3/18 func Run(sourceData []byte, fieldList []StructField) ([]byte, error) { + mapRuleList := make([]jsonFilter.MapRule, 0) + for _, itemParam := range fieldList { + mapRuleList = append(mapRuleList, jsonFilter.MapRule{ + SourcePath: itemParam.SourcePath, + TargetPath: itemParam.TargetPath, + Required: false, + DataType: itemParam.Type, + DefaultValue: itemParam.DefaultValue, + }) + } + filterInstance := jsonFilter.NewFilter(string(sourceData), mapRuleList) + if err := filterInstance.Deal(); nil != err { + return nil, err + } + formatByteData := filterInstance.Byte() handleInstance := &handle{ - sourceData: sourceData, + sourceData: formatByteData, fieldList: fieldList, parentFieldTable: map[string]bool{}, } @@ -70,6 +86,9 @@ type handle struct { // Run 执行验证 func (h *handle) Run() ([]byte, error) { + if len(h.fieldList) == 0 { + return []byte("{}"), nil + } for _, field := range h.fieldList { if len(field.Errmsg) == 0 { field.Errmsg = field.JsonTag + " : 参数校验不通过"