-
Notifications
You must be signed in to change notification settings - Fork 4
/
validator.go
105 lines (89 loc) · 2.6 KB
/
validator.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
96
97
98
99
100
101
102
103
104
105
package main
import (
"bytes"
"fmt"
"html/template"
"github.com/achiku/varfmt"
)
// Validators validators
type Validators map[string]Validator
// Render rendor validators
func (vs Validators) Render() []byte {
var src bytes.Buffer
// constants
fmt.Fprint(&src, "// regexp string constants\n")
fmt.Fprint(&src, "const (\n")
for _, v := range vs {
fmt.Fprintf(&src, "%s\n", v.RegexpConst())
}
fmt.Fprint(&src, ")\n")
// vars
fmt.Fprint(&src, "// regexp objects\n")
fmt.Fprint(&src, "var (\n")
for _, v := range vs {
fmt.Fprintf(&src, "%s\n", v.RegexpVar())
}
fmt.Fprint(&src, ")\n")
fmt.Fprint(&src, "// use a single instance of Validate, it caches struct info\n")
fmt.Fprint(&src, "var validate *validator.Validate\n")
// function definitions
for _, v := range vs {
fmt.Fprintf(&src, "%s\n", v.ValidatorFunc())
}
// register validation functions
fmt.Fprint(&src, "func init() {\n")
fmt.Fprint(&src, "validate = validator.New()\n")
for _, v := range vs {
fmt.Fprintf(&src, "%s\n", v.RegisterFunc())
}
fmt.Fprint(&src, "}\n")
return src.Bytes()
}
// Validator validator
type Validator struct {
Name string
RegexpString string
}
// RegexpConstName const name
func (val Validator) RegexpConstName() string {
return varfmt.PublicVarName(val.Name) + "RegexString"
}
// RegexpVarName var regexp name
func (val Validator) RegexpVarName() string {
return varfmt.PublicVarName(val.Name) + "Regex"
}
// ValidateFuncName validator func name
func (val Validator) ValidateFuncName() string {
return varfmt.PublicVarName(val.Name) + "Validator"
}
// RegexpConst const def
func (val Validator) RegexpConst() string {
return fmt.Sprintf("%s = `%s`", val.RegexpConstName(), val.RegexpString)
}
// RegexpVar var regexp def
func (val Validator) RegexpVar() string {
return fmt.Sprintf("%s = regexp.MustCompile(%s)", val.RegexpVarName(), val.RegexpConstName())
}
// ValidatorFunc validator
func (val Validator) ValidatorFunc() string {
// ignore errors since it always succeeds
tmpl, _ := template.New("").Parse(`
// {{ .ValidateFuncName }} for validation
func {{ .ValidateFuncName }}(fl validator.FieldLevel) bool {
return {{ .RegexpVarName }}.MatchString(fl.Field().String())
}`)
var src bytes.Buffer
tmpl.Execute(&src, val)
return src.String()
}
// RegisterFunc register validator
func (val Validator) RegisterFunc() string {
// ignore errors since it always succeeds
tmpl, _ := template.New("").Parse(`
if err := validate.RegisterValidation("{{ .ValidateFuncName }}", {{ .ValidateFuncName }}); err != nil {
log.Fatal(err)
}`)
var src bytes.Buffer
tmpl.Execute(&src, val)
return src.String()
}