-
Notifications
You must be signed in to change notification settings - Fork 1
/
validation.go
58 lines (51 loc) · 1.38 KB
/
validation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package kid
import (
"errors"
"fmt"
"reflect"
"strings"
"github.com/go-playground/locales/zh"
ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
zh_translations "github.com/go-playground/validator/v10/translations/zh"
)
type Rules map[string]interface{}
var Validator = NewValidator()
func NewValidator() *validator.Validate {
zh := zh.New()
uni := ut.New(zh)
trans, _ := uni.GetTranslator("zh")
validate := validator.New()
if err := zh_translations.RegisterDefaultTranslations(validate, trans); err != nil {
panic(fmt.Errorf("注册参数验证翻译器失败: %s", err.Error()))
}
return validate
}
func Struct(value interface{}) error {
return Validator.Struct(value)
}
func Verify(ctx *Context, rules Rules) error {
var val = make(map[string]interface{})
for key, _ := range rules {
v := ctx.find(key)
if len(v) <= 0 {
return &validator.InvalidValidationError{Type: reflect.TypeOf(v)}
}
val[key] = v
}
result := Validator.ValidateMap(val, rules)
var errBuild strings.Builder
if len(result) > 0 {
for k, v := range result {
validErr, ok := v.(validator.ValidationErrors)
if !ok {
return errors.New("验证参数错误")
}
for _, e := range validErr {
errBuild.WriteString(fmt.Sprintf("参数: [%s] 错误,参数要求为: [%s];", k, e.Tag()))
}
}
return errors.New(errBuild.String())
}
return nil
}