Skip to content

Commit 8ba7e82

Browse files
committed
[client] add ListMessagesOptions validation
1 parent 9b7f395 commit 8ba7e82

1 file changed

Lines changed: 17 additions & 7 deletions

File tree

smsgateway/requests_3rdparty.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package smsgateway
22

33
import (
4+
"fmt"
45
"net/url"
56
"strconv"
67
"time"
@@ -86,13 +87,22 @@ func (o ListInboxOptions) ToURLValues() url.Values {
8687

8788
// ListMessagesOptions holds optional filters for listing messages.
8889
type ListMessagesOptions struct {
89-
From *time.Time
90-
To *time.Time
91-
State *string
92-
DeviceID *string
93-
Limit *int
94-
Offset *int
95-
IncludeContent *bool
90+
From *time.Time `query:"from" validate:"omitempty"`
91+
To *time.Time `query:"to" validate:"omitempty"`
92+
State *string `query:"state" validate:"omitempty,oneof=Pending Cancelling Cancelled Processed Sent Delivered Failed"`
93+
DeviceID *string `query:"deviceId" validate:"omitempty,len=21"`
94+
Limit *int `query:"limit" validate:"omitempty,min=1,max=100"`
95+
Offset *int `query:"offset" validate:"omitempty,min=0"`
96+
IncludeContent *bool `query:"includeContent"`
97+
}
98+
99+
// Validate checks if the ListMessagesOptions are valid.
100+
func (o ListMessagesOptions) Validate() error {
101+
if o.From != nil && o.To != nil && o.From.After(*o.To) {
102+
return fmt.Errorf("%w: `from` date must be before `to` date", ErrValidationFailed)
103+
}
104+
105+
return nil
96106
}
97107

98108
// ToURLValues returns the ListMessagesOptions as URL query parameters.

0 commit comments

Comments
 (0)