9
9
"github.com/mrz1836/go-validate"
10
10
)
11
11
12
- //Customer is a very basic model showing validation mixed with JSON
12
+ // Customer is a very basic model showing validation mixed with JSON
13
13
type Customer struct {
14
14
Age uint `validation:"min=18" json:"age"`
15
15
Balance float32 `validation:"min=0" json:"balance"`
@@ -23,17 +23,17 @@ type Customer struct {
23
23
SocialSecurityNumber string `json:"-"`
24
24
}
25
25
26
- //Valid is a custom method for the model that will run all built-in validations and also run any custom validations
26
+ // Valid is a custom method for the model that will run all built-in validations and also run any custom validations
27
27
func (c * Customer ) Valid () (bool , []validate.ValidationError ) {
28
28
29
- //Run all validations off the struct configuration
29
+ // Run all validations off the struct configuration
30
30
_ , errs := validate .IsValid (* c )
31
31
32
32
//
33
33
//Customize: errs (you can add/remove your own errs)
34
34
//
35
35
36
- //Showing use of a public validation method (extra validations outside of the struct built-in validations)
36
+ // Showing use of a public validation method (extra validations outside of the struct built-in validations)
37
37
ok , err := validate .IsValidSocial (c .SocialSecurityNumber )
38
38
if ! ok && err != nil {
39
39
errs = append (errs , validate.ValidationError {
@@ -42,22 +42,22 @@ func (c *Customer) Valid() (bool, []validate.ValidationError) {
42
42
})
43
43
}
44
44
45
- //Return error if found
45
+ // Return error if found
46
46
return len (errs ) == 0 , errs
47
47
}
48
48
49
- //Add your custom validations
49
+ // Add your custom validations
50
50
func init () {
51
51
52
- //Add your own custom validations
52
+ // Add your own custom validations
53
53
//
54
54
//
55
55
}
56
56
57
- //main example (just an example of validating a model's data before persisting into a database)
57
+ // main example (just an example of validating a model's data before persisting into a database)
58
58
func main () {
59
59
60
- //Start with some model & data
60
+ // Start with some model & data
61
61
customer := & Customer {
62
62
Age : 21 ,
63
63
Balance : 1.00 ,
@@ -70,13 +70,13 @@ func main() {
70
70
SocialSecurityNumber : "212126768" ,
71
71
}
72
72
73
- //Validate the model - Run before saving into database
73
+ // Validate the model - Run before saving into database
74
74
_ , errs := customer .Valid ()
75
75
if errs != nil {
76
76
fmt .Printf ("Customer validation failed! %+v" , errs )
77
77
} else {
78
78
fmt .Println ("Customer validation succeed!" )
79
79
}
80
80
81
- //customer.Save() //Now save to a database
81
+ // customer.Save() // Now save to a database
82
82
}
0 commit comments