Files
validate/chars.go
T

35 lines
712 B
Go
Raw Normal View History

2023-10-07 10:52:11 +01:00
package validate
import (
"strings"
)
2023-10-07 13:42:17 +01:00
// Validation error.
var (
ErrDisallowedChars = NewError("contains disallowed characters")
2023-10-07 13:42:17 +01:00
)
2023-10-07 10:52:11 +01:00
// 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 {
2023-10-07 10:52:11 +01:00
if !strings.ContainsRune(allow, r) {
2023-10-07 13:42:17 +01:00
return ErrDisallowedChars
2023-10-07 10:52:11 +01:00
}
}
return nil
}
}
2023-11-20 17:59:05 +00:00
// 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
}
}