-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathexample_nested_struct_pointer_test.go
47 lines (39 loc) · 1.21 KB
/
example_nested_struct_pointer_test.go
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
package validating_test
import (
"fmt"
v "github.com/RussellLuo/validating/v3"
)
type Address2 struct {
Country, Province, City string
}
type Person2 struct {
Name string
Age int
Address *Address2
}
func makeSchema2(p *Person2) v.Schema {
return v.Schema{
v.F("name", p.Name): v.LenString(1, 5),
v.F("age", p.Age): v.Lte(50),
v.F("address", p.Address): v.All(
v.Nonzero[*Address2]().Msg("is nil"),
v.Nested(func(addr *Address2) v.Validator {
return v.Schema{
v.F("country", addr.Country): v.Nonzero[string](),
v.F("province", addr.Province): v.Nonzero[string](),
v.F("city", addr.City): v.Nonzero[string](),
}
}),
),
}
}
func Example_nestedStructPointer() {
p1 := Person2{}
err := v.Validate(makeSchema2(&p1))
fmt.Printf("err of p1: %+v\n", err)
p2 := Person2{Age: 60, Address: &Address2{}}
err = v.Validate(makeSchema2(&p2))
fmt.Printf("err of p2: %+v\n", err)
//err of p1: name: INVALID(has an invalid length), address: INVALID(is nil)
//err of p2: name: INVALID(has an invalid length), age: INVALID(is greater than the given value), address.country: INVALID(is zero valued), address.province: INVALID(is zero valued), address.city: INVALID(is zero valued)
}