@@ -0,0 +1,3 @@
|
||||
// Package validate contains reusable validation helpers and an error collector
|
||||
// for API and form inputs.
|
||||
package validate
|
||||
@@ -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
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package validate
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
)
|
||||
|
||||
var (
|
||||
// EmailRX matches the email-address format accepted by Gardomatic.
|
||||
EmailRX = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
|
||||
// ColorRX matches the hexadecimal RGB colors accepted for user colors.
|
||||
ColorRX = regexp.MustCompile(`^#[0-9a-fA-F]{6}$`)
|
||||
)
|
||||
|
||||
// Validator collects validation failures keyed by input field.
|
||||
type Validator struct {
|
||||
Errors map[string]string
|
||||
NonFieldErrors []string
|
||||
FieldErrors map[string]string
|
||||
}
|
||||
|
||||
// New returns an empty Validator.
|
||||
func New() *Validator {
|
||||
return &Validator{Errors: make(map[string]string)}
|
||||
}
|
||||
|
||||
// Valid reports whether no validation errors have been recorded.
|
||||
func (v *Validator) Valid() bool {
|
||||
return len(v.Errors) == 0 && len(v.FieldErrors) == 0 && len(v.NonFieldErrors) == 0
|
||||
}
|
||||
|
||||
// AddError records message for key unless key already has an error.
|
||||
func (v *Validator) AddError(key, message string) {
|
||||
if _, exists := v.Errors[key]; !exists {
|
||||
v.Errors[key] = message
|
||||
}
|
||||
}
|
||||
|
||||
// Check records message for key when ok is false.
|
||||
func (v *Validator) Check(ok bool, key, message string) {
|
||||
if !ok {
|
||||
v.AddError(key, message)
|
||||
}
|
||||
}
|
||||
|
||||
// AddNonFieldError records an error that is not associated with one field.
|
||||
func (v *Validator) AddNonFieldError(message string) {
|
||||
v.NonFieldErrors = append(v.NonFieldErrors, message)
|
||||
}
|
||||
|
||||
// AddFieldError records a form-specific field error.
|
||||
func (v *Validator) AddFieldError(key, message string) {
|
||||
|
||||
if v.FieldErrors == nil {
|
||||
v.FieldErrors = make(map[string]string)
|
||||
}
|
||||
|
||||
if _, exists := v.FieldErrors[key]; !exists {
|
||||
v.FieldErrors[key] = message
|
||||
}
|
||||
}
|
||||
|
||||
// CheckField records a form-specific field error when ok is false.
|
||||
func (v *Validator) CheckField(ok bool, key, message string) {
|
||||
if !ok {
|
||||
v.AddFieldError(key, message)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user