Files
2024-07-19 14:11:53 +01:00

34 lines
691 B
Go

package validate
import (
"strings"
)
var (
ErrDisallowedChars = NewError("contains disallowed characters")
)
// Chars validates whether a string contains only allowed characters.
func Chars(allow string) func(string) error {
return func(value string) error {
for _, r := range value {
if !strings.ContainsRune(allow, r) {
return ErrDisallowedChars
}
}
return nil
}
}
// ExceptChars validates whether a string does not contain disallowed characters.
func ExceptChars(disallow string) func(string) error {
return func(value string) error {
for _, r := range disallow {
if strings.ContainsRune(value, r) {
return ErrDisallowedChars
}
}
return nil
}
}