-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.go
112 lines (97 loc) · 3.33 KB
/
main.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
106
107
108
109
110
111
112
package main
import (
"context"
"fmt"
"log"
"net/url"
"github.com/go-playground/form/v4"
"github.com/go-playground/mold/v4/modifiers"
"github.com/go-playground/mold/v4/scrubbers"
"github.com/go-playground/validator/v10"
)
// This example is centered around a form post, but doesn't have to be
// just trying to give a well rounded real life example.
// <form method="POST">
// <input type="text" name="Name" value="joeybloggs"/>
// <input type="text" name="Age" value="3"/>
// <input type="text" name="Gender" value="Male"/>
// <input type="text" name="Address[0].Name" value="26 Here Blvd."/>
// <input type="text" name="Address[0].Phone" value="9(999)999-9999"/>
// <input type="text" name="Address[1].Name" value="26 There Blvd."/>
// <input type="text" name="Address[1].Phone" value="1(111)111-1111"/>
// <input type="text" name="active" value="true"/>
// <input type="submit"/>
// </form>
var (
conform = modifiers.New()
scrub = scrubbers.New()
validate = validator.New()
decoder = form.NewDecoder()
)
// Address contains address information
type Address struct {
Name string `mod:"trim" validate:"required"`
Phone string `mod:"trim" validate:"required"`
}
// User contains user information
type User struct {
Name string `mod:"trim" validate:"required" scrub:"name"`
Age uint8 ` validate:"required,gt=0,lt=130"`
Gender string ` validate:"required"`
Email string `mod:"trim" validate:"required,email" scrub:"emails"`
Address []Address ` validate:"required,dive"`
Active bool `form:"active"`
Misc map[string]string `mod:"dive,keys,trim,endkeys,trim"`
}
func main() {
// this simulates the results of http.Request's ParseForm() function
values := parseForm()
var user User
// must pass a pointer
err := decoder.Decode(&user, values)
if err != nil {
log.Panic(err)
}
fmt.Printf("Decoded:%+v\n\n", user)
// great now lets conform our values, after all a human input the data
// nobody's perfect
err = conform.Struct(context.Background(), &user)
if err != nil {
log.Panic(err)
}
fmt.Printf("Conformed:%+v\n\n", user)
// that's better all those extra spaces are gone
// let's validate the data
err = validate.Struct(user)
if err != nil {
log.Panic(err)
}
// ok now we know our data is good, let's do something with it like:
// save to database
// process request
// etc....
// ok now I'm done working with my data
// let's log or store it somewhere
// oh wait a minute, we have some sensitive PII data
// let's make sure that's de-identified first
err = scrub.Struct(context.Background(), &user)
if err != nil {
log.Panic(err)
}
fmt.Printf("Scrubbed:%+v\n\n", user)
}
// this simulates the results of http.Request's ParseForm() function
func parseForm() url.Values {
return url.Values{
"Name": []string{" joeybloggs "},
"Age": []string{"3"},
"Gender": []string{"Male"},
"Email": []string{"[email protected] "},
"Address[0].Name": []string{"26 Here Blvd."},
"Address[0].Phone": []string{"9(999)999-9999"},
"Address[1].Name": []string{"26 There Blvd."},
"Address[1].Phone": []string{"1(111)111-1111"},
"active": []string{"true"},
"Misc[ b4 ]": []string{" b4 "},
}
}