2024-01-30 00:02:28 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/go-playground/validator/v10"
|
2024-05-04 22:24:25 +00:00
|
|
|
"github.com/thomiceli/opengist/internal/i18n"
|
2024-01-30 00:02:28 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type OpengistValidator struct {
|
|
|
|
v *validator.Validate
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewValidator() *OpengistValidator {
|
|
|
|
v := validator.New()
|
|
|
|
_ = v.RegisterValidation("notreserved", validateReservedKeywords)
|
|
|
|
_ = v.RegisterValidation("alphanumdash", validateAlphaNumDash)
|
|
|
|
_ = v.RegisterValidation("alphanumdashorempty", validateAlphaNumDashOrEmpty)
|
|
|
|
return &OpengistValidator{v}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cv *OpengistValidator) Validate(i interface{}) error {
|
|
|
|
return cv.v.Struct(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cv *OpengistValidator) Var(field interface{}, tag string) error {
|
|
|
|
return cv.v.Var(field, tag)
|
|
|
|
}
|
|
|
|
|
2024-05-04 22:24:25 +00:00
|
|
|
func ValidationMessages(err *error, locale *i18n.Locale) string {
|
2024-01-30 00:02:28 +00:00
|
|
|
errs := (*err).(validator.ValidationErrors)
|
|
|
|
messages := make([]string, len(errs))
|
|
|
|
for i, e := range errs {
|
|
|
|
switch e.Tag() {
|
|
|
|
case "max":
|
2024-05-04 22:24:25 +00:00
|
|
|
messages[i] = locale.String("validation.is-too-long", e.Field())
|
2024-01-30 00:02:28 +00:00
|
|
|
case "required":
|
2024-05-04 22:24:25 +00:00
|
|
|
messages[i] = locale.String("validation.should-not-be-empty", e.Field())
|
2024-01-30 00:02:28 +00:00
|
|
|
case "excludes":
|
2024-05-04 22:24:25 +00:00
|
|
|
messages[i] = locale.String("validation.should-not-include-sub-directory", e.Field())
|
2024-01-30 00:02:28 +00:00
|
|
|
case "alphanum":
|
2024-05-04 22:24:25 +00:00
|
|
|
messages[i] = locale.String("validation.should-only-contain-alphanumeric-characters", e.Field())
|
2024-01-30 00:02:28 +00:00
|
|
|
case "alphanumdash":
|
|
|
|
case "alphanumdashorempty":
|
2024-05-04 22:24:25 +00:00
|
|
|
messages[i] = locale.String("validation.should-only-contain-alphanumeric-characters-and-dashes", e.Field())
|
2024-01-30 00:02:28 +00:00
|
|
|
case "min":
|
2024-05-04 22:24:25 +00:00
|
|
|
messages[i] = locale.String("validation.not-enough", e.Field())
|
2024-01-30 00:02:28 +00:00
|
|
|
case "notreserved":
|
2024-05-04 22:24:25 +00:00
|
|
|
messages[i] = locale.String("validation.invalid", e.Field())
|
2024-01-30 00:02:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(messages, " ; ")
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateReservedKeywords(fl validator.FieldLevel) bool {
|
|
|
|
name := fl.Field().String()
|
|
|
|
|
|
|
|
restrictedNames := map[string]struct{}{}
|
2024-02-24 17:09:23 +00:00
|
|
|
for _, restrictedName := range []string{"assets", "register", "login", "logout", "settings", "admin-panel", "all", "search", "init", "healthcheck", "preview"} {
|
2024-01-30 00:02:28 +00:00
|
|
|
restrictedNames[restrictedName] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the name is not in the restricted names, it is valid
|
|
|
|
_, ok := restrictedNames[name]
|
|
|
|
return !ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateAlphaNumDash(fl validator.FieldLevel) bool {
|
|
|
|
return regexp.MustCompile(`^[a-zA-Z0-9-]+$`).MatchString(fl.Field().String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateAlphaNumDashOrEmpty(fl validator.FieldLevel) bool {
|
|
|
|
return regexp.MustCompile(`^$|^[a-zA-Z0-9-]+$`).MatchString(fl.Field().String())
|
|
|
|
}
|