2023-11-17 20:39:00 +03:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
|
|
)
|
|
|
|
|
|
|
|
var Validator *validator.Validate
|
|
|
|
|
|
|
|
func init() {
|
2023-11-19 13:06:38 +03:00
|
|
|
Validator = validator.New(validator.WithRequiredStructEnabled())
|
2023-11-17 20:39:00 +03:00
|
|
|
_ = Validator.RegisterValidation("validregexp", isValidRegExp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isValidRegExp(fl validator.FieldLevel) bool {
|
|
|
|
expr := fl.Field().String()
|
|
|
|
if expr == "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
_, err := regexp.Compile(expr)
|
|
|
|
return err == nil
|
|
|
|
}
|