Newer
Older
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/errormdl"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/loggermdl"
requestValidator "corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/validationmdl/validationcore"
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
// var cnt int
// //ValidateRequest func validates the given model
// func ValidateRequest(httpRequest *http.Request, validationRules, validationMessages requestValidator.MapData) map[string]interface{} {
// cnt++
// //Get the content type of the request as validations for content types are different
// contentType := httpRequest.Header.Get("Content-Type")
// //Initialize the validation errors as blank
// var validationErrors url.Values
// //Set validator options
// opts := requestValidator.Options{
// Request: httpRequest,
// Rules: validationRules,
// }
// //Set custom validation messages if sent from user
// if validationMessages != nil {
// opts.Messages = validationMessages
// }
// if contentType == "application/json" || contentType == "text/plain" {
// //Validate request type json and text (RAW data from request)
// data := make(map[string]interface{}, 0)
// opts.Data = &data
// validator := requestValidator.New(opts)
// validationErrors = validator.ValidateJSON()
// } else {
// //Validate request type form-data, form-urlencoded
// validator := requestValidator.New(opts)
// validationErrors = validator.Validate()
// }
// if len(validationErrors) > 0 {
// errs := map[string]interface{}{"validationErrors": validationErrors}
// return errs
// }
// return nil
// }
// // ValidateStruct validates the structures with govalidator
// func ValidateStruct(structToValidate interface{}) error {
// validationResult, err := govalidator.ValidateStruct(structToValidate)
// if err != nil {
// return err
// }
// if !errormdl.CheckBool(validationResult) {
// return errors.New("ERROR:ValidateStruct function error")
// }
// return nil
// }
// //ValidateJSONData to validate JSON data
// func ValidateJSONData(jsonData []byte, validationRules, validationMessages requestValidator.MapData) map[string]interface{} {
// //Initialize the validation errors as blank
// var validationErrors url.Values
// //Set validator options
// opts := requestValidator.Options{
// Rules: validationRules,
// JSONData: jsonData,
// }
// //Set custom validation messages if sent from user
// if validationMessages != nil {
// opts.Messages = validationMessages
// }
// validator := requestValidator.New(opts)
// validationErrors = validator.ValidateJSONData()
// if len(validationErrors) > 0 {
// errs := map[string]interface{}{"validationErrors": validationErrors}
// return errs
// }
// return nil
// }
// ValidateJSONString to validate JSON data
func ValidateJSONString(jsonString string, validationRules, validationMessages requestValidator.MapData) map[string]interface{} {
var validationErrors url.Values
opts := requestValidator.Options{
data, ok := gjson.Parse(jsonString).Value().(map[string]interface{})
if !ok {
loggermdl.LogError("can not cast to map")
return nil
if len(validationErrors) > 0 {
errs := map[string]interface{}{"validationErrors": validationErrors}
return errs
}
return nil
}
// ValidateGJSONResult to validate JSON data
func ValidateGJSONResult(rs *gjson.Result, validationRules, validationMessages requestValidator.MapData) map[string]interface{} {
var validationErrors url.Values
opts := requestValidator.Options{
Rules: validationRules,
}
loggermdl.LogError("can not cast to map", rs.Value())
return map[string]interface{}{"error": "cannot cast to map"}
}
opts.Data = data
validator := requestValidator.New(opts)
validationErrors = validator.ValidateJSONString()
if len(validationErrors) > 0 {
errs := map[string]interface{}{"validationErrors": validationErrors}
return errs
}
return nil
}
// ValidateStruct validates the structures with govalidator
func ValidateStruct(structToValidate interface{}) error {
validationResult, err := govalidator.ValidateStruct(structToValidate)
if err != nil {
return err
}
if !errormdl.CheckBool(validationResult) {
return errors.New("ERROR:ValidateStruct function error")
}
return nil
}