Newer
Older
"github.com/tidwall/gjson"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/loggermdl"
requestValidator "corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/validationmdl/validationcore"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/errormdl"
//ValidateRequest func validates the given model
func ValidateRequest(httpRequest *http.Request, validationRules, validationMessages requestValidator.MapData) map[string]interface{} {
//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
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)
validationErrors = validator.ValidateJSON()
} else {
//Validate request type form-data, form-urlencoded
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 !errormdl.CheckBool(validationResult) {
return errors.New("ERROR:ValidateStruct function error")
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
//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
}
//ValidateJSONData to validate JSON data
func ValidateJSONString(jsonString string, 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,
}
// ffjson.Unmarshal([]byte(jsonString), &opts.Data)
data, ok := gjson.Parse(jsonString).Value().(map[string]interface{})
if !ok {
loggermdl.LogError("can not cast to map")
return nil
}
opts.Data = data
// decodeError := json.NewDecoder(bytes.NewBuffer([]byte(jsonString))).Decode(opts.Data)
// if decodeError != nil {
// return nil
// }
//Set custom validation messages if sent from user
// if validationMessages != nil {
// opts.Messages = validationMessages
// }
validator := requestValidator.New(opts)
validationErrors = validator.ValidateJSONString()
if len(validationErrors) > 0 {
errs := map[string]interface{}{"validationErrors": validationErrors}
return errs
}
return nil
}