-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidators.go
More file actions
257 lines (219 loc) · 6.21 KB
/
validators.go
File metadata and controls
257 lines (219 loc) · 6.21 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package utils
import (
"encoding/csv"
"errors"
"fmt"
"log"
"regexp"
"strconv"
"github.com/CodeChefVIT/devsoc-backend-26/pkg/models"
"github.com/go-playground/validator/v10"
"net/url"
"strings"
"unicode/utf8"
)
var Validate *validator.Validate
var alphaNumRegex = regexp.MustCompile(`^[a-zA-Z0-9\s]+$`)
var regNoRegex = regexp.MustCompile(`^[0-9]{2}[A-Z]{3}[0-9]{4}$`)
func ValidateAlphaNum(str string) bool {
return alphaNumRegex.MatchString(str)
}
func InitValidator() {
Validate = validator.New()
Validate.RegisterValidation("regno", func(fl validator.FieldLevel) bool {
return regNoRegex.MatchString(fl.Field().String())
})
}
func FormatValidationErrors(err error) []string {
msgs := []string{}
ve, ok := err.(validator.ValidationErrors)
if !ok || len(ve) == 0 {
return []string{"validation issue :- contact us"}
}
for _, e := range ve {
switch e.Tag() {
case "regno":
msgs = append(msgs, fmt.Sprintf("%s must be in format 24ABC1234", e.Field()))
case "required":
msgs = append(msgs, fmt.Sprintf("%s field is required", e.Field()))
case "email":
msgs = append(msgs, fmt.Sprintf("%s field is invalid email format", e.Field()))
case "endswith":
msgs = append(msgs, fmt.Sprintf("%s field must end with @vitstudent.ac.in", e.Field()))
case "url":
msgs = append(msgs, fmt.Sprintf("%s field is invalid URL format", e.Field()))
case "len":
msgs = append(msgs, fmt.Sprintf("%s field is invalid length", e.Field()))
case "alphanum":
msgs = append(msgs, fmt.Sprintf("%s field must contain only letters or numbers", e.Field()))
case "numeric":
msgs = append(msgs, fmt.Sprintf("%s field must be numeric", e.Field()))
case "oneof":
msgs = append(msgs, fmt.Sprintf("%s field has invalid value", e.Field()))
case "uppercase":
msgs = append(msgs, fmt.Sprintf("%s field must be uppercase", e.Field()))
case "gt":
msgs = append(msgs, fmt.Sprintf("%s field must be greater than 0", e.Field()))
case "gte":
msgs = append(msgs, fmt.Sprintf("%s field must be greater than or equal to 0", e.Field()))
case "min":
msgs = append(msgs, fmt.Sprintf("%s field is too small", e.Field()))
case "max":
msgs = append(msgs, fmt.Sprintf("%s field is too large", e.Field()))
default:
msgs = append(msgs, fmt.Sprintf("%s field is invalid", e.Field()))
}
}
return msgs
}
func ValSub(t, d, g, f, o string) string {
chk := func(v string) bool {
u, err := url.ParseRequestURI(v)
return err == nil && (u.Scheme == "http" || u.Scheme == "https")
}
t = strings.TrimSpace(t)
if t == "" {
return "title is required"
}
if utf8.RuneCountInString(t) > 100 {
return "title too long"
}
d = strings.TrimSpace(d)
if d == "" {
return "description is required"
}
if utf8.RuneCountInString(d) > 1000 {
return "description too long"
}
g = strings.TrimSpace(g)
if g == "" {
return "github link is required"
}
if !chk(g) {
return "invalid github url"
}
if strings.TrimSpace(f) != "" && !chk(f) {
return "invalid figma url"
}
if strings.TrimSpace(o) != "" && !chk(o) {
return "invalid url"
}
return ""
}
func IdeaCreateVal(t, d, tr string) []string {
errors := []string{}
t = strings.TrimSpace(t)
if t == "" {
errors = append(errors, "title is required")
}
if utf8.RuneCountInString(t) > 100 {
errors = append(errors, "title too long")
}
d = strings.TrimSpace(d)
if d == "" {
errors = append(errors, "description is required")
}
if utf8.RuneCountInString(d) > 1000 {
errors = append(errors, "description too long")
}
validTracks := map[string]struct{}{
"Hotfoot AI Challenges": {},
"Digital Economy": {},
"Media & Entertainment": {},
"Tech for Good": {},
"Open Innovation": {},
}
if _, ok := validTracks[tr]; !ok {
errors = append(errors, "track not valid")
}
return errors
}
func IdeaUpdateVal(t, d, tr string) []string {
errors := []string{}
if strings.TrimSpace(t) != "" {
title := strings.TrimSpace(t)
if utf8.RuneCountInString(title) > 100 {
errors = append(errors, "title too long")
}
}
if strings.TrimSpace(d) != "" {
desc := strings.TrimSpace(d)
if utf8.RuneCountInString(desc) > 1000 {
errors = append(errors, "description too long")
}
}
if strings.TrimSpace(tr) != "" {
validTracks := map[string]struct{}{
"Hotfoot AI Challenges": {},
"Digital Economy": {},
"Media & Entertainment": {},
"Tech for Good": {},
"Open Innovation": {},
}
if _, ok := validTracks[tr]; !ok {
errors = append(errors, "track not valid")
}
}
return errors
}
type ValidatedPanelRow struct {
PanelNo int32
Members []models.PanelMember
}
func ValidatePanelCSV(cr *csv.Reader) ([][]string, error) {
rows, err := cr.ReadAll()
if err != nil {
return nil, errors.New("invalid csv file/format")
}
if len(rows) == 0 {
return nil, errors.New("empty csv file")
}
return rows, nil
}
func ValidatePanelRow(i int, row []string) (int, []models.PanelMember, error) {
if len(row) < 2 {
return 0, nil, errors.New("row " + strconv.Itoa(i) + " has fewer than 2 columns")
}
no, err := strconv.Atoi(strings.TrimSpace(row[0]))
if err != nil {
return 0, nil, errors.New("row " + strconv.Itoa(i) + " has invalid panel_no")
}
membersRaw := strings.Split(row[1], ";")
var members []models.PanelMember
for _, m := range membersRaw {
parts := strings.Split(strings.TrimSpace(m), ":")
if len(parts) != 2 {
return 0, nil, errors.New("row " + strconv.Itoa(i) + " invalid member format; expected 'name:domain|domain'")
}
name := strings.TrimSpace(parts[0])
if name == "" {
return 0, nil, errors.New("row " + strconv.Itoa(i) + " member name empty")
}
domainParts := strings.Split(parts[1], "|")
var domains []string
for _, d := range domainParts {
d = strings.TrimSpace(d)
if d != "" {
domains = append(domains, d)
}
}
if len(domains) == 0 {
return 0, nil, errors.New("row " + strconv.Itoa(i) + " member must have at least one domain")
}
members = append(members, models.PanelMember{
Name: name,
Domains: domains,
})
}
return no, members, nil
}
func ValidatePayload(v any) error {
if Validate == nil {
return errors.New("validator not initialized")
}
if err := Validate.Struct(v); err != nil {
log.Printf("Validation failed: %v", err)
return err
}
return nil
}