-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator.go
143 lines (126 loc) · 2.89 KB
/
validator.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package jsonvalidator
import (
"encoding/json"
"fmt"
"reflect"
"regexp"
"strings"
"github.com/go-playground/validator"
)
const (
tokenInt = "{int}"
tokenFloat = "{float}"
tokenString = "{string}"
tokenList = "{list}"
tokenRe = "{re}" // regular expression
tokenSeparator = "|"
typeErrMsg = "tag [%s] expected [%s], but received [%+v]"
)
var vd *validator.Validate
func init() {
vd = validator.New()
}
func ValidateJson(input, tpl string) (err error) {
var inputItf interface{}
if err := json.Unmarshal([]byte(input), &inputItf); err != nil {
return err
}
var tplItf interface{}
if err := json.Unmarshal([]byte(tpl), &tplItf); err != nil {
return err
}
return validate(inputItf, tplItf, "")
}
func ValidateObject(input, tpl interface{}, ) (err error) {
return validate(input, tpl, "")
}
func validate(val, tpl interface{}, tag string) (err error) {
if tpl == nil {
return
}
switch inputData := val.(type) {
case []interface{}:
switch tplData := tpl.(type) {
case []interface{}:
if len(inputData) != len(tplData) {
err = fmt.Errorf("tag [%s] not match", tag)
return
}
for idx, iv := range inputData {
err = validate(iv, tplData[idx], fmt.Sprintf("%s[%d]", tag, idx))
if err != nil {
return
}
}
case string:
return validateVar(inputData, tplData, tag)
default:
return fmt.Errorf("tag [%s] type inconsistent", tag)
}
case map[string]interface{}:
switch tplData := tpl.(type) {
case map[string]interface{}:
for k, iv := range inputData {
err = validate(iv, tplData[k], k)
if err != nil {
return
}
}
default:
return fmt.Errorf("tag [%s] type inconsistent", tag)
}
default:
switch tplData := tpl.(type) {
case string:
return validateVar(inputData, tplData, tag)
default:
return fmt.Errorf("tag [%s] type inconsistent", tag)
}
}
return
}
func validateVar(val interface{}, tpl, tag string) (err error) {
ss := strings.Split(tpl, tokenSeparator)
if len(ss) != 2 {
return
}
token := ss[0]
fieldTag := ss[1]
defer func() {
if err != nil {
err = fmt.Errorf(strings.Replace(err.Error(), "''", fmt.Sprintf("'%s'", tag), -1))
}
}()
err = checkKind(token, val, tag)
if err != nil {
return
}
switch token {
case tokenInt, tokenFloat, tokenString, tokenList:
err = vd.Var(val, fieldTag)
case tokenRe:
exp := regexp.MustCompile(fieldTag)
match := exp.Match([]byte(fmt.Sprint(val)))
if !match {
err = fmt.Errorf(typeErrMsg, tag, fieldTag, fmt.Sprint(val))
}
}
return
}
func checkKind(token string, val interface{}, tag string) error {
invalid := false
switch token {
case tokenInt, tokenFloat:
if reflect.TypeOf(val).Kind() != reflect.Float64 {
invalid = true
}
case tokenString:
if reflect.TypeOf(val).Kind() != reflect.String {
invalid = true
}
}
if invalid {
return fmt.Errorf(typeErrMsg, tag, token, reflect.TypeOf(val))
}
return nil
}