Convert to time.Time before asserting in isNeField - #1619
Conversation
The struct branch admits any type convertible to time.Time, but isNeField asserted getValue(...) directly instead of converting first, so a named type with a time.Time underlying panicked. Every other comparison validator converts.
|
The |
I opened a PR to fix the linter issues: #1618 |
|
Thank you for your contribution! |
nefieldpanics on a named type whose underlying type istime.Time.isNeField's struct branch (baked_in.go:1072) guards onConvertibleTo(timeType), which admitsany named type with a
time.Timeunderlying — then asserts without converting:getValuereturnsval.Interface()(validator.go:516), so the dynamic type is stillMyTimeand the assertion fails.
It is the only one
Of the 28
getValue(...).(time.Time)sites inbaked_in.go, 26 convert first. The two that donot are this pair.
isEqField:1402,isGteField:2326,isGtField:2370,isLteField:2554,isLtField:2598andisNeCrossStructField:1308— the same not-equal semantics in the siblingfamily — all do:
Same input through four of them, before the change:
Change
Two lines, adding the
Convert(timeType)the siblings already have.Convertis a no-op for aplain
time.Time, so nothing on the currently covered paths changes, and the v10 API is untouched.Test added next to the other
nefieldtests. Verified red/green by reverting onlybaked_in.goand keeping the test — it panics with
interface conversion: interface {} is validator.namedTime.It also pins the semantics rather than just the absence of a panic: equal named-time values must
fail the tag, different ones must pass.
go test -cover -race ./...passes across every package.gofmt -lreportsdoc.goandtranslations/ko/ko_test.goboth before and after, so this branch does not change it.validator_test.go:5317-5414coversnefieldagainsttime.Time,*time.Timeandtime.Duration, which is why this shape was never exercised.Disclosure: prepared with AI assistance; I verified the reproduction, the red/green runs and the
race suite myself.