Skip to content

Commit

Permalink
Merge pull request #159 from vgogolev1703/patch-1
Browse files Browse the repository at this point in the history
Revert "fix: *string(nil) not equal &string #152"
  • Loading branch information
casualjim committed Dec 1, 2023
2 parents 194d97e + 695454b commit b2d5112
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 47 deletions.
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

0 comments on commit b2d5112

Please sign in to comment.