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

49
run.go
View File

@ -114,13 +114,42 @@ func getDataStatus(val gjson.Result, dataType string) string {
return "" return ""
} }
// formatInputVal ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:02 2024/5/2
func formatInputVal(val gjson.Result, rule *define.FieldRule) (any, error) {
inputVal := val.Value()
if nil == inputVal {
if rule.IsRequired {
return nil, fmt.Errorf("%v : data is required, but get nil", rule.Path)
}
if rule.DisableAutoConvert {
inputVal = rule.DefaultValue
} else {
inputVal = strings.TrimSpace(rule.DefaultValue)
}
} else {
if !rule.DisableAutoConvert {
if inputValStr, ok := inputVal.(string); ok {
inputVal = inputValStr
}
}
}
return inputVal, nil
}
// validate 验证字段 // validate 验证字段
// //
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 14:32 2024/4/29 // Date : 14:32 2024/4/29
func validate(sourceData []byte, val gjson.Result, rule *define.FieldRule) (any, error) { func validate(sourceData []byte, val gjson.Result, rule *define.FieldRule) (any, error) {
inputVal := val.Value() var (
err error
inputVal any
)
if !val.Exists() { if !val.Exists() {
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)
@ -128,24 +157,10 @@ func validate(sourceData []byte, val gjson.Result, rule *define.FieldRule) (any,
// TODO : 验证有条件必传 // TODO : 验证有条件必传
inputVal = rule.DefaultValue inputVal = rule.DefaultValue
} else { } else {
if nil == inputVal { if inputVal, err = formatInputVal(val, rule); nil != err {
if rule.IsRequired { return nil, err
return nil, fmt.Errorf("%v : data is required, but get nil", rule.Path)
}
if rule.DisableAutoConvert {
inputVal = rule.DefaultValue
} else {
inputVal = strings.TrimSpace(rule.DefaultValue)
}
} else {
if !rule.DisableAutoConvert {
if inputValStr, ok := inputVal.(string); ok {
inputVal = inputValStr
}
}
} }
} }
return handleData(inputVal, rule) return handleData(inputVal, rule)
} }