27 lines
968 B
Go
27 lines
968 B
Go
package validator
|
|
|
|
// 定义自定义错误消息
|
|
var customMessages = map[string]string{
|
|
"Name.min": "姓名不能少于1个字",
|
|
"Name.required": "姓名是必填项",
|
|
"Name.name": "姓名只能包含中文",
|
|
"Mobile.required": "手机号是必填项",
|
|
"Mobile.min": "电话号码必须为有效的中国电话号码",
|
|
"Mobile.max": "电话号码必须为有效的中国电话号码",
|
|
"Mobile.mobile": "电话号码必须为有效的中国电话号码",
|
|
"IDCard.required": "身份证号是必填项",
|
|
"IDCard.idCard": "无效的身份证号码",
|
|
"Password.min": "密码不能少于8位数",
|
|
"Password.max": "密码不能超过32位数",
|
|
"password.password": "密码强度太弱",
|
|
}
|
|
|
|
// 获取自定义错误消息
|
|
func GetErrorMessage(field, tag string) string {
|
|
key := field + "." + tag
|
|
if msg, exists := customMessages[key]; exists {
|
|
return msg
|
|
}
|
|
return "请输入正确格式的参数"
|
|
}
|