mirror of
https://github.com/flipped-aurora/gin-vue-admin.git
synced 2026-09-21 20:35:16 +00:00
增加验证规则复用
This commit is contained in:
@@ -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
|
||||
}
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user