Files
validate/chars.go
T

25 lines
443 B
Go
Raw Normal View History

2023-10-07 10:52:11 +01:00
package validate
import (
2023-10-07 13:42:17 +01:00
"errors"
2023-10-07 10:52:11 +01:00
"strings"
)
2023-10-07 13:42:17 +01:00
// Validation error.
var (
ErrDisallowedChars = errors.New("contains disallowed characters")
)
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 {
rs := []rune(value)
for _, r := range rs {
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
}
}