-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation.go
63 lines (55 loc) · 1.47 KB
/
validation.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
package aform
import (
"fmt"
"github.com/go-playground/validator/v10"
"strconv"
"strings"
"sync"
)
var doValidateInitOnce sync.Once
var validate *validator.Validate
func defaultValidate() *validator.Validate {
doValidateInitOnce.Do(func() {
validate = validator.New()
_ = validate.RegisterValidation("boolean", func(fl validator.FieldLevel) bool {
_, err := parseBool(fl.Field().String())
return err == nil
})
setValidationTranslations(validate)
})
return validate
}
func validateValue(value string, tag string) []Error {
err := defaultValidate().Var(value, tag)
if err != nil {
var all []Error
for _, err := range err.(validator.ValidationErrors) {
all = append(all, ErrorWrap(errorFromFieldError{fieldError: err}))
}
return all
}
return nil
}
func buildValidationRules(required bool, rules ...string) string {
var parts []string
if required {
parts = append(parts, RequiredErrorCode)
}
for _, rule := range rules {
parts = append(parts, rule)
}
return strings.Join(parts, ",")
}
func buildValidationChoicesRule(choices ...string) string {
var parts []string
for _, choice := range choices {
parts = append(parts, fmt.Sprintf("'%s'", choice))
}
return ChoiceErrorCode + "=" + strings.Join(parts, " ")
}
func buildValidationMinRule(min uint) string {
return MinLengthErrorCode + "=" + strconv.FormatUint(uint64(min), 10)
}
func buildValidationMaxRule(max uint) string {
return MaxLengthErrorCode + "=" + strconv.FormatUint(uint64(max), 10)
}