utils/validators.go
2023-11-03 20:43:13 +01:00

76 lines
1.4 KiB
Go

package utils
import (
"net/url"
"os"
"github.com/google/uuid"
)
// 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
}
// 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
}
// 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
}
return true
}
// IsValidUUID returns true, if s is a valid UUID
func IsValidUUID(s string) bool {
_, err := uuid.Parse(s)
return err == nil
}