This repository has been archived by the owner on May 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
validator.go
118 lines (99 loc) · 3.25 KB
/
validator.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package ginerrors
import (
"errors"
"fmt"
"strings"
"github.com/gin-gonic/gin"
"google.golang.org/genproto/googleapis/rpc/errdetails"
"gopkg.in/go-playground/validator.v9"
errs "github.com/microparts/errors-go"
)
type langName string
type validationRule string
type errorPattern string
type validationErrors map[validationRule]errorPattern
func (ve errorPattern) string() string {
return string(ve)
}
var CommonValidationErrors = map[langName]validationErrors{
"ru": {
"ek": "Ошибка валидации для свойства `%s` с правилом `%s`",
"required": "Свойство `%s` обязательно для заполнения",
"gt": "Свойство `%s` должно содержать более `%s` элементов",
},
"en": {
"ek": "Fail to validate field `%s` with rule `%s`",
"required": "Field `%s` is required",
"gt": "Field `%s` must contain more than `%s` elements",
},
"es": {
"ek": "",
"required": "",
"gt": "",
},
}
var (
defaultLang = "ru"
ErrNotFound = errors.New("route not found")
ErrNoMethod = errors.New("method not allowed")
ErrServerError = errors.New("internal server error")
ErrRecordNotFound = errors.New("record not found")
)
func getLang(c *gin.Context) langName {
lang := c.GetHeader("lang")
if lang == "" {
lang = c.DefaultQuery("lang", defaultLang)
}
return langName(lang)
}
// validationErrors Формирование массива ошибок
func makeErrorsSlice(err error, lang langName) map[errs.FieldName][]errs.ValidationError {
ve := make(map[errs.FieldName][]errs.ValidationError)
for _, e := range err.(validator.ValidationErrors) {
field := getFieldName(e.Namespace(), e.Field())
if _, ok := ve[field]; !ok {
ve[field] = make([]errs.ValidationError, 0)
}
ve[field] = append(
ve[field],
getErrMessage(validationRule(e.ActualTag()), field, e.Param(), lang),
)
}
return ve
}
func makeErrorsSliceFromViolations(violations []*errdetails.BadRequest_FieldViolation) map[errs.FieldName][]errs.ValidationError {
ve := make(map[errs.FieldName][]errs.ValidationError)
for _, v := range violations {
if v == nil {
continue
}
field := errs.FieldName(v.Field)
if _, ok := ve[field]; !ok {
ve[field] = make([]errs.ValidationError, 0)
}
e := errs.ValidationError(v.Description)
ve[field] = append(ve[field], e)
}
return ve
}
func getFieldName(namespace string, field string) errs.FieldName {
namespace = strings.Replace(namespace, "]", "", -1)
namespace = strings.Replace(namespace, "[", ".", -1)
namespaceSlice := strings.Split(namespace, ".")
fieldName := field
if len(namespaceSlice) > 2 {
fieldName = strings.Join([]string{strings.Join(namespaceSlice[1:len(namespaceSlice)-1], "."), field}, ".")
}
return errs.FieldName(fieldName)
}
func getErrMessage(errorType validationRule, field errs.FieldName, param string, lang langName) errs.ValidationError {
errKey := errorType
_, ok := CommonValidationErrors[lang][errorType]
if !ok {
errKey = "ek"
}
if param != "" && errKey == "ek" {
return errs.ValidationError(fmt.Sprintf(CommonValidationErrors[lang][errKey].string(), field, errorType))
}
return errs.ValidationError(fmt.Sprintf(CommonValidationErrors[lang][errKey].string(), field))
}