Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize value 0 for int #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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