-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.go
95 lines (76 loc) · 3.74 KB
/
validate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Package validate contains the support for validating models.
package validate
import (
"errors"
"reflect"
"regexp"
"strings"
"github.com/go-playground/locales/en"
ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
en_translations "github.com/go-playground/validator/v10/translations/en"
"github.com/google/uuid"
)
// validate holds the settings and caches for validating request struct values.
var validate *validator.Validate
// translator is a cache of locale and translation information.
var translator ut.Translator
// emailRegex is the regular expression used to determine if a string is an email.
// https://github.com/go-playground/validator/blob/v10.10.0/regexes.go#L73
var emailRegex *regexp.Regexp
func init() {
// Instantiate a validator.
validate = validator.New()
// Create a translator for english so the error messages are
// more human-readable than technical.
translator, _ = ut.New(en.New(), en.New()).GetTranslator("en")
// Register the english error messages for use.
en_translations.RegisterDefaultTranslations(validate, translator)
// Use JSON tag names for errors instead of Go struct names.
validate.RegisterTagNameFunc(func(fld reflect.StructField) string {
name := strings.SplitN(fld.Tag.Get("json"), ",", 2)[0]
if name == "-" {
return ""
}
return name
})
// emailRegexString is the regular expression string used to compile into a regexp.
// https://github.com/go-playground/validator/blob/v10.10.0/regexes.go#L18
const emailRegexString = "^(?:(?:(?:(?:[a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+(?:\\.([a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\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}
// Check validates the provided model against it's declared tags.
func Check(val any) error {
if err := validate.Struct(val); err != nil {
// Use a type assertion to get the real error value.
verrors, ok := err.(validator.ValidationErrors)
if !ok {
return err
}
var fields FieldErrors
for _, verror := range verrors {
field := FieldError{
Field: verror.Field(),
Error: verror.Translate(translator),
}
fields = append(fields, field)
}
return fields
}
return nil
}
// GenerateID generate a unique id for entities.
func GenerateID() string {
return uuid.NewString()
}
// CheckID validates that the format of an id is valid.
func CheckID(id string) error {
if _, err := uuid.Parse(id); err != nil {
return errors.New("ID is not in its proper form")
}
return nil
}
// CheckEmail validates that the string is an email.
func CheckEmail(email string) bool {
return emailRegex.MatchString(email)
}