add all validator, simplify error messages

This commit is contained in:
Aneurin Barker Snook
2023-10-07 13:42:17 +01:00
parent c2efe16dc0
commit c74486d122
8 changed files with 98 additions and 19 deletions
+7 -2
View File
@@ -1,17 +1,22 @@
package validate
import (
"fmt"
"errors"
"strings"
)
// Validation error.
var (
ErrDisallowedChars = errors.New("contains disallowed characters")
)
// 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 ErrDisallowedChars
}
}
return nil