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
19 changes: 19 additions & 0 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
tagKeySeparator = "="
structOnlyTag = "structonly"
omitempty = "omitempty"
required = "required"
fieldErrMsg = "Field validation for \"%s\" failed on the \"%s\" tag"
structErrMsg = "Struct:%s\n"
)
Expand Down Expand Up @@ -390,6 +391,24 @@ func (v *Validate) structRecursive(top interface{}, current interface{}, s inter
continue
}

if valueField.Kind() == reflect.Ptr && valueField.IsNil() {

if strings.Contains(cField.tag, omitempty) {
continue
}

if strings.Contains(cField.tag, required) {

validationErrors.Errors[cField.name] = &FieldError{
Field: cField.name,
Tag: required,
Value: valueField.Interface(),
}

continue
}
}

if structErrors := v.structRecursive(top, valueField.Interface(), valueField.Interface()); structErrors != nil {
validationErrors.StructErrors[cField.name] = structErrors
// free up memory map no longer needed
Expand Down
99 changes: 95 additions & 4 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,98 @@ func AssertMapFieldError(t *testing.T, s map[string]*FieldError, field string, e
EqualSkip(t, 2, val.Tag, expectedTag)
}

func TestNilStructPointerValidation(t *testing.T) {
type Inner struct {
Data string
}

type Outer struct {
Inner *Inner `validate:"omitempty"`
}

inner := &Inner{
Data: "test",
}

outer := &Outer{
Inner: inner,
}

errs := validate.Struct(outer)
Equal(t, errs, nil)

outer = &Outer{
Inner: nil,
}

errs = validate.Struct(outer)
Equal(t, errs, nil)

type Inner2 struct {
Data string
}

type Outer2 struct {
Inner2 *Inner2 `validate:"required"`
}

inner2 := &Inner2{
Data: "test",
}

outer2 := &Outer2{
Inner2: inner2,
}

errs = validate.Struct(outer2)
Equal(t, errs, nil)

outer2 = &Outer2{
Inner2: nil,
}

errs = validate.Struct(outer2)
NotEqual(t, errs, nil)

type Inner3 struct {
Data string
}

type Outer3 struct {
Inner3 *Inner3
}

inner3 := &Inner3{
Data: "test",
}

outer3 := &Outer3{
Inner3: inner3,
}

errs = validate.Struct(outer3)
Equal(t, errs, nil)

type Inner4 struct {
Data string
}

type Outer4 struct {
Inner4 *Inner4 `validate:"-"`
}

inner4 := &Inner4{
Data: "test",
}

outer4 := &Outer4{
Inner4: inner4,
}

errs = validate.Struct(outer4)
Equal(t, errs, nil)
}

func TestSSNValidation(t *testing.T) {
tests := []struct {
param string
Expand Down Expand Up @@ -1100,7 +1192,7 @@ func TestStructOnlyValidation(t *testing.T) {
InnerStruct: nil,
}

errs := validate.Struct(outer).Flatten()
errs := validate.Struct(outer)
NotEqual(t, errs, nil)

inner := &Inner{
Expand All @@ -1111,9 +1203,8 @@ func TestStructOnlyValidation(t *testing.T) {
InnerStruct: inner,
}

errs = validate.Struct(outer).Flatten()
NotEqual(t, errs, nil)
Equal(t, len(errs), 0)
errs = validate.Struct(outer)
Equal(t, errs, nil)
}

func TestGtField(t *testing.T) {
Expand Down