utils/validators.go

76 lines
1.4 KiB
Go
Raw Normal View History

2023-10-02 23:42:16 +02:00
package utils
2023-10-13 20:38:08 +02:00
import (
"net/url"
"os"
2023-11-03 20:43:13 +01:00
"github.com/google/uuid"
2023-10-13 20:38:08 +02:00
)
// DoesFileExist returns true, if a file exists (and actually is a file)
func DoesFileExist(p string) bool {
s, err := os.Stat(p)
if os.IsNotExist(err) {
return false
}
if s.IsDir() {
return false
}
return true
}
// DoesFolderExist returns true, if a folder exists (and actually is a folder)
func DoesFolderExist(p string) bool {
s, err := os.Stat(p)
if os.IsNotExist(err) {
return false
}
if !s.IsDir() {
return false
}
return true
}
// DoesStringContainsNonWhitelistedSubstrings returns false if s contains substrings which are not in whitelist
func DoesStringContainsNonWhitelistedSubstrings(s string, whitelist map[string]string) bool {
for _, char := range s {
_, charIsWhitelisted := whitelist[string(char)]
if !charIsWhitelisted {
return true
}
}
return false
}
2023-10-02 23:42:16 +02:00
// IsStringInSliceOfStrings returns true, if slice contains target
func IsStringInSliceOfStrings(slice []string, target string) bool {
for _, v := range slice {
if v == target {
return true
}
}
return false
}
2023-11-03 20:43:13 +01:00
// IsValidURL returns true, if s is a valid URL
func IsValidURL(s string) bool {
2023-10-02 23:42:16 +02:00
u, err := url.Parse(s)
if err != nil || u.Scheme == "" || u.Host == "" {
return false
}
return true
}
2023-11-03 20:43:13 +01:00
// IsValidUUID returns true, if s is a valid UUID
func IsValidUUID(s string) bool {
_, err := uuid.Parse(s)
return err == nil
}