-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvalidator.go
More file actions
138 lines (129 loc) · 4.41 KB
/
validator.go
File metadata and controls
138 lines (129 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* Copyright (c) 2024. Devtron Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package timeRangeLib
import (
"errors"
"golang.org/x/exp/slices"
"strconv"
"strings"
)
func (tr TimeRange) ValidateTimeRange() error {
if !slices.Contains(AllowedFrequencies, tr.Frequency) {
return errors.New(string(InvalidFrequencyType))
}
if tr.Frequency != Fixed {
err := validateHourMinute(tr.HourMinuteFrom)
if err != nil {
return err
}
err = validateHourMinute(tr.HourMinuteTo)
if err != nil {
return err
}
// Ensure HourMinuteFrom < HourMinuteTo for Daily and Weekly frequencies
if tr.Frequency == Daily || tr.Frequency == Weekly {
if isToBeforeFrom(tr.HourMinuteFrom, tr.HourMinuteTo) {
return errors.New(string(InvalidHourMinuteForWeeklyAndDaily))
}
}
}
if !tr.TimeFrom.IsZero() && !tr.TimeTo.IsZero() {
if tr.Frequency == Fixed {
if tr.TimeFrom.After(tr.TimeTo) {
return errors.New(string(TimeFromLessThanTimeTo))
}
if tr.TimeFrom.Equal(tr.TimeTo) {
return errors.New(string(TimeFromEqualToTimeTo))
}
} else {
// Validate that the date range is valid (same date safe for recurrence type windows)
if !isDateFromBeforeTo(tr.TimeFrom, tr.TimeTo) {
return errors.New(string(DayFromOrToNotValid))
}
}
}
switch tr.Frequency {
case Daily:
if tr.HourMinuteFrom == "" || tr.HourMinuteTo == "" {
return errors.New(string(HourMinuteNotPresent))
}
case Fixed:
if tr.TimeFrom.IsZero() || tr.TimeTo.IsZero() {
return errors.New(string(TimeFromOrToNotPresent))
}
case Weekly:
if len(tr.Weekdays) == 0 {
return errors.New(string(WeekDaysNotPresent))
}
for _, weekday := range tr.Weekdays {
if weekday < 0 || weekday > 6 {
return errors.New(string(WeekDayOutsideRange))
}
}
case WeeklyRange:
if tr.WeekdayFrom == 0 || tr.WeekdayTo == 0 {
return errors.New(string(WeekDayFromOrToNotPresent))
}
if (tr.WeekdayFrom < 0 || tr.WeekdayFrom > 6) || (tr.WeekdayTo < 0 || tr.WeekdayTo > 6) {
return errors.New(string(WeekDayOutsideRange))
}
// Same weekday with a non-positive intra-day span collapses the window to zero/negative
// duration, which stalls the next-window search. Reject it (mirrors the Monthly check).
if tr.WeekdayFrom == tr.WeekdayTo && isToBeforeFrom(tr.HourMinuteFrom, tr.HourMinuteTo) {
return errors.New(string(ToBeforeFrom))
}
case Monthly:
if tr.DayFrom == 0 || tr.DayTo == 0 {
return errors.New(string(DayFromOrToNotPresent))
}
if tr.DayFrom == tr.DayTo && isToBeforeFrom(tr.HourMinuteFrom, tr.HourMinuteTo) {
return errors.New(string(ToBeforeFrom))
}
// DayFrom > DayTo => check for the correct value
if tr.DayFrom > 0 && tr.DayTo > 0 && tr.DayFrom > tr.DayTo {
return errors.New(string(DayFromOrToNotValid))
}
if tr.DayFrom < 0 && tr.DayTo > 0 {
return errors.New(string(DayFromOrToNotValid))
}
// this is to prevent overlapping windows crossing to next month for both negatives
if tr.DayFrom < 0 && tr.DayTo < 0 && tr.DayFrom > tr.DayTo {
return errors.New(string(BothLessThanZeroAndFromGreaterThanTo))
}
// this is an edge case where with negative 'to' date results into a date before the 'from' date
// example: 26,-4 will pe prevented because for February it will become invalid
// also currently max negative supported is third last day of the month
if (tr.DayTo < 0 && tr.DayFrom > 0 && tr.DayFrom > 29+tr.DayTo) || tr.DayTo < -3 || tr.DayFrom < -3 {
return errors.New(string(DayFromOrToNotValid))
}
}
return nil
}
func validateHourMinute(hourMinute string) error {
parts := strings.Split(hourMinute, ":")
if len(parts) != 2 {
return errors.New("HourMinute is not valid, should be strictly of format HH:MM")
}
hh, err := strconv.Atoi(parts[0])
if err != nil || hh > 23 || hh < 0 {
return errors.New("Hour is not valid" + parts[0])
}
mm, err := strconv.Atoi(parts[1])
if err != nil || mm > 59 || mm < 0 {
return errors.New("Minute is not valid" + parts[1])
}
return nil
}