You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
2.6 KiB
Go
81 lines
2.6 KiB
Go
package validator
|
|
|
|
import (
|
|
"git.runcible.io/learning/pulley/internal/data"
|
|
"regexp"
|
|
"slices"
|
|
"time"
|
|
)
|
|
|
|
// 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.
|
|
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])?)*$")
|
|
)
|
|
|
|
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)}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
// Generic function which returns true if a specific value is in a list of permitted
|
|
// values.
|
|
func PermittedValue[T comparable](value T, permittedValues ...T) bool {
|
|
return slices.Contains(permittedValues, 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)
|
|
}
|
|
|
|
// Generic function which returns true if all values in a slice are unique.
|
|
func Unique[T comparable](values []T) bool {
|
|
uniqueValues := make(map[T]bool)
|
|
|
|
for _, value := range values {
|
|
uniqueValues[value] = true
|
|
}
|
|
|
|
return len(values) == len(uniqueValues)
|
|
}
|
|
|
|
func ValidateMovie(v *Validator, m *data.Movie) {
|
|
|
|
v.Check(m.Title != "", "title", "must be provided")
|
|
v.Check(len(m.Title) <= 500, "title", "must not be more than 500 bytes long")
|
|
v.Check(m.Year != 0, "year", "must be provided")
|
|
v.Check(m.Year >= 1888, "year", "must be greater than 1888")
|
|
v.Check(m.Year <= int32(time.Now().Year()), "year", "must not be in the future")
|
|
v.Check(m.Runtime != 0, "runtime", "must be provided")
|
|
v.Check(m.Runtime > 0, "runtime", "must be a positive integer")
|
|
v.Check(m.Genres != nil, "genres", "must be provided")
|
|
v.Check(len(m.Genres) >= 1, "genres", "must contain one genre")
|
|
v.Check(len(m.Genres) <= 5, "genres", "must not contain more than 5 genres")
|
|
v.Check(Unique(m.Genres), "genres", "must not contain duplicate values")
|
|
}
|