feat: 完成validate 10 验证规则解析
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
||||
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
utilPkg "git.zhangdeman.cn/zhangdeman/util"
|
||||
"git.zhangdeman.cn/zhangdeman/wrapper/op_string"
|
||||
)
|
||||
|
||||
// ParseValidateRule 解析验证规则
|
||||
@@ -59,24 +60,19 @@ func ParseValidateRule(dataType reflect.Type, ruleStr string) ValidateRule {
|
||||
continue
|
||||
}
|
||||
switch ruleType {
|
||||
case consts.ValidateRuleLte.String(), consts.ValidateRuleGte.String(), consts.ValidateRuleGt.String(), consts.ValidateRuleLt.String(): // 数字取值范围
|
||||
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.ValidateRuleLte.String():
|
||||
case consts.ValidatorRuleLte.String():
|
||||
rule.Lte = &val
|
||||
case consts.ValidateRuleGte.String():
|
||||
case consts.ValidatorRuleGte.String():
|
||||
rule.Gte = &val
|
||||
case consts.ValidateRuleGt.String():
|
||||
case consts.ValidatorRuleGt.String():
|
||||
rule.Gt = &val
|
||||
case consts.ValidateRuleLt.String():
|
||||
case consts.ValidatorRuleLt.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():
|
||||
@@ -85,11 +81,29 @@ func ParseValidateRule(dataType reflect.Type, ruleStr string) ValidateRule {
|
||||
rule.Max = &val
|
||||
}
|
||||
}
|
||||
case consts.ValidateRuleEq.String():
|
||||
case consts.ValidatorRuleEq.String():
|
||||
rule.Eq = ruleValue
|
||||
case consts.ValidateRuleNe.String():
|
||||
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
|
||||
@@ -102,10 +116,11 @@ type ValidateRule struct {
|
||||
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:"长度大于等于"`
|
||||
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:"正则表达式"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user