This commit is contained in:
白茶清欢 2024-05-02 18:36:19 +08:00
parent addbd6327a
commit 4e652c64b7

35
run.go
View File

@ -114,20 +114,13 @@ func getDataStatus(val gjson.Result, dataType string) string {
return "" return ""
} }
// validate 验证字段 // formatInputVal ...
// //
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 14:32 2024/4/29 // Date : 18:02 2024/5/2
func validate(sourceData []byte, val gjson.Result, rule *define.FieldRule) (any, error) { func formatInputVal(val gjson.Result, rule *define.FieldRule) (any, error) {
inputVal := val.Value() inputVal := val.Value()
if !val.Exists() {
if rule.IsRequired {
return nil, fmt.Errorf("%v : field is required, but not found", rule.Path)
}
// TODO : 验证有条件必传
inputVal = rule.DefaultValue
} else {
if nil == inputVal { if nil == inputVal {
if rule.IsRequired { if rule.IsRequired {
return nil, fmt.Errorf("%v : data is required, but get nil", rule.Path) return nil, fmt.Errorf("%v : data is required, but get nil", rule.Path)
@ -144,8 +137,30 @@ func validate(sourceData []byte, val gjson.Result, rule *define.FieldRule) (any,
} }
} }
} }
return inputVal, nil
} }
// validate 验证字段
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:32 2024/4/29
func validate(sourceData []byte, val gjson.Result, rule *define.FieldRule) (any, error) {
var (
err error
inputVal any
)
if !val.Exists() {
if rule.IsRequired {
return nil, fmt.Errorf("%v : field is required, but not found", rule.Path)
}
// TODO : 验证有条件必传
inputVal = rule.DefaultValue
} else {
if inputVal, err = formatInputVal(val, rule); nil != err {
return nil, err
}
}
return handleData(inputVal, rule) return handleData(inputVal, rule)
} }