utils/validators.go

68 lines
1.2 KiB
Go

package utils
import (
"net/url"
"os"
)
// 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
}