Create and use isValidURL

This commit is contained in:
Jason Streifling 2024-10-16 16:14:34 +02:00
parent 4f70da9a17
commit 14696371e2
3 changed files with 8 additions and 4 deletions

View File

@ -2,6 +2,7 @@ package atomfeed
import (
"mime"
"net/url"
"strings"
)
@ -12,6 +13,11 @@ type (
URI string
)
func isValidURL(testURL URI) bool {
_, err := url.ParseRequestURI(string(testURL))
return err == nil
}
func isCompositeMediaType(mediaType string) bool {
mediaType, _, err := mime.ParseMediaType(mediaType)
if err != nil {

View File

@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"mime"
"net/url"
"reflect"
)
@ -23,7 +22,7 @@ func newOutOfLineContent(mediaType string, content any) (*OutOfLineContent, erro
return nil, fmt.Errorf("content type %T incompatible with out of line content", content)
}
if _, err := url.ParseRequestURI(content.(string)); err != nil {
if !isValidURL(content.(URI)) {
return nil, errors.New("content not a valid uri")
}

View File

@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"net/mail"
"net/url"
)
type Person struct {
@ -30,7 +29,7 @@ func (p *Person) Check() error {
}
if p.URI != "" {
if _, err := url.ParseRequestURI(string(p.URI)); err != nil {
if !isValidURL(p.URI) {
return fmt.Errorf("uri element of person %v not correctly formatted", p.Name)
}
}