From 4811e08f0b7e1f9f512929c9a0e9128b64dd0d7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Thu, 11 Jul 2024 18:27:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E9=AA=8C=E8=AF=81=E6=94=AF?= =?UTF-8?q?=E6=8C=81*=5Fptr?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- run.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/run.go b/run.go index 6f58b73..b060c28 100644 --- a/run.go +++ b/run.go @@ -84,12 +84,12 @@ func RunWithResult(sourceData map[string]any, ruleList []*define.FieldRule) ([]b // 通过有条件必传, 填充 is_required checkRuleConditionRequiredRule(byteData, itemRule) inputFieldVal := gjson.GetBytes(byteData, itemRule.Path) - if formatRule, err := validate(byteData, inputFieldVal, itemRule); nil != err { + if formatValue, err := validate(byteData, inputFieldVal, itemRule); nil != err { return nil, err } else { if !itemRule.DisableRewrite { // 更新数据 - byteData, _ = sjson.SetBytes(byteData, itemRule.Path, formatRule) + byteData, _ = sjson.SetBytes(byteData, itemRule.Path, formatValue) } } } @@ -219,10 +219,14 @@ func validate(sourceData []byte, val gjson.Result, rule *define.FieldRule) (any, inputVal any ) - if !val.Exists() { + if !val.Exists() || nil == val.Value() { if rule.IsRequired { return nil, fmt.Errorf("%v : field is required, but not found", rule.Path) } + if strings.HasSuffix(rule.Type, "_ptr") { + // 指针类型数据, 无需验证 + return nil, nil + } inputVal = rule.DefaultValue } else { if inputVal, err = formatInputVal(val, rule); nil != err {