💯Go Struct and Field validation, including Cross Field, Cross Struct, Map, Slice and Array diving
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
package validators
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NotBlank is the validation function for validating if the current field
|
|
|
|
// has a value or length greater than zero, or is not a space only string.
|
|
|
|
func NotBlank(fl validator.FieldLevel) bool {
|
|
|
|
field := fl.Field()
|
|
|
|
|
|
|
|
switch field.Kind() {
|
|
|
|
case reflect.String:
|
|
|
|
return len(strings.TrimSpace(field.String())) > 0
|
|
|
|
case reflect.Chan, reflect.Map, reflect.Slice, reflect.Array:
|
|
|
|
return field.Len() > 0
|
|
|
|
case reflect.Ptr, reflect.Interface, reflect.Func:
|
|
|
|
return !field.IsNil()
|
|
|
|
default:
|
|
|
|
return field.IsValid() && field.Interface() != reflect.Zero(field.Type()).Interface()
|
|
|
|
}
|
|
|
|
}
|