-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
50 lines (42 loc) · 988 Bytes
/
Copy pathexample_test.go
File metadata and controls
50 lines (42 loc) · 988 Bytes
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
package validator_test
import (
"fmt"
"github.com/azghr/forge/validator"
)
func ExampleValidateStruct() {
type User struct {
Name string `validate:"nonzero"`
Email string `validate:"email"`
}
u := User{Name: "Alice", Email: "x@x"}
if err := validator.ValidateStruct(u); err != nil {
fmt.Println("Invalid:", err)
} else {
fmt.Println("Valid")
}
// Output: Valid
}
func ExampleValidateStruct_errors() {
type User struct {
Name string `validate:"nonzero"`
Email string `validate:"email"`
}
u := User{Name: "", Email: "bad"}
err := validator.ValidateStruct(u)
if err != nil {
fmt.Println(err)
}
// Output: Name: nonzero (and 1 more errors)
}
func ExampleValidateStruct_customTag() {
type Config struct {
Host string `check:"nonzero"`
Port int `check:"nonzero"`
}
cfg := Config{Host: "localhost", Port: 0}
err := validator.ValidateStruct(cfg, validator.WithTagName("check"))
if err != nil {
fmt.Println(err)
}
// Output: Port: nonzero
}