-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation.go
53 lines (44 loc) · 1.23 KB
/
validation.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
package openapi
import (
"fmt"
"net/http"
kinf "github.com/getkin/kin-openapi/openapi3filter"
kinr "github.com/getkin/kin-openapi/routers"
"github.com/go-chi/render"
)
type validation func(http.Handler) http.Handler
func newValidation(v *validator) validation {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
vlt, err := v.validate(req)
if err != nil {
fmt.Println(err.Error())
res.WriteHeader(http.StatusInternalServerError)
return
}
if vlt != nil {
render.Status(req, http.StatusBadRequest)
render.JSON(res, req, vlt)
return
}
next.ServeHTTP(res, req)
})
}
}
type validator struct{ routeFinder kinr.Router }
func (v *validator) validate(req *http.Request) (*violation, error) {
route, params, err := v.routeFinder.FindRoute(req)
if err != nil {
return nil, fmt.Errorf("unable to find route: %w", err)
}
input := &kinf.RequestValidationInput{
Request: req,
PathParams: params,
Route: route,
Options: &kinf.Options{ExcludeResponseBody: true, MultiError: true},
}
if err = kinf.ValidateRequest(req.Context(), input); err != nil {
return violationsFromError(err), nil
}
return new(violation), nil
}