-
Notifications
You must be signed in to change notification settings - Fork 53
/
valid.go
48 lines (40 loc) · 1.49 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
47
48
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(defRule zvalid.Engine, key string, name ...string) zvalid.Engine {
return valid(defRule, c.GetParam(key), key, name...)
}
// ValidQuery get and verify query
func (c *Context) ValidQuery(defRule zvalid.Engine, key string, name ...string) zvalid.Engine {
return valid(defRule, c.DefaultQuery(key, ""), key, name...)
}
// ValidForm get and verify postform
func (c *Context) ValidForm(defRule zvalid.Engine, key string, name ...string) zvalid.Engine {
return valid(defRule, c.DefaultPostForm(key, ""), key, name...)
}
// ValidJSON get and verify json
func (c *Context) ValidJSON(defRule zvalid.Engine, key string, name ...string) zvalid.Engine {
return valid(defRule, c.GetJSON(key).String(), key, name...)
}
// Valid Valid from -> query -> parame
func (c *Context) Valid(defRule zvalid.Engine, key string, name ...string) zvalid.Engine {
value, contentType := "", c.ContentType()
if contentType == c.ContentType(ContentTypeJSON) || contentType == c.ContentType(ContentTypePlain) {
value = c.GetJSON(key).String()
}
if value == "" {
value = c.DefaultFormOrQuery(key, "")
}
if value == "" {
value = c.GetParam(key)
}
return valid(defRule, value, key, name...)
}
func valid(defRule zvalid.Engine, value, key string, name ...string) (valid zvalid.Engine) {
return defRule.Verifi(value, name...)
}