Skip to content

Commit 619321c

Browse files
author
Dean Karn
authored
fix edge case (#1163)
## Fixes Or Enhances Fixes #1162 by not skipping the `required` for non nested structs accidentally introduced in last release.
1 parent d57b3a8 commit 619321c

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

validator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func (v *validate) traverseField(ctx context.Context, parent reflect.Value, curr
173173
// structs. Since it's basically nonsensical to use `required` with a non-pointer struct
174174
// are explicitly skipping the required validation for it. This WILL be removed in the
175175
// next major version.
176-
if !v.v.requiredStructEnabled && ct != nil && ct.tag == requiredTag {
176+
if isNestedStruct && !v.v.requiredStructEnabled && ct != nil && ct.tag == requiredTag {
177177
ct = ct.next
178178
}
179179
}

validator_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13534,3 +13534,26 @@ func TestNestedStructValidation(t *testing.T) {
1353413534
})
1353513535
}
1353613536
}
13537+
13538+
func TestTimeRequired(t *testing.T) {
13539+
validate := New()
13540+
validate.RegisterTagNameFunc(func(fld reflect.StructField) string {
13541+
name := strings.SplitN(fld.Tag.Get("json"), ",", 2)[0]
13542+
13543+
if name == "-" {
13544+
return ""
13545+
}
13546+
13547+
return name
13548+
})
13549+
13550+
type TestTime struct {
13551+
Time time.Time `validate:"required"`
13552+
}
13553+
13554+
var testTime TestTime
13555+
13556+
err := validate.Struct(&testTime)
13557+
NotEqual(t, err, nil)
13558+
AssertError(t, err.(ValidationErrors), "TestTime.Time", "TestTime.Time", "Time", "Time", "required")
13559+
}

0 commit comments

Comments
 (0)