OSDN Git Service

new repo
[bytom/vapor.git] / vendor / gopkg.in / go-playground / validator.v9 / examples_test.go
1 package validator_test
2
3 // import (
4 //      "fmt"
5
6 //      "gopkg.in/go-playground/validator.v8"
7 // )
8
9 // func ExampleValidate_new() {
10 //      config := &validator.Config{TagName: "validate"}
11
12 //      validator.New(config)
13 // }
14
15 // func ExampleValidate_field() {
16 //      // This should be stored somewhere globally
17 //      var validate *validator.Validate
18
19 //      config := &validator.Config{TagName: "validate"}
20
21 //      validate = validator.New(config)
22
23 //      i := 0
24 //      errs := validate.Field(i, "gt=1,lte=10")
25 //      err := errs.(validator.ValidationErrors)[""]
26 //      fmt.Println(err.Field)
27 //      fmt.Println(err.Tag)
28 //      fmt.Println(err.Kind) // NOTE: Kind and Type can be different i.e. time Kind=struct and Type=time.Time
29 //      fmt.Println(err.Type)
30 //      fmt.Println(err.Param)
31 //      fmt.Println(err.Value)
32 //      //Output:
33 //      //
34 //      //gt
35 //      //int
36 //      //int
37 //      //1
38 //      //0
39 // }
40
41 // func ExampleValidate_struct() {
42 //      // This should be stored somewhere globally
43 //      var validate *validator.Validate
44
45 //      config := &validator.Config{TagName: "validate"}
46
47 //      validate = validator.New(config)
48
49 //      type ContactInformation struct {
50 //              Phone  string `validate:"required"`
51 //              Street string `validate:"required"`
52 //              City   string `validate:"required"`
53 //      }
54
55 //      type User struct {
56 //              Name               string `validate:"required,excludesall=!@#$%^&*()_+-=:;?/0x2C"` // 0x2C = comma (,)
57 //              Age                int8   `validate:"required,gt=0,lt=150"`
58 //              Email              string `validate:"email"`
59 //              ContactInformation []*ContactInformation
60 //      }
61
62 //      contactInfo := &ContactInformation{
63 //              Street: "26 Here Blvd.",
64 //              City:   "Paradeso",
65 //      }
66
67 //      user := &User{
68 //              Name:               "Joey Bloggs",
69 //              Age:                31,
70 //              Email:              "joeybloggs@gmail.com",
71 //              ContactInformation: []*ContactInformation{contactInfo},
72 //      }
73
74 //      errs := validate.Struct(user)
75 //      for _, v := range errs.(validator.ValidationErrors) {
76 //              fmt.Println(v.Field) // Phone
77 //              fmt.Println(v.Tag)   // required
78 //              //... and so forth
79 //              //Output:
80 //              //Phone
81 //              //required
82 //      }
83 // }