forked from viveknarang/krama-AI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidation_product.go
60 lines (43 loc) · 1.76 KB
/
validation_product.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
package main
import (
"net/http"
"strings"
"gopkg.in/validator.v2"
)
func validateProduct(w http.ResponseWriter, r *http.Request, product PRODUCT) bool {
if errs := validator.Validate(product); errs != nil {
respondWith(w, r, nil, "Error(s) found in the product data: "+errs.Error(), nil, http.StatusBadRequest, false)
return false
}
for _, img := range product.Images {
if !isValidURL(img) {
respondWith(w, r, nil, "Image URL: "+img+" is not a valid URL", nil, http.StatusBadRequest, false)
return false
}
}
if len(product.Attributes) > 0 {
for key, value := range product.Attributes {
if strings.Contains(typeof(key), "interface") || strings.Contains(typeof(value), "interface") {
respondWith(w, r, nil, "Attribute field keys or values cannot be complex object. They need to be simple types like int, float or boolean etc ...", nil, http.StatusBadRequest, false)
return false
}
if !isValidAttributeKey(key) {
respondWith(w, r, nil, "Attribute field key: "+key+" is not following attribute naming rules. Please check API documentation.", nil, http.StatusBadRequest, false)
return false
}
}
}
if len(product.Selectors) > 0 {
for key, value := range product.Selectors {
if strings.Contains(typeof(key), "interface") || strings.Contains(typeof(value), "interface") {
respondWith(w, r, nil, "Selectors field keys or values cannot be complex object. They need to be simple types like int, float or boolean etc ...", nil, http.StatusBadRequest, false)
return false
}
if !isValidAttributeKey(key) {
respondWith(w, r, nil, "Selectors field key: "+key+" is not following selectors attribute naming rules. Please check API documentation.", nil, http.StatusBadRequest, false)
return false
}
}
}
return true
}