safety_text.go 410 B

1234567891011121314151617
  1. package validation
  2. import (
  3. val "github.com/go-playground/validator/v10"
  4. "regexp"
  5. )
  6. func safetyText(fl val.FieldLevel) bool {
  7. asciiPattern := `^[a-zA-Z0-9-_. ]*$`
  8. unicodePattern := `^[\p{L}\p{N}-_. ]*$`
  9. asciiRegex := regexp.MustCompile(asciiPattern)
  10. unicodeRegex := regexp.MustCompile(unicodePattern)
  11. str := fl.Field().String()
  12. return asciiRegex.MatchString(str) || unicodeRegex.MatchString(str)
  13. }