|
| 1 | +package validator |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + gonethttpctx "github.com/ralvarezdev/go-net/http/context" |
| 6 | + gonethttphandler "github.com/ralvarezdev/go-net/http/handler" |
| 7 | + goreflect "github.com/ralvarezdev/go-reflect" |
| 8 | + govalidatorstructmapper "github.com/ralvarezdev/go-validator/struct/mapper" |
| 9 | + "net/http" |
| 10 | +) |
| 11 | + |
| 12 | +// Middleware struct |
| 13 | +type Middleware struct { |
| 14 | + handler gonethttphandler.Handler |
| 15 | + generator govalidatorstructmapper.Generator |
| 16 | +} |
| 17 | + |
| 18 | +// NewMiddleware creates a new Middleware instance |
| 19 | +func NewMiddleware( |
| 20 | + handler gonethttphandler.Handler, |
| 21 | + generator govalidatorstructmapper.Generator, |
| 22 | +) (*Middleware, error) { |
| 23 | + // Check if the handler or the generator is nil |
| 24 | + if handler == nil { |
| 25 | + return nil, gonethttphandler.ErrNilHandler |
| 26 | + } |
| 27 | + if generator == nil { |
| 28 | + return nil, govalidatorstructmapper.ErrNilGenerator |
| 29 | + } |
| 30 | + |
| 31 | + return &Middleware{ |
| 32 | + handler: handler, |
| 33 | + }, nil |
| 34 | +} |
| 35 | + |
| 36 | +// Validate validates the request body and stores it in the context |
| 37 | +func (m *Middleware) Validate( |
| 38 | + body interface{}, |
| 39 | + createValidateFn interface{}, |
| 40 | +) func(next http.Handler) http.Handler { |
| 41 | + // Get the type of the body |
| 42 | + bodyType := goreflect.GetTypeOf(body) |
| 43 | + |
| 44 | + // Create the mapper |
| 45 | + mapper, err := m.generator.NewMapper(body) |
| 46 | + if err != nil { |
| 47 | + panic(err) |
| 48 | + } |
| 49 | + |
| 50 | + return func(next http.Handler) http.Handler { |
| 51 | + return http.HandlerFunc( |
| 52 | + func(w http.ResponseWriter, r *http.Request) { |
| 53 | + // Get a new instance of the body |
| 54 | + dest := goreflect.NewInstanceFromType(bodyType) |
| 55 | + |
| 56 | + // Get the validate function |
| 57 | + results, err := goreflect.CallFunction( |
| 58 | + createValidateFn, |
| 59 | + dest, |
| 60 | + mapper, |
| 61 | + ) |
| 62 | + if err != nil { |
| 63 | + panic(err) |
| 64 | + } |
| 65 | + validateFn := results[0] |
| 66 | + |
| 67 | + // Parse the validate function |
| 68 | + if validateFn == nil { |
| 69 | + panic(ErrNilValidateFn) |
| 70 | + } |
| 71 | + parsedValidateFn, ok := validateFn.(func() (interface{}, error)) |
| 72 | + if !ok { |
| 73 | + panic(ErrInvalidValidateFn) |
| 74 | + } |
| 75 | + |
| 76 | + // Decode the request body |
| 77 | + if !m.handler.Parse( |
| 78 | + w, |
| 79 | + r, |
| 80 | + dest, |
| 81 | + parsedValidateFn, |
| 82 | + ) { |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + // Store the validated body in the context |
| 87 | + ctx := context.WithValue( |
| 88 | + r.Context(), |
| 89 | + gonethttpctx.CtxBodyKey, |
| 90 | + dest, |
| 91 | + ) |
| 92 | + next.ServeHTTP(w, r.WithContext(ctx)) |
| 93 | + }, |
| 94 | + ) |
| 95 | + } |
| 96 | +} |
0 commit comments