flipped-aurora/gin-vue-admin · warning
格式校验不通过
Error message
格式校验不通过
What it means
Verify supports a `mj:` tag rule of the form regexp=<pattern>. When such a rule matches, it calls regexpMatch(pattern, val.String()); if the field's string value does not match the regular expression, it returns errors.New(fieldName + "格式校验不通过"), i.e. '<Field> failed format validation'. Note it uses val.String(), so for non-string kinds (int, float) the value is rendered via fmt/String semantics and may never match the intended pattern.
Source
Thrown at server/utils/validator.go:159
// 递归的唯一目的是够到内嵌提升上来的字段(如 IdVerify 的 ID、PageInfoVerify 的 Page)。
// 具名关联字段(User/Dept/Meta 等)是独立业务对象,不属于当前结构体自身,不应被扫描——
// 否则关联对象内嵌的 GVA_MODEL.ID 恒为 0,会让 IdVerify 误报"ID值不能为空"。
// 需要单独校验某个具名子结构时,调用方直接 Verify(x.Sub, rule)(项目既有用法)。
if tagVal.Anonymous && tagVal.Type.Kind() == reflect.Struct {
if err = Verify(val.Interface(), roleMap); err != nil {
return err
}
}
if len(roleMap[tagVal.Name]) > 0 {
for _, v := range roleMap[tagVal.Name] {
switch {
case v == "notEmpty":
if isBlank(val) {
return errors.New(tagVal.Name + "值不能为空")
}
case strings.Split(v, "=")[0] == "regexp":
if !regexpMatch(strings.Split(v, "=")[1], val.String()) {
return errors.New(tagVal.Name + "格式校验不通过")
}
case compareMap[strings.Split(v, "=")[0]]:
if !compareVerify(val, v) {
return errors.New(tagVal.Name + "长度或值不在合法范围," + v)
}
}
}
}
}
return nil
}
//@author: [piexlmax](https://github.com/piexlmax)
//@function: compareVerify
//@description: 长度和数字的校验方法 根据类型自动校验
//@param: value reflect.Value, VerifyStr string
//@return: bool
View on GitHub (pinned to 3136500ef3)
Solutions
- Inspect the field's `mj:"regexp=..."` tag and test the actual value against the pattern (go test with regexp.MustCompile(pattern).MatchString).
- Fix the client to send values in the format the regex expects, or align frontend validation with the server regex.
- If the pattern itself is wrong/outdated, update the `mj:` tag to the correct regular expression.
- For non-string fields, convert or re-type the field so val.String() returns a value the pattern can match (or drop the regexp rule for numeric fields).
Example fix
// before
Phone string `json:"phone" mj:"regexp=^1[3-9][0-9]{9}$"` // value "12ab"
// after
// send a well-formed value or correct the pattern
Phone string `json:"phone" mj:"regexp=^\\d{11}$"` // value "13800138000" Defensive patterns
Strategy: validation
Validate before calling
var emailRe = regexp.MustCompile(`^[\w.+-]+@[\w-]+\.[\w.]+$`)
if !emailRe.MatchString(req.Email) {
return errors.New("email format invalid")
} Try / catch
if err := utils.Verify(req, utils.Rules{}); err != nil {
if strings.HasSuffix(err.Error(), "格式校验不通过") {
c.JSON(400, gin.H{"msg": "输入格式不正确: " + err.Error()})
return
}
return err
} Prevention
- Keep frontend regexes identical to the `mj:regexp=` tags
- Test every regexp tag with go tests using boundary values
- Avoid regexp rules on non-string fields
- Anchor patterns (^...$) to prevent accidental partial matches
When it happens
Trigger: A struct field tagged mj:"regexp=^1[3-9][0-9]{9}$" (or any pattern) receives a value that does not match the regex — e.g. a phone number with letters, an email without '@', or an integer field whose String() form does not fit a string-oriented pattern.
Common situations: Users submit IDs, phone numbers, or codes with wrong format; the regex pattern in the tag was written for a different format than the frontend enforces; the field is numeric but the regexp was designed for strings so val.String() yields an unexpected representation.
Related errors
AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31).
Data as JSON: /api/errors/667d70b0bf6bc28e.
Report an issue: GitHub.