diff --git a/validators.go b/validators.go index ec71bce..fa9092d 100644 --- a/validators.go +++ b/validators.go @@ -3,6 +3,8 @@ package utils import ( "net/url" "os" + + "github.com/google/uuid" ) // DoesFileExist returns true, if a file exists (and actually is a file) @@ -56,8 +58,8 @@ func IsStringInSliceOfStrings(slice []string, target string) bool { return false } -// IsValidUrl returns true, if s is a valid URL -func IsValidUrl(s string) bool { +// IsValidURL returns true, if s is a valid URL +func IsValidURL(s string) bool { u, err := url.Parse(s) if err != nil || u.Scheme == "" || u.Host == "" { return false @@ -65,3 +67,9 @@ func IsValidUrl(s string) bool { return true } + +// IsValidUUID returns true, if s is a valid UUID +func IsValidUUID(s string) bool { + _, err := uuid.Parse(s) + return err == nil +} diff --git a/validators_test.go b/validators_test.go index 0ec21fd..28c7fd6 100644 --- a/validators_test.go +++ b/validators_test.go @@ -38,7 +38,7 @@ func TestIsStringInSliceOfStrings(t *testing.T) { } } -func TestIsValidUrl(t *testing.T) { +func TestIsValidURL(t *testing.T) { data := map[string]bool{ "": false, "0x0001f346": false, @@ -49,7 +49,27 @@ func TestIsValidUrl(t *testing.T) { } for d, expectedResult := range data { - result := IsValidUrl(d) + result := IsValidURL(d) + if result != expectedResult { + t.Errorf("\ngot: %t\nwanted: %t\nfor: %q", result, expectedResult, d) + } + } +} + +func TestIsValidUUID(t *testing.T) { + data := map[string]bool{ + "": false, + "0x0001f346": false, + "0179eb94-7a81-11ee-b962-0242ac120002": true, + "c3bcd438-7a80-11ee-b962-0242ac120002": true, + "0a245e0a-7a81-11ee-b962-0242ac120002": true, + "9d90b107-fc59-45f8-a622-f55b0a1396d6": true, + "f327c2b7-15a5-43c4-aefb-c66c6f34a32e": true, + "02cb18ec-5d6a-428d-8159-643bde3f43bb": true, + } + + for d, expectedResult := range data { + result := IsValidUUID(d) if result != expectedResult { t.Errorf("\ngot: %t\nwanted: %t\nfor: %q", result, expectedResult, d) }