/
disallow.go
36 lines (31 loc) 路 1.16 KB
/
disallow.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
package middleware
import (
"net/http"
"strings"
"github.com/System-Glitch/goyave/lang"
"github.com/System-Glitch/goyave"
"github.com/System-Glitch/goyave/validation"
)
// DisallowNonValidatedFields validates that all fields in the request
// are validated by the RuleSet.
// Returns "422 Unprocessable Entity" and an error message if the user
// has sent non-validated field(s).
// Fields ending with "_confirmation" are ignored.
// If the body parsing failed, this middleware immediately passes to the next handler.
func DisallowNonValidatedFields(next goyave.Handler) goyave.Handler {
return func(response *goyave.Response, request *goyave.Request) {
nonValidated := validation.Errors{}
if request.Data != nil {
for field := range request.Data {
if _, exists := request.Rules[field]; !exists && !strings.HasSuffix(field, "_confirmation") {
nonValidated[field] = append(nonValidated[field], lang.Get(request.Lang, "disallow-non-validated-fields"))
}
}
if len(nonValidated) != 0 {
response.JSON(http.StatusUnprocessableEntity, map[string]validation.Errors{"validationError": nonValidated})
return
}
}
next(response, request)
}
}