Skip to content

Commit 54927e9

Browse files
Dean KarnDean Karn
authored andcommitted
Merge pull request #124 from bluesuncorp/v6-development
Slit out assertion funcs. & add single field validation example
2 parents b9cdaa4 + f462515 commit 54927e9

File tree

3 files changed

+73
-145
lines changed

3 files changed

+73
-145
lines changed

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Usage and documentation
3434

3535
Please see http://godoc.org/gopkg.in/bluesuncorp/validator.v6 for detailed usage docs.
3636

37-
##### Example:
37+
##### Examples:
3838
```go
3939
package main
4040

@@ -73,6 +73,12 @@ func main() {
7373

7474
validate = validator.New(config)
7575

76+
validateStruct()
77+
validateField()
78+
}
79+
80+
func validateStruct() {
81+
7682
address := &Address{
7783
Street: "Eavesdown Docks",
7884
Planet: "Persphone",
@@ -109,6 +115,19 @@ func main() {
109115

110116
// save user to database
111117
}
118+
119+
func validateField() {
120+
myEmail := "joeybloggs.gmail.com"
121+
122+
errs := validate.Field(myEmail, "required,email")
123+
124+
if errs != nil {
125+
fmt.Println(errs) // output: Key: "" Error:Field validation for "" failed on the "email" tag
126+
return
127+
}
128+
129+
// email ok, move on
130+
}
112131
```
113132

114133
Benchmarks

examples/simple.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ func main() {
3535

3636
validate = validator.New(config)
3737

38+
validateStruct()
39+
validateField()
40+
}
41+
42+
func validateStruct() {
43+
3844
address := &Address{
3945
Street: "Eavesdown Docks",
4046
Planet: "Persphone",
@@ -71,3 +77,16 @@ func main() {
7177

7278
// save user to database
7379
}
80+
81+
func validateField() {
82+
myEmail := "joeybloggs.gmail.com"
83+
84+
errs := validate.Field(myEmail, "required,email")
85+
86+
if errs != nil {
87+
fmt.Println(errs) // output: Key: "" Error:Field validation for "" failed on the "email" tag
88+
return
89+
}
90+
91+
// email ok, move on
92+
}

0 commit comments

Comments
 (0)