-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathexample_nested_struct_map_test.go
64 lines (55 loc) · 1.56 KB
/
example_nested_struct_map_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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package validating_test
import (
"fmt"
v "github.com/RussellLuo/validating/v3"
)
type Member struct {
Name string
}
type Person1 struct {
Name string
Age int
Family map[string]*Member
}
func makeSchema1(p *Person1) v.Schema {
return v.Schema{
v.F("name", p.Name): v.LenString(1, 5),
v.F("age", p.Age): v.Nonzero[int](),
v.F("family", p.Family): v.EachMap[map[string]*Member](v.Nested(func(member *Member) v.Validator {
return v.Schema{
v.F("name", member.Name): v.LenString(10, 15).Msg("is too long"),
}
})),
}
}
// The equivalent implementation using Map.
//
//nolint:golint,unused
func makeSchema1_Map(p *Person1) v.Schema {
return v.Schema{
v.F("name", p.Name): v.LenString(1, 5),
v.F("age", p.Age): v.Nonzero[int](),
v.F("family", p.Family): v.Map(func(m map[string]*Member) map[string]v.Validator {
schemas := make(map[string]v.Validator)
for relation, member := range m {
schemas[relation] = v.Schema{
v.F("name", member.Name): v.LenString(10, 15).Msg("is too long"),
}
}
return schemas
}),
}
}
func Example_nestedStructMap() {
p1 := Person1{}
err := v.Validate(makeSchema1(&p1))
fmt.Printf("err of p1: %+v\n", err)
p2 := Person1{Family: map[string]*Member{
"father": {"father's name"},
"mother": {"mother's name is long"},
}}
err = v.Validate(makeSchema1(&p2))
fmt.Printf("err of p2: %+v\n", err)
//err of p1: name: INVALID(has an invalid length), age: INVALID(is zero valued)
//err of p2: name: INVALID(has an invalid length), age: INVALID(is zero valued), family[mother].name: INVALID(is too long)
}