@@ -0,0 +1,44 @@
|
||||
package validate
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"slices"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// PermittedValue reports whether value appears in permittedValues.
|
||||
func PermittedValue[T comparable](value T, permittedValues ...T) bool {
|
||||
return slices.Contains(permittedValues, value)
|
||||
}
|
||||
|
||||
// Matches reports whether value satisfies rx.
|
||||
func Matches(value string, rx *regexp.Regexp) bool {
|
||||
return rx.MatchString(value)
|
||||
}
|
||||
|
||||
// Unique reports whether values contains no duplicate elements.
|
||||
func Unique[T comparable](values []T) bool {
|
||||
uniqueValues := make(map[T]bool)
|
||||
|
||||
for _, value := range values {
|
||||
uniqueValues[value] = true
|
||||
}
|
||||
|
||||
return len(values) == len(uniqueValues)
|
||||
}
|
||||
|
||||
// NotBlank reports whether value contains non-whitespace characters.
|
||||
func NotBlank(value string) bool {
|
||||
return strings.TrimSpace(value) != ""
|
||||
}
|
||||
|
||||
// MaxChars reports whether value contains at most n Unicode code points.
|
||||
func MaxChars(value string, n int) bool {
|
||||
return utf8.RuneCountInString(value) <= n
|
||||
}
|
||||
|
||||
// MinChars reports whether value contains at least n Unicode code points.
|
||||
func MinChars(value string, n int) bool {
|
||||
return utf8.RuneCountInString(value) >= n
|
||||
}
|
||||
Reference in New Issue
Block a user