增加验证规则复用

This commit is contained in:
QM303176530
2020-05-10 16:21:00 +08:00
parent 760a242118
commit bf4344323d
9 changed files with 89 additions and 122 deletions
-38
View File
@@ -1,38 +0,0 @@
// 空值校验工具 仅用于检验空字符串 其余类型请勿使用
package utils
import (
"errors"
"fmt"
"reflect"
)
func HasGap(input interface{}) error {
getType := reflect.TypeOf(input)
getValue := reflect.ValueOf(input)
// 获取方法字段
for i := 0; i < getType.NumField(); i++ {
field := getType.Field(i)
value := getValue.Field(i).Interface()
switch value.(type) {
case string:
if value == "" {
fmt.Printf("%s为空", field.Name)
return errors.New(fmt.Sprintf("%s为空", field.Name))
}
default:
if value == nil {
fmt.Printf("%s为空", field.Name)
return errors.New(fmt.Sprintf("%s为空", field.Name))
}
}
}
// 获取方法
// 1. 先获取interface的reflect.Type,然后通过.NumMethod进行遍历
//for i := 0; i < getType.NumMethod(); i++ {
// m := getType.Method(i)
// fmt.Printf("%s: %v\n", m.Name, m.Type)
//}
return nil
}
+14
View File
@@ -9,6 +9,20 @@ import (
type Rules map[string][]string
type RulesMap map[string]Rules
var CustomizeMap = make(map[string]Rules)
// 注册自定义规则方案建议在路由初始化层即注册
func RegisterRule(key string, rule Rules) (err error) {
if CustomizeMap[key] != nil {
return errors.New(key + "已注册,无法重复注册")
} else {
CustomizeMap[key] = rule
return nil
}
}
// 非空 不能为其对应类型的0值
func NotEmpty() string {
return "notEmpty"