参数增加必传解析
This commit is contained in:
55
validateRule.go
Normal file
55
validateRule.go
Normal file
@ -0,0 +1,55 @@
|
||||
// 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"
|
||||
"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
|
||||
}
|
||||
|
||||
// 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