-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
validate_params.go
54 lines (49 loc) · 1.28 KB
/
validate_params.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
package fuego
import "fmt"
type ValidableCtx interface {
GetOpenAPIParams() map[string]OpenAPIParam
HasQueryParam(key string) bool
HasHeader(key string) bool
HasCookie(key string) bool
}
// ValidateParams checks if all required parameters are present in the request.
func ValidateParams(c ValidableCtx) error {
for k, param := range c.GetOpenAPIParams() {
if param.Default != nil {
// skip: param has a default
continue
}
if param.Required {
switch param.Type {
case QueryParamType:
if !c.HasQueryParam(k) {
err := fmt.Errorf("%s is a required query param", k)
return BadRequestError{
Title: "Query Param Not Found",
Err: err,
Detail: "cannot parse request parameter: " + err.Error(),
}
}
case HeaderParamType:
if !c.HasHeader(k) {
err := fmt.Errorf("%s is a required header", k)
return BadRequestError{
Title: "Header Not Found",
Err: err,
Detail: "cannot parse request parameter: " + err.Error(),
}
}
case CookieParamType:
if !c.HasCookie(k) {
err := fmt.Errorf("%s is a required cookie", k)
return BadRequestError{
Title: "Cookie Not Found",
Err: err,
Detail: "cannot parse request parameter: " + err.Error(),
}
}
}
}
}
return nil
}