add chars validation

This commit is contained in:
Aneurin Barker Snook
2023-10-07 10:52:11 +01:00
parent 1a3c213dc1
commit b4b5b8b752
2 changed files with 54 additions and 0 deletions
+19
View File
@@ -0,0 +1,19 @@
package validate
import (
"fmt"
"strings"
)
// 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) {
return fmt.Errorf("Contains disallowed characters")
}
}
return nil
}
}