Skip to content

Commit

Permalink
Serialize value 0 for int
Browse files Browse the repository at this point in the history
  • Loading branch information
Rens van Leeuwen committed Apr 9, 2020
1 parent d0ddd91 commit 64c7ed1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
9 changes: 6 additions & 3 deletions policy/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
10 changes: 7 additions & 3 deletions policy/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 64c7ed1

Please sign in to comment.