-
Notifications
You must be signed in to change notification settings - Fork 56
/
status.go
124 lines (114 loc) · 2.71 KB
/
status.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package config
import (
"net/http"
"reflect"
"github.com/go-playground/validator"
)
var (
AllHttpStatuses = []int{
// 1xx
http.StatusContinue,
http.StatusSwitchingProtocols,
http.StatusProcessing,
http.StatusEarlyHints,
// 2xx
http.StatusOK,
http.StatusCreated,
http.StatusAccepted,
http.StatusNonAuthoritativeInfo,
http.StatusNoContent,
http.StatusResetContent,
http.StatusPartialContent,
http.StatusMultiStatus,
http.StatusAlreadyReported,
http.StatusIMUsed,
// 3xx
http.StatusMultipleChoices,
http.StatusMovedPermanently,
http.StatusFound,
http.StatusSeeOther,
http.StatusNotModified,
http.StatusUseProxy,
http.StatusTemporaryRedirect,
http.StatusPermanentRedirect,
// 4xx
http.StatusBadRequest,
http.StatusUnauthorized,
http.StatusPaymentRequired,
http.StatusForbidden,
http.StatusNotFound,
http.StatusMethodNotAllowed,
http.StatusNotAcceptable,
http.StatusProxyAuthRequired,
http.StatusRequestTimeout,
http.StatusConflict,
http.StatusGone,
http.StatusLengthRequired,
http.StatusPreconditionFailed,
http.StatusRequestEntityTooLarge,
http.StatusRequestURITooLong,
http.StatusUnsupportedMediaType,
http.StatusRequestedRangeNotSatisfiable,
http.StatusExpectationFailed,
http.StatusTeapot,
http.StatusMisdirectedRequest,
http.StatusUnprocessableEntity,
http.StatusLocked,
http.StatusFailedDependency,
http.StatusTooEarly,
http.StatusUpgradeRequired,
http.StatusPreconditionRequired,
http.StatusTooManyRequests,
http.StatusRequestHeaderFieldsTooLarge,
http.StatusUnavailableForLegalReasons,
// 5xx
http.StatusInternalServerError,
http.StatusNotImplemented,
http.StatusBadGateway,
http.StatusServiceUnavailable,
http.StatusGatewayTimeout,
http.StatusHTTPVersionNotSupported,
http.StatusVariantAlsoNegotiates,
http.StatusInsufficientStorage,
http.StatusLoopDetected,
http.StatusNotExtended,
http.StatusNetworkAuthenticationRequired,
}
)
type HTTPStatusCodeList struct {
StatusCodes []int
}
func ValidateStatusList(fl validator.FieldLevel) bool {
field := fl.Field()
switch field.Kind() {
case reflect.Int, reflect.Int64, reflect.Float64:
confStatusCode := int(field.Int())
statusInvalid := true
for _, httpStatus := range AllHttpStatuses {
if confStatusCode == httpStatus {
statusInvalid = false
break
}
}
if statusInvalid {
return false
}
case reflect.Slice:
for i := 0; i < field.Len(); i++ {
confStatusCode := int(field.Index(i).Int())
statusInvalid := true
for _, httpStatus := range AllHttpStatuses {
if confStatusCode == httpStatus {
statusInvalid = false
break
}
}
if statusInvalid {
return false
}
}
default:
return false
}
return true
}