/
validation.go
96 lines (83 loc) · 2.25 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
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
package errors
import (
"bytes"
"fmt"
"strings"
"github.com/valyala/fasthttp"
validator "gopkg.in/go-playground/validator.v9"
)
var replacer = strings.NewReplacer("[", ".", "]", "")
func replaceBrackets(field string) string {
return replacer.Replace(field)
}
func getFieldName(namespace string) string {
parts := strings.SplitN(namespace, ".", 2)
if len(parts) > 1 {
return replaceBrackets(parts[1])
}
return replaceBrackets(namespace)
}
type ErrorWithValidation interface {
Errors() map[string][]string
}
// ValidationError implements wrapping validation errors into a reportable
type ValidationError struct {
reason error
errors map[string][]string
}
// Code returns the "validation" error code.
func (err *ValidationError) Code() string {
return "validation"
}
func (err *ValidationError) Errors() map[string][]string {
if err.errors == nil {
err.errors = make(map[string][]string, 0)
if reason := Reason(err); reason != nil {
if validationErrors, ok := reason.(validator.ValidationErrors); ok {
for _, validationErr := range validationErrors {
field := getFieldName(validationErr.Namespace())
err.errors[field] = append(err.errors[field], validationErr.ActualTag())
}
}
}
}
return err.errors
}
func (err *ValidationError) AppendData(response ErrorResponse) {
response.SetParam("code", err.Code())
response.SetParam("statusCode", fasthttp.StatusBadRequest)
if errors := err.Errors(); len(errors) > 0 {
response.SetParam("errors", errors)
}
}
// Unwrap returns the next error in the error chain.
// If there is no next error, Unwrap returns nil.
func (err *ValidationError) Unwrap() error {
return err.reason
}
func (err *ValidationError) Error() string {
message := "validation"
if err.reason == nil {
return message
}
if errWithMessage, ok := err.reason.(ErrorWithMessage); ok {
message = errWithMessage.Message()
}
errors := err.Errors()
if len(errors) == 0 {
return message
}
buff := bytes.NewBufferString("")
for field, rules := range errors {
if buff.Len() > 0 {
buff.WriteString("; ")
}
buff.WriteString(fmt.Sprintf(`"%s" failed on %s`, field, rules))
}
return fmt.Sprintf("%s: %s", message, buff.String())
}
func WrapValidation(reason error) error {
return &ValidationError{
reason: reason,
}
}