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

View File

@ -4,7 +4,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"mime" "mime"
"net/url"
"reflect" "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) 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") return nil, errors.New("content not a valid uri")
} }

View File

@ -5,7 +5,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"net/mail" "net/mail"
"net/url"
) )
type Person struct { type Person struct {
@ -30,7 +29,7 @@ func (p *Person) Check() error {
} }
if p.URI != "" { 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) return fmt.Errorf("uri element of person %v not correctly formatted", p.Name)
} }
} }