-
Notifications
You must be signed in to change notification settings - Fork 13
/
validator.go
68 lines (52 loc) · 1.41 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
62
63
64
65
66
67
68
package mux
import "net/http"
//Validator validates the incomming value against a valid value/s
type Validator interface {
Validate(RouteInterface) error
}
//MethodValidator validates the string against a method.
type MethodValidator map[string]struct{}
// newMethodValidator returns default method validator
func newMethodValidator() MethodValidator {
return MethodValidator(methods)
}
// methods all possible standard methods
var methods = map[string]struct{}{
http.MethodGet: {},
http.MethodPost: {},
http.MethodPatch: {},
http.MethodDelete: {},
http.MethodHead: {},
http.MethodPut: {},
http.MethodOptions: {},
http.MethodConnect: {},
}
func (v MethodValidator) Validate(r RouteInterface) error {
if _, found := v[r.GetMethodName()]; !found {
return NewBadMethodError(r.GetMethodName())
}
return nil
}
//pathValidator check if a path is set and validates the value .
type pathValidator struct{}
func newPathValidator() pathValidator {
return pathValidator{}
}
func (v pathValidator) Validate(r RouteInterface) error {
foundPathMatcher := false
for _, m := range r.GetMatchers() {
if m.Rank() == rankPath {
foundPathMatcher = true
}
}
if !foundPathMatcher {
return NewBadPathError("Patch matcher is missing")
}
if len(r.GetPath()) == 0 {
return NewBadPathError("Path is empty")
}
if r.GetPath()[0] != '/' {
return NewBadPathError("Path starts not with a /")
}
return nil
}