Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 3 additions & 15 deletions values.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,22 +161,10 @@ func ReadOnly(ctx context.Context, path, in string, data interface{}) *errors.Va
func Required(path, in string, data interface{}) *errors.Validation {
val := reflect.ValueOf(data)
if val.IsValid() {
typ := reflect.TypeOf(data)
switch typ.Kind() {
case reflect.Pointer:
if val.IsNil() {
return errors.Required(path, in, data)
}
if reflect.DeepEqual(reflect.Zero(val.Elem().Type()).Interface(), val.Elem().Interface()) {
return errors.Required(path, in, data)
}
return nil
default:
if reflect.DeepEqual(reflect.Zero(val.Type()).Interface(), val.Interface()) {
return errors.Required(path, in, data)
}
return nil
if reflect.DeepEqual(reflect.Zero(val.Type()).Interface(), val.Interface()) {
return errors.Required(path, in, data)
}
return nil
}
return errors.Required(path, in, data)
}
Expand Down
32 changes: 0 additions & 32 deletions values_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,54 +195,22 @@ func TestValues_ValidateRequired(t *testing.T) {
path := "test"
in := "body"

emptyString := ""
emptyStringStruct := struct {
A *string
}{
A: &emptyString,
}

emptyNumber := 0
emptyNumberStruct := struct {
A *int
}{
A: &emptyNumber,
}

RequiredFail := []interface{}{
"",
0,
nil,
emptyStringStruct.A,
emptyNumberStruct.A,
}

for _, v := range RequiredFail {
err = Required(path, in, v)
assert.Error(t, err)
}

notEmptyString := "bla"
notEmptyStringStruct := struct {
A *string
}{
A: &notEmptyString,
}

notEmptyNumber := 1
notEmptyNumberStruct := struct {
A *int
}{
A: &notEmptyNumber,
}

RequiredSuccess := []interface{}{
" ",
"bla-bla-bla",
2,
[]interface{}{21, []int{}, "testString"},
notEmptyStringStruct.A,
notEmptyNumberStruct.A,
}

for _, v := range RequiredSuccess {
Expand Down