-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountry_code.go
More file actions
57 lines (49 loc) · 1.62 KB
/
country_code.go
File metadata and controls
57 lines (49 loc) · 1.62 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
package validator_ext
import (
ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
"strings"
)
var extCountryCodes = map[string]bool{
"BONAIRE_SINT_EUSTATIUS_AND_SABA": true,
"BOSNIA_AND_HERZEGOVINA": true,
"GEORGIA": true,
"GUINEA": true,
"HOLY_SEE__THE_": true,
"JERSEY": true,
"JORDAN": true,
"SINT_MAARTEN__DUTCH_PART_": true,
"SVALBARD_AND_JAN_MAYEN": true,
"SYRIAN_ARAB_REPUBLIC__THE_": true,
"UNITED_STATES_MINOR_OUTLYING_ISLANDS__THE_": true,
"WALLIS_AND_FUTUNA": true,
}
func extCountryCode(fl validator.FieldLevel) bool {
s := fl.Field().String()
if s == "" {
return true
}
return extCountryCodes[s]
}
func registerIsCountryEn(validate *validator.Validate, trans ut.Translator) {
err := validate.RegisterTranslation(IsCountry, trans, func(ut ut.Translator) error {
return ut.Add(IsCountry, "[{0}] must be valid country code", true)
}, func(ut ut.Translator, fe validator.FieldError) string {
t, _ := ut.T(IsCountry, strings.ToLower(fe.Field()))
return t
})
if err != nil {
panic(err)
}
}
func registerIsCountryZh(validate *validator.Validate, trans ut.Translator) {
err := validate.RegisterTranslation(IsCountry, trans, func(ut ut.Translator) error {
return ut.Add(IsCountry, "【{0}】必须是有效的国家编码", true)
}, func(ut ut.Translator, fe validator.FieldError) string {
t, _ := ut.T(IsCountry, strings.ToLower(fe.Field()))
return t
})
if err != nil {
panic(err)
}
}