Cleaned up some validation code, stricter validation

This commit is contained in:
yusing
2025-01-26 14:43:48 +08:00
parent 254224c0e8
commit 1586610a44
23 changed files with 590 additions and 468 deletions

47
internal/notif/base.go Normal file
View File

@@ -0,0 +1,47 @@
package notif
import (
"net/url"
"strings"
E "github.com/yusing/go-proxy/internal/error"
)
type ProviderBase struct {
Name string `json:"name" validate:"required"`
URL string `json:"url" validate:"url"`
Token string `json:"token"`
}
var (
ErrMissingToken = E.New("token is required")
ErrURLMissingScheme = E.New("url missing scheme, expect 'http://' or 'https://'")
)
// Validate implements the utils.CustomValidator interface.
func (base *ProviderBase) Validate() E.Error {
if base.Token == "" {
return ErrMissingToken
}
if !strings.HasPrefix(base.URL, "http://") && !strings.HasPrefix(base.URL, "https://") {
return ErrURLMissingScheme
}
u, err := url.Parse(base.URL)
if err != nil {
return E.Wrap(err)
}
base.URL = u.String()
return nil
}
func (base *ProviderBase) GetName() string {
return base.Name
}
func (base *ProviderBase) GetURL() string {
return base.URL
}
func (base *ProviderBase) GetToken() string {
return base.Token
}