Files

67 lines
2.0 KiB
Go

package validator
import (
"regexp"
"slices"
)
// Declare a regular expression for sanity checking the format of email addresses (we'll
// use this later in the book). If you're interested, this regular expression pattern is
// taken from https://html.spec.whatwg.org/#valid-e-mail-address. Note: if you're
// reading this in PDF or EPUB format and cannot see the full pattern, please see the
// note further down the page.
var (
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])?)*$")
)
// Define a new Validator type which contains a map of validation errors.
type Validator struct {
Errors map[string]string
}
// New is a helper which creates a new Validator instance with an empty errors map.
func New() *Validator {
return &Validator{Errors: make(map[string]string)}
}
// Valid returns true if the errors map doesn't contain any entries.
func (v *Validator) Valid() bool {
return len(v.Errors) == 0
}
// AddError adds an error message to the map (so long as no entry already exists for
// the given key).
func (v *Validator) AddError(key, message string) {
if _, exists := v.Errors[key]; !exists {
v.Errors[key] = message
}
}
// Check adds an error message to the map only if a validation check is not 'ok'.
func (v *Validator) Check(ok bool, key, message string) {
if !ok {
v.AddError(key, message)
}
}
// In returns true if a specific value is in a list of strings.
func In(value string, list ...string) bool {
return slices.Contains(list, value)
}
// Matches returns true if a string value matches a specific regexp pattern.
func Matches(value string, rx *regexp.Regexp) bool {
return rx.MatchString(value)
}
// Unique returns true if all string values in a slice are unique.
func Unique(values []string) bool {
uniqueValues := make(map[string]bool, 5)
for _, value := range values {
uniqueValues[value] = true
}
return len(values) == len(uniqueValues)
}