...
1 package main
2
3 import (
4 "fmt"
5 "github.com/go-playground/validator/v10"
6 )
7
8 var validate *validator.Validate
9
10 func main() {
11 validate = validator.New()
12
13 validateMap()
14 validateNestedMap()
15 }
16
17 func validateMap() {
18 user := map[string]interface{}{"name": "Arshiya Kiani", "email": "zytel3301@gmail.com"}
19
20
21
22
23 rules := map[string]interface{}{"name": "required,min=8,max=32", "email": "omitempty,required,email"}
24
25
26
27
28
29
30 errs := validate.ValidateMap(user, rules)
31
32 if len(errs) > 0 {
33 fmt.Println(errs)
34
35 }
36
37
38 }
39
40 func validateNestedMap() {
41
42 data := map[string]interface{}{
43 "name": "Arshiya Kiani",
44 "email": "zytel3301@gmail.com",
45 "details": map[string]interface{}{
46 "family_members": map[string]interface{}{
47 "father_name": "Micheal",
48 "mother_name": "Hannah",
49 },
50 "salary": "1000",
51 "phones": []map[string]interface{}{
52 {
53 "number": "11-111-1111",
54 "remark": "home",
55 },
56 {
57 "number": "22-222-2222",
58 "remark": "work",
59 },
60 },
61 },
62 }
63
64
65
66 rules := map[string]interface{}{
67 "name": "min=4,max=32",
68 "email": "required,email",
69 "details": map[string]interface{}{
70 "family_members": map[string]interface{}{
71 "father_name": "required,min=4,max=32",
72 "mother_name": "required,min=4,max=32",
73 },
74 "salary": "number",
75 "phones": map[string]interface{}{
76 "number": "required,min=4,max=32",
77 "remark": "required,min=1,max=32",
78 },
79 },
80 }
81
82 if len(validate.ValidateMap(data, rules)) == 0 {
83
84 }
85
86
87 }
88
View as plain text