增加字符串子串的验证
This commit is contained in:
parent
050fdcd504
commit
6a9339c7de
@ -58,7 +58,8 @@ type ValueLimit struct {
|
||||
//
|
||||
// Date : 11:16 2024/4/29
|
||||
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数据的限制
|
||||
|
@ -13,6 +13,7 @@ import (
|
||||
"git.zhangdeman.cn/zhangdeman/util"
|
||||
"git.zhangdeman.cn/zhangdeman/wrapper"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// handleFloat 处理float数据
|
||||
@ -118,6 +119,32 @@ func handleString(inputVal any, rule *define.FieldRule) (string, error) {
|
||||
if nil == rule.ValueLimit {
|
||||
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 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))
|
||||
|
@ -29,7 +29,8 @@ func NewDefaultFieldRule(path string, dataType string, isRequired bool, defaultV
|
||||
Min: nil,
|
||||
Max: nil,
|
||||
String: &define.StringValueLimit{
|
||||
IncludeSubStrList: make([]string, 0),
|
||||
IncludeSubStrList: make([]string, 0),
|
||||
NotIncludeSubStrList: make([]string, 0),
|
||||
},
|
||||
Map: &define.MapValueLimit{IncludeFieldList: make([]string, 0)},
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user