Skip to content

Commit

Permalink
fix: Add validation tag to verify subscription with empty Channels
Browse files Browse the repository at this point in the history
Add validation tag `gt=0` to verify subscription:
- empty Channels should be invalid
- nil Channels should be valid

Close #565

Signed-off-by: weichou <weichou1229@gmail.com>
weichou1229 committed Mar 26, 2021
1 parent 93bae74 commit 6cd5e03
Showing 2 changed files with 7 additions and 1 deletion.
6 changes: 6 additions & 0 deletions v2/dtos/requests/subscription_test.go
Original file line number Diff line number Diff line change
@@ -211,6 +211,10 @@ func TestUpdateSubscriptionRequest_Validate(t *testing.T) {
unsupportedChannelType.Subscription.Channels = []dtos.Address{
dtos.NewMQTTAddress("host", 123, "publisher", "topic"),
}
validWithoutChannels := NewUpdateSubscriptionRequest(updateSubscriptionData())
validWithoutChannels.Subscription.Channels = nil
invalidEmptyChannels := NewUpdateSubscriptionRequest(updateSubscriptionData())
invalidEmptyChannels.Subscription.Channels = []dtos.Address{}

categoryNameWithReservedChar := NewUpdateSubscriptionRequest(updateSubscriptionData())
categoryNameWithReservedChar.Subscription.Categories = []string{namesWithReservedChar[0]}
@@ -239,6 +243,8 @@ func TestUpdateSubscriptionRequest_Validate(t *testing.T) {
{"invalid, category name containing reserved chars", categoryNameWithReservedChar, true},
{"invalid, receiver name containing reserved chars", receiverNameWithReservedChars, true},
{"invalid, resendInterval is not specified in ISO8601 format", invalidResendInterval, true},
{"valid, without channels", validWithoutChannels, false},
{"invalid, empty channels", invalidEmptyChannels, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2 changes: 1 addition & 1 deletion v2/dtos/subscription.go
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@ type Subscription struct {
type UpdateSubscription struct {
Id *string `json:"id,omitempty" validate:"required_without=Name,edgex-dto-uuid"`
Name *string `json:"name,omitempty" validate:"required_without=Id,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Channels []Address `json:"channels,omitempty" validate:"omitempty,dive"`
Channels []Address `json:"channels,omitempty" validate:"omitempty,gt=0,dive"`
Receiver *string `json:"receiver,omitempty" validate:"omitempty,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Categories []string `json:"categories,omitempty" validate:"omitempty,dive,gt=0,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
Labels []string `json:"labels,omitempty" validate:"omitempty,dive,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`

0 comments on commit 6cd5e03

Please sign in to comment.