Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix edge case #1163

Merged
merged 1 commit into from
Sep 13, 2023
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
2 changes: 1 addition & 1 deletion validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (v *validate) traverseField(ctx context.Context, parent reflect.Value, curr
// structs. Since it's basically nonsensical to use `required` with a non-pointer struct
// are explicitly skipping the required validation for it. This WILL be removed in the
// next major version.
if !v.v.requiredStructEnabled && ct != nil && ct.tag == requiredTag {
if isNestedStruct && !v.v.requiredStructEnabled && ct != nil && ct.tag == requiredTag {
ct = ct.next
}
}
Expand Down
23 changes: 23 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13534,3 +13534,26 @@ func TestNestedStructValidation(t *testing.T) {
})
}
}

func TestTimeRequired(t *testing.T) {
validate := New()
validate.RegisterTagNameFunc(func(fld reflect.StructField) string {
name := strings.SplitN(fld.Tag.Get("json"), ",", 2)[0]

if name == "-" {
return ""
}

return name
})

type TestTime struct {
Time time.Time `validate:"required"`
}

var testTime TestTime

err := validate.Struct(&testTime)
NotEqual(t, err, nil)
AssertError(t, err.(ValidationErrors), "TestTime.Time", "TestTime.Time", "Time", "Time", "required")
}