-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext_validation.go
More file actions
112 lines (104 loc) · 3 KB
/
Copy pathcontext_validation.go
File metadata and controls
112 lines (104 loc) · 3 KB
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
package servex
import (
"errors"
"fmt"
"io"
"net/http"
)
// ReadAndValidate reads a JSON from the request body to a variable of the provided type and validates it with size limits.
func ReadAndValidate[T interface{ Validate() error }](r *http.Request) (T, error) {
return ReadAndValidateWithLimit[T](r, defaultMaxJSONBodySize)
}
// ReadAndValidateWithLimit reads and validates a JSON from the request body with a specific size limit.
func ReadAndValidateWithLimit[T interface{ Validate() error }](r *http.Request, maxSize int64) (T, error) {
var req T
if maxSize <= 0 {
maxSize = defaultMaxJSONBodySize
}
if r.Body == nil {
return req, errors.New("request body is nil")
}
bytes, err := io.ReadAll(io.LimitReader(r.Body, maxSize+1))
if err != nil {
return req, fmt.Errorf("read: %w", err)
}
// Check if we hit the size limit
if int64(len(bytes)) > maxSize {
return req, fmt.Errorf("request body too large (max: %d bytes)", maxSize)
}
if err := json.Unmarshal(bytes, &req); err != nil {
return req, fmt.Errorf("unmarshal: %w", err)
}
if err := req.Validate(); err != nil {
return req, fmt.Errorf("invalid body: %w", err)
}
return req, nil
}
// ReadAndValidate reads a JSON from the request body to the provided variable and validates it with size limits.
// You should provide a pointer to the variable.
// Example:
//
// type User struct {
// Name string `json:"name"`
// Email string `json:"email"`
// }
//
// func (u *User) Validate() error {
// if u.Name == "" {
// return errors.New("name is required")
// }
// return nil
// }
//
// func createUser(w http.ResponseWriter, r *http.Request) {
// ctx := servex.C(w, r)
//
// var user User
// if err := ctx.ReadAndValidate(&user); err != nil {
// ctx.BadRequest(err, "Invalid JSON payload")
// return
// }
//
// // Process user...
// ctx.JSON(map[string]string{"status": "created"})
// }
func (ctx *Context) ReadAndValidate(body interface{ Validate() error }) error {
return ctx.ReadAndValidateWithLimit(body, ctx.maxJSONBodySize)
}
// ReadAndValidateWithLimit reads a JSON from the request body to the provided variable and validates it with size limits.
// You should provide a pointer to the variable.
// Example:
//
// type User struct {
// Name string `json:"name"`
// Email string `json:"email"`
// }
//
// func (u *User) Validate() error {
// if u.Name == "" {
// return errors.New("name is required")
// }
// return nil
// }
//
// func createUser(w http.ResponseWriter, r *http.Request) {
// ctx := servex.C(w, r)
//
// var user User
// if err := ctx.ReadAndValidateWithLimit(&user, 1024); err != nil {
// ctx.BadRequest(err, "Invalid JSON payload")
// return
// }
//
// // Process user...
// ctx.JSON(map[string]string{"status": "created"})
// }
func (ctx *Context) ReadAndValidateWithLimit(body interface{ Validate() error }, maxSize int64) error {
if err := ctx.ReadJSONWithLimit(body, maxSize); err != nil {
return err
}
if err := body.Validate(); err != nil {
return fmt.Errorf("invalid body: %w", err)
}
return nil
}