优化数据类型验证

This commit is contained in:
白茶清欢 2024-11-25 14:54:36 +08:00
parent bc555966fa
commit da9aa14f0e
5 changed files with 17 additions and 11 deletions

View File

@ -7,7 +7,9 @@
// Date : 2024-04-29 10:51
package define
import "git.zhangdeman.cn/zhangdeman/consts/enums"
import (
"git.zhangdeman.cn/zhangdeman/consts"
)
// FieldRule 字段验证规则
//
@ -16,7 +18,7 @@ import "git.zhangdeman.cn/zhangdeman/consts/enums"
// Date : 10:52 2024/4/29
type FieldRule struct {
Path string `json:"path"` // 字段路径
Type enums.DataType `json:"type"` // 数据类型, 具体枚举值参见 git.zhangdeman.cn/zhangdeman/consts
Type consts.DataType `json:"type"` // 数据类型, 具体枚举值参见 git.zhangdeman.cn/zhangdeman/consts
DisableRewrite bool `json:"disable_rewrite"` // 验证完相关数据类型之后, 不要重新给字段赋值
DefaultValue any `json:"default_value"` // 默认值, 统一以字符串传入, 会转为最终设置的类型
IsRequired bool `json:"is_required"` // 是否必传
@ -38,7 +40,7 @@ type FieldRule struct {
// Date : 10:58 2024/4/29
type RequiredCondition struct {
DependOnField string `json:"depend_on_field"` // 依赖数据源中的那一个字段
DependOnFieldType enums.DataType `json:"depend_on_field_type"` // 依赖数据源数据类型
DependOnFieldType consts.DataType `json:"depend_on_field_type"` // 依赖数据源数据类型
DependOnFieldStatus []string `json:"depend_on_field_status"` // 依赖数据状态 : NOT_FOUND / IS_NIL / IS_ZERO / IS_EMPTY / IS_FALSE
}

2
go.mod
View File

@ -3,7 +3,7 @@ module git.zhangdeman.cn/gateway/validator
go 1.22.2
require (
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125061350-1f5050978fc3
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125065114-f919222003d9
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241108082010-42ae8fe5ebdc
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241125062526-91423fb146e0

2
go.sum
View File

@ -2,6 +2,8 @@ git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241023090605-10cff9173059 h1:TPAYdT
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241023090605-10cff9173059/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125061350-1f5050978fc3 h1:/40XIygeSxRhPQc3/7pKGpV5hg8jwrMwh1+YiyCHdNI=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125061350-1f5050978fc3/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125065114-f919222003d9 h1:TP/M3WnGsxh0Vr6YuS1i28hw1oV//YbdCoI46PUBIA0=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125065114-f919222003d9/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20241024134352-ce2d84d282ba h1:hgajrPZGoDY9P+x6iqcS06pnu5t+N7DOfpmRwb+TZ4s=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20241024134352-ce2d84d282ba/go.mod h1:V4Dfg1v/JVIZGEKCm6/aehs8hK+Xow1dkL1yiQymXlQ=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20241101082529-28a6c68e38a4 h1:s6d4b6yY+NaK1AzoBD1pxqsuygEHQz0Oie86c45geDw=

View File

@ -9,7 +9,7 @@ package validator
import (
"git.zhangdeman.cn/gateway/validator/define"
"git.zhangdeman.cn/zhangdeman/consts/enums"
"git.zhangdeman.cn/zhangdeman/consts"
)
// NewDefaultFieldRule ...
@ -17,7 +17,7 @@ import (
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:01 2024/4/29
func NewDefaultFieldRule(path string, dataType enums.DataType, isRequired bool, defaultValue string) *define.FieldRule {
func NewDefaultFieldRule(path string, dataType consts.DataType, isRequired bool, defaultValue string) *define.FieldRule {
r := &define.FieldRule{
Path: path,
Type: dataType,

8
run.go
View File

@ -13,7 +13,6 @@ import (
"fmt"
"git.zhangdeman.cn/gateway/validator/define"
"git.zhangdeman.cn/zhangdeman/consts"
"git.zhangdeman.cn/zhangdeman/consts/enums"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
"strings"
@ -127,7 +126,7 @@ func checkRuleConditionRequiredRule(sourceData []byte, rule *define.FieldRule) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:59 2024/4/29
func getDataStatus(val gjson.Result, dataType enums.DataType) string {
func getDataStatus(val gjson.Result, dataType consts.DataType) string {
if !val.Exists() {
return consts.DataStatusNotFound
}
@ -247,7 +246,10 @@ func validate(sourceData []byte, val gjson.Result, rule *define.FieldRule) (any,
//
// Date : 14:43 2024/4/29
func handleData(inputVal any, rule *define.FieldRule) (any, error) {
rule.Type = enums.DataType(strings.ToLower(rule.Type.String()))
rule.Type = consts.DataType(strings.ToLower(rule.Type.String()))
if !rule.Type.IsValid() {
return nil, fmt.Errorf("%v : data type %v is invalid", rule.Path, rule.Type.String())
}
// 处理真实的map和序列化之后的map
if strings.HasPrefix(rule.Type.String(), "map") {
if strings.HasSuffix(rule.Type.String(), "_marshal") {