112 lines
3.3 KiB
Go
112 lines
3.3 KiB
Go
// Package util ...
|
|
//
|
|
// Description : util ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 2026-01-05 18:48
|
|
package util
|
|
|
|
import (
|
|
"reflect"
|
|
"strings"
|
|
|
|
"git.zhangdeman.cn/zhangdeman/consts"
|
|
utilPkg "git.zhangdeman.cn/zhangdeman/util"
|
|
)
|
|
|
|
// ParseValidateRule 解析验证规则
|
|
func ParseValidateRule(dataType reflect.Type, ruleStr string) ValidateRule {
|
|
if dataType.Kind() == reflect.Ptr {
|
|
dataType = dataType.Elem()
|
|
}
|
|
dataKind := dataType.Kind()
|
|
rule := ValidateRule{
|
|
Omitempty: false,
|
|
Required: false,
|
|
Lte: nil,
|
|
Gte: nil,
|
|
Lt: nil,
|
|
Gt: nil,
|
|
Len: nil,
|
|
Max: nil,
|
|
Min: nil,
|
|
Eq: nil,
|
|
Ne: nil,
|
|
Oneof: nil,
|
|
}
|
|
ruleList := strings.Split(ruleStr, ",")
|
|
for _, itemRule := range ruleList {
|
|
itemRule = strings.ToLower(strings.TrimSpace(itemRule))
|
|
if len(itemRule) == 0 {
|
|
continue
|
|
}
|
|
if strings.Contains(itemRule, "=") {
|
|
// 一定是无需要值的验证规则
|
|
switch itemRule {
|
|
case consts.ValidatorRuleCommonRequired.String(): // 必传
|
|
rule.Required = true
|
|
case consts.ValidatorRuleCommonOmitempty.String(): // 为空则不校验
|
|
rule.Omitempty = true
|
|
}
|
|
continue
|
|
}
|
|
itemRuleArr := strings.Split(itemRule, "=")
|
|
ruleType := itemRuleArr[0]
|
|
ruleValue := strings.Join(itemRuleArr[1:], "=")
|
|
if len(ruleValue) == 0 {
|
|
// 未配置值的校验规则
|
|
continue
|
|
}
|
|
switch ruleType {
|
|
case consts.ValidateRuleLte.String(), consts.ValidateRuleGte.String(), consts.ValidateRuleGt.String(), consts.ValidateRuleLt.String(): // 数字取值范围
|
|
var val float64
|
|
if err := utilPkg.ConvertAssign(&val, ruleValue); nil == err {
|
|
switch ruleType {
|
|
case consts.ValidateRuleLte.String():
|
|
rule.Lte = &val
|
|
case consts.ValidateRuleGte.String():
|
|
rule.Gte = &val
|
|
case consts.ValidateRuleGt.String():
|
|
rule.Gt = &val
|
|
case consts.ValidateRuleLt.String():
|
|
rule.Lt = &val
|
|
}
|
|
}
|
|
case consts.ValidatorRuleCommonLen.String(), consts.ValidatorRuleCommonMin.String(), consts.ValidatorRuleCommonMax.String(): // 长度取值范围
|
|
var val uint
|
|
if err := utilPkg.ConvertAssign(&val, ruleValue); nil == err {
|
|
switch ruleType {
|
|
case consts.ValidatorRuleCommonLen.String():
|
|
rule.Len = &val
|
|
case consts.ValidatorRuleCommonMin.String():
|
|
rule.Min = &val
|
|
case consts.ValidatorRuleCommonMax.String():
|
|
rule.Max = &val
|
|
}
|
|
}
|
|
case consts.ValidateRuleEq.String():
|
|
rule.Eq = ruleValue
|
|
case consts.ValidateRuleNe.String():
|
|
rule.Ne = ruleValue
|
|
case consts.ValidatorRuleCommonOneOf.String():
|
|
}
|
|
}
|
|
return rule
|
|
}
|
|
|
|
type ValidateRule struct {
|
|
Omitempty bool `json:"omitempty" dc:"为空则不校验"`
|
|
Required bool `json:"required" dc:"必传校验"`
|
|
Lte *float64 `json:"lte" dc:"数字类型小于等于"`
|
|
Gte *float64 `json:"gte" dc:"数字类型大于等于"`
|
|
Lt *float64 `json:"lt" dc:"数字类型小于"`
|
|
Gt *float64 `json:"gt" dc:"数字类型大于"`
|
|
Len *uint `json:"len" dc:"长度等于"`
|
|
Max *uint `json:"max" dc:"长度小于等于"`
|
|
Min *uint `json:"min" dc:"长度大于等于"`
|
|
Eq any `json:"eq" dc:"等于"`
|
|
Ne any `json:"ne" dc:"不等于"`
|
|
Oneof []any `json:"oneof" dc:"枚举值列表"`
|
|
}
|