validator/handle_base.go

165 lines
4.4 KiB
Go

// Package validator ...
//
// Description : validator ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-05-01 22:34
package validator
import (
"fmt"
"git.zhangdeman.cn/gateway/validator/define"
"git.zhangdeman.cn/zhangdeman/util"
"git.zhangdeman.cn/zhangdeman/wrapper"
"strconv"
"strings"
)
// handleFloat 处理float数据
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:29 2024/4/29
func handleFloat(inputVal any, rule *define.FieldRule) (float64, error) {
var (
err error
formatData float64
)
if err = util.ConvertAssign(&formatData, inputVal); nil != err {
return 0, err
}
if !rule.AllowZero && formatData == 0 {
return 0, fmt.Errorf("%v : field type is float, but zero val is not allowed", rule.Path)
}
if nil == rule.ValueLimit {
return formatData, nil
}
if nil != rule.ValueLimit.Min && formatData < *rule.ValueLimit.Min {
return 0, fmt.Errorf("%v : field type is float, min val is [%v], real val is [%v]", rule.Path, *rule.ValueLimit.Min, formatData)
}
if nil != rule.ValueLimit.Max && formatData > *rule.ValueLimit.Max {
return 0, fmt.Errorf("%v : field type is float, max val is %v, real val is %v", rule.Path, *rule.ValueLimit.Max, formatData)
}
if len(rule.ValueLimit.EnumList) > 0 {
if wrapper.ArrayType(rule.ValueLimit.EnumList).Has(strconv.FormatFloat(formatData, 'f', -1, 64)) < 0 {
return 0, fmt.Errorf("%v : field type is float, real val is %v, is not in enum list", rule.Path, formatData)
}
}
return formatData, nil
}
// handleInt ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:36 2024/4/29
func handleInt(inputVal any, rule *define.FieldRule) (int64, error) {
var (
err error
formatData int64
)
if err = util.ConvertAssign(&formatData, inputVal); nil != err {
return 0, err
}
if _, err = handleFloat(formatData, rule); nil != err {
return 0, err
}
return formatData, nil
}
// handleUint ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:28 2024/4/30
func handleUint(inputVal any, rule *define.FieldRule) (uint64, error) {
var (
err error
formatData uint64
)
if err = util.ConvertAssign(&formatData, inputVal); nil != err {
return 0, err
}
if _, err = handleFloat(formatData, rule); nil != err {
return 0, err
}
return formatData, nil
}
// handleBool...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:26 2024/4/30
func handleBool(inputVal any, rule *define.FieldRule) (bool, error) {
var (
err error
formatData bool
)
if err = util.ConvertAssign(&formatData, inputVal); nil != err {
return false, err
}
return formatData, nil
}
// handleString ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:53 2024/4/29
func handleString(inputVal any, rule *define.FieldRule) (string, error) {
var (
err error
formatData string
)
if err = util.ConvertAssign(&formatData, inputVal); nil != err {
return "", err
}
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))
}
}
if nil != rule.ValueLimit.Max {
if float64(len(formatData)) >= *rule.ValueLimit.Max {
return "", fmt.Errorf("%v : data type is string, max length is %v, real length is %v", rule.Path, *rule.ValueLimit.Max, len(formatData))
}
}
if len(rule.ValueLimit.EnumList) > 0 {
if wrapper.ArrayType(rule.ValueLimit.EnumList).Has(formatData) < 0 {
return "", fmt.Errorf("%v : data type is string, not in enum list, real val is %v", rule.Path, formatData)
}
}
return formatData, nil
}