roese-utils/rsvalidator/rsvalidator.go

37 lines
785 B
Go

package rsvalidator
import (
"regexp"
"strings"
"github.com/go-playground/validator/v10"
)
type ErrorResponse struct {
FailedField string
Tag string
Value string
}
var Validate = validator.New()
func ValidateStruct(event interface{}) []*ErrorResponse {
var errors []*ErrorResponse
err := Validate.Struct(event)
if err != nil {
for _, err := range err.(validator.ValidationErrors) {
var element ErrorResponse
element.FailedField = err.StructNamespace()
element.Tag = err.Tag()
element.Value = err.Param()
errors = append(errors, &element)
}
}
return errors
}
func ValidateNumericString(fl validator.FieldLevel) bool {
str := fl.Field().String()
return strings.TrimSpace(str) != "" && regexp.MustCompile(`^[0-9]+$`).MatchString(str)
}