-
Notifications
You must be signed in to change notification settings - Fork 53
/
valid.go
46 lines (38 loc) · 1.32 KB
/
valid.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
package znet
import (
"github.com/sohaha/zlsgo/zvalid"
)
func (c *Context) ValidRule() *zvalid.Engine {
return zvalid.New()
}
// ValidParam get and verify routing parameters
func (c *Context) ValidParam(rule *zvalid.Engine, key string, name ...string) *zvalid.Engine {
return valid(rule, c.GetParam(key), key, name...)
}
// ValidQuery get and verify query
func (c *Context) ValidQuery(rule *zvalid.Engine, key string, name ...string) *zvalid.Engine {
return valid(rule, c.DefaultQuery(key, ""), key, name...)
}
// ValidForm get and verify postform
func (c *Context) ValidForm(rule *zvalid.Engine, key string, name ...string) *zvalid.Engine {
return valid(rule, c.DefaultPostForm(key, ""), key, name...)
}
// ValidJSON get and verify json
func (c *Context) ValidJSON(rule *zvalid.Engine, key string, name ...string) *zvalid.Engine {
return valid(rule, c.GetJSON(key).String(), key, name...)
}
// Valid Valid from -> query -> parame
func (c *Context) Valid(rule *zvalid.Engine, key string, name ...string) *zvalid.Engine {
value := c.DefaultFormOrQuery(key, "")
if value == "" {
value = c.GetParam(key)
}
return valid(rule, value, key, name...)
}
func valid(rule *zvalid.Engine, value, key string, name ...string) (valid *zvalid.Engine) {
if rule == nil {
rule = zvalid.New()
}
valid = rule.Verifi(value, name...)
return
}