增加单元测试 + 修复BUG

This commit is contained in:
2024-05-02 17:57:28 +08:00
parent 867ff22e6d
commit addbd6327a
4 changed files with 193 additions and 8 deletions

18
run.go
View File

@ -128,11 +128,21 @@ func validate(sourceData []byte, val gjson.Result, rule *define.FieldRule) (any,
// TODO : 验证有条件必传
inputVal = rule.DefaultValue
} else {
if inputVal == nil {
if !rule.AllowNil {
return nil, fmt.Errorf("%v : field value is nil, but not allowed", rule.Path)
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
}
}
inputVal = rule.DefaultValue
}
}