增加默认表达式生成方法,同时支持自定义表达式生成方法,覆盖默认行为

This commit is contained in:
白茶清欢 2025-01-24 11:15:59 +08:00
parent 978e52089c
commit 064f5e2c5b
3 changed files with 64 additions and 4 deletions

View File

@ -15,12 +15,12 @@ import "git.zhangdeman.cn/zhangdeman/consts"
// //
// Date : 18:46 2025/1/23 // Date : 18:46 2025/1/23
// //
// 参数说明 // 参数说明
// //
// - validateRule: 参数验证规则, 具体参见静态库声明 // - validateRule: 参数验证规则, 具体参见静态库声明
// - paramList: 验证规则需要的参数列表, 可以为空 // - paramList: 验证规则需要的参数列表, 可以为空
// //
// 返回值说明 // 返回值说明
// - express: 生成的表达式 // - express: 生成的表达式
// - err: 表达式生成过程中出现的异常 // - err: 表达式生成过程中出现的异常
type IValidateRuleGenerateFunc func(validateRule consts.ValidatorRule, paramList ...any) (express string, err error) type IValidateRuleGenerateFunc func(validateRule consts.ValidatorRule, paramList ...any) (express string, err error)

35
v10/default.go Normal file
View File

@ -0,0 +1,35 @@
// Package v10 ...
//
// Description : v10 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2025-01-24 10:32
package v10
import (
"errors"
"fmt"
"git.zhangdeman.cn/zhangdeman/consts"
"strings"
)
// DefaultValidateRuleGenerateFunc 验证规则生成的默认方法
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:33 2025/1/24
func DefaultValidateRuleGenerateFunc(validateRule consts.ValidatorRule, paramList ...any) (express string, err error) {
ruleConfig := validateRule.Config()
if len(ruleConfig.ValidatorRule.String()) == 0 {
return "", errors.New("validate rule is empty")
}
if len(paramList) == 0 {
return ruleConfig.ValidatorRule.String(), nil
}
paramStrList := make([]string, 0)
for _, param := range paramList {
paramStrList = append(paramStrList, fmt.Sprintf("%v", param))
}
return ruleConfig.ValidatorRule.String() + "=" + strings.Join(paramStrList, " "), nil
}

25
v10/init.go Normal file
View File

@ -0,0 +1,25 @@
// Package v10 ...
//
// Description : v10 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2025-01-24 11:09
package v10
import "git.zhangdeman.cn/gateway/validator/v10/abstract"
// validatorRuleExpressGenerateFunc 验证规则表达式生成方法
var validatorRuleExpressGenerateFunc abstract.IValidateRuleGenerateFunc = DefaultValidateRuleGenerateFunc
// SetValidatorRuleExpressGenerateFunc 使用自定义的验证规则表达式函数覆盖默认的函数
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:11 2025/1/24
func SetValidatorRuleExpressGenerateFunc(generateFunc abstract.IValidateRuleGenerateFunc) {
if nil == generateFunc {
return
}
validatorRuleExpressGenerateFunc = generateFunc
}