forked from cohesivestack/valgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
valgo_test.go
161 lines (131 loc) · 3.02 KB
/
valgo_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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package valgo
import (
"encoding/json"
"fmt"
)
func Example() {
val := Is(String("Bob", "full_name").Not().Blank().OfLengthBetween(4, 20)).
Is(Number(17, "age").GreaterThan(18))
if !val.Valid() {
out, _ := json.MarshalIndent(val.Error(), "", " ")
fmt.Println(string(out))
}
// Output: {
// "age": [
// "Age must be greater than \"18\""
// ],
// "full_name": [
// "Full name must have a length between \"4\" and \"20\""
// ]
// }
}
func ExampleIs() {
val := Is(String("Bob", "full_name").Not().Blank().OfLengthBetween(4, 20)).
Is(Number(17, "age").GreaterThan(18)).
Is(String("singl", "status").InSlice([]string{"married", "single"}))
if !val.Valid() {
out, _ := json.MarshalIndent(val.Error(), "", " ")
fmt.Println(string(out))
}
// Output: {
// "age": [
// "Age must be greater than \"18\""
// ],
// "full_name": [
// "Full name must have a length between \"4\" and \"20\""
// ],
// "status": [
// "Status is not valid"
// ]
// }
}
func ExampleNew() {
month := 5
monthDay := 11
val := New()
if month == 6 {
val.Is(Number(monthDay, "month_day").LessOrEqualTo(10))
}
if val.Valid() {
fmt.Println("The validation passes")
}
// Output: The validation passes
}
func ExampleIn() {
type Address struct {
Name string
Street string
}
type Person struct {
Name string
Address Address
}
p := Person{"Bob", Address{"", "1600 Amphitheatre Pkwy"}}
val := Is(String(p.Name, "name").OfLengthBetween(4, 20)).
In("address", Is(
String(p.Address.Name, "name").Not().Blank()).Is(
String(p.Address.Street, "street").Not().Blank()))
if !val.Valid() {
out, _ := json.MarshalIndent(val.Error(), "", " ")
fmt.Println(string(out))
}
// output: {
// "address.name": [
// "Name can't be blank"
// ],
// "name": [
// "Name must have a length between \"4\" and \"20\""
// ]
// }
}
func ExampleInRow() {
type Address struct {
Name string
Street string
}
type Person struct {
Name string
Addresses []Address
}
p := Person{
"Bob",
[]Address{
{"", "1600 Amphitheatre Pkwy"},
{"Home", ""},
},
}
val := Is(String(p.Name, "name").OfLengthBetween(4, 20))
for i, a := range p.Addresses {
val.InRow("addresses", i, Is(
String(a.Name, "name").Not().Blank()).Is(
String(a.Street, "street").Not().Blank()))
}
if !val.Valid() {
out, _ := json.MarshalIndent(val.Error(), "", " ")
fmt.Println(string(out))
}
// output: {
// "addresses[0].name": [
// "Name can't be blank"
// ],
// "addresses[1].street": [
// "Street can't be blank"
// ],
// "name": [
// "Name must have a length between \"4\" and \"20\""
// ]
// }
}
func ExampleCheck() {
val := Check(String("", "full_name").Not().Blank().OfLengthBetween(4, 20))
if !val.Valid() {
out, _ := json.MarshalIndent(val.Error(), "", " ")
fmt.Println(string(out))
}
// output: {
// "full_name": [
// "Full name can't be blank",
// "Full name must have a length between \"4\" and \"20\""
// ]
// }
}