Files
validate/chars.go
T

23 lines
412 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
}
}