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.

111 lines
2.1 KiB

4 years ago
package validator
import (
"gitoa.ru/go-4devs/console/input/flag"
4 years ago
"gitoa.ru/go-4devs/console/input/value"
4 years ago
)
//nolint:gocyclo,cyclop
func NotBlank(fl flag.Flag) func(value.Value) error {
4 years ago
return func(in value.Value) error {
4 years ago
switch {
case fl.IsAny() && in.Any() != nil:
4 years ago
return nil
case fl.IsArray():
return arrayNotBlank(fl, in)
case fl.IsInt() && in.Int() != 0:
4 years ago
return nil
case fl.IsInt64() && in.Int64() != 0:
4 years ago
return nil
case fl.IsUint() && in.Uint() != 0:
4 years ago
return nil
case fl.IsUint64() && in.Uint64() != 0:
4 years ago
return nil
case fl.IsFloat64() && in.Float64() != 0:
4 years ago
return nil
case fl.IsDuration() && in.Duration() != 0:
4 years ago
return nil
case fl.IsTime() && !in.Time().IsZero():
4 years ago
return nil
case fl.IsString() && len(in.String()) > 0:
4 years ago
return nil
}
return ErrNotBlank
}
}
//nolint:gocyclo,gocognit,cyclop
func arrayNotBlank(fl flag.Flag, in value.Value) error {
4 years ago
switch {
case fl.IsInt() && len(in.Ints()) > 0:
4 years ago
for _, i := range in.Ints() {
if i == 0 {
return ErrNotBlank
}
}
return nil
case fl.IsInt64() && len(in.Int64s()) > 0:
4 years ago
for _, i := range in.Int64s() {
if i == 0 {
return ErrNotBlank
}
}
return nil
case fl.IsUint() && len(in.Uints()) > 0:
4 years ago
for _, u := range in.Uints() {
if u == 0 {
return ErrNotBlank
}
}
return nil
case fl.IsUint64() && len(in.Uint64s()) > 0:
4 years ago
for _, u := range in.Uint64s() {
if u == 0 {
return ErrNotBlank
}
}
return nil
case fl.IsFloat64() && len(in.Float64s()) > 0:
4 years ago
for _, f := range in.Float64s() {
if f == 0 {
return ErrNotBlank
}
}
return nil
case fl.IsBool() && len(in.Bools()) > 0:
4 years ago
return nil
case fl.IsDuration() && len(in.Durations()) > 0:
4 years ago
for _, d := range in.Durations() {
if d == 0 {
return ErrNotBlank
}
}
return nil
case fl.IsTime() && len(in.Times()) > 0:
4 years ago
for _, t := range in.Times() {
if t.IsZero() {
return ErrNotBlank
}
}
return nil
case fl.IsString() && len(in.Strings()) > 0:
4 years ago
for _, st := range in.Strings() {
if len(st) == 0 {
return ErrNotBlank
}
}
return nil
}
return ErrNotBlank
}