validationmdl.go 3.66 KiB
Newer Older
Rahul A. Sutar's avatar
Rahul A. Sutar committed
package validationmdl

import (
	"errors"
Rahul A. Sutar's avatar
Rahul A. Sutar committed
	"net/http"
	"net/url"

Roshan Patil's avatar
Roshan Patil committed
	"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"
Mayuri Shinde's avatar
Mayuri Shinde committed
	govalidator "github.com/asaskevich/govalidator"
Roshan Patil's avatar
Roshan Patil committed
var cnt int

Rahul A. Sutar's avatar
Rahul A. Sutar committed
//ValidateRequest func validates the given model
Mayuri Shinde's avatar
Mayuri Shinde committed
func ValidateRequest(httpRequest *http.Request, validationRules, validationMessages requestValidator.MapData) map[string]interface{} {
Roshan Patil's avatar
Roshan Patil committed
	cnt++
Rahul A. Sutar's avatar
Rahul A. Sutar committed
	//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
Mayuri Shinde's avatar
Mayuri Shinde committed
	opts := requestValidator.Options{
Rahul A. Sutar's avatar
Rahul A. Sutar committed
		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" {
Rahul A. Sutar's avatar
Rahul A. Sutar committed
		//Validate request type json and text (RAW data from request)
		data := make(map[string]interface{}, 0)
Rahul A. Sutar's avatar
Rahul A. Sutar committed
		opts.Data = &data
Mayuri Shinde's avatar
Mayuri Shinde committed
		validator := requestValidator.New(opts)
Rahul A. Sutar's avatar
Rahul A. Sutar committed
		validationErrors = validator.ValidateJSON()

	} else {
		//Validate request type form-data, form-urlencoded
Mayuri Shinde's avatar
Mayuri Shinde committed
		validator := requestValidator.New(opts)
Rahul A. Sutar's avatar
Rahul A. Sutar committed
		validationErrors = validator.Validate()
	}

	if len(validationErrors) > 0 {
		errs := map[string]interface{}{"validationErrors": validationErrors}
		return errs
	}
	return nil
}
Mayuri Shinde's avatar
Mayuri Shinde committed
// ValidateStruct validates the structures with govalidator
func ValidateStruct(structToValidate interface{}) error {
	validationResult, err := govalidator.ValidateStruct(structToValidate)
Mayuri Shinde's avatar
Mayuri Shinde committed
	if err != nil {
		return err
	}
	if !errormdl.CheckBool(validationResult) {
		return errors.New("ERROR:ValidateStruct function error")
Rahul A. Sutar's avatar
Rahul A. Sutar committed
	}
	return nil
}
Roshan Patil's avatar
Roshan Patil committed

//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
}