OSDN Git Service

new repo
[bytom/vapor.git] / vendor / gopkg.in / go-playground / validator.v9 / _examples / simple / main.go
1 package main
2
3 import (
4         "fmt"
5
6         "gopkg.in/go-playground/validator.v9"
7 )
8
9 // User contains user information
10 type User struct {
11         FirstName      string     `validate:"required"`
12         LastName       string     `validate:"required"`
13         Age            uint8      `validate:"gte=0,lte=130"`
14         Email          string     `validate:"required,email"`
15         FavouriteColor string     `validate:"iscolor"`                // alias for 'hexcolor|rgb|rgba|hsl|hsla'
16         Addresses      []*Address `validate:"required,dive,required"` // a person can have a home and cottage...
17 }
18
19 // Address houses a users address information
20 type Address struct {
21         Street string `validate:"required"`
22         City   string `validate:"required"`
23         Planet string `validate:"required"`
24         Phone  string `validate:"required"`
25 }
26
27 // use a single instance of Validate, it caches struct info
28 var validate *validator.Validate
29
30 func main() {
31
32         validate = validator.New()
33
34         validateStruct()
35         validateVariable()
36 }
37
38 func validateStruct() {
39
40         address := &Address{
41                 Street: "Eavesdown Docks",
42                 Planet: "Persphone",
43                 Phone:  "none",
44         }
45
46         user := &User{
47                 FirstName:      "Badger",
48                 LastName:       "Smith",
49                 Age:            135,
50                 Email:          "Badger.Smith@gmail.com",
51                 FavouriteColor: "#000-",
52                 Addresses:      []*Address{address},
53         }
54
55         // returns nil or ValidationErrors ( map[string]*FieldError )
56         err := validate.Struct(user)
57         if err != nil {
58
59                 // this check is only needed when your code could produce
60                 // an invalid value for validation such as interface with nil
61                 // value most including myself do not usually have code like this.
62                 if _, ok := err.(*validator.InvalidValidationError); ok {
63                         fmt.Println(err)
64                         return
65                 }
66
67                 for _, err := range err.(validator.ValidationErrors) {
68
69                         fmt.Println(err.Namespace())
70                         fmt.Println(err.Field())
71                         fmt.Println(err.StructNamespace()) // can differ when a custom TagNameFunc is registered or
72                         fmt.Println(err.StructField())     // by passing alt name to ReportError like below
73                         fmt.Println(err.Tag())
74                         fmt.Println(err.ActualTag())
75                         fmt.Println(err.Kind())
76                         fmt.Println(err.Type())
77                         fmt.Println(err.Value())
78                         fmt.Println(err.Param())
79                         fmt.Println()
80                 }
81
82                 // from here you can create your own error messages in whatever language you wish
83                 return
84         }
85
86         // save user to database
87 }
88
89 func validateVariable() {
90
91         myEmail := "joeybloggs.gmail.com"
92
93         errs := validate.Var(myEmail, "required,email")
94
95         if errs != nil {
96                 fmt.Println(errs) // output: Key: "" Error:Field validation for "" failed on the "email" tag
97                 return
98         }
99
100         // email ok, move on
101 }