127 lines
3.9 KiB
Go
127 lines
3.9 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"
|
|
"git.zhangdeman.cn/zhangdeman/wrapper/op_string"
|
|
)
|
|
|
|
// 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.ValidatorRuleLte.String(), consts.ValidatorRuleGte.String(), consts.ValidatorRuleGt.String(), consts.ValidatorRuleLt.String(), // 数字取值范围
|
|
consts.ValidatorRuleCommonLen.String(), consts.ValidatorRuleCommonMin.String(), consts.ValidatorRuleCommonMax.String(): // 长度取值范围
|
|
var val float64
|
|
if err := utilPkg.ConvertAssign(&val, ruleValue); nil == err {
|
|
switch ruleType {
|
|
case consts.ValidatorRuleLte.String():
|
|
rule.Lte = &val
|
|
case consts.ValidatorRuleGte.String():
|
|
rule.Gte = &val
|
|
case consts.ValidatorRuleGt.String():
|
|
rule.Gt = &val
|
|
case consts.ValidatorRuleLt.String():
|
|
rule.Lt = &val
|
|
case consts.ValidatorRuleCommonLen.String():
|
|
rule.Len = &val
|
|
case consts.ValidatorRuleCommonMin.String():
|
|
rule.Min = &val
|
|
case consts.ValidatorRuleCommonMax.String():
|
|
rule.Max = &val
|
|
}
|
|
}
|
|
case consts.ValidatorRuleEq.String():
|
|
rule.Eq = ruleValue
|
|
case consts.ValidatorRuleNe.String():
|
|
rule.Ne = ruleValue
|
|
case consts.ValidatorRuleRegexp.String():
|
|
rule.Regexp = ruleValue
|
|
case consts.ValidatorRuleCommonOneOf.String():
|
|
rule.Oneof = make([]any, 0)
|
|
switch dataKind {
|
|
case reflect.String:
|
|
valList := strings.Split(ruleValue, " ")
|
|
for _, item := range valList {
|
|
rule.Oneof = append(rule.Oneof, item)
|
|
}
|
|
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint,
|
|
reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int,
|
|
reflect.Float32, reflect.Float64:
|
|
valList := op_string.ToBaseTypeSlice[float64](ruleValue, " ").Value
|
|
for _, item := range valList {
|
|
rule.Oneof = append(rule.Oneof, item)
|
|
}
|
|
default:
|
|
}
|
|
}
|
|
}
|
|
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 *float64 `json:"len" dc:"长度等于"`
|
|
Max *float64 `json:"max" dc:"长度小于等于"`
|
|
Min *float64 `json:"min" dc:"长度大于等于"`
|
|
Eq any `json:"eq" dc:"等于"`
|
|
Ne any `json:"ne" dc:"不等于"`
|
|
Oneof []any `json:"oneof" dc:"枚举值列表"`
|
|
Regexp string `json:"regexp" dc:"正则表达式"`
|
|
}
|