Repository navigation
Expand file tree
/
Copy pathcontact_handler_validator.go
More file actions
359 lines (304 loc) · 11.4 KB
/
Copy pathcontact_handler_validator.go
File metadata and controls
359 lines (304 loc) · 11.4 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package validators
import (
"bytes"
"context"
"encoding/csv"
"fmt"
"io"
"mime/multipart"
"net/mail"
"net/url"
"path/filepath"
"strconv"
"strings"
"github.com/NdoleStudio/httpsms/pkg/entities"
"github.com/NdoleStudio/httpsms/pkg/requests"
"github.com/NdoleStudio/httpsms/pkg/telemetry"
"github.com/NdoleStudio/stacktrace"
"github.com/nyaruka/phonenumbers"
"github.com/thedevsaddam/govalidator"
)
const (
maxContactBatch = 1000
maxContactUploadBytes = 500 * 1024
contactUploadDocumentKey = "document"
contactUploadContactsKey = "contacts"
contactCSVColumnName = "Name"
contactCSVColumnEmails = "Emails"
contactCSVColumnPhones = "PhoneNumbers"
contactCSVContentType = "text/csv"
contactCSVContentTypeAlt = "application/csv"
contactCSVContentTypeBin = "application/octet-stream"
contactCSVContentTypeXls = "application/vnd.ms-excel"
)
// ContactHandlerValidator validates models used in handlers.ContactHandler
type ContactHandlerValidator struct {
validator
logger telemetry.Logger
tracer telemetry.Tracer
}
// NewContactHandlerValidator creates a new handlers.ContactHandler validator
func NewContactHandlerValidator(
logger telemetry.Logger,
tracer telemetry.Tracer,
) (v *ContactHandlerValidator) {
return &ContactHandlerValidator{
logger: logger.WithService(fmt.Sprintf("%T", v)),
tracer: tracer,
}
}
// ValidateStore validates a contact create request.
func (validator *ContactHandlerValidator) ValidateStore(_ context.Context, request requests.ContactStoreRequest) url.Values {
result := url.Values{}
if len(request.Contacts) == 0 {
result.Add(contactUploadContactsKey, "You must provide at least one contact.")
return result
}
if len(request.Contacts) > maxContactBatch {
result.Add(contactUploadContactsKey, fmt.Sprintf("You cannot create more than %d contacts in one request.", maxContactBatch))
return result
}
for index, item := range request.Contacts {
validator.validateItem(result, contactUploadContactsKey, "Contact", index+1, item)
}
return result
}
// ValidateUpdate validates a contact update request.
func (validator *ContactHandlerValidator) ValidateUpdate(_ context.Context, request requests.ContactUpdateRequest) url.Values {
result := url.Values{}
validator.validateItem(result, contactUploadContactsKey, "Contact", 1, requests.ContactItem{
Name: request.Name,
Emails: request.Emails,
PhoneNumbers: request.PhoneNumbers,
Properties: request.Properties,
})
return result
}
// ValidateIndex validates a contact index request.
func (validator *ContactHandlerValidator) ValidateIndex(_ context.Context, request requests.ContactIndex) url.Values {
result := govalidator.New(govalidator.Options{
Data: &request,
Rules: govalidator.MapData{
"limit": []string{"required", "numeric"},
"skip": []string{"required", "numeric"},
"query": []string{"max:100"},
"sort_by": []string{"in:name,updated_at"},
},
}).ValidateStruct()
if limit, err := strconv.Atoi(request.Limit); request.Limit != "" && err == nil {
if limit < 1 || limit > 100 {
result.Add("limit", "The limit must be between 1 and 100.")
}
}
if skip, err := strconv.Atoi(request.Skip); request.Skip != "" && err == nil {
if skip < 0 {
result.Add("skip", "The skip must be greater than or equal to 0.")
}
}
return result
}
// ValidateUpload parses and validates a CSV contacts upload.
func (validator *ContactHandlerValidator) ValidateUpload(ctx context.Context, userID entities.UserID, header *multipart.FileHeader) ([]requests.ContactItem, url.Values) {
_, span, ctxLogger := validator.tracer.StartWithLogger(ctx, validator.logger)
defer span.End()
result := url.Values{}
if header == nil {
result.Add(contactUploadDocumentKey, "The CSV file is required.")
return nil, result
}
if !isContactCSVFile(header) {
ctxLogger.Error(stacktrace.NewErrorf("cannot parse file [%s] for user [%s] with content type [%s]", header.Filename, userID, header.Header.Get("Content-Type")))
result.Add(contactUploadDocumentKey, fmt.Sprintf("The file [%s] is not a valid CSV file. Only CSV files are supported.", header.Filename))
return nil, result
}
content, errors := validator.parseContactUploadBytes(ctxLogger, userID, header)
if len(errors) != 0 {
return nil, errors
}
rows, errors := validator.parseContactCSV(ctxLogger, userID, header.Filename, content)
if len(errors) != 0 {
return nil, errors
}
if len(rows) > maxContactBatch {
result.Add(contactUploadDocumentKey, fmt.Sprintf("The uploaded file must contain no more than %d records.", maxContactBatch))
return nil, result
}
items := make([]requests.ContactItem, 0, len(rows))
for _, row := range rows {
// Sanitize each row exactly like the JSON create path (SanitizeContactItem)
// before validating it, so both entry points accept and normalize the same
// phone/email formats. Row-indexed error messages are preserved because the
// row number is still passed to validateItem.
item := requests.SanitizeContactItem(requests.ContactItem{
Name: row.values[contactCSVColumnName],
Emails: splitContactMultiValue(row.values[contactCSVColumnEmails]),
PhoneNumbers: splitContactMultiValue(row.values[contactCSVColumnPhones]),
})
items = append(items, item)
validator.validateItem(result, contactUploadDocumentKey, "Row", row.number, item)
}
if len(items) == 0 {
result.Add(contactUploadDocumentKey, "The uploaded file must contain at least one contact.")
}
return items, result
}
func (validator *ContactHandlerValidator) validateItem(result url.Values, key string, label string, index int, item requests.ContactItem) {
prefix := fmt.Sprintf("%s [%d]", label, index)
if strings.TrimSpace(item.Name) == "" {
result.Add(key, fmt.Sprintf("%s: The name is required.", prefix))
}
if !hasNonEmptyValue(item.PhoneNumbers) {
result.Add(key, fmt.Sprintf("%s: At least one phone number is required.", prefix))
}
for _, number := range item.PhoneNumbers {
cleanNumber := strings.TrimSpace(number)
if cleanNumber == "" {
result.Add(key, fmt.Sprintf("%s: The phone number is required.", prefix))
continue
}
parsed, err := phonenumbers.Parse(cleanNumber, phonenumbers.UNKNOWN_REGION)
if err != nil || !phonenumbers.IsValidNumber(parsed) {
result.Add(key, fmt.Sprintf("%s: The phone number [%s] is not a valid E.164 phone number.", prefix, cleanNumber))
}
}
for _, email := range item.Emails {
cleanEmail := strings.TrimSpace(email)
if cleanEmail == "" {
result.Add(key, fmt.Sprintf("%s: The email is not a valid email address.", prefix))
continue
}
address, err := mail.ParseAddress(cleanEmail)
if err != nil || address.Address != cleanEmail {
result.Add(key, fmt.Sprintf("%s: The email [%s] is not a valid email address.", prefix, cleanEmail))
}
}
}
func (validator *ContactHandlerValidator) parseContactUploadBytes(ctxLogger telemetry.Logger, userID entities.UserID, header *multipart.FileHeader) ([]byte, url.Values) {
result := url.Values{}
if header.Size > maxContactUploadBytes {
result.Add(contactUploadDocumentKey, "The CSV file must be less than or equal to 500 KB.")
return nil, result
}
file, err := header.Open()
if err != nil {
ctxLogger.Error(stacktrace.Propagatef(err, "cannot open file [%s] for reading for user [%s]", header.Filename, userID))
result.Add(contactUploadDocumentKey, fmt.Sprintf("Cannot open the uploaded file [%s].", header.Filename))
return nil, result
}
defer func() {
if closeErr := file.Close(); closeErr != nil {
ctxLogger.Error(stacktrace.Propagatef(closeErr, "cannot close file [%s] for user [%s]", header.Filename, userID))
}
}()
content, err := io.ReadAll(io.LimitReader(file, maxContactUploadBytes+1))
if err != nil {
ctxLogger.Error(stacktrace.Propagatef(err, "cannot read file [%s] for user [%s]", header.Filename, userID))
result.Add(contactUploadDocumentKey, fmt.Sprintf("Cannot read the uploaded file [%s].", header.Filename))
return nil, result
}
if len(content) > maxContactUploadBytes {
result.Add(contactUploadDocumentKey, "The CSV file must be less than or equal to 500 KB.")
return nil, result
}
return content, result
}
type contactCSVRow struct {
number int
values map[string]string
}
func (validator *ContactHandlerValidator) parseContactCSV(ctxLogger telemetry.Logger, userID entities.UserID, filename string, content []byte) ([]contactCSVRow, url.Values) {
result := url.Values{}
reader := csv.NewReader(bytes.NewReader(content))
reader.TrimLeadingSpace = true
headers, err := reader.Read()
if err == io.EOF {
result.Add(contactUploadDocumentKey, fmt.Sprintf("Cannot parse the uploaded CSV file [%s]. The file is empty.", filename))
return nil, result
}
if err != nil {
ctxLogger.Error(stacktrace.Propagatef(err, "cannot read CSV header from file [%s] for user [%s]", filename, userID))
result.Add(contactUploadDocumentKey, fmt.Sprintf("Cannot parse the uploaded CSV file [%s]. Use the official httpSMS contacts template.", filename))
return nil, result
}
columnIndexes, ok := contactCSVColumnIndexes(headers)
if !ok {
result.Add(contactUploadDocumentKey, "The uploaded CSV file must contain the columns [Name, Emails, PhoneNumbers].")
return nil, result
}
reader.FieldsPerRecord = len(headers)
var rows []contactCSVRow
for rowNumber := 2; ; rowNumber++ {
record, readErr := reader.Read()
if readErr == io.EOF {
break
}
if readErr != nil {
ctxLogger.Error(stacktrace.Propagatef(readErr, "cannot read CSV row [%d] from file [%s] for user [%s]", rowNumber, filename, userID))
result.Add(contactUploadDocumentKey, fmt.Sprintf("Cannot parse the uploaded CSV file [%s]. Use the official httpSMS contacts template.", filename))
return nil, result
}
row := contactCSVRow{
number: rowNumber,
values: map[string]string{
contactCSVColumnName: record[columnIndexes[contactCSVColumnName]],
contactCSVColumnEmails: record[columnIndexes[contactCSVColumnEmails]],
contactCSVColumnPhones: record[columnIndexes[contactCSVColumnPhones]],
},
}
rows = append(rows, row)
if len(rows) > maxContactBatch {
return rows, result
}
}
return rows, result
}
func contactCSVColumnIndexes(headers []string) (map[string]int, bool) {
required := []string{contactCSVColumnName, contactCSVColumnEmails, contactCSVColumnPhones}
indexes := map[string]int{}
for index, header := range headers {
indexes[strings.TrimSpace(header)] = index
}
for _, column := range required {
if _, ok := indexes[column]; !ok {
return nil, false
}
}
return indexes, true
}
func isContactCSVFile(header *multipart.FileHeader) bool {
if strings.ToLower(filepath.Ext(header.Filename)) != ".csv" {
return false
}
contentType := strings.ToLower(strings.TrimSpace(header.Header.Get("Content-Type")))
if index := strings.Index(contentType, ";"); index >= 0 {
contentType = strings.TrimSpace(contentType[:index])
}
return contentType == "" ||
contentType == contactCSVContentType ||
contentType == contactCSVContentTypeAlt ||
contentType == contactCSVContentTypeBin ||
contentType == contactCSVContentTypeXls
}
func splitContactMultiValue(value string) []string {
if strings.TrimSpace(value) == "" {
return nil
}
var result []string
for _, item := range strings.FieldsFunc(value, func(r rune) bool {
return r == ',' || r == ';'
}) {
item = strings.TrimSpace(item)
if item != "" {
result = append(result, item)
}
}
return result
}
func hasNonEmptyValue(values []string) bool {
for _, value := range values {
if strings.TrimSpace(value) != "" {
return true
}
}
return false
}