You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
|`NewSliceRule(fieldExpr string, innerRule Rule) `| apply `innerRule` to every elements in slice/array |
132
-
|`NewMapRule(fieldExpr string, keyRule Rule, valueRule Rule)`| apply `keyRule` to keys in map, and `ValueRule` to valuse in map. `keyRule` or `ValueRule` can be nil. |
| len |`NewLengthRule(fieldExpr string, ge int, le int)`|
141
-
| required_if, required_without,etc | implemented by compositions of `NewAndRule(rules []Rule) Rule`, `NewOrRule(rules []Rule)`, `NewNotRule(innerRule Rule)`|
142
-
143
-
144
-
145
-
### easy for checker, hard for validatior
146
-
147
-
The main drawback of `validator` is, validation rule is attached to fields in struct via tag, which is intrusive, and hard to read the validation logic.
148
-
149
-
1. validation sturct of third party
150
32
151
33
```go
152
-
package thrid_party
153
-
154
-
typeParamstruct{
155
-
Age`validate:"min=18,max=80"`
34
+
typeItemstruct {
35
+
Info typeInfo
156
36
}
157
-
```
158
-
159
-
In user's package, try to change min to 20, `validator` can not change the validation rule, as we cannot change the struct layout outside our packages.
160
37
161
-
```go
162
-
package main
163
-
164
-
funcvalidate(pthrid_party.Param)(isValidbool){
165
-
....
38
+
// typeInfo.Type = "range", length of typeInfo.Type is 2,elements meets format of "2006-01-02"
39
+
// typeInfo.Type = "last", length of typeInfo.Typeis 1,elements meets of format positive integer,
40
+
// Granularity must be one of day/week/month
41
+
type typeInfo struct {
42
+
Typestring
43
+
Range []string
44
+
Unitstring
45
+
Granularitystring
166
46
}
167
47
168
-
```
169
-
170
-
If use `checker`, the rule is simpler:
171
-
172
-
```go
173
-
rule:= checker.NewRangeRuleInt("Age", 20, 80)
174
-
checker.Add(rule, "invlaid age")
175
-
```
176
-
177
-
178
-
Because validation rule of `checker` is decoupled from struct, which makes changes validation rule easy.
179
-
180
-
2. validate the length of linkedlist
181
-
182
-
The example is [here](_checker_test/linkedlist_test.go).
183
-
184
-
```go
185
-
type list struct {
186
-
Name *string
187
-
Next *list `validate:"nonzero"`
188
-
}
189
-
```
190
-
191
-
To validate the length of linkedlist, requiring the first node's `Next` cannot be nil. `validator` cannot do this, for the same tag is attached to the same field.
0 commit comments