sshpoke/internal/server/driver/util/validator.go

24 lines
428 B
Go

package util
import (
"regexp"
"github.com/go-playground/validator/v10"
)
var Validator *validator.Validate
func init() {
Validator = validator.New(validator.WithRequiredStructEnabled())
_ = 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
}