修复默认值解析以及枚举值解析的BUG
This commit is contained in:
102
validate_rule.go
Normal file
102
validate_rule.go
Normal file
@ -0,0 +1,102 @@
|
||||
// Package api_doc ...
|
||||
//
|
||||
// Description : api_doc ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2025-02-13 15:26
|
||||
package api_doc
|
||||
|
||||
import (
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
ValidateRule = validateRule{}
|
||||
)
|
||||
|
||||
type validateRule struct{}
|
||||
|
||||
// IsRequired 判断是否必传
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:32 2025/2/13
|
||||
func (r validateRule) IsRequired(structField reflect.StructField) bool {
|
||||
ruleTable := r.getValidateRuleTable(structField)
|
||||
_, exist := ruleTable[consts.ValidatorRuleCommonRequired.String()]
|
||||
// 存在即为必传
|
||||
return exist
|
||||
}
|
||||
|
||||
// Enum 获取枚举值
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:23 2025/2/13
|
||||
func (r validateRule) Enum(structField reflect.StructField) []any {
|
||||
ruleTable := r.getValidateRuleTable(structField)
|
||||
oneOfValue, _ := ruleTable[consts.ValidatorRuleCommonOneOf.String()]
|
||||
if len(oneOfValue) == 0 {
|
||||
return []any{}
|
||||
}
|
||||
fieldType := structField.Type.Kind().String()
|
||||
valStrArr := strings.Split(oneOfValue, " ")
|
||||
anySlice := make([]any, 0)
|
||||
for _, val := range valStrArr {
|
||||
if strings.HasPrefix(fieldType, "int") {
|
||||
i, _ := strconv.Atoi(val)
|
||||
anySlice = append(anySlice, i)
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(fieldType, "uint") {
|
||||
uintVal, _ := strconv.ParseUint(val, 10, 64)
|
||||
anySlice = append(anySlice, uintVal)
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(fieldType, "float") {
|
||||
floatVal, _ := strconv.ParseFloat(val, 64)
|
||||
anySlice = append(anySlice, floatVal)
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(fieldType, "string") {
|
||||
anySlice = append(anySlice, val)
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(fieldType, "bool") {
|
||||
if val == "true" {
|
||||
anySlice = append(anySlice, true)
|
||||
} else {
|
||||
anySlice = append(anySlice, false)
|
||||
}
|
||||
continue
|
||||
}
|
||||
}
|
||||
return anySlice
|
||||
}
|
||||
|
||||
// getValidateRuleTable 解析验证规则表
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:29 2025/2/13
|
||||
func (r validateRule) getValidateRuleTable(structField reflect.StructField) map[string]string {
|
||||
res := map[string]string{}
|
||||
ruleStr := ParseStructField.GetValidateRule(structField)
|
||||
if len(ruleStr) == 0 {
|
||||
return res
|
||||
}
|
||||
expressList := strings.Split(ruleStr, ",")
|
||||
for _, item := range expressList {
|
||||
if strings.Contains(item, "=") {
|
||||
arr := strings.Split(item, "=")
|
||||
res[strings.TrimSpace(arr[0])] = strings.Join(arr[1:], "=")
|
||||
} else {
|
||||
res[strings.TrimSpace(item)] = ""
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
Reference in New Issue
Block a user