数据验证支持*_ptr

This commit is contained in:
白茶清欢 2024-07-11 18:27:40 +08:00
parent 3b4302677b
commit 4811e08f0b

10
run.go
View File

@ -84,12 +84,12 @@ func RunWithResult(sourceData map[string]any, ruleList []*define.FieldRule) ([]b
// 通过有条件必传, 填充 is_required // 通过有条件必传, 填充 is_required
checkRuleConditionRequiredRule(byteData, itemRule) checkRuleConditionRequiredRule(byteData, itemRule)
inputFieldVal := gjson.GetBytes(byteData, itemRule.Path) 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 return nil, err
} else { } else {
if !itemRule.DisableRewrite { 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 inputVal any
) )
if !val.Exists() { if !val.Exists() || nil == val.Value() {
if rule.IsRequired { if rule.IsRequired {
return nil, fmt.Errorf("%v : field is required, but not found", rule.Path) 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 inputVal = rule.DefaultValue
} else { } else {
if inputVal, err = formatInputVal(val, rule); nil != err { if inputVal, err = formatInputVal(val, rule); nil != err {