notblank.go 676 B

12345678910111213141516171819202122232425
  1. package validators
  2. import (
  3. "reflect"
  4. "strings"
  5. "gopkg.in/go-playground/validator.v9"
  6. )
  7. // NotBlank is the validation function for validating if the current field
  8. // has a value or length greater than zero, or is not a space only string.
  9. func NotBlank(fl validator.FieldLevel) bool {
  10. field := fl.Field()
  11. switch field.Kind() {
  12. case reflect.String:
  13. return len(strings.TrimSpace(field.String())) > 0
  14. case reflect.Chan, reflect.Map, reflect.Slice, reflect.Array:
  15. return field.Len() > 0
  16. case reflect.Ptr, reflect.Interface, reflect.Func:
  17. return !field.IsNil()
  18. default:
  19. return field.IsValid() && field.Interface() != reflect.Zero(field.Type()).Interface()
  20. }
  21. }