2023-10-07 10:52:11 +01:00
|
|
|
package validate
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
2023-10-07 13:42:17 +01:00
|
|
|
// Validation error.
|
|
|
|
|
var (
|
2023-11-19 20:27:33 +00:00
|
|
|
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 {
|
2023-11-19 20:27:33 +00:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|