This repository has been archived by the owner on Jul 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation.go
174 lines (146 loc) · 2.68 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package utils
import (
"encoding/json"
"regexp"
)
var (
emailRegex = regexp.MustCompile(`^[a-z0-9._%+\-]+@[a-z0-9.\-]$`)
phoneRegex = regexp.MustCompile(`^\+\d{11,15}$`)
fullShortlinkRegex = regexp.MustCompile(`([a-zA-Z0-9]+[_-]?){1,256}$`)
shortlinkRegex = regexp.MustCompile(`([a-zA-Z0-9]+[_-]?){5,256}$`)
resourceNameRegex = regexp.MustCompile(`([a-z]+[_-]?){2,255}$`)
)
func ValidateEmail(email string) bool {
return emailRegex.MatchString(email)
}
func ValidatePhoneNumber(phone string) bool {
return phoneRegex.MatchString(phone)
}
func ValidateShortlink(r string) bool {
return shortlinkRegex.MatchString(r)
}
func ValidateFullShortlink(r string) bool {
return fullShortlinkRegex.MatchString(r)
}
func ValidateResourceName(r string) bool {
return resourceNameRegex.MatchString(r)
}
func CheckInt(old, new int) int {
if new != old {
if new != 0 {
return new
}
}
return old
}
func CheckInt8(old, new int8) int8 {
if new != old {
if new != 0 {
return new
}
}
return old
}
func CheckInt16(old, new int16) int16 {
if new != old {
if new != 0 {
return new
}
}
return old
}
func CheckInt32(old, new int32) int32 {
if new != old {
if new != 0 {
return new
}
}
return old
}
func CheckInt64(old, new int64) int64 {
if new != old {
if new != 0 {
return new
}
}
return old
}
func CheckUint8(old, new uint8) uint8 {
if new != old {
if new != 0 {
return new
}
}
return old
}
func CheckUint16(old, new uint16) uint16 {
if new != old {
if new != 0 {
return new
}
}
return old
}
func CheckUint32(old, new uint32) uint32 {
if new != old {
if new != 0 {
return new
}
}
return old
}
func CheckUint64(old, new uint64) uint64 {
if new != old {
if new != 0 {
return new
}
}
return old
}
func CheckString(old, new string) string {
if new != old {
if new != "" {
return new
}
}
return old
}
//jsonb
type PropertyMap map[string]interface{}
func (p *PropertyMap) Scan(source json.RawMessage) error {
jsonStr := string(source)
//p = make(map[string]interface{})
err := json.Unmarshal([]byte(jsonStr), &p)
if err != nil {
return err
}
return nil
}
func CheckJson(old, new json.RawMessage) (json.RawMessage, error) {
var o, n map[string]interface{}
if old == nil || new == nil {
return nil, nil
}
if err := json.Unmarshal(old, &o); err != nil {
return nil, err
}
if err := json.Unmarshal(new, &n); err != nil {
return nil, err
}
for i := range n {
newProp := n[i]
for j := range o {
oldProp := o[j]
if newProp != oldProp {
o[j] = newProp
} else {
o[i] = oldProp
}
}
}
res, err := json.Marshal(o)
if err != nil {
return nil, err
}
return res, nil
}