papibot/pkg/validation/validation.go
Javier Feliz d9be5a78c8
All checks were successful
Run Go Tests / Run Tests (pull_request) Successful in 13m35s
Add validation package
2025-02-09 00:25:41 -05:00

46 lines
852 B
Go

package validation
import (
"errors"
neturl "net/url"
"strings"
)
// Errors
var (
ErrNotAUrl = errors.New("not a URL")
ErrIncorrectProtocol = errors.New("incorrect url protocol")
ErrServiceUnsupported = errors.New("not a URL")
)
// Music hosts that we support
var musicHosts = []string{
"youtube.com",
}
func IsUrl(url string) (bool, *neturl.URL) {
parsed, err := neturl.ParseRequestURI(url)
return (err == nil), parsed
}
func IsMusicUrl(url string) (bool, error) {
isUrl, parsed := IsUrl(url)
if !isUrl {
return false, ErrNotAUrl
}
// If we have a scheme and it's not http/https we fail
if parsed.Scheme != "" && !strings.Contains(parsed.Scheme, "http") {
return false, ErrIncorrectProtocol
}
for _, host := range musicHosts {
if host == parsed.Host {
return true, nil
}
}
return false, ErrServiceUnsupported
}