数据验证支持*_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
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 {