roese-utils/rsvalidator/rsvalidator.go

37 lines
785 B
Go
Raw Normal View History

2023-10-08 12:08:10 +02:00
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
}
2023-10-08 12:41:50 +02:00
func ValidateNumericString(fl validator.FieldLevel) bool {
2023-10-08 12:08:10 +02:00
str := fl.Field().String()
return strings.TrimSpace(str) != "" && regexp.MustCompile(`^[0-9]+$`).MatchString(str)
}