apskel-pos-backend/internal/validator/notification_validator.go
2026-05-10 10:57:38 +07:00

54 lines
1.7 KiB
Go

package validator
import (
"fmt"
"apskel-pos-be/internal/constants"
"apskel-pos-be/internal/contract"
)
type NotificationValidator interface {
ValidateSendRequest(req *contract.SendNotificationRequest) (error, string)
ValidateBroadcastRequest(req *contract.BroadcastNotificationRequest) (error, string)
ValidateListRequest(req *contract.ListNotificationsRequest) (error, string)
}
type NotificationValidatorImpl struct{}
func NewNotificationValidator() *NotificationValidatorImpl {
return &NotificationValidatorImpl{}
}
func (v *NotificationValidatorImpl) ValidateSendRequest(req *contract.SendNotificationRequest) (error, string) {
if req.Title == "" {
return fmt.Errorf("title is required"), constants.MissingFieldErrorCode
}
if req.Body == "" {
return fmt.Errorf("body is required"), constants.MissingFieldErrorCode
}
if len(req.ReceiverIDs) == 0 {
return fmt.Errorf("at least one receiver_id is required"), constants.MissingFieldErrorCode
}
return nil, ""
}
func (v *NotificationValidatorImpl) ValidateBroadcastRequest(req *contract.BroadcastNotificationRequest) (error, string) {
if req.Title == "" {
return fmt.Errorf("title is required"), constants.MissingFieldErrorCode
}
if req.Body == "" {
return fmt.Errorf("body is required"), constants.MissingFieldErrorCode
}
return nil, ""
}
func (v *NotificationValidatorImpl) ValidateListRequest(req *contract.ListNotificationsRequest) (error, string) {
if req.Page < 1 {
return fmt.Errorf("page must be greater than 0"), constants.ValidationErrorCode
}
if req.Limit < 1 || req.Limit > 100 {
return fmt.Errorf("limit must be between 1 and 100"), constants.ValidationErrorCode
}
return nil, ""
}