增加字符串子串的验证

This commit is contained in:
白茶清欢 2024-05-16 21:38:52 +08:00
parent 050fdcd504
commit 6a9339c7de
3 changed files with 31 additions and 2 deletions

View File

@ -58,7 +58,8 @@ type ValueLimit struct {
// //
// Date : 11:16 2024/4/29 // Date : 11:16 2024/4/29
type StringValueLimit struct { type StringValueLimit struct {
IncludeSubStrList []string `json:"include_sub_str_list"` // 必须包含指定的子串 IncludeSubStrList []string `json:"include_sub_str_list"` // 必须包含指定的子串, 必须全部包含
NotIncludeSubStrList []string `json:"not_include_sub_str_list"` // 必须不能包含的子串, 包含任意一个都不行
} }
// MapValueLimit map数据的限制 // MapValueLimit map数据的限制

View File

@ -13,6 +13,7 @@ import (
"git.zhangdeman.cn/zhangdeman/util" "git.zhangdeman.cn/zhangdeman/util"
"git.zhangdeman.cn/zhangdeman/wrapper" "git.zhangdeman.cn/zhangdeman/wrapper"
"strconv" "strconv"
"strings"
) )
// handleFloat 处理float数据 // handleFloat 处理float数据
@ -118,6 +119,32 @@ func handleString(inputVal any, rule *define.FieldRule) (string, error) {
if nil == rule.ValueLimit { if nil == rule.ValueLimit {
return formatData, nil return formatData, nil
} }
if nil != rule.ValueLimit.String {
if len(rule.ValueLimit.String.IncludeSubStrList) > 0 {
include := true
for _, item := range rule.ValueLimit.String.IncludeSubStrList {
if !strings.Contains(formatData, item) {
include = false
break
}
}
if !include {
return "", fmt.Errorf("%v : data type is string, value is %v, not include must substr", rule.Path, formatData)
}
}
if len(rule.ValueLimit.String.NotIncludeSubStrList) > 0 {
include := false
for _, item := range rule.ValueLimit.String.NotIncludeSubStrList {
if strings.Contains(formatData, item) {
include = true
break
}
}
if include {
return "", fmt.Errorf("%v : data type is string, value is %v, include not must substr", rule.Path, formatData)
}
}
}
if nil != rule.ValueLimit.Min { if nil != rule.ValueLimit.Min {
if float64(len(formatData)) < *rule.ValueLimit.Min { if float64(len(formatData)) < *rule.ValueLimit.Min {
return "", fmt.Errorf("%v : data type is string, min length is %v, real length is %v", rule.Path, *rule.ValueLimit.Min, len(formatData)) return "", fmt.Errorf("%v : data type is string, min length is %v, real length is %v", rule.Path, *rule.ValueLimit.Min, len(formatData))

View File

@ -30,6 +30,7 @@ func NewDefaultFieldRule(path string, dataType string, isRequired bool, defaultV
Max: nil, Max: nil,
String: &define.StringValueLimit{ String: &define.StringValueLimit{
IncludeSubStrList: make([]string, 0), IncludeSubStrList: make([]string, 0),
NotIncludeSubStrList: make([]string, 0),
}, },
Map: &define.MapValueLimit{IncludeFieldList: make([]string, 0)}, Map: &define.MapValueLimit{IncludeFieldList: make([]string, 0)},
}, },