179 lines
4.1 KiB
Go
179 lines
4.1 KiB
Go
// Package validator ...
|
|
//
|
|
// Description : validator ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 2024-05-01 22:33
|
|
package validator
|
|
|
|
import (
|
|
"fmt"
|
|
"git.zhangdeman.cn/gateway/validator/define"
|
|
"git.zhangdeman.cn/zhangdeman/serialize"
|
|
"github.com/tidwall/gjson"
|
|
)
|
|
|
|
// handleMapStringFloat ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 16:38 2024/4/29
|
|
func handleMapStringFloat(inputVal any, rule *define.FieldRule) (map[string]float64, error) {
|
|
var (
|
|
err error
|
|
res map[string]float64
|
|
)
|
|
|
|
if err = strOrMapConvert(inputVal, &res); nil != err {
|
|
return nil, err
|
|
}
|
|
|
|
dataFieldTable := make(map[string]string)
|
|
for k, _ := range res {
|
|
dataFieldTable[k] = k
|
|
}
|
|
|
|
if err = validateMap(dataFieldTable, rule); nil != err {
|
|
return nil, err
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
// handleMapStringAny ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 17:19 2024/4/29
|
|
func handleMapStringAny(inputVal any, rule *define.FieldRule) (map[string]any, error) {
|
|
var (
|
|
err error
|
|
res map[string]any
|
|
)
|
|
|
|
if err = strOrMapConvert(inputVal, &res); nil != err {
|
|
return nil, err
|
|
}
|
|
|
|
dataFieldTable := make(map[string]string)
|
|
for k, _ := range res {
|
|
dataFieldTable[k] = k
|
|
}
|
|
|
|
if err = validateMap(dataFieldTable, rule); nil != err {
|
|
return nil, err
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
// handleMapStringSlice...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 17:42 2024/4/29
|
|
func handleMapStringSlice(inputVal any, rule *define.FieldRule) (map[string][]any, error) {
|
|
var (
|
|
err error
|
|
res map[string][]any
|
|
)
|
|
|
|
if err = strOrMapConvert(inputVal, &res); nil != err {
|
|
return nil, err
|
|
}
|
|
|
|
dataFieldTable := make(map[string]string)
|
|
for k, _ := range res {
|
|
dataFieldTable[k] = k
|
|
}
|
|
|
|
if err = validateMap(dataFieldTable, rule); nil != err {
|
|
return nil, err
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
// handleMapAnyAny map[any]any处理
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 17:33 2024/4/29
|
|
func handleMapAnyAny(inputVal any, rule *define.FieldRule) (map[any]any, error) {
|
|
var (
|
|
err error
|
|
res = map[any]any{}
|
|
jsonRes gjson.Result
|
|
)
|
|
|
|
if inputValStr, ok := inputVal.(string); ok {
|
|
jsonRes = gjson.Parse(inputValStr)
|
|
} else {
|
|
jsonRes = gjson.Parse(serialize.JSON.MarshalForString(inputVal))
|
|
}
|
|
|
|
if !jsonRes.IsObject() {
|
|
return nil, fmt.Errorf("%v : is not a map", rule.Path)
|
|
}
|
|
fieldTable := make(map[string]string)
|
|
jsonRes.ForEach(func(key, value gjson.Result) bool {
|
|
fieldTable[key.String()] = key.String()
|
|
res[key.Value()] = value.Value()
|
|
return true
|
|
})
|
|
if err = validateMap(fieldTable, rule); nil != err {
|
|
return nil, err
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
// strOrMapConvert 字符串或map转map
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 17:26 2024/4/29
|
|
func strOrMapConvert(inputVal any, receiver any) error {
|
|
var (
|
|
err error
|
|
)
|
|
|
|
if inputValStr, ok := inputVal.(string); ok {
|
|
if err = serialize.JSON.UnmarshalWithNumber([]byte(inputValStr), receiver); nil != err {
|
|
return err
|
|
}
|
|
} else {
|
|
byteData := serialize.JSON.MarshalForByte(inputVal)
|
|
if err = serialize.JSON.UnmarshalWithNumber(byteData, receiver); nil != err {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// validateMap 验证map数据
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 17:21 2024/4/29
|
|
func validateMap(dataFieldTable map[string]string, rule *define.FieldRule) error {
|
|
if nil == rule.ValueLimit {
|
|
return nil
|
|
}
|
|
if nil != rule.ValueLimit.Min && float64(len(dataFieldTable)) < *rule.ValueLimit.Min {
|
|
return fmt.Errorf("%v : data type is map, min item cnt is %v, real item cnt is %v", rule.Path, *rule.ValueLimit.Min, len(dataFieldTable))
|
|
}
|
|
if nil != rule.ValueLimit.Max && float64(len(dataFieldTable)) >= *rule.ValueLimit.Max {
|
|
return fmt.Errorf("%v : data type is map, max item cnt is %v, real item max is %v", rule.Path, *rule.ValueLimit.Max, len(dataFieldTable))
|
|
}
|
|
if nil != rule.ValueLimit.Map {
|
|
for _, itemField := range rule.ValueLimit.Map.IncludeFieldList {
|
|
if _, exist := dataFieldTable[itemField]; !exist {
|
|
return fmt.Errorf("%v : data type is map, %v field is required, but not found", rule.Path, itemField)
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|