diff --git a/policy/policy_test.go b/policy/policy_test.go index c3e4ea2..7ccffbb 100644 --- a/policy/policy_test.go +++ b/policy/policy_test.go @@ -282,15 +282,18 @@ func TestCreateNotificationPolicy_Validate(t *testing.T) { err = request.Validate() assert.Nil(t, err) - request.DelayAction = &DelayAction{DelayOption: NextSunday, UntilHour: -3} + invalidHour, validHour := -3, 3 + invalidMinute, validMinute := 60, 55 + + request.DelayAction = &DelayAction{DelayOption: NextSunday, UntilHour: &invalidHour} err = request.Validate() assert.Equal(t, "delayAction's UntilHour or UntilMinute is not valid", err.Error()) - request.DelayAction = &DelayAction{DelayOption: NextSunday, UntilHour: 5, UntilMinute: 60} + request.DelayAction = &DelayAction{DelayOption: NextSunday, UntilHour: &validHour, UntilMinute: &invalidMinute} err = request.Validate() assert.Equal(t, "delayAction's UntilHour or UntilMinute is not valid", err.Error()) - request.DelayAction = &DelayAction{DelayOption: NextSunday, UntilHour: 5, UntilMinute: 55} + request.DelayAction = &DelayAction{DelayOption: NextSunday, UntilHour: &validHour, UntilMinute: &validMinute} err = request.Validate() assert.Nil(t, err) diff --git a/policy/request.go b/policy/request.go index 7839bfd..5afc8c6 100644 --- a/policy/request.go +++ b/policy/request.go @@ -547,8 +547,8 @@ type DeDuplicationAction struct { type DelayAction struct { DelayOption DelayType `json:"delayOption,omitempty"` - UntilMinute int `json:"untilMinute,omitempty"` - UntilHour int `json:"untilHour,omitempty"` + UntilMinute *int `json:"untilMinute,omitempty"` + UntilHour *int `json:"untilHour,omitempty"` Duration *Duration `json:"duration,omitempty"` } @@ -667,12 +667,16 @@ func ValidateDelayAction(action DelayAction) error { return err } } - if action.DelayOption != ForDuration && ((action.UntilHour < 0 || action.UntilHour > 23) || (action.UntilMinute < 0 || action.UntilMinute > 59)) { + if action.DelayOption != ForDuration && (!validDuration(action.UntilHour, 23) || !validDuration(action.UntilMinute, 59)) { return errors.New("delayAction's UntilHour or UntilMinute is not valid") } return nil } +func validDuration(value *int, max int) bool { + return value != nil && *value >= 0 && *value <= max +} + func ValidateResponders(responders *[]alert.Responder) error { for _, responder := range *responders { if responder.Type != alert.UserResponder && responder.Type != alert.TeamResponder {