-
Notifications
You must be signed in to change notification settings - Fork 102
/
validator.go
61 lines (53 loc) · 1.2 KB
/
validator.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
/********************************
*** Multiplexer for Go ***
*** Bone is under MIT license ***
*** Code by CodingFerret ***
*** github.com/go-zoo ***
*********************************/
package bone
// Validator can be passed to a route to validate the params
type Validator interface {
Validate(string) bool
}
type validatorFunc struct {
validateFunc func(string) bool
}
func newValidatorFunc(v func(string) bool) validatorFunc {
return validatorFunc{validateFunc: v}
}
func (v validatorFunc) Validate(s string) bool {
return v.validateFunc(s)
}
type validatorInfo struct {
start int
end int
name string
}
func containsValidators(path string) []validatorInfo {
var index []int
for i, c := range path {
if c == '|' {
index = append(index, i)
}
}
if len(index) > 0 {
var validators []validatorInfo
for i, pos := range index {
if i+1 == len(index) {
validators = append(validators, validatorInfo{
start: pos,
end: len(path),
name: path[pos:len(path)],
})
} else {
validators = append(validators, validatorInfo{
start: pos,
end: index[i+1],
name: path[pos:index[i+1]],
})
}
}
return validators
}
return nil
}