so much
This commit is contained in:
1 parent
943408255c
commit
ffa5cd9cd7
38 files changed
+2938
-68
No files matched your search
@@ -0,0 +1,61 @@
|
||||
package validator
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
)
|
||||
|
||||
// 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])?)*$")
|
||||
)
|
||||
|
||||
type Validator struct {
|
||||
Errors map[string]string
|
||||
}
|
||||
|
||||
func New() *Validator {
|
||||
return &Validator{Errors: make(map[string]string)}
|
||||
}
|
||||
|
||||
func (v *Validator) Valid() bool {
|
||||
return len(v.Errors) == 0
|
||||
}
|
||||
|
||||
func (v *Validator) AddError(key, message string) {
|
||||
if _, exists := v.Errors[key]; !exists {
|
||||
v.Errors[key] = message
|
||||
}
|
||||
}
|
||||
|
||||
func (v *Validator) Check(ok bool, key, message string) {
|
||||
if !ok {
|
||||
v.AddError(key, message)
|
||||
}
|
||||
}
|
||||
|
||||
func In(value string, list ...string) bool {
|
||||
for i := range list {
|
||||
if value == list[i] {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func Matches(value string, rx *regexp.Regexp) bool {
|
||||
return rx.MatchString(value)
|
||||
}
|
||||
|
||||
func Unique(values []string) bool {
|
||||
uniqueValues := make(map[string]bool)
|
||||
|
||||
for _, value := range values {
|
||||
uniqueValues[value] = true
|
||||
}
|
||||
|
||||
return len(values) == len(uniqueValues)
|
||||
}
|
||||
Reference in new issue
Block a user