From d328742b66ee827b86e280e8c04f122437de682c Mon Sep 17 00:00:00 2001 From: BaggerOne Date: Wed, 5 Feb 2020 14:19:31 -0500 Subject: [PATCH] Ensure UnixNano quirks for far-future dates don't break it --- validators/time_after_time.go | 7 +++++++ validators/time_after_time_test.go | 10 ++++++++++ 2 files changed, 17 insertions(+) diff --git a/validators/time_after_time.go b/validators/time_after_time.go index f6ad527..c188c23 100644 --- a/validators/time_after_time.go +++ b/validators/time_after_time.go @@ -17,6 +17,13 @@ type TimeAfterTime struct { // IsValid adds an error if the FirstTime is not after the SecondTime. func (v *TimeAfterTime) IsValid(errors *validate.Errors) { + + // UnixNano wraps around to negative numbers when a time is too far + // into the future (e.g. 260 years) + if v.FirstTime.Year() > v.SecondTime.Year() { + return + } + if v.FirstTime.UnixNano() >= v.SecondTime.UnixNano() { return } diff --git a/validators/time_after_time_test.go b/validators/time_after_time_test.go index 29f658e..383a850 100644 --- a/validators/time_after_time_test.go +++ b/validators/time_after_time_test.go @@ -33,4 +33,14 @@ func Test_TimeAfterTime(t *testing.T) { r.Equal(1, errors.Count()) r.Equal(errors.Get("opens_at"), []string{"OpensAt must be later than Now."}) + + firstTime := now.AddDate(260, 0, 0) + v = TimeAfterTime{ + FirstName: "Opens At", FirstTime: firstTime, + SecondName: "Now", SecondTime: now, + } + + errors = validate.NewErrors() + v.IsValid(errors) + r.Equal(0, errors.Count()) }