Files
kleiax 904d14b64c
CI / test (push) Canceled after 0s
Initial commit
2026-09-12 22:22:17 +02:00

45 lines
1.1 KiB
Go

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
}